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 <noreply@anthropic.com>
This commit is contained in:
2026-11-27 10:17:57 -05:00
co-authored by Claude Opus 5.5
parent a48219d807
commit 548e31ea57
6 changed files with 128 additions and 22 deletions
@@ -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,
@@ -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),
)
}
}
@@ -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,
@@ -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),
)
}
}
@@ -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()
}
}
@@ -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)
}
}