From 548e31ea57ea42f987e73828782167002f9f213d Mon Sep 17 00:00:00 2001 From: Bryan Van Deusen Date: Fri, 27 Nov 2026 10:17:57 -0500 Subject: [PATCH] fix(android): a settings card that fails to load offers Try again instead of a blank form (DRY A7) The Profile card showed its form once loading stopped, whether or not the load had worked. After a failed load the name and email fields were empty and Save was enabled, so saving wrote blanks over the real profile. ListenBrainz had the same shape. Both cards now go through SettingsLoad: a spinner while loading, one settings row with Try again when the load failed, and the form only once the data is in. The view models keep a load failure in loadError, apart from the form's save message. The web's cards do the same (SettingsLoadStatus). Co-Authored-By: Claude Opus 5.5 --- .../minstrel/settings/ui/ListenBrainzCard.kt | 15 ++--- .../settings/ui/ListenBrainzViewModel.kt | 6 +- .../minstrel/settings/ui/ProfileCard.kt | 15 ++--- .../minstrel/settings/ui/ProfileViewModel.kt | 6 +- .../minstrel/settings/ui/SettingsLoad.kt | 51 +++++++++++++++++ .../settings/ui/ProfileViewModelTest.kt | 57 +++++++++++++++++++ 6 files changed, 128 insertions(+), 22 deletions(-) create mode 100644 android/app/src/main/java/com/fabledsword/minstrel/settings/ui/SettingsLoad.kt create mode 100644 android/app/src/test/java/com/fabledsword/minstrel/settings/ui/ProfileViewModelTest.kt diff --git a/android/app/src/main/java/com/fabledsword/minstrel/settings/ui/ListenBrainzCard.kt b/android/app/src/main/java/com/fabledsword/minstrel/settings/ui/ListenBrainzCard.kt index 2625f1d7..3cce4374 100644 --- a/android/app/src/main/java/com/fabledsword/minstrel/settings/ui/ListenBrainzCard.kt +++ b/android/app/src/main/java/com/fabledsword/minstrel/settings/ui/ListenBrainzCard.kt @@ -1,9 +1,7 @@ package com.fabledsword.minstrel.settings.ui -import androidx.compose.foundation.layout.Box import androidx.compose.foundation.layout.Spacer import androidx.compose.foundation.layout.fillMaxWidth -import androidx.compose.foundation.layout.padding import androidx.compose.foundation.layout.size import androidx.compose.material3.Button import androidx.compose.material3.CircularProgressIndicator @@ -12,7 +10,6 @@ import androidx.compose.material3.OutlinedTextField import androidx.compose.material3.Text import androidx.compose.runtime.Composable import androidx.compose.runtime.getValue -import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.text.input.PasswordVisualTransformation import androidx.compose.ui.unit.dp @@ -27,12 +24,12 @@ import com.fabledsword.minstrel.theme.SwitchRow fun ListenBrainzCard(viewModel: ListenBrainzViewModel = hiltViewModel()) { val state by viewModel.state.collectAsStateWithLifecycle() Section("ListenBrainz") { - if (state.isLoading) { - Box( - modifier = Modifier.fillMaxWidth().padding(16.dp), - contentAlignment = Alignment.Center, - ) { CircularProgressIndicator() } - } else { + SettingsLoad( + isLoading = state.isLoading, + loadError = state.loadError, + what = "ListenBrainz", + onRetry = viewModel::refresh, + ) { ListenBrainzForm( status = state.status, tokenInput = state.tokenInput, diff --git a/android/app/src/main/java/com/fabledsword/minstrel/settings/ui/ListenBrainzViewModel.kt b/android/app/src/main/java/com/fabledsword/minstrel/settings/ui/ListenBrainzViewModel.kt index 6e7bcb41..551378cd 100644 --- a/android/app/src/main/java/com/fabledsword/minstrel/settings/ui/ListenBrainzViewModel.kt +++ b/android/app/src/main/java/com/fabledsword/minstrel/settings/ui/ListenBrainzViewModel.kt @@ -19,6 +19,8 @@ data class ListenBrainzUiState( val status: ListenBrainzStatus? = null, val tokenInput: String = "", val message: String? = null, + /** Why the last load failed; the card then offers Try again instead of the form. */ + val loadError: String? = null, ) /** @@ -47,7 +49,7 @@ class ListenBrainzViewModel @Inject constructor( fun refresh() { viewModelScope.launch { - internal.update { it.copy(isLoading = true, message = null) } + internal.update { it.copy(isLoading = true, message = null, loadError = null) } try { val s = repository.getListenBrainz() internal.update { it.copy(isLoading = false, status = s) } @@ -57,7 +59,7 @@ class ListenBrainzViewModel @Inject constructor( internal.update { it.copy( isLoading = false, - message = ErrorCopy.fromThrowable(e), + loadError = ErrorCopy.fromThrowable(e), ) } } diff --git a/android/app/src/main/java/com/fabledsword/minstrel/settings/ui/ProfileCard.kt b/android/app/src/main/java/com/fabledsword/minstrel/settings/ui/ProfileCard.kt index 6de99a4a..db426559 100644 --- a/android/app/src/main/java/com/fabledsword/minstrel/settings/ui/ProfileCard.kt +++ b/android/app/src/main/java/com/fabledsword/minstrel/settings/ui/ProfileCard.kt @@ -1,10 +1,8 @@ package com.fabledsword.minstrel.settings.ui -import androidx.compose.foundation.layout.Box import androidx.compose.foundation.layout.Spacer import androidx.compose.foundation.layout.fillMaxWidth import androidx.compose.foundation.layout.height -import androidx.compose.foundation.layout.padding import androidx.compose.foundation.layout.size import androidx.compose.material3.Button import androidx.compose.material3.CircularProgressIndicator @@ -13,7 +11,6 @@ import androidx.compose.material3.OutlinedTextField import androidx.compose.material3.Text import androidx.compose.runtime.Composable import androidx.compose.runtime.getValue -import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.unit.dp import androidx.hilt.navigation.compose.hiltViewModel @@ -25,12 +22,12 @@ import com.fabledsword.minstrel.theme.Section fun ProfileCard(viewModel: ProfileViewModel = hiltViewModel()) { val state by viewModel.state.collectAsStateWithLifecycle() Section("Profile") { - if (state.isLoading) { - Box( - modifier = Modifier.fillMaxWidth().padding(16.dp), - contentAlignment = Alignment.Center, - ) { CircularProgressIndicator() } - } else { + SettingsLoad( + isLoading = state.isLoading, + loadError = state.loadError, + what = "your profile", + onRetry = viewModel::refresh, + ) { ProfileForm( displayName = state.displayName, email = state.email, diff --git a/android/app/src/main/java/com/fabledsword/minstrel/settings/ui/ProfileViewModel.kt b/android/app/src/main/java/com/fabledsword/minstrel/settings/ui/ProfileViewModel.kt index d375a7f1..052968f1 100644 --- a/android/app/src/main/java/com/fabledsword/minstrel/settings/ui/ProfileViewModel.kt +++ b/android/app/src/main/java/com/fabledsword/minstrel/settings/ui/ProfileViewModel.kt @@ -20,6 +20,8 @@ data class ProfileUiState( val email: String = "", val loadedProfile: MyProfile? = null, val message: String? = null, + /** Why the last load failed; the card then offers Try again instead of the form. */ + val loadError: String? = null, ) /** @@ -55,7 +57,7 @@ class ProfileViewModel @Inject constructor( fun refresh() { viewModelScope.launch { - internal.update { it.copy(isLoading = true, message = null) } + internal.update { it.copy(isLoading = true, message = null, loadError = null) } try { val profile = repository.getProfile() internal.update { @@ -72,7 +74,7 @@ class ProfileViewModel @Inject constructor( internal.update { it.copy( isLoading = false, - message = ErrorCopy.fromThrowable(e), + loadError = ErrorCopy.fromThrowable(e), ) } } diff --git a/android/app/src/main/java/com/fabledsword/minstrel/settings/ui/SettingsLoad.kt b/android/app/src/main/java/com/fabledsword/minstrel/settings/ui/SettingsLoad.kt new file mode 100644 index 00000000..3854d46d --- /dev/null +++ b/android/app/src/main/java/com/fabledsword/minstrel/settings/ui/SettingsLoad.kt @@ -0,0 +1,51 @@ +package com.fabledsword.minstrel.settings.ui + +import androidx.compose.foundation.layout.Box +import androidx.compose.foundation.layout.fillMaxWidth +import androidx.compose.foundation.layout.padding +import androidx.compose.material3.CircularProgressIndicator +import androidx.compose.material3.MaterialTheme +import androidx.compose.material3.Text +import androidx.compose.material3.TextButton +import androidx.compose.runtime.Composable +import androidx.compose.ui.Alignment +import androidx.compose.ui.Modifier +import androidx.compose.ui.unit.dp +import com.composables.icons.lucide.Lucide +import com.composables.icons.lucide.TriangleAlert +import com.fabledsword.minstrel.theme.SettingRow + +/** + * A settings card's load state, shared by the cards that fetch what they + * show (Profile, ListenBrainz). While loading it shows a spinner; when the + * load failed it shows one row saying so, with Try again; only once the + * data is in does it show [content]. + * + * The form is held back on a failed load on purpose: its fields would be + * empty rather than the saved values, and saving them would overwrite the + * real settings with blanks. The web's settings cards do the same + * (SettingsLoadStatus). + */ +@Composable +fun SettingsLoad( + isLoading: Boolean, + loadError: String?, + what: String, + onRetry: () -> Unit, + content: @Composable () -> Unit, +) { + when { + isLoading -> Box( + modifier = Modifier.fillMaxWidth().padding(16.dp), + contentAlignment = Alignment.Center, + ) { CircularProgressIndicator() } + loadError != null -> SettingRow( + title = "Couldn't load $what", + summary = loadError, + icon = Lucide.TriangleAlert, + tone = MaterialTheme.colorScheme.error, + trailing = { TextButton(onClick = onRetry) { Text("Try again") } }, + ) + else -> content() + } +} diff --git a/android/app/src/test/java/com/fabledsword/minstrel/settings/ui/ProfileViewModelTest.kt b/android/app/src/test/java/com/fabledsword/minstrel/settings/ui/ProfileViewModelTest.kt new file mode 100644 index 00000000..7af1912b --- /dev/null +++ b/android/app/src/test/java/com/fabledsword/minstrel/settings/ui/ProfileViewModelTest.kt @@ -0,0 +1,57 @@ +package com.fabledsword.minstrel.settings.ui + +import com.fabledsword.minstrel.models.MyProfile +import com.fabledsword.minstrel.settings.data.MeRepository +import com.fabledsword.minstrel.testutil.MainDispatcherExtension +import io.mockk.coEvery +import io.mockk.mockk +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.extension.ExtendWith +import java.io.IOException +import kotlin.test.assertEquals +import kotlin.test.assertNotNull +import kotlin.test.assertNull + +/** + * A profile that failed to load is reported as a load failure, which the + * card shows as Try again in place of the form. Reported as an ordinary + * message, it would leave the form up with blank fields that Save would + * write over the real profile. + */ +@ExtendWith(MainDispatcherExtension::class) +class ProfileViewModelTest { + private val profile = MyProfile( + id = "u1", + username = "listener", + displayName = "A Listener", + email = "listener@example.com", + isAdmin = false, + ) + + @Test + fun `a failed load is a load error, not a form message`() { + val repository: MeRepository = mockk { + coEvery { getProfile() } throws IOException("unreachable") + } + + val state = ProfileViewModel(repository).state.value + + assertNotNull(state.loadError) + assertNull(state.message) + assertNull(state.loadedProfile) + } + + @Test + fun `try again after a failed load fills the form`() { + val repository: MeRepository = mockk() + coEvery { repository.getProfile() } throws IOException("unreachable") andThen profile + val viewModel = ProfileViewModel(repository) + + viewModel.refresh() + + val state = viewModel.state.value + assertNull(state.loadError) + assertEquals("A Listener", state.displayName) + assertEquals("listener@example.com", state.email) + } +}