feat(ui): complete ABox app experience redesign #10
+12
-1
@@ -38,9 +38,16 @@ class DataStoreUiPreferencesRepository internal constructor(
|
|||||||
transform: (UiPreferences) -> UiPreferences,
|
transform: (UiPreferences) -> UiPreferences,
|
||||||
): UiPreferencesWriteResult = try {
|
): UiPreferencesWriteResult = try {
|
||||||
dataStore.edit { storedPreferences ->
|
dataStore.edit { storedPreferences ->
|
||||||
storedPreferences.putAll(transform(preferencesFrom(storedPreferences)))
|
val updatedPreferences = try {
|
||||||
|
transform(preferencesFrom(storedPreferences))
|
||||||
|
} catch (cause: Throwable) {
|
||||||
|
throw TransformFailure(cause)
|
||||||
|
}
|
||||||
|
storedPreferences.putAll(updatedPreferences)
|
||||||
}
|
}
|
||||||
UiPreferencesWriteResult.Success
|
UiPreferencesWriteResult.Success
|
||||||
|
} catch (failure: TransformFailure) {
|
||||||
|
throw failure.cause
|
||||||
} catch (cause: CancellationException) {
|
} catch (cause: CancellationException) {
|
||||||
throw cause
|
throw cause
|
||||||
} catch (_: IOException) {
|
} catch (_: IOException) {
|
||||||
@@ -71,6 +78,10 @@ class DataStoreUiPreferencesRepository internal constructor(
|
|||||||
private inline fun <reified T : Enum<T>> String.enumOrDefault(default: T): T =
|
private inline fun <reified T : Enum<T>> String.enumOrDefault(default: T): T =
|
||||||
enumValues<T>().firstOrNull { it.name == this } ?: default
|
enumValues<T>().firstOrNull { it.name == this } ?: default
|
||||||
|
|
||||||
|
private class TransformFailure(
|
||||||
|
override val cause: Throwable,
|
||||||
|
) : RuntimeException(cause)
|
||||||
|
|
||||||
private companion object {
|
private companion object {
|
||||||
val RENDERER = stringPreferencesKey("renderer")
|
val RENDERER = stringPreferencesKey("renderer")
|
||||||
val THEME_MODE = stringPreferencesKey("theme_mode")
|
val THEME_MODE = stringPreferencesKey("theme_mode")
|
||||||
|
|||||||
+12
@@ -99,6 +99,18 @@ class DataStoreUiPreferencesRepositoryTest {
|
|||||||
assertEquals(failure, actual)
|
assertEquals(failure, actual)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun updateRethrowsTransformIOException() = runTest {
|
||||||
|
val repository = DataStoreUiPreferencesRepository(InMemoryPreferencesDataStore())
|
||||||
|
val failure = IOException("invalid transform")
|
||||||
|
|
||||||
|
val actual = runCatching {
|
||||||
|
repository.update { throw failure }
|
||||||
|
}.exceptionOrNull()
|
||||||
|
|
||||||
|
assertEquals(failure, actual)
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun updateRethrowsCancellationException() = runTest {
|
fun updateRethrowsCancellationException() = runTest {
|
||||||
val cancellation = CancellationException("cancelled")
|
val cancellation = CancellationException("cancelled")
|
||||||
|
|||||||
Reference in New Issue
Block a user