From 26e9b6cb27466497d841715b2ae1164e4ffd015c Mon Sep 17 00:00:00 2001 From: austinried <4966622+austinried@users.noreply.github.com> Date: Tue, 29 Sep 2026 07:33:51 +0000 Subject: [PATCH] cancel in-flight downloads when offline mode turns on --- .../subtracks/data/repo/DownloadRepository.kt | 5 +++++ .../data/repo/DownloadRepositoryTest.kt | 17 +++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/app/src/main/kotlin/com/subtracks/data/repo/DownloadRepository.kt b/app/src/main/kotlin/com/subtracks/data/repo/DownloadRepository.kt index 4ad1519e..b715c9fa 100644 --- a/app/src/main/kotlin/com/subtracks/data/repo/DownloadRepository.kt +++ b/app/src/main/kotlin/com/subtracks/data/repo/DownloadRepository.kt @@ -76,6 +76,11 @@ class DownloadRepository( scope.launch { sourceRepository.activeSourceId().distinctUntilChanged().collect { reconcile() } } + scope.launch { + sourceRepository.offline.collect { offline -> + if (offline) cancelActive() + } + } } fun close() { diff --git a/app/src/test/kotlin/com/subtracks/data/repo/DownloadRepositoryTest.kt b/app/src/test/kotlin/com/subtracks/data/repo/DownloadRepositoryTest.kt index 9565f04a..63806bfc 100644 --- a/app/src/test/kotlin/com/subtracks/data/repo/DownloadRepositoryTest.kt +++ b/app/src/test/kotlin/com/subtracks/data/repo/DownloadRepositoryTest.kt @@ -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 {