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 <noreply@anthropic.com>
This commit is contained in:
1 parent
fa4eab47b3
commit
2ba604ffc7
7 files changed
+50
-5
No files matched your search
@@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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<Int>, 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<HomeViewModel>(factory = viewModelFactory { initializer { HomeViewModel(history) } })
|
||||
val vm = viewModel<HomeViewModel>(factory = viewModelFactory { initializer { HomeViewModel(history, pending) } })
|
||||
HomeScreen(vm, onSignOut)
|
||||
}
|
||||
composable(Destination.History.route) {
|
||||
|
||||
@@ -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) }
|
||||
|
||||
@@ -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<RecentScrobble> = 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<Int> = emptyFlow(),
|
||||
) : ViewModel() {
|
||||
private val _state = MutableStateFlow(HomeState())
|
||||
val state: StateFlow<HomeState> = _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 {
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user