1
Fork 0
mirror of https://github.com/Steffo99/twom.git synced 2024-11-21 23:54:26 +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.unit.dp
import eu.steffo.twom.R
import eu.steffo.twom.ui.input.HomeserverField
import eu.steffo.twom.ui.input.HomeserverFieldState
import eu.steffo.twom.ui.input.SelectHomeserverField
import eu.steffo.twom.ui.input.SelectHomeserverFieldState
import eu.steffo.twom.ui.scaffold.LocalMatrix
import eu.steffo.twom.ui.scaffold.TwoMMatrixProvider
import eu.steffo.twom.ui.scaffold.TwoMTopAppBar
@ -45,9 +45,9 @@ fun HomeserverContents() {
val matrix = LocalMatrix.current
var homeserver by rememberSaveable { mutableStateOf("") }
var homeserverFieldState by rememberSaveable { mutableStateOf(HomeserverFieldState.Empty) }
var homeserverUrlValid by rememberSaveable { mutableStateOf<Boolean?>(null) }
var homeserverFlowValid by rememberSaveable { mutableStateOf<Boolean?>(null) }
var state by rememberSaveable { mutableStateOf(SelectHomeserverFieldState.Empty) }
var urlValid by rememberSaveable { mutableStateOf<Boolean?>(null) }
var flowValid by rememberSaveable { mutableStateOf<Boolean?>(null) }
TwoMTopAppBar {
Column(
@ -61,55 +61,55 @@ fun HomeserverContents() {
Row(
modifier = Modifier.padding(start = 20.dp, end = 20.dp, bottom = 10.dp)
) {
HomeserverField(
SelectHomeserverField(
modifier = Modifier.fillMaxWidth(),
value = homeserver,
onValueChange = OnValueChange@{
homeserver = it
homeserverUrlValid = null
homeserverFlowValid = null
homeserverFieldState = HomeserverFieldState.Empty
urlValid = null
flowValid = null
state = SelectHomeserverFieldState.Empty
if(homeserver == "") {
return@OnValueChange
}
if(!(URLUtil.isHttpUrl(homeserver) || URLUtil.isHttpsUrl(homeserver))) {
homeserverFieldState = HomeserverFieldState.Error
homeserverUrlValid = false
state = SelectHomeserverFieldState.Error
urlValid = false
return@OnValueChange
}
homeserverUrlValid = true
urlValid = true
val uri = Uri.parse(homeserver)
scope.launch ValidateFlows@{
homeserverFieldState = HomeserverFieldState.Waiting
state = SelectHomeserverFieldState.Waiting
delay(500L)
if(homeserver != it) return@ValidateFlows
val authenticationService = matrix!!.authenticationService()
homeserverFieldState = HomeserverFieldState.Validating
state = SelectHomeserverFieldState.Validating
try {
authenticationService.getLoginFlow(HomeServerConnectionConfig(
homeServerUri = uri,
))
}
catch(e: Throwable) {
homeserverFieldState = HomeserverFieldState.Error
homeserverFlowValid = false
state = SelectHomeserverFieldState.Error
flowValid = false
return@ValidateFlows
}
homeserverFieldState = HomeserverFieldState.Done
homeserverFlowValid = true
state = SelectHomeserverFieldState.Done
flowValid = true
}
},
state = homeserverFieldState,
state = state,
error =
if(homeserverUrlValid == false) LocalContext.current.getString(R.string.homeserver_error_malformedurl)
else if(homeserverFlowValid == false) LocalContext.current.getString(R.string.homeserver_error_notmatrix)
if(urlValid == false) LocalContext.current.getString(R.string.homeserver_error_malformedurl)
else if(flowValid == false) LocalContext.current.getString(R.string.homeserver_error_notmatrix)
else null
,
enabled = (LocalMatrix.current != null),
@ -120,7 +120,7 @@ fun HomeserverContents() {
) {
Button(
onClick = {},
enabled = homeserverUrlValid == true && homeserverFlowValid == true,
enabled = urlValid == true && flowValid == true,
modifier = Modifier.fillMaxWidth(),
) {
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
enum class HomeserverFieldState {
enum class SelectHomeserverFieldState {
Empty,
Waiting,
Validating,
@ -27,12 +27,12 @@ enum class HomeserverFieldState {
@Composable
@Preview
fun HomeserverField(
fun SelectHomeserverField(
modifier: Modifier = Modifier,
value: String = "",
onValueChange: (String) -> Unit = {},
enabled: Boolean = true,
state: HomeserverFieldState = HomeserverFieldState.Empty,
state: SelectHomeserverFieldState = SelectHomeserverFieldState.Empty,
error: String? = null,
) {
TextField(
@ -50,11 +50,11 @@ fun HomeserverField(
trailingIcon = {
Icon(
when(state) {
HomeserverFieldState.Empty -> Icons.Default.Build
HomeserverFieldState.Waiting -> Icons.Default.Create
HomeserverFieldState.Validating -> Icons.Default.Send
HomeserverFieldState.Error -> Icons.Default.Close
HomeserverFieldState.Done -> Icons.Default.Check
SelectHomeserverFieldState.Empty -> Icons.Default.Build
SelectHomeserverFieldState.Waiting -> Icons.Default.Create
SelectHomeserverFieldState.Validating -> Icons.Default.Send
SelectHomeserverFieldState.Error -> Icons.Default.Close
SelectHomeserverFieldState.Done -> Icons.Default.Check
},
LocalContext.current.getString(R.string.homeserver_trailingicon_validating_description)
)