mirror of
https://github.com/Steffo99/twom.git
synced 2025-02-16 16:23:57 +00:00
Experiment with login some more
This commit is contained in:
parent
0937c4ec45
commit
1204669c88
5 changed files with 64 additions and 31 deletions
|
@ -2,7 +2,6 @@ package eu.steffo.twom.matrix
|
||||||
|
|
||||||
import TwoMRoomDisplayNameFallbackProvider
|
import TwoMRoomDisplayNameFallbackProvider
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
import androidx.compose.runtime.currentComposer
|
|
||||||
import org.matrix.android.sdk.api.Matrix
|
import org.matrix.android.sdk.api.Matrix
|
||||||
import org.matrix.android.sdk.api.MatrixConfiguration
|
import org.matrix.android.sdk.api.MatrixConfiguration
|
||||||
import org.matrix.android.sdk.api.session.Session
|
import org.matrix.android.sdk.api.session.Session
|
||||||
|
@ -26,7 +25,15 @@ object TwoMMatrix {
|
||||||
}
|
}
|
||||||
|
|
||||||
var session: Session? = null
|
var session: Session? = null
|
||||||
private set
|
set(value) {
|
||||||
|
if (field != null) {
|
||||||
|
closeSession()
|
||||||
|
}
|
||||||
|
field = value
|
||||||
|
if (field != null) {
|
||||||
|
openSession()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fun tryInitSessionFromStorage() {
|
fun tryInitSessionFromStorage() {
|
||||||
val lastSession = matrix?.authenticationService()?.getLastAuthenticatedSession()
|
val lastSession = matrix?.authenticationService()?.getLastAuthenticatedSession()
|
||||||
|
@ -36,21 +43,16 @@ object TwoMMatrix {
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Does this throw an error if the session is already open?
|
// TODO: Does this throw an error if the session is already open?
|
||||||
fun openSession() {
|
private fun openSession() {
|
||||||
val currentSession = session ?: throw SessionNotInitializedError()
|
val currentSession = session ?: throw SessionNotInitializedError()
|
||||||
currentSession.open()
|
currentSession.open()
|
||||||
currentSession.syncService().startSync(true)
|
currentSession.syncService().startSync(true)
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Does this throw an error if the session is already closed?
|
// TODO: Does this throw an error if the session is already closed?
|
||||||
fun closeSession() {
|
private fun closeSession() {
|
||||||
val currentSession = session ?: throw SessionNotInitializedError()
|
val currentSession = session ?: throw SessionNotInitializedError()
|
||||||
currentSession.close()
|
currentSession.close()
|
||||||
currentSession.syncService().stopSync()
|
currentSession.syncService().stopSync()
|
||||||
}
|
}
|
||||||
|
|
||||||
fun clearSession() {
|
|
||||||
closeSession()
|
|
||||||
session = null
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,19 +7,7 @@ import androidx.activity.ComponentActivity
|
||||||
import androidx.activity.compose.setContent
|
import androidx.activity.compose.setContent
|
||||||
import androidx.activity.result.ActivityResultLauncher
|
import androidx.activity.result.ActivityResultLauncher
|
||||||
import androidx.activity.result.contract.ActivityResultContracts
|
import androidx.activity.result.contract.ActivityResultContracts
|
||||||
import androidx.compose.foundation.layout.Row
|
|
||||||
import androidx.compose.foundation.layout.fillMaxWidth
|
|
||||||
import androidx.compose.foundation.layout.padding
|
|
||||||
import androidx.compose.material3.Button
|
|
||||||
import androidx.compose.material3.ExperimentalMaterial3Api
|
|
||||||
import androidx.compose.material3.Scaffold
|
|
||||||
import androidx.compose.material3.Text
|
|
||||||
import androidx.compose.material3.TopAppBar
|
|
||||||
import androidx.compose.ui.Modifier
|
|
||||||
import androidx.compose.ui.platform.LocalContext
|
|
||||||
import eu.steffo.twom.R
|
|
||||||
import eu.steffo.twom.ui.homeserver.SelectHomeserverActivity
|
import eu.steffo.twom.ui.homeserver.SelectHomeserverActivity
|
||||||
import eu.steffo.twom.ui.theme.TwoMTheme
|
|
||||||
|
|
||||||
|
|
||||||
class LoginActivity : ComponentActivity() {
|
class LoginActivity : ComponentActivity() {
|
||||||
|
@ -29,7 +17,22 @@ class LoginActivity : ComponentActivity() {
|
||||||
super.onCreate(savedInstanceState)
|
super.onCreate(savedInstanceState)
|
||||||
|
|
||||||
homeserverLauncher = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) {
|
homeserverLauncher = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) {
|
||||||
Log.i("Garasauto", it.data?.getStringExtra(SelectHomeserverActivity.HOMESERVER_EXTRA_KEY) ?: "Undefined")
|
val selectedHomeserver =
|
||||||
|
it.data?.getStringExtra(SelectHomeserverActivity.HOMESERVER_EXTRA_KEY)
|
||||||
|
Log.d("LoginActivity", "Selected homeserver: $selectedHomeserver")
|
||||||
|
setContent {
|
||||||
|
LoginActivityScaffold(
|
||||||
|
selectedHomeserver = selectedHomeserver,
|
||||||
|
onSelectHomeserver = {
|
||||||
|
homeserverLauncher.launch(
|
||||||
|
Intent(
|
||||||
|
applicationContext,
|
||||||
|
SelectHomeserverActivity::class.java
|
||||||
|
)
|
||||||
|
)
|
||||||
|
},
|
||||||
|
)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -9,26 +9,34 @@ import androidx.compose.material3.TextField
|
||||||
import androidx.compose.runtime.Composable
|
import androidx.compose.runtime.Composable
|
||||||
import androidx.compose.runtime.getValue
|
import androidx.compose.runtime.getValue
|
||||||
import androidx.compose.runtime.mutableStateOf
|
import androidx.compose.runtime.mutableStateOf
|
||||||
|
import androidx.compose.runtime.rememberCoroutineScope
|
||||||
import androidx.compose.runtime.saveable.rememberSaveable
|
import androidx.compose.runtime.saveable.rememberSaveable
|
||||||
import androidx.compose.runtime.setValue
|
import androidx.compose.runtime.setValue
|
||||||
import androidx.compose.ui.Modifier
|
import androidx.compose.ui.Modifier
|
||||||
import androidx.compose.ui.platform.LocalContext
|
import androidx.compose.ui.platform.LocalContext
|
||||||
import androidx.compose.ui.tooling.preview.Preview
|
import androidx.compose.ui.tooling.preview.Preview
|
||||||
import eu.steffo.twom.R
|
import eu.steffo.twom.R
|
||||||
|
import eu.steffo.twom.matrix.TwoMMatrix
|
||||||
import eu.steffo.twom.ui.BASE_PADDING
|
import eu.steffo.twom.ui.BASE_PADDING
|
||||||
|
import kotlinx.coroutines.launch
|
||||||
|
import org.matrix.android.sdk.api.session.Session
|
||||||
|
|
||||||
|
|
||||||
@Composable
|
@Composable
|
||||||
@Preview(showBackground = true)
|
@Preview(showBackground = true)
|
||||||
fun LoginActivityControl(
|
fun LoginActivityControl(
|
||||||
modifier: Modifier = Modifier,
|
modifier: Modifier = Modifier,
|
||||||
|
selectedHomeserver: String? = null,
|
||||||
onSelectHomeserver: () -> Unit = {},
|
onSelectHomeserver: () -> Unit = {},
|
||||||
onComplete: (username: String, password: String) -> Unit = { _, _ -> },
|
onLogin: (session: Session) -> Unit = {},
|
||||||
) {
|
) {
|
||||||
|
val scope = rememberCoroutineScope()
|
||||||
|
|
||||||
var username by rememberSaveable { mutableStateOf("") }
|
var username by rememberSaveable { mutableStateOf("") }
|
||||||
var password by rememberSaveable { mutableStateOf("") }
|
var password by rememberSaveable { mutableStateOf("") }
|
||||||
|
|
||||||
|
var loggingIn by rememberSaveable { mutableStateOf(false) }
|
||||||
|
|
||||||
Column(modifier) {
|
Column(modifier) {
|
||||||
Row(BASE_PADDING) {
|
Row(BASE_PADDING) {
|
||||||
Text(LocalContext.current.getString(R.string.login_text))
|
Text(LocalContext.current.getString(R.string.login_text))
|
||||||
|
@ -56,6 +64,15 @@ fun LoginActivityControl(
|
||||||
supportingText = {
|
supportingText = {
|
||||||
Text(LocalContext.current.getString(R.string.login_username_supporting))
|
Text(LocalContext.current.getString(R.string.login_username_supporting))
|
||||||
},
|
},
|
||||||
|
prefix = {
|
||||||
|
Text("@")
|
||||||
|
},
|
||||||
|
suffix = {
|
||||||
|
// TODO: Properly perform the login process
|
||||||
|
val localpart = selectedHomeserver?.replace(Regex("^https?://"), "")
|
||||||
|
Text(":$localpart")
|
||||||
|
},
|
||||||
|
enabled = (selectedHomeserver != null),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
Row(BASE_PADDING) {
|
Row(BASE_PADDING) {
|
||||||
|
@ -72,15 +89,26 @@ fun LoginActivityControl(
|
||||||
supportingText = {
|
supportingText = {
|
||||||
Text(LocalContext.current.getString(R.string.login_password_supporting))
|
Text(LocalContext.current.getString(R.string.login_password_supporting))
|
||||||
},
|
},
|
||||||
|
enabled = (selectedHomeserver != null),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
Row(BASE_PADDING) {
|
Row(BASE_PADDING) {
|
||||||
Button(
|
Button(
|
||||||
modifier = Modifier.fillMaxWidth(),
|
modifier = Modifier.fillMaxWidth(),
|
||||||
onClick = {
|
onClick = {
|
||||||
onComplete(username, password)
|
val wizard = TwoMMatrix.matrix!!.authenticationService().getLoginWizard()
|
||||||
|
|
||||||
|
scope.launch {
|
||||||
|
val session = wizard.login(
|
||||||
|
login = "@$username:$selectedHomeserver",
|
||||||
|
password = password,
|
||||||
|
initialDeviceName = "Garasauto", // TODO
|
||||||
|
deviceId = "Garasauto", // TODO
|
||||||
|
)
|
||||||
|
TwoMMatrix.session = session
|
||||||
|
}
|
||||||
},
|
},
|
||||||
enabled = false,
|
enabled = (username != "" && TwoMMatrix.matrix != null),
|
||||||
) {
|
) {
|
||||||
Text(LocalContext.current.getString(R.string.login_complete_text))
|
Text(LocalContext.current.getString(R.string.login_complete_text))
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,12 +1,8 @@
|
||||||
package eu.steffo.twom.ui.login
|
package eu.steffo.twom.ui.login
|
||||||
|
|
||||||
import android.content.Intent
|
|
||||||
import androidx.compose.foundation.layout.Row
|
|
||||||
import androidx.compose.foundation.layout.fillMaxWidth
|
|
||||||
import androidx.compose.foundation.layout.padding
|
import androidx.compose.foundation.layout.padding
|
||||||
import androidx.compose.material.icons.Icons
|
import androidx.compose.material.icons.Icons
|
||||||
import androidx.compose.material.icons.filled.ArrowBack
|
import androidx.compose.material.icons.filled.ArrowBack
|
||||||
import androidx.compose.material3.Button
|
|
||||||
import androidx.compose.material3.ExperimentalMaterial3Api
|
import androidx.compose.material3.ExperimentalMaterial3Api
|
||||||
import androidx.compose.material3.Icon
|
import androidx.compose.material3.Icon
|
||||||
import androidx.compose.material3.IconButton
|
import androidx.compose.material3.IconButton
|
||||||
|
@ -19,6 +15,7 @@ import androidx.compose.ui.platform.LocalContext
|
||||||
import androidx.compose.ui.tooling.preview.Preview
|
import androidx.compose.ui.tooling.preview.Preview
|
||||||
import eu.steffo.twom.R
|
import eu.steffo.twom.R
|
||||||
import eu.steffo.twom.ui.theme.TwoMTheme
|
import eu.steffo.twom.ui.theme.TwoMTheme
|
||||||
|
import org.matrix.android.sdk.api.session.Session
|
||||||
|
|
||||||
|
|
||||||
@OptIn(ExperimentalMaterial3Api::class)
|
@OptIn(ExperimentalMaterial3Api::class)
|
||||||
|
@ -26,7 +23,9 @@ import eu.steffo.twom.ui.theme.TwoMTheme
|
||||||
@Preview
|
@Preview
|
||||||
fun LoginActivityScaffold(
|
fun LoginActivityScaffold(
|
||||||
onBack: () -> Unit = {},
|
onBack: () -> Unit = {},
|
||||||
|
selectedHomeserver: String? = null,
|
||||||
onSelectHomeserver: () -> Unit = {},
|
onSelectHomeserver: () -> Unit = {},
|
||||||
|
onLogin: (session: Session) -> Unit = {},
|
||||||
) {
|
) {
|
||||||
TwoMTheme {
|
TwoMTheme {
|
||||||
Scaffold(
|
Scaffold(
|
||||||
|
@ -46,6 +45,7 @@ fun LoginActivityScaffold(
|
||||||
) {
|
) {
|
||||||
LoginActivityControl(
|
LoginActivityControl(
|
||||||
modifier = Modifier.padding(it),
|
modifier = Modifier.padding(it),
|
||||||
|
selectedHomeserver = selectedHomeserver,
|
||||||
onSelectHomeserver = onSelectHomeserver,
|
onSelectHomeserver = onSelectHomeserver,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
|
@ -23,8 +23,8 @@
|
||||||
<string name="login_text">To use TwoM, you need to log into a Matrix homeserver. Currently, TwoM supports only username and password authentication.</string>
|
<string name="login_text">To use TwoM, you need to log into a Matrix homeserver. Currently, TwoM supports only username and password authentication.</string>
|
||||||
<string name="login_complete_text">Log in</string>
|
<string name="login_complete_text">Log in</string>
|
||||||
<string name="login_selecthomeserver_text">Select homeserver</string>
|
<string name="login_selecthomeserver_text">Select homeserver</string>
|
||||||
<string name="login_username_placeholder">\@steffo:candy.steffo.eu</string>
|
<string name="login_username_placeholder">steffo</string>
|
||||||
<string name="login_username_supporting">The Matrix ID to login as, including the @ symbol and the homeserver name.</string>
|
<string name="login_username_supporting">The Matrix ID to login as.</string>
|
||||||
<string name="login_password_placeholder">p4ssw0rd!</string>
|
<string name="login_password_placeholder">p4ssw0rd!</string>
|
||||||
<string name="login_password_supporting">The password of the Matrix account.</string>
|
<string name="login_password_supporting">The password of the Matrix account.</string>
|
||||||
<string name="login_username_label">Username</string>
|
<string name="login_username_label">Username</string>
|
||||||
|
|
Loading…
Add table
Reference in a new issue