cancel in-flight downloads when offline mode turns on

This commit is contained in:
austinried
2026-10-04 00:30:05 +00:00
parent ae4c500ae1
commit 26e9b6cb27
2 changed files with 22 additions and 0 deletions
@@ -76,6 +76,11 @@ class DownloadRepository(
scope.launch {
sourceRepository.activeSourceId().distinctUntilChanged().collect { reconcile() }
}
scope.launch {
sourceRepository.offline.collect { offline ->
if (offline) cancelActive()
}
}
}
fun close() {
@@ -345,6 +345,23 @@ class DownloadRepositoryTest {
assertNotNull(artwork.uri(1, coverArtKey(1, PLAYLIST_ART, false)))
}
@Test
fun goingOfflineCancelsActiveDownloads() =
runTest {
seedLibrary()
repository.start()
val active: (SongDownload) -> Boolean = {
it.status == DownloadStatus.Queued || it.status == DownloadStatus.Running
}
runBlocking { repository.download(1, "s1") }
await { allRows().any(active) }
runBlocking { sources.setOfflineMode(true) }
await { allRows().none(active) }
assertTrue(engine.cancelled.isNotEmpty())
}
@Test
fun theDownloadStartsBeforeItsArtworkIsFetched() =
runTest {