From 23a7b18076c3dabfb5779711c51fc55ea5047e1d Mon Sep 17 00:00:00 2001 From: Bryan Van Deusen Date: Tue, 24 Nov 2026 13:00:24 -0500 Subject: [PATCH] fix(android): the update bar moves while a download already under way finishes On Wi-Fi the app fetches a new build ahead, holding the download turn. An Update tap during that waited behind it with a bar stuck at zero, because the fetch reported its progress to nobody. The repository now publishes the running download's progress, and the bar follows it whoever started the download. Co-Authored-By: Claude Opus 5.5 --- .../minstrel/update/data/AppUpdates.kt | 12 ++++++++++- .../minstrel/update/data/UpdateRepository.kt | 21 ++++++++++++++++++- .../update/data/UpdateRepositoryTest.kt | 10 +++++++++ 3 files changed, 41 insertions(+), 2 deletions(-) diff --git a/android/app/src/main/java/com/fabledsword/minstrel/update/data/AppUpdates.kt b/android/app/src/main/java/com/fabledsword/minstrel/update/data/AppUpdates.kt index 40d79c4a..1e09ab00 100644 --- a/android/app/src/main/java/com/fabledsword/minstrel/update/data/AppUpdates.kt +++ b/android/app/src/main/java/com/fabledsword/minstrel/update/data/AppUpdates.kt @@ -146,7 +146,17 @@ class AppUpdates @Inject constructor( if (mutable.value.stage != InstallStage.IDLE) return mutable.update { it.copy(stage = InstallStage.DOWNLOADING, progress = 0f, problem = null) } scope.launch { - val download = updates.download(offer) { done -> mutable.update { it.copy(progress = done) } } + // Follows whichever download is running, not only one this call + // starts: when the Wi-Fi fetch is already bringing the build + // down, this waits behind it and its bar should move with it. + val following = launch { + updates.downloadProgress.collect { done -> mutable.update { it.copy(progress = done) } } + } + val download = try { + updates.download(offer) {} + } finally { + following.cancel() + } when (download) { is Download.Problem -> mutable.update { it.copy(stage = InstallStage.IDLE, problem = download.message) diff --git a/android/app/src/main/java/com/fabledsword/minstrel/update/data/UpdateRepository.kt b/android/app/src/main/java/com/fabledsword/minstrel/update/data/UpdateRepository.kt index 5b1e96a9..175e5fba 100644 --- a/android/app/src/main/java/com/fabledsword/minstrel/update/data/UpdateRepository.kt +++ b/android/app/src/main/java/com/fabledsword/minstrel/update/data/UpdateRepository.kt @@ -5,6 +5,9 @@ import com.fabledsword.minstrel.models.UpdateInfo import com.fabledsword.minstrel.models.wire.UpdateInfoWire import com.fabledsword.minstrel.update.api.ClientVersionApi import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.flow.MutableStateFlow +import kotlinx.coroutines.flow.StateFlow +import kotlinx.coroutines.flow.asStateFlow import kotlinx.coroutines.sync.Mutex import kotlinx.coroutines.sync.withLock import kotlinx.coroutines.withContext @@ -66,6 +69,16 @@ class UpdateRepository( private val legacyApk: File? = null, ) { private val turns = Mutex() + private val fetched = MutableStateFlow(0f) + + /** + * How far the download under way has got, 0 to 1, whichever caller + * started it. An Update tap that arrives while the Wi-Fi fetch is + * already bringing the build down waits its turn behind it, and follows + * this to show that download moving rather than a bar stuck at zero. + * Back to 0 once no download is running. + */ + val downloadProgress: StateFlow = fetched.asStateFlow() suspend fun check(): Offer { if (!offers) return Offer.Current @@ -118,9 +131,13 @@ class UpdateRepository( private suspend fun fetch(info: UpdateInfo, progress: (Float) -> Unit): Download { val target = waiting(info) val part = File(dir, target.name + PART) + fetched.value = 0f return try { val digest = withContext(Dispatchers.IO) { - save(apk.open(info.apkUrl), part, info.sizeBytes, progress) + save(apk.open(info.apkUrl), part, info.sizeBytes) { done -> + fetched.value = done + progress(done) + } } withContext(Dispatchers.IO) { settle(info, part, target, digest) } } catch (e: CancellationException) { @@ -129,6 +146,8 @@ class UpdateRepository( } catch (e: IOException) { part.delete() Download.Problem("Couldn't download the update: ${ErrorCopy.fromThrowable(e)}") + } finally { + fetched.value = 0f } } diff --git a/android/app/src/test/java/com/fabledsword/minstrel/update/data/UpdateRepositoryTest.kt b/android/app/src/test/java/com/fabledsword/minstrel/update/data/UpdateRepositoryTest.kt index 1e295e79..09ff9fc6 100644 --- a/android/app/src/test/java/com/fabledsword/minstrel/update/data/UpdateRepositoryTest.kt +++ b/android/app/src/test/java/com/fabledsword/minstrel/update/data/UpdateRepositoryTest.kt @@ -92,6 +92,16 @@ class UpdateRepositoryTest { assertEquals(1, downloads) } + @Test + fun `the download under way is visible to anyone waiting on it, and settles back to zero`() = runTest { + val r = repo() + val info = (r.check() as Offer.Newer).info + val seen = mutableListOf() + r.download(info) { done -> seen += r.downloadProgress.value.also { assertEquals(done, it) } } + assertEquals(1f, seen.last()) + assertEquals(0f, r.downloadProgress.value) + } + @Test fun `a download cut off partway is not taken for the build`() = runTest { val r = repo(body = { apkBytes.copyOf(5) })