1
Fork 0
mirror of https://github.com/Steffo99/twom.git synced 2024-11-22 08:04:26 +00:00

Handle localization for static errors

This commit is contained in:
Steffo 2024-01-06 18:51:02 +01:00
parent a7240b50a5
commit 59702f1b15
Signed by: steffo
GPG key ID: 2A24051445686895
2 changed files with 26 additions and 13 deletions

View file

@ -18,6 +18,7 @@ import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.runtime.setValue import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.tooling.preview.Preview import androidx.compose.ui.tooling.preview.Preview
import eu.steffo.twom.R import eu.steffo.twom.R
import eu.steffo.twom.matrix.TwoMMatrix import eu.steffo.twom.matrix.TwoMMatrix
@ -44,13 +45,14 @@ fun LoginActivityContent(
var password by rememberSaveable { mutableStateOf("") } var password by rememberSaveable { mutableStateOf("") }
var loginStep by rememberSaveable { mutableStateOf(LoginStep.NONE) } var loginStep by rememberSaveable { mutableStateOf(LoginStep.NONE) }
var errorMessage by rememberSaveable { mutableStateOf<String?>(null) } var errorMessageId by rememberSaveable { mutableStateOf<Int?>(null) }
var errorMessageString by rememberSaveable { mutableStateOf<String?>(null) }
Column(modifier) { Column(modifier) {
LinearProgressIndicator( LinearProgressIndicator(
modifier = Modifier.fillMaxWidth(), modifier = Modifier.fillMaxWidth(),
progress = loginStep.step.toFloat() / LoginStep.DONE.step.toFloat(), progress = loginStep.step.toFloat() / LoginStep.DONE.step.toFloat(),
color = if (errorMessage != null) MaterialTheme.colorScheme.error else ProgressIndicatorDefaults.linearColor color = if (errorMessageId != null || errorMessageString != null) MaterialTheme.colorScheme.error else ProgressIndicatorDefaults.linearColor
) )
Row(TwoMPadding.base) { Row(TwoMPadding.base) {
Text(LocalContext.current.getString(R.string.login_text)) Text(LocalContext.current.getString(R.string.login_text))
@ -94,7 +96,8 @@ fun LoginActivityContent(
modifier = Modifier.fillMaxWidth(), modifier = Modifier.fillMaxWidth(),
onClick = { onClick = {
scope.launch Login@{ scope.launch Login@{
errorMessage = null errorMessageId = null
errorMessageString = null
Log.d("Login", "Getting authentication service...") Log.d("Login", "Getting authentication service...")
loginStep = LoginStep.SERVICE loginStep = LoginStep.SERVICE
@ -114,7 +117,7 @@ fun LoginActivityContent(
"User seems to have input an invalid Matrix ID: $username", "User seems to have input an invalid Matrix ID: $username",
e e
) )
errorMessage = "The input Matrix ID is invalid." errorMessageId = R.string.login_error_username_invalid
return@Login return@Login
} catch (e: Throwable) { } catch (e: Throwable) {
// TODO: It sure would be nice to know which exceptions can be thrown here. // TODO: It sure would be nice to know which exceptions can be thrown here.
@ -123,7 +126,7 @@ fun LoginActivityContent(
"Something went wrong while retrieving .well-known data for: $username", "Something went wrong while retrieving .well-known data for: $username",
e e
) )
errorMessage = e.message errorMessageString = e.toString()
return@Login return@Login
} }
if (wellKnown !is WellknownResult.Prompt) { if (wellKnown !is WellknownResult.Prompt) {
@ -131,8 +134,7 @@ fun LoginActivityContent(
"Login", "Login",
"Data is not .well-known for: $username" "Data is not .well-known for: $username"
) )
errorMessage = errorMessageId = R.string.login_error_wellknown_missing
"Non .well-known Matrix servers are not currently supported by TwoM."
return@Login return@Login
} }
@ -153,7 +155,7 @@ fun LoginActivityContent(
"Something went wrong while retrieving login flows for: ${wellKnown.homeServerUrl}", "Something went wrong while retrieving login flows for: ${wellKnown.homeServerUrl}",
e e
) )
errorMessage = e.message errorMessageString = e.message
return@Login return@Login
} }
@ -169,7 +171,7 @@ fun LoginActivityContent(
"Something went wrong while creating the login wizard.", "Something went wrong while creating the login wizard.",
e e
) )
errorMessage = e.message errorMessageString = e.message
return@Login return@Login
} }
@ -189,7 +191,7 @@ fun LoginActivityContent(
"Something went wrong while logging in as: $username", "Something went wrong while logging in as: $username",
e e
) )
errorMessage = e.message errorMessageString = e.message
return@Login return@Login
} }
@ -202,14 +204,23 @@ fun LoginActivityContent(
onLogin(session) onLogin(session)
} }
}, },
enabled = (username != "" && (loginStep == LoginStep.NONE || errorMessage != null)), enabled = (username != "" && (loginStep == LoginStep.NONE || errorMessageId != null)),
) { ) {
Text(LocalContext.current.getString(R.string.login_complete_text)) Text(LocalContext.current.getString(R.string.login_complete_text))
} }
} }
if (errorMessage != null) { if (errorMessageId != null) {
Row(TwoMPadding.base) { Row(TwoMPadding.base) {
ErrorText(text = errorMessage ?: "Unknown error") // FIXME: Can this cause an error?
ErrorText(
text = if (errorMessageString != null) {
errorMessageString!!
} else if (errorMessageId != null) {
stringResource(errorMessageId!!)
} else {
"Unknown error"
}
)
} }
} }
} }

View file

@ -34,4 +34,6 @@
<string name="create_complete_text">Create</string> <string name="create_complete_text">Create</string>
<string name="room_options_label">Room options</string> <string name="room_options_label">Room options</string>
<string name="error">Error</string> <string name="error">Error</string>
<string name="login_error_username_invalid">The entered username is not a valid Matrix ID.</string>
<string name="login_error_wellknown_missing">Failed to detect well-known data for the specified Matrix homeserver. The server might not exist, be offline, or not be supporting well-known lookups.</string>
</resources> </resources>