From 1204669c8832bb8455bcc3544c14a8fe4e71ff42 Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Wed, 22 Nov 2023 04:13:08 +0100 Subject: [PATCH] Experiment with login some more --- .../java/eu/steffo/twom/matrix/TwoMMatrix.kt | 20 ++++++----- .../eu/steffo/twom/ui/login/LoginActivity.kt | 29 +++++++++------- .../twom/ui/login/LoginActivityControl.kt | 34 +++++++++++++++++-- .../twom/ui/login/LoginActivityScaffold.kt | 8 ++--- app/src/main/res/values/strings.xml | 4 +-- 5 files changed, 64 insertions(+), 31 deletions(-) diff --git a/app/src/main/java/eu/steffo/twom/matrix/TwoMMatrix.kt b/app/src/main/java/eu/steffo/twom/matrix/TwoMMatrix.kt index 98f25d0..8487d2f 100644 --- a/app/src/main/java/eu/steffo/twom/matrix/TwoMMatrix.kt +++ b/app/src/main/java/eu/steffo/twom/matrix/TwoMMatrix.kt @@ -2,7 +2,6 @@ package eu.steffo.twom.matrix import TwoMRoomDisplayNameFallbackProvider import android.content.Context -import androidx.compose.runtime.currentComposer import org.matrix.android.sdk.api.Matrix import org.matrix.android.sdk.api.MatrixConfiguration import org.matrix.android.sdk.api.session.Session @@ -26,7 +25,15 @@ object TwoMMatrix { } var session: Session? = null - private set + set(value) { + if (field != null) { + closeSession() + } + field = value + if (field != null) { + openSession() + } + } fun tryInitSessionFromStorage() { val lastSession = matrix?.authenticationService()?.getLastAuthenticatedSession() @@ -36,21 +43,16 @@ object TwoMMatrix { } // TODO: Does this throw an error if the session is already open? - fun openSession() { + private fun openSession() { val currentSession = session ?: throw SessionNotInitializedError() currentSession.open() currentSession.syncService().startSync(true) } // TODO: Does this throw an error if the session is already closed? - fun closeSession() { + private fun closeSession() { val currentSession = session ?: throw SessionNotInitializedError() currentSession.close() currentSession.syncService().stopSync() } - - fun clearSession() { - closeSession() - session = null - } } diff --git a/app/src/main/java/eu/steffo/twom/ui/login/LoginActivity.kt b/app/src/main/java/eu/steffo/twom/ui/login/LoginActivity.kt index 5901921..5c09442 100644 --- a/app/src/main/java/eu/steffo/twom/ui/login/LoginActivity.kt +++ b/app/src/main/java/eu/steffo/twom/ui/login/LoginActivity.kt @@ -7,19 +7,7 @@ import androidx.activity.ComponentActivity import androidx.activity.compose.setContent import androidx.activity.result.ActivityResultLauncher 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.theme.TwoMTheme class LoginActivity : ComponentActivity() { @@ -29,7 +17,22 @@ class LoginActivity : ComponentActivity() { super.onCreate(savedInstanceState) 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 + ) + ) + }, + ) + } } } diff --git a/app/src/main/java/eu/steffo/twom/ui/login/LoginActivityControl.kt b/app/src/main/java/eu/steffo/twom/ui/login/LoginActivityControl.kt index 4ef33fa..cfca323 100644 --- a/app/src/main/java/eu/steffo/twom/ui/login/LoginActivityControl.kt +++ b/app/src/main/java/eu/steffo/twom/ui/login/LoginActivityControl.kt @@ -9,26 +9,34 @@ import androidx.compose.material3.TextField import androidx.compose.runtime.Composable import androidx.compose.runtime.getValue import androidx.compose.runtime.mutableStateOf +import androidx.compose.runtime.rememberCoroutineScope import androidx.compose.runtime.saveable.rememberSaveable import androidx.compose.runtime.setValue import androidx.compose.ui.Modifier import androidx.compose.ui.platform.LocalContext import androidx.compose.ui.tooling.preview.Preview import eu.steffo.twom.R +import eu.steffo.twom.matrix.TwoMMatrix import eu.steffo.twom.ui.BASE_PADDING +import kotlinx.coroutines.launch +import org.matrix.android.sdk.api.session.Session @Composable @Preview(showBackground = true) fun LoginActivityControl( modifier: Modifier = Modifier, + selectedHomeserver: String? = null, onSelectHomeserver: () -> Unit = {}, - onComplete: (username: String, password: String) -> Unit = { _, _ -> }, + onLogin: (session: Session) -> Unit = {}, ) { + val scope = rememberCoroutineScope() var username by rememberSaveable { mutableStateOf("") } var password by rememberSaveable { mutableStateOf("") } + var loggingIn by rememberSaveable { mutableStateOf(false) } + Column(modifier) { Row(BASE_PADDING) { Text(LocalContext.current.getString(R.string.login_text)) @@ -56,6 +64,15 @@ fun LoginActivityControl( supportingText = { 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) { @@ -72,15 +89,26 @@ fun LoginActivityControl( supportingText = { Text(LocalContext.current.getString(R.string.login_password_supporting)) }, + enabled = (selectedHomeserver != null), ) } Row(BASE_PADDING) { Button( modifier = Modifier.fillMaxWidth(), 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)) } diff --git a/app/src/main/java/eu/steffo/twom/ui/login/LoginActivityScaffold.kt b/app/src/main/java/eu/steffo/twom/ui/login/LoginActivityScaffold.kt index c701822..3b2a91f 100644 --- a/app/src/main/java/eu/steffo/twom/ui/login/LoginActivityScaffold.kt +++ b/app/src/main/java/eu/steffo/twom/ui/login/LoginActivityScaffold.kt @@ -1,12 +1,8 @@ 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.material.icons.Icons import androidx.compose.material.icons.filled.ArrowBack -import androidx.compose.material3.Button import androidx.compose.material3.ExperimentalMaterial3Api import androidx.compose.material3.Icon import androidx.compose.material3.IconButton @@ -19,6 +15,7 @@ import androidx.compose.ui.platform.LocalContext import androidx.compose.ui.tooling.preview.Preview import eu.steffo.twom.R import eu.steffo.twom.ui.theme.TwoMTheme +import org.matrix.android.sdk.api.session.Session @OptIn(ExperimentalMaterial3Api::class) @@ -26,7 +23,9 @@ import eu.steffo.twom.ui.theme.TwoMTheme @Preview fun LoginActivityScaffold( onBack: () -> Unit = {}, + selectedHomeserver: String? = null, onSelectHomeserver: () -> Unit = {}, + onLogin: (session: Session) -> Unit = {}, ) { TwoMTheme { Scaffold( @@ -46,6 +45,7 @@ fun LoginActivityScaffold( ) { LoginActivityControl( modifier = Modifier.padding(it), + selectedHomeserver = selectedHomeserver, onSelectHomeserver = onSelectHomeserver, ) } diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index f046f15..1df452a 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -23,8 +23,8 @@ To use TwoM, you need to log into a Matrix homeserver. Currently, TwoM supports only username and password authentication. Log in Select homeserver - \@steffo:candy.steffo.eu - The Matrix ID to login as, including the @ symbol and the homeserver name. + steffo + The Matrix ID to login as. p4ssw0rd! The password of the Matrix account. Username