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

Improve code readability

This commit is contained in:
Steffo 2023-11-19 17:14:14 +01:00
parent 2a9a3d240d
commit 80d30b3ea6
Signed by: steffo
GPG key ID: 2A24051445686895
2 changed files with 30 additions and 30 deletions

View file

@ -19,8 +19,8 @@ import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.tooling.preview.Preview import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.dp
import eu.steffo.twom.R import eu.steffo.twom.R
import eu.steffo.twom.ui.input.HomeserverField import eu.steffo.twom.ui.input.SelectHomeserverField
import eu.steffo.twom.ui.input.HomeserverFieldState import eu.steffo.twom.ui.input.SelectHomeserverFieldState
import eu.steffo.twom.ui.scaffold.LocalMatrix import eu.steffo.twom.ui.scaffold.LocalMatrix
import eu.steffo.twom.ui.scaffold.TwoMMatrixProvider import eu.steffo.twom.ui.scaffold.TwoMMatrixProvider
import eu.steffo.twom.ui.scaffold.TwoMTopAppBar import eu.steffo.twom.ui.scaffold.TwoMTopAppBar
@ -45,9 +45,9 @@ fun HomeserverContents() {
val matrix = LocalMatrix.current val matrix = LocalMatrix.current
var homeserver by rememberSaveable { mutableStateOf("") } var homeserver by rememberSaveable { mutableStateOf("") }
var homeserverFieldState by rememberSaveable { mutableStateOf(HomeserverFieldState.Empty) } var state by rememberSaveable { mutableStateOf(SelectHomeserverFieldState.Empty) }
var homeserverUrlValid by rememberSaveable { mutableStateOf<Boolean?>(null) } var urlValid by rememberSaveable { mutableStateOf<Boolean?>(null) }
var homeserverFlowValid by rememberSaveable { mutableStateOf<Boolean?>(null) } var flowValid by rememberSaveable { mutableStateOf<Boolean?>(null) }
TwoMTopAppBar { TwoMTopAppBar {
Column( Column(
@ -61,55 +61,55 @@ fun HomeserverContents() {
Row( Row(
modifier = Modifier.padding(start = 20.dp, end = 20.dp, bottom = 10.dp) modifier = Modifier.padding(start = 20.dp, end = 20.dp, bottom = 10.dp)
) { ) {
HomeserverField( SelectHomeserverField(
modifier = Modifier.fillMaxWidth(), modifier = Modifier.fillMaxWidth(),
value = homeserver, value = homeserver,
onValueChange = OnValueChange@{ onValueChange = OnValueChange@{
homeserver = it homeserver = it
homeserverUrlValid = null urlValid = null
homeserverFlowValid = null flowValid = null
homeserverFieldState = HomeserverFieldState.Empty state = SelectHomeserverFieldState.Empty
if(homeserver == "") { if(homeserver == "") {
return@OnValueChange return@OnValueChange
} }
if(!(URLUtil.isHttpUrl(homeserver) || URLUtil.isHttpsUrl(homeserver))) { if(!(URLUtil.isHttpUrl(homeserver) || URLUtil.isHttpsUrl(homeserver))) {
homeserverFieldState = HomeserverFieldState.Error state = SelectHomeserverFieldState.Error
homeserverUrlValid = false urlValid = false
return@OnValueChange return@OnValueChange
} }
homeserverUrlValid = true urlValid = true
val uri = Uri.parse(homeserver) val uri = Uri.parse(homeserver)
scope.launch ValidateFlows@{ scope.launch ValidateFlows@{
homeserverFieldState = HomeserverFieldState.Waiting state = SelectHomeserverFieldState.Waiting
delay(500L) delay(500L)
if(homeserver != it) return@ValidateFlows if(homeserver != it) return@ValidateFlows
val authenticationService = matrix!!.authenticationService() val authenticationService = matrix!!.authenticationService()
homeserverFieldState = HomeserverFieldState.Validating state = SelectHomeserverFieldState.Validating
try { try {
authenticationService.getLoginFlow(HomeServerConnectionConfig( authenticationService.getLoginFlow(HomeServerConnectionConfig(
homeServerUri = uri, homeServerUri = uri,
)) ))
} }
catch(e: Throwable) { catch(e: Throwable) {
homeserverFieldState = HomeserverFieldState.Error state = SelectHomeserverFieldState.Error
homeserverFlowValid = false flowValid = false
return@ValidateFlows return@ValidateFlows
} }
homeserverFieldState = HomeserverFieldState.Done state = SelectHomeserverFieldState.Done
homeserverFlowValid = true flowValid = true
} }
}, },
state = homeserverFieldState, state = state,
error = error =
if(homeserverUrlValid == false) LocalContext.current.getString(R.string.homeserver_error_malformedurl) if(urlValid == false) LocalContext.current.getString(R.string.homeserver_error_malformedurl)
else if(homeserverFlowValid == false) LocalContext.current.getString(R.string.homeserver_error_notmatrix) else if(flowValid == false) LocalContext.current.getString(R.string.homeserver_error_notmatrix)
else null else null
, ,
enabled = (LocalMatrix.current != null), enabled = (LocalMatrix.current != null),
@ -120,7 +120,7 @@ fun HomeserverContents() {
) { ) {
Button( Button(
onClick = {}, onClick = {},
enabled = homeserverUrlValid == true && homeserverFlowValid == true, enabled = urlValid == true && flowValid == true,
modifier = Modifier.fillMaxWidth(), modifier = Modifier.fillMaxWidth(),
) { ) {
Text(LocalContext.current.getString(R.string.login_button_continue_text)) Text(LocalContext.current.getString(R.string.login_button_continue_text))

View file

@ -16,7 +16,7 @@ import androidx.compose.ui.tooling.preview.Preview
import eu.steffo.twom.R import eu.steffo.twom.R
enum class HomeserverFieldState { enum class SelectHomeserverFieldState {
Empty, Empty,
Waiting, Waiting,
Validating, Validating,
@ -27,12 +27,12 @@ enum class HomeserverFieldState {
@Composable @Composable
@Preview @Preview
fun HomeserverField( fun SelectHomeserverField(
modifier: Modifier = Modifier, modifier: Modifier = Modifier,
value: String = "", value: String = "",
onValueChange: (String) -> Unit = {}, onValueChange: (String) -> Unit = {},
enabled: Boolean = true, enabled: Boolean = true,
state: HomeserverFieldState = HomeserverFieldState.Empty, state: SelectHomeserverFieldState = SelectHomeserverFieldState.Empty,
error: String? = null, error: String? = null,
) { ) {
TextField( TextField(
@ -50,11 +50,11 @@ fun HomeserverField(
trailingIcon = { trailingIcon = {
Icon( Icon(
when(state) { when(state) {
HomeserverFieldState.Empty -> Icons.Default.Build SelectHomeserverFieldState.Empty -> Icons.Default.Build
HomeserverFieldState.Waiting -> Icons.Default.Create SelectHomeserverFieldState.Waiting -> Icons.Default.Create
HomeserverFieldState.Validating -> Icons.Default.Send SelectHomeserverFieldState.Validating -> Icons.Default.Send
HomeserverFieldState.Error -> Icons.Default.Close SelectHomeserverFieldState.Error -> Icons.Default.Close
HomeserverFieldState.Done -> Icons.Default.Check SelectHomeserverFieldState.Done -> Icons.Default.Check
}, },
LocalContext.current.getString(R.string.homeserver_trailingicon_validating_description) LocalContext.current.getString(R.string.homeserver_trailingicon_validating_description)
) )