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

Experiment with login some more

This commit is contained in:
Steffo 2023-11-22 04:13:08 +01:00
parent 0937c4ec45
commit 1204669c88
Signed by: steffo
GPG key ID: 2A24051445686895
5 changed files with 64 additions and 31 deletions

View file

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

View file

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

View file

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

View file

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

View file

@ -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_complete_text">Log in</string>
<string name="login_selecthomeserver_text">Select homeserver</string>
<string name="login_username_placeholder">\@steffo:candy.steffo.eu</string>
<string name="login_username_supporting">The Matrix ID to login as, including the @ symbol and the homeserver name.</string>
<string name="login_username_placeholder">steffo</string>
<string name="login_username_supporting">The Matrix ID to login as.</string>
<string name="login_password_placeholder">p4ssw0rd!</string>
<string name="login_password_supporting">The password of the Matrix account.</string>
<string name="login_username_label">Username</string>