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

Refactor LocalizableError to be immutable

This commit is contained in:
Steffo 2024-01-30 03:29:02 +01:00
parent 99ae904633
commit deb0b8be61
Signed by: steffo
GPG key ID: 5ADA3868646C3FC0
5 changed files with 44 additions and 70 deletions

View file

@ -4,53 +4,24 @@ import androidx.annotation.StringRes
import androidx.compose.runtime.Composable import androidx.compose.runtime.Composable
import androidx.compose.ui.res.stringResource import androidx.compose.ui.res.stringResource
class LocalizableError { data class LocalizableError(
@StringRes @StringRes val stringResourceId: Int,
var stringResourceId: Int? = null val throwable: Throwable? = null,
private set )
var throwable: Throwable? = null @Composable
private set fun LocalizableError?.render(): String? {
return if (this == null) {
fun set(stringResourceId: Int) { null
this.stringResourceId = stringResourceId } else if (this.throwable == null) {
this.throwable = null stringResource(stringResourceId)
} } else {
stringResource(stringResourceId, throwable.toString())
fun set(stringResourceId: Int, throwable: Throwable) {
this.stringResourceId = stringResourceId
this.throwable = throwable
}
fun clear() {
this.stringResourceId = null
this.throwable = null
}
fun occurred(): Boolean {
return stringResourceId != null
}
@Composable
fun renderString(): String? {
val stringResourceId = this.stringResourceId
val throwable = this.throwable
return if (stringResourceId == null) {
null
} else if (throwable == null) {
stringResource(stringResourceId)
} else {
stringResource(stringResourceId, throwable.toString())
}
}
@Composable
fun Show(contents: @Composable (rendered: String) -> Unit) {
val rendered = renderString()
if (rendered != null) {
contents(rendered)
}
} }
} }
@Composable
fun LocalizableError?.Display(contents: @Composable (rendered: String) -> Unit) {
val rendered = this.render() ?: return
contents(rendered)
}

View file

@ -21,6 +21,7 @@ import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext import androidx.compose.ui.platform.LocalContext
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.composables.errorhandling.Display
import eu.steffo.twom.composables.errorhandling.ErrorText import eu.steffo.twom.composables.errorhandling.ErrorText
import eu.steffo.twom.composables.errorhandling.LocalizableError import eu.steffo.twom.composables.errorhandling.LocalizableError
import eu.steffo.twom.composables.fields.PasswordField import eu.steffo.twom.composables.fields.PasswordField
@ -58,10 +59,10 @@ fun LoginForm(
var password by rememberSaveable { mutableStateOf("") } var password by rememberSaveable { mutableStateOf("") }
var loginStep by rememberSaveable { mutableStateOf(LoginStep.NONE) } var loginStep by rememberSaveable { mutableStateOf(LoginStep.NONE) }
val error by remember { mutableStateOf(LocalizableError()) } var error by remember { mutableStateOf<LocalizableError?>(null) }
suspend fun doLogin() { suspend fun doLogin() {
error.clear() error = null
Log.d("Login", "Getting authentication service...") Log.d("Login", "Getting authentication service...")
loginStep = LoginStep.SERVICE loginStep = LoginStep.SERVICE
@ -81,7 +82,7 @@ fun LoginForm(
"User seems to have input an invalid Matrix ID: $username", "User seems to have input an invalid Matrix ID: $username",
e e
) )
error.set(R.string.login_error_username_invalid) error = LocalizableError(R.string.login_error_username_invalid)
return return
} catch (e: Throwable) { } catch (e: Throwable) {
Log.e( Log.e(
@ -89,7 +90,7 @@ fun LoginForm(
"Something went wrong while retrieving .well-known data for: $username", "Something went wrong while retrieving .well-known data for: $username",
e e
) )
error.set(R.string.login_error_wellknown_generic, e) error = LocalizableError(R.string.login_error_wellknown_generic, e)
return return
} }
if (wellKnown !is WellknownResult.Prompt) { if (wellKnown !is WellknownResult.Prompt) {
@ -97,7 +98,7 @@ fun LoginForm(
"Login", "Login",
"Data is not .well-known for: $username" "Data is not .well-known for: $username"
) )
error.set(R.string.login_error_wellknown_missing) error = LocalizableError(R.string.login_error_wellknown_missing)
return return
} }
@ -119,7 +120,7 @@ fun LoginForm(
"Something went wrong while retrieving login flows for: ${wellKnown.homeServerUrl}", "Something went wrong while retrieving login flows for: ${wellKnown.homeServerUrl}",
e e
) )
error.set(R.string.login_error_flows_generic, e) error = LocalizableError(R.string.login_error_flows_generic, e)
return return
} }
@ -134,7 +135,7 @@ fun LoginForm(
"Something went wrong while setting up the login wizard.", "Something went wrong while setting up the login wizard.",
e e
) )
error.set(R.string.login_error_wizard_generic, e) error = LocalizableError(R.string.login_error_wizard_generic, e)
return return
} }
@ -153,7 +154,7 @@ fun LoginForm(
"Something went wrong while logging in as: $username", "Something went wrong while logging in as: $username",
e e
) )
error.set(R.string.login_error_login_generic, e) error = LocalizableError(R.string.login_error_login_generic, e)
return return
} }
@ -170,7 +171,7 @@ fun LoginForm(
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 (error.occurred()) MaterialTheme.colorScheme.error else ProgressIndicatorDefaults.linearColor color = if (error != null) MaterialTheme.colorScheme.error else ProgressIndicatorDefaults.linearColor
) )
Row(Modifier.basePadding()) { Row(Modifier.basePadding()) {
Text(LocalContext.current.getString(R.string.login_text)) Text(LocalContext.current.getString(R.string.login_text))
@ -211,7 +212,7 @@ fun LoginForm(
Row(Modifier.basePadding()) { Row(Modifier.basePadding()) {
Button( Button(
modifier = Modifier.fillMaxWidth(), modifier = Modifier.fillMaxWidth(),
enabled = (username != "" && (loginStep == LoginStep.NONE || error.occurred())), enabled = (username != "" && (loginStep == LoginStep.NONE || error != null)),
onClick = { onClick = {
scope.launch { doLogin() } scope.launch { doLogin() }
}, },
@ -219,7 +220,7 @@ fun LoginForm(
Text(LocalContext.current.getString(R.string.login_complete_text)) Text(LocalContext.current.getString(R.string.login_complete_text))
} }
} }
error.Show { error.Display {
Row(Modifier.basePadding()) { Row(Modifier.basePadding()) {
ErrorText( ErrorText(
text = it text = it

View file

@ -52,7 +52,7 @@ fun RoomListItem(
} }
var expanded by rememberSaveable { mutableStateOf(false) } var expanded by rememberSaveable { mutableStateOf(false) }
val error by remember { mutableStateOf(LocalizableError()) } var error by remember { mutableStateOf<LocalizableError?>(null) }
// TODO: Display a running indicator // TODO: Display a running indicator
@ -65,7 +65,7 @@ fun RoomListItem(
session.roomService().joinRoom(roomId, "Opened the invite") session.roomService().joinRoom(roomId, "Opened the invite")
} catch (e: Throwable) { } catch (e: Throwable) {
Log.e("Main", "Failed to open invite to room `$roomId`: $error") Log.e("Main", "Failed to open invite to room `$roomId`: $error")
error.set(R.string.main_error_join_generic, e) error = LocalizableError(R.string.main_error_join_generic, e)
return return
} }
Log.d("Main", "Successfully opened invite to room `$roomId`!") Log.d("Main", "Successfully opened invite to room `$roomId`!")
@ -81,7 +81,7 @@ fun RoomListItem(
session.roomService().leaveRoom(roomId, "Decided to leave the room") session.roomService().leaveRoom(roomId, "Decided to leave the room")
} catch (e: Throwable) { } catch (e: Throwable) {
Log.e("Main", "Failed to leave room `$roomId`: $error") Log.e("Main", "Failed to leave room `$roomId`: $error")
error.set(R.string.main_error_leave_generic, e) error = LocalizableError(R.string.main_error_leave_generic, e)
return return
} }
Log.d("Main", "Successfully left room `$roomId`!") Log.d("Main", "Successfully left room `$roomId`!")

View file

@ -15,6 +15,7 @@ import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource import androidx.compose.ui.res.stringResource
import eu.steffo.twom.R import eu.steffo.twom.R
import eu.steffo.twom.composables.errorhandling.Display
import eu.steffo.twom.composables.errorhandling.ErrorText import eu.steffo.twom.composables.errorhandling.ErrorText
import eu.steffo.twom.composables.errorhandling.LoadingText import eu.steffo.twom.composables.errorhandling.LoadingText
import eu.steffo.twom.composables.errorhandling.LocalizableError import eu.steffo.twom.composables.errorhandling.LocalizableError
@ -67,7 +68,7 @@ fun InviteUserForm(
) )
var busy by rememberSaveable { mutableStateOf(false) } var busy by rememberSaveable { mutableStateOf(false) }
val error by remember { mutableStateOf(LocalizableError()) } var error by remember { mutableStateOf<LocalizableError?>(null) }
Button( Button(
modifier = Modifier modifier = Modifier
@ -78,7 +79,7 @@ fun InviteUserForm(
onClick = { onClick = {
scope.launch SendInvite@{ scope.launch SendInvite@{
busy = true busy = true
error.clear() error = null
Log.d("Room", "Sending invite to `$userId`...") Log.d("Room", "Sending invite to `$userId`...")
@ -86,7 +87,7 @@ fun InviteUserForm(
room.membershipService().invite(userId) room.membershipService().invite(userId)
} catch (e: Throwable) { } catch (e: Throwable) {
Log.e("Room", "Failed to send invite to `$userId`: $error") Log.e("Room", "Failed to send invite to `$userId`: $error")
error.set(R.string.room_error_invite_generic, e) error = LocalizableError(R.string.room_error_invite_generic, e)
busy = false busy = false
return@SendInvite return@SendInvite
} }
@ -103,7 +104,7 @@ fun InviteUserForm(
) )
} }
error.Show { error.Display {
ErrorText( ErrorText(
modifier = Modifier modifier = Modifier
.basePadding() .basePadding()

View file

@ -14,6 +14,7 @@ import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource import androidx.compose.ui.res.stringResource
import eu.steffo.twom.R import eu.steffo.twom.R
import eu.steffo.twom.composables.errorhandling.Display
import eu.steffo.twom.composables.errorhandling.ErrorText import eu.steffo.twom.composables.errorhandling.ErrorText
import eu.steffo.twom.composables.errorhandling.LoadingText import eu.steffo.twom.composables.errorhandling.LoadingText
import eu.steffo.twom.composables.errorhandling.LocalizableError import eu.steffo.twom.composables.errorhandling.LocalizableError
@ -76,13 +77,13 @@ fun ViewRoomForm() {
val published = observeRSVP(room = room, member = member)!! val published = observeRSVP(room = room, member = member)!!
var isPublishRunning by rememberSaveable { mutableStateOf(false) } var isPublishRunning by rememberSaveable { mutableStateOf(false) }
val publishError by remember { mutableStateOf(LocalizableError()) } var publishError by remember { mutableStateOf<LocalizableError?>(null) }
RSVPForm( RSVPForm(
published = published, published = published,
onRequestPublish = { answer, comment -> onRequestPublish = { answer, comment ->
isPublishRunning = true isPublishRunning = true
publishError.clear() publishError = null
scope.launch Publish@{ scope.launch Publish@{
Log.d( Log.d(
@ -100,7 +101,7 @@ fun ViewRoomForm() {
) )
} catch (e: Throwable) { } catch (e: Throwable) {
Log.e("Room", "Failed to update eu.steffo.twom.rsvp: $publishError") Log.e("Room", "Failed to update eu.steffo.twom.rsvp: $publishError")
publishError.set(R.string.room_error_publish_generic, e) publishError = LocalizableError(R.string.room_error_publish_generic, e)
isPublishRunning = false isPublishRunning = false
return@Publish return@Publish
} }
@ -122,7 +123,7 @@ fun ViewRoomForm() {
"Room", "Room",
"Failed to redact old RSVP: $publishError" "Failed to redact old RSVP: $publishError"
) )
publishError.set(R.string.room_error_redact_generic, e) publishError = LocalizableError(R.string.room_error_redact_generic, e)
isPublishRunning = false isPublishRunning = false
return@Publish return@Publish
} }
@ -139,7 +140,7 @@ fun ViewRoomForm() {
isPublishRunning = isPublishRunning, isPublishRunning = isPublishRunning,
) )
publishError.Show { publishError.Display {
Row(Modifier.basePadding()) { Row(Modifier.basePadding()) {
ErrorText( ErrorText(
text = it text = it