1
Fork 0
mirror of https://github.com/Steffo99/twom.git synced 2024-11-25 01:24:24 +00:00

Handle log out

This commit is contained in:
Steffo 2023-11-29 01:51:01 +01:00
parent 17dec236c2
commit 9f7df9bfa4
Signed by: steffo
GPG key ID: 2A24051445686895
9 changed files with 157 additions and 82 deletions

View file

@ -31,7 +31,7 @@
</provider>
<activity
android:name=".matrix.MatrixActivity"
android:name=".main.MainActivity"
android:exported="true"
android:theme="@android:style/Theme.NoTitleBar">
<intent-filter>

View file

@ -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

View file

@ -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<Intent>
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()
}
}
}

View file

@ -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,
) {

View file

@ -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

View file

@ -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,
)

View file

@ -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
)
}
}
}
}

View file

@ -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<Intent>
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))
}
}

View file

@ -23,4 +23,5 @@
<string name="notloggedin_login_text">Login</string>
<string name="loggedin_text">You are logged in.</string>
<string name="account_label">My account</string>
<string name="profile_logout_text">Logout</string>
</resources>