1
Fork 0
mirror of https://github.com/Steffo99/twom.git synced 2024-11-21 15:44: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.ui.res.stringResource
class LocalizableError {
@StringRes
var stringResourceId: Int? = null
private set
data class LocalizableError(
@StringRes val stringResourceId: Int,
val throwable: Throwable? = null,
)
var throwable: Throwable? = null
private set
fun set(stringResourceId: Int) {
this.stringResourceId = stringResourceId
this.throwable = null
}
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?.render(): String? {
return if (this == null) {
null
} else if (this.throwable == null) {
stringResource(stringResourceId)
} else {
stringResource(stringResourceId, throwable.toString())
}
}
@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.tooling.preview.Preview
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.LocalizableError
import eu.steffo.twom.composables.fields.PasswordField
@ -58,10 +59,10 @@ fun LoginForm(
var password by rememberSaveable { mutableStateOf("") }
var loginStep by rememberSaveable { mutableStateOf(LoginStep.NONE) }
val error by remember { mutableStateOf(LocalizableError()) }
var error by remember { mutableStateOf<LocalizableError?>(null) }
suspend fun doLogin() {
error.clear()
error = null
Log.d("Login", "Getting authentication service...")
loginStep = LoginStep.SERVICE
@ -81,7 +82,7 @@ fun LoginForm(
"User seems to have input an invalid Matrix ID: $username",
e
)
error.set(R.string.login_error_username_invalid)
error = LocalizableError(R.string.login_error_username_invalid)
return
} catch (e: Throwable) {
Log.e(
@ -89,7 +90,7 @@ fun LoginForm(
"Something went wrong while retrieving .well-known data for: $username",
e
)
error.set(R.string.login_error_wellknown_generic, e)
error = LocalizableError(R.string.login_error_wellknown_generic, e)
return
}
if (wellKnown !is WellknownResult.Prompt) {
@ -97,7 +98,7 @@ fun LoginForm(
"Login",
"Data is not .well-known for: $username"
)
error.set(R.string.login_error_wellknown_missing)
error = LocalizableError(R.string.login_error_wellknown_missing)
return
}
@ -119,7 +120,7 @@ fun LoginForm(
"Something went wrong while retrieving login flows for: ${wellKnown.homeServerUrl}",
e
)
error.set(R.string.login_error_flows_generic, e)
error = LocalizableError(R.string.login_error_flows_generic, e)
return
}
@ -134,7 +135,7 @@ fun LoginForm(
"Something went wrong while setting up the login wizard.",
e
)
error.set(R.string.login_error_wizard_generic, e)
error = LocalizableError(R.string.login_error_wizard_generic, e)
return
}
@ -153,7 +154,7 @@ fun LoginForm(
"Something went wrong while logging in as: $username",
e
)
error.set(R.string.login_error_login_generic, e)
error = LocalizableError(R.string.login_error_login_generic, e)
return
}
@ -170,7 +171,7 @@ fun LoginForm(
LinearProgressIndicator(
modifier = Modifier.fillMaxWidth(),
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()) {
Text(LocalContext.current.getString(R.string.login_text))
@ -211,7 +212,7 @@ fun LoginForm(
Row(Modifier.basePadding()) {
Button(
modifier = Modifier.fillMaxWidth(),
enabled = (username != "" && (loginStep == LoginStep.NONE || error.occurred())),
enabled = (username != "" && (loginStep == LoginStep.NONE || error != null)),
onClick = {
scope.launch { doLogin() }
},
@ -219,7 +220,7 @@ fun LoginForm(
Text(LocalContext.current.getString(R.string.login_complete_text))
}
}
error.Show {
error.Display {
Row(Modifier.basePadding()) {
ErrorText(
text = it

View file

@ -52,7 +52,7 @@ fun RoomListItem(
}
var expanded by rememberSaveable { mutableStateOf(false) }
val error by remember { mutableStateOf(LocalizableError()) }
var error by remember { mutableStateOf<LocalizableError?>(null) }
// TODO: Display a running indicator
@ -65,7 +65,7 @@ fun RoomListItem(
session.roomService().joinRoom(roomId, "Opened the invite")
} catch (e: Throwable) {
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
}
Log.d("Main", "Successfully opened invite to room `$roomId`!")
@ -81,7 +81,7 @@ fun RoomListItem(
session.roomService().leaveRoom(roomId, "Decided to leave the room")
} catch (e: Throwable) {
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
}
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.res.stringResource
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.LoadingText
import eu.steffo.twom.composables.errorhandling.LocalizableError
@ -67,7 +68,7 @@ fun InviteUserForm(
)
var busy by rememberSaveable { mutableStateOf(false) }
val error by remember { mutableStateOf(LocalizableError()) }
var error by remember { mutableStateOf<LocalizableError?>(null) }
Button(
modifier = Modifier
@ -78,7 +79,7 @@ fun InviteUserForm(
onClick = {
scope.launch SendInvite@{
busy = true
error.clear()
error = null
Log.d("Room", "Sending invite to `$userId`...")
@ -86,7 +87,7 @@ fun InviteUserForm(
room.membershipService().invite(userId)
} catch (e: Throwable) {
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
return@SendInvite
}
@ -103,7 +104,7 @@ fun InviteUserForm(
)
}
error.Show {
error.Display {
ErrorText(
modifier = Modifier
.basePadding()

View file

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