From 2ba604ffc716530e0776a400282ece2e9b5bc068 Mon Sep 17 00:00:00 2001 From: Matthew Daubney Date: Mon, 12 Oct 2026 11:56:22 +0100 Subject: [PATCH] Show queued scrobbles on Home and friendlier offline errors Home shows "N scrobbles waiting to send" from the live queue count. Network failures reading history now say "Can't reach the server" instead of surfacing raw exception text. Co-Authored-By: Claude Sonnet 5.5 --- .../main/java/dev/daubney/scrobbled/MainActivity.kt | 2 +- .../scrobbled/data/history/HistoryRepository.kt | 7 ++++++- .../java/dev/daubney/scrobbled/ui/Navigation.kt | 5 +++-- .../dev/daubney/scrobbled/ui/home/HomeScreen.kt | 8 ++++++++ .../dev/daubney/scrobbled/ui/home/HomeViewModel.kt | 13 ++++++++++++- .../scrobbled/data/history/HistoryRepositoryTest.kt | 8 ++++++++ .../daubney/scrobbled/ui/home/HomeViewModelTest.kt | 12 ++++++++++++ 7 files changed, 50 insertions(+), 5 deletions(-) diff --git a/app/src/main/java/dev/daubney/scrobbled/MainActivity.kt b/app/src/main/java/dev/daubney/scrobbled/MainActivity.kt index 7b87778..a93d84b 100644 --- a/app/src/main/java/dev/daubney/scrobbled/MainActivity.kt +++ b/app/src/main/java/dev/daubney/scrobbled/MainActivity.kt @@ -30,7 +30,7 @@ class MainActivity : ComponentActivity() { setContent { ScrobbledTheme { val state by connectVm.state.collectAsState() - if (state.connected) ScrobbledApp(app.history, app.scrobbles, app.trackSearch, onSignOut = connectVm::disconnect) else ConnectScreen(connectVm) + if (state.connected) ScrobbledApp(app.history, app.queue.observeCount(), app.scrobbles, app.trackSearch, onSignOut = connectVm::disconnect) else ConnectScreen(connectVm) } } } diff --git a/app/src/main/java/dev/daubney/scrobbled/data/history/HistoryRepository.kt b/app/src/main/java/dev/daubney/scrobbled/data/history/HistoryRepository.kt index 659fa3c..0705ccf 100644 --- a/app/src/main/java/dev/daubney/scrobbled/data/history/HistoryRepository.kt +++ b/app/src/main/java/dev/daubney/scrobbled/data/history/HistoryRepository.kt @@ -29,7 +29,12 @@ class DefaultHistoryRepository( try { return block(apiFor(creds.serverUrl), creds.bearerToken) } catch (e: ApiException) { - throw if (e.status == 401) SessionExpiredException() else e + throw when (e.status) { + 401 -> SessionExpiredException() + // Transport failures carry raw exception text ("unexpected end of stream on ..."); say it plainly. + -1 -> ApiException(-1, null, "Can't reach the server. Check your connection.") + else -> e + } } } } diff --git a/app/src/main/java/dev/daubney/scrobbled/ui/Navigation.kt b/app/src/main/java/dev/daubney/scrobbled/ui/Navigation.kt index 88c80ce..8c82a14 100644 --- a/app/src/main/java/dev/daubney/scrobbled/ui/Navigation.kt +++ b/app/src/main/java/dev/daubney/scrobbled/ui/Navigation.kt @@ -10,6 +10,7 @@ import androidx.compose.material3.NavigationBarItemDefaults import androidx.compose.material3.Scaffold import androidx.compose.material3.Text import androidx.compose.runtime.Composable +import kotlinx.coroutines.flow.Flow import androidx.compose.runtime.getValue import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier @@ -38,7 +39,7 @@ enum class Destination(val route: String, val label: String) { } @Composable -fun ScrobbledApp(history: HistoryRepository, scrobbles: ScrobbleRepository, trackSearch: TrackSearch, onSignOut: () -> Unit) { +fun ScrobbledApp(history: HistoryRepository, pending: Flow, scrobbles: ScrobbleRepository, trackSearch: TrackSearch, onSignOut: () -> Unit) { val nav = rememberNavController() val current by nav.currentBackStackEntryAsState() Scaffold( @@ -58,7 +59,7 @@ fun ScrobbledApp(history: HistoryRepository, scrobbles: ScrobbleRepository, trac ) { padding -> NavHost(nav, startDestination = Destination.Home.route, modifier = Modifier.padding(bottom = padding.calculateBottomPadding())) { composable(Destination.Home.route) { - val vm = viewModel(factory = viewModelFactory { initializer { HomeViewModel(history) } }) + val vm = viewModel(factory = viewModelFactory { initializer { HomeViewModel(history, pending) } }) HomeScreen(vm, onSignOut) } composable(Destination.History.route) { diff --git a/app/src/main/java/dev/daubney/scrobbled/ui/home/HomeScreen.kt b/app/src/main/java/dev/daubney/scrobbled/ui/home/HomeScreen.kt index f32089e..ff0c973 100644 --- a/app/src/main/java/dev/daubney/scrobbled/ui/home/HomeScreen.kt +++ b/app/src/main/java/dev/daubney/scrobbled/ui/home/HomeScreen.kt @@ -59,6 +59,14 @@ fun HomeScreen(vm: HomeViewModel, onSignOut: () -> Unit) { } item { NowPlayingPanel(state.nowPlaying) } state.summary?.let { item { SummaryRow(it) } } + if (state.pending > 0) { + item { + Text( + if (state.pending == 1) "1 scrobble waiting to send" else "${state.pending} scrobbles waiting to send", + color = Glow, style = MaterialTheme.typography.labelLarge, + ) + } + } state.error?.let { item { Text(it, color = MaterialTheme.colorScheme.error) } } if (state.recent.isNotEmpty()) { item { Text("Recent", style = MaterialTheme.typography.titleMedium) } diff --git a/app/src/main/java/dev/daubney/scrobbled/ui/home/HomeViewModel.kt b/app/src/main/java/dev/daubney/scrobbled/ui/home/HomeViewModel.kt index 4d44a49..de2ab30 100644 --- a/app/src/main/java/dev/daubney/scrobbled/ui/home/HomeViewModel.kt +++ b/app/src/main/java/dev/daubney/scrobbled/ui/home/HomeViewModel.kt @@ -6,9 +6,11 @@ import dev.daubney.scrobbled.data.history.HistoryRepository import dev.daubney.scrobbled.data.history.NowPlaying import dev.daubney.scrobbled.data.history.RecentScrobble import dev.daubney.scrobbled.data.history.Summary +import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.StateFlow import kotlinx.coroutines.flow.asStateFlow +import kotlinx.coroutines.flow.emptyFlow import kotlinx.coroutines.flow.update import kotlinx.coroutines.launch @@ -16,14 +18,23 @@ data class HomeState( val nowPlaying: NowPlaying? = null, val summary: Summary? = null, val recent: List = emptyList(), + /** Scrobbles saved on this device that haven't reached the server yet. */ + val pending: Int = 0, val loaded: Boolean = false, val error: String? = null, ) -class HomeViewModel(private val history: HistoryRepository) : ViewModel() { +class HomeViewModel( + private val history: HistoryRepository, + pending: Flow = emptyFlow(), +) : ViewModel() { private val _state = MutableStateFlow(HomeState()) val state: StateFlow = _state.asStateFlow() + init { + viewModelScope.launch { pending.collect { n -> _state.update { it.copy(pending = n) } } } + } + /** Full refresh: summary, latest scrobbles and now playing. Each part fails independently. */ fun refresh() { viewModelScope.launch { diff --git a/app/src/test/java/dev/daubney/scrobbled/data/history/HistoryRepositoryTest.kt b/app/src/test/java/dev/daubney/scrobbled/data/history/HistoryRepositoryTest.kt index ec841c5..b1702b4 100644 --- a/app/src/test/java/dev/daubney/scrobbled/data/history/HistoryRepositoryTest.kt +++ b/app/src/test/java/dev/daubney/scrobbled/data/history/HistoryRepositoryTest.kt @@ -112,4 +112,12 @@ class HistoryRepositoryTest { assertEquals("Kaput", e.message) } } + + @Test + fun unreachableServerGivesPlainMessage() = runTest { + server.shutdown() + try { repo.summary(); fail() } catch (e: ApiException) { + assertEquals("Can't reach the server. Check your connection.", e.message) + } + } } diff --git a/app/src/test/java/dev/daubney/scrobbled/ui/home/HomeViewModelTest.kt b/app/src/test/java/dev/daubney/scrobbled/ui/home/HomeViewModelTest.kt index 7a52e8c..57c16fa 100644 --- a/app/src/test/java/dev/daubney/scrobbled/ui/home/HomeViewModelTest.kt +++ b/app/src/test/java/dev/daubney/scrobbled/ui/home/HomeViewModelTest.kt @@ -3,6 +3,7 @@ package dev.daubney.scrobbled.ui.home import dev.daubney.scrobbled.data.history.NowPlaying import dev.daubney.scrobbled.ui.FakeHistory import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.ExperimentalCoroutinesApi import kotlinx.coroutines.test.UnconfinedTestDispatcher import kotlinx.coroutines.test.resetMain @@ -66,4 +67,15 @@ class HomeViewModelTest { assertEquals(playing, vm.state.value.nowPlaying) assertNull(vm.state.value.error) } + + @Test + fun pendingCountFollowsTheQueue() { + val queue = MutableStateFlow(0) + val vm = HomeViewModel(FakeHistory(), queue) + assertEquals(0, vm.state.value.pending) + queue.value = 3 + assertEquals(3, vm.state.value.pending) + queue.value = 0 + assertEquals(0, vm.state.value.pending) + } }