From 9f7df9bfa4c2d0c69fea5a1d49c854e296496209 Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Wed, 29 Nov 2023 01:51:01 +0100 Subject: [PATCH] Handle log out --- app/src/main/AndroidManifest.xml | 2 +- .../steffo/twom/login/LoginActivityControl.kt | 3 + .../java/eu/steffo/twom/main/MainActivity.kt | 85 +++++++++++++++++++ .../MainActivityLoggedInControl.kt} | 4 +- .../MainActivityNotLoggedInControl.kt} | 2 +- .../MainActivityScaffold.kt} | 23 ++--- .../eu/steffo/twom/main/ProfileIconButton.kt | 57 +++++++++++++ .../eu/steffo/twom/matrix/MatrixActivity.kt | 62 -------------- app/src/main/res/values/strings.xml | 1 + 9 files changed, 157 insertions(+), 82 deletions(-) create mode 100644 app/src/main/java/eu/steffo/twom/main/MainActivity.kt rename app/src/main/java/eu/steffo/twom/{matrix/MatrixActivityLoggedInControl.kt => main/MainActivityLoggedInControl.kt} (90%) rename app/src/main/java/eu/steffo/twom/{matrix/MatrixActivityNotLoggedInControl.kt => main/MainActivityNotLoggedInControl.kt} (97%) rename app/src/main/java/eu/steffo/twom/{matrix/MatrixActivityScaffold.kt => main/MainActivityScaffold.kt} (66%) create mode 100644 app/src/main/java/eu/steffo/twom/main/ProfileIconButton.kt delete mode 100644 app/src/main/java/eu/steffo/twom/matrix/MatrixActivity.kt diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 43049df..8022ad1 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -31,7 +31,7 @@ diff --git a/app/src/main/java/eu/steffo/twom/login/LoginActivityControl.kt b/app/src/main/java/eu/steffo/twom/login/LoginActivityControl.kt index e2baee8..a588db9 100644 --- a/app/src/main/java/eu/steffo/twom/login/LoginActivityControl.kt +++ b/app/src/main/java/eu/steffo/twom/login/LoginActivityControl.kt @@ -98,6 +98,9 @@ fun LoginActivityControl( loginStep = LoginStep.SERVICE val auth = TwoMMatrix.matrix.authenticationService() + Log.d("Login", "Resetting authentication service...") + auth.reset() + Log.d("Login", "Retrieving .well-known data for: $username") loginStep = LoginStep.WELLKNOWN lateinit var wellKnown: WellknownResult diff --git a/app/src/main/java/eu/steffo/twom/main/MainActivity.kt b/app/src/main/java/eu/steffo/twom/main/MainActivity.kt new file mode 100644 index 0000000..e27717c --- /dev/null +++ b/app/src/main/java/eu/steffo/twom/main/MainActivity.kt @@ -0,0 +1,85 @@ +package eu.steffo.twom.main + +import android.content.Intent +import android.os.Bundle +import android.util.Log +import androidx.activity.ComponentActivity +import androidx.activity.compose.setContent +import androidx.activity.result.ActivityResult +import androidx.activity.result.ActivityResultLauncher +import androidx.activity.result.contract.ActivityResultContracts +import androidx.lifecycle.lifecycleScope +import eu.steffo.twom.login.LoginActivity +import eu.steffo.twom.matrix.TwoMMatrix +import kotlinx.coroutines.launch +import org.matrix.android.sdk.api.session.Session + + +class MainActivity : ComponentActivity() { + private lateinit var loginLauncher: ActivityResultLauncher + + private var session: Session? = null + + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + + TwoMMatrix.ensureMatrix(applicationContext) + + loginLauncher = + registerForActivityResult( + ActivityResultContracts.StartActivityForResult(), + this::handleLoginResult + ) + + // This calls recompose() by itself + openLastSession() + } + + private fun openLastSession() { + session = TwoMMatrix.matrix.authenticationService().getLastAuthenticatedSession() + session?.open() + recompose() + } + + private fun recompose() { + Log.d("Main", "Recomposing...") + setContent { + MatrixActivityScaffold( + onClickLogin = this::onClickLogin, + onClickLogout = this::onClickLogout, + session = session, + ) + } + } + + private fun onClickLogin() { + Log.d("Main", "Clicked login, launching login activity...") + loginLauncher.launch(Intent(applicationContext, LoginActivity::class.java)) + } + + private fun handleLoginResult(result: ActivityResult) { + when (result.resultCode) { + RESULT_OK -> { + Log.d( + "Main", + "Login activity returned a successful result, trying to get session..." + ) + openLastSession() + } + + else -> { + Log.d("Main", "Login activity was cancelled.") + } + } + } + + private fun onClickLogout() { + lifecycleScope.launch { + Log.d("Main", "Clicked logout, signing out...") + session!!.signOutService().signOut(true) + session = null + Log.d("Main", "Done logging out, recomposing...") + recompose() + } + } +} diff --git a/app/src/main/java/eu/steffo/twom/matrix/MatrixActivityLoggedInControl.kt b/app/src/main/java/eu/steffo/twom/main/MainActivityLoggedInControl.kt similarity index 90% rename from app/src/main/java/eu/steffo/twom/matrix/MatrixActivityLoggedInControl.kt rename to app/src/main/java/eu/steffo/twom/main/MainActivityLoggedInControl.kt index 241f68f..7a9c21b 100644 --- a/app/src/main/java/eu/steffo/twom/matrix/MatrixActivityLoggedInControl.kt +++ b/app/src/main/java/eu/steffo/twom/main/MainActivityLoggedInControl.kt @@ -1,4 +1,4 @@ -package eu.steffo.twom.matrix +package eu.steffo.twom.main import androidx.compose.foundation.layout.Column import androidx.compose.foundation.layout.Row @@ -11,7 +11,7 @@ import eu.steffo.twom.theme.TwoMPadding import org.matrix.android.sdk.api.session.Session @Composable -fun MatrixActivityLoggedInControl( +fun MainActivityLoggedInControl( session: Session, modifier: Modifier = Modifier, ) { diff --git a/app/src/main/java/eu/steffo/twom/matrix/MatrixActivityNotLoggedInControl.kt b/app/src/main/java/eu/steffo/twom/main/MainActivityNotLoggedInControl.kt similarity index 97% rename from app/src/main/java/eu/steffo/twom/matrix/MatrixActivityNotLoggedInControl.kt rename to app/src/main/java/eu/steffo/twom/main/MainActivityNotLoggedInControl.kt index 03db1a7..ae3d520 100644 --- a/app/src/main/java/eu/steffo/twom/matrix/MatrixActivityNotLoggedInControl.kt +++ b/app/src/main/java/eu/steffo/twom/main/MainActivityNotLoggedInControl.kt @@ -1,4 +1,4 @@ -package eu.steffo.twom.matrix +package eu.steffo.twom.main import androidx.compose.foundation.layout.Column import androidx.compose.foundation.layout.Row diff --git a/app/src/main/java/eu/steffo/twom/matrix/MatrixActivityScaffold.kt b/app/src/main/java/eu/steffo/twom/main/MainActivityScaffold.kt similarity index 66% rename from app/src/main/java/eu/steffo/twom/matrix/MatrixActivityScaffold.kt rename to app/src/main/java/eu/steffo/twom/main/MainActivityScaffold.kt index 7fc1d8f..f24de96 100644 --- a/app/src/main/java/eu/steffo/twom/matrix/MatrixActivityScaffold.kt +++ b/app/src/main/java/eu/steffo/twom/main/MainActivityScaffold.kt @@ -1,12 +1,8 @@ -package eu.steffo.twom.matrix +package eu.steffo.twom.main import androidx.compose.foundation.layout.padding -import androidx.compose.material.icons.Icons -import androidx.compose.material.icons.filled.AccountCircle import androidx.compose.material3.CenterAlignedTopAppBar import androidx.compose.material3.ExperimentalMaterial3Api -import androidx.compose.material3.Icon -import androidx.compose.material3.IconButton import androidx.compose.material3.Scaffold import androidx.compose.material3.Text import androidx.compose.runtime.Composable @@ -22,6 +18,7 @@ import org.matrix.android.sdk.api.session.Session @Preview fun MatrixActivityScaffold( onClickLogin: () -> Unit = {}, + onClickLogout: () -> Unit = {}, session: Session? = null, ) { TwoMTheme { @@ -32,22 +29,16 @@ fun MatrixActivityScaffold( Text(LocalContext.current.getString(R.string.app_name)) }, actions = { - if (session != null) { - IconButton( - onClick = {} - ) { - Icon( - Icons.Filled.AccountCircle, - LocalContext.current.getString(R.string.account_label) - ) - } - } + ProfileIconButton( + session = session, + onClickLogout = onClickLogout + ) }, ) } ) { if (session != null) { - MatrixActivityLoggedInControl( + MainActivityLoggedInControl( modifier = Modifier.padding(it), session = session, ) diff --git a/app/src/main/java/eu/steffo/twom/main/ProfileIconButton.kt b/app/src/main/java/eu/steffo/twom/main/ProfileIconButton.kt new file mode 100644 index 0000000..daf5484 --- /dev/null +++ b/app/src/main/java/eu/steffo/twom/main/ProfileIconButton.kt @@ -0,0 +1,57 @@ +package eu.steffo.twom.main + +import androidx.compose.foundation.layout.Box +import androidx.compose.material.icons.Icons +import androidx.compose.material.icons.filled.AccountCircle +import androidx.compose.material3.DropdownMenu +import androidx.compose.material3.DropdownMenuItem +import androidx.compose.material3.Icon +import androidx.compose.material3.IconButton +import androidx.compose.material3.Text +import androidx.compose.runtime.Composable +import androidx.compose.runtime.getValue +import androidx.compose.runtime.mutableStateOf +import androidx.compose.runtime.remember +import androidx.compose.runtime.setValue +import androidx.compose.ui.Modifier +import androidx.compose.ui.platform.LocalContext +import androidx.compose.ui.res.stringResource +import androidx.compose.ui.tooling.preview.Preview +import eu.steffo.twom.R +import org.matrix.android.sdk.api.session.Session + +@Composable +@Preview(showBackground = true) +fun ProfileIconButton( + modifier: Modifier = Modifier, + session: Session? = null, + onClickLogout: () -> Unit = {}, +) { + var expanded by remember { mutableStateOf(false) } + + Box { + IconButton( + enabled = (session != null), + onClick = { expanded = true }, + ) { + Icon( + Icons.Filled.AccountCircle, + LocalContext.current.getString(R.string.account_label) + ) + } + DropdownMenu( + expanded = expanded, + onDismissRequest = { expanded = false }, + ) { + if (session != null) { + DropdownMenuItem( + text = { + Text(stringResource(id = R.string.profile_logout_text)) + }, + onClick = onClickLogout + ) + } + } + } + +} \ No newline at end of file diff --git a/app/src/main/java/eu/steffo/twom/matrix/MatrixActivity.kt b/app/src/main/java/eu/steffo/twom/matrix/MatrixActivity.kt deleted file mode 100644 index 4dcc9bf..0000000 --- a/app/src/main/java/eu/steffo/twom/matrix/MatrixActivity.kt +++ /dev/null @@ -1,62 +0,0 @@ -package eu.steffo.twom.matrix - -import android.content.Intent -import android.os.Bundle -import android.util.Log -import androidx.activity.ComponentActivity -import androidx.activity.compose.setContent -import androidx.activity.result.ActivityResultLauncher -import androidx.activity.result.contract.ActivityResultContracts -import androidx.compose.material3.ExperimentalMaterial3Api -import eu.steffo.twom.login.LoginActivity -import org.matrix.android.sdk.api.session.Session - - -@OptIn(ExperimentalMaterial3Api::class) -class MatrixActivity : ComponentActivity() { - private lateinit var loginLauncher: ActivityResultLauncher - - private var session: Session? = null - - override fun onCreate(savedInstanceState: Bundle?) { - super.onCreate(savedInstanceState) - actionBar?.hide() - - TwoMMatrix.ensureMatrix(applicationContext) - - loginLauncher = - registerForActivityResult(ActivityResultContracts.StartActivityForResult()) HandleResult@{ - Log.d("Matrix", "LoginActivity has returned a result!") - - when (it.resultCode) { - RESULT_OK -> { - session = - TwoMMatrix.matrix.authenticationService().getLastAuthenticatedSession() - - setContent { - MatrixActivityScaffold( - onClickLogin = this::onClickLogin, - session = session, - ) - } - } - } - } - - // Check if there's a session already stored - session = TwoMMatrix.matrix.authenticationService().getLastAuthenticatedSession() - - setContent { - MatrixActivityScaffold( - onClickLogin = this::onClickLogin, - session = session, - ) - } - - // No onStart is needed, as session already sets content when it's changed - } - - private fun onClickLogin() { - loginLauncher.launch(Intent(applicationContext, LoginActivity::class.java)) - } -} diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 40fc6c0..70a2bf3 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -23,4 +23,5 @@ Login You are logged in. My account + Logout \ No newline at end of file