fix(android): the update bar moves while a download already under way finishes
release / web (push) Successful in 1m13s
release / go (push) Successful in 1m42s
release / govulncheck (push) Successful in 19s
release / integration (push) Successful in 3m53s
release / android (push) Successful in 4m37s
release / Build signed APK (push) Successful in 4m52s
release / Attach APK to the Release (tag releases only) (push) Skipped
release / Build + push container image (push) Successful in 27s
release / Verify release artifacts (tag releases only) (push) Skipped
release / web (push) Successful in 1m13s
release / go (push) Successful in 1m42s
release / govulncheck (push) Successful in 19s
release / integration (push) Successful in 3m53s
release / android (push) Successful in 4m37s
release / Build signed APK (push) Successful in 4m52s
release / Attach APK to the Release (tag releases only) (push) Skipped
release / Build + push container image (push) Successful in 27s
release / Verify release artifacts (tag releases only) (push) Skipped
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 <noreply@anthropic.com>
This commit is contained in:
@@ -146,7 +146,17 @@ class AppUpdates @Inject constructor(
|
|||||||
if (mutable.value.stage != InstallStage.IDLE) return
|
if (mutable.value.stage != InstallStage.IDLE) return
|
||||||
mutable.update { it.copy(stage = InstallStage.DOWNLOADING, progress = 0f, problem = null) }
|
mutable.update { it.copy(stage = InstallStage.DOWNLOADING, progress = 0f, problem = null) }
|
||||||
scope.launch {
|
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) {
|
when (download) {
|
||||||
is Download.Problem -> mutable.update {
|
is Download.Problem -> mutable.update {
|
||||||
it.copy(stage = InstallStage.IDLE, problem = download.message)
|
it.copy(stage = InstallStage.IDLE, problem = download.message)
|
||||||
|
|||||||
@@ -5,6 +5,9 @@ import com.fabledsword.minstrel.models.UpdateInfo
|
|||||||
import com.fabledsword.minstrel.models.wire.UpdateInfoWire
|
import com.fabledsword.minstrel.models.wire.UpdateInfoWire
|
||||||
import com.fabledsword.minstrel.update.api.ClientVersionApi
|
import com.fabledsword.minstrel.update.api.ClientVersionApi
|
||||||
import kotlinx.coroutines.Dispatchers
|
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.Mutex
|
||||||
import kotlinx.coroutines.sync.withLock
|
import kotlinx.coroutines.sync.withLock
|
||||||
import kotlinx.coroutines.withContext
|
import kotlinx.coroutines.withContext
|
||||||
@@ -66,6 +69,16 @@ class UpdateRepository(
|
|||||||
private val legacyApk: File? = null,
|
private val legacyApk: File? = null,
|
||||||
) {
|
) {
|
||||||
private val turns = Mutex()
|
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<Float> = fetched.asStateFlow()
|
||||||
|
|
||||||
suspend fun check(): Offer {
|
suspend fun check(): Offer {
|
||||||
if (!offers) return Offer.Current
|
if (!offers) return Offer.Current
|
||||||
@@ -118,9 +131,13 @@ class UpdateRepository(
|
|||||||
private suspend fun fetch(info: UpdateInfo, progress: (Float) -> Unit): Download {
|
private suspend fun fetch(info: UpdateInfo, progress: (Float) -> Unit): Download {
|
||||||
val target = waiting(info)
|
val target = waiting(info)
|
||||||
val part = File(dir, target.name + PART)
|
val part = File(dir, target.name + PART)
|
||||||
|
fetched.value = 0f
|
||||||
return try {
|
return try {
|
||||||
val digest = withContext(Dispatchers.IO) {
|
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) }
|
withContext(Dispatchers.IO) { settle(info, part, target, digest) }
|
||||||
} catch (e: CancellationException) {
|
} catch (e: CancellationException) {
|
||||||
@@ -129,6 +146,8 @@ class UpdateRepository(
|
|||||||
} catch (e: IOException) {
|
} catch (e: IOException) {
|
||||||
part.delete()
|
part.delete()
|
||||||
Download.Problem("Couldn't download the update: ${ErrorCopy.fromThrowable(e)}")
|
Download.Problem("Couldn't download the update: ${ErrorCopy.fromThrowable(e)}")
|
||||||
|
} finally {
|
||||||
|
fetched.value = 0f
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+10
@@ -92,6 +92,16 @@ class UpdateRepositoryTest {
|
|||||||
assertEquals(1, downloads)
|
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<Float>()
|
||||||
|
r.download(info) { done -> seen += r.downloadProgress.value.also { assertEquals(done, it) } }
|
||||||
|
assertEquals(1f, seen.last())
|
||||||
|
assertEquals(0f, r.downloadProgress.value)
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun `a download cut off partway is not taken for the build`() = runTest {
|
fun `a download cut off partway is not taken for the build`() = runTest {
|
||||||
val r = repo(body = { apkBytes.copyOf(5) })
|
val r = repo(body = { apkBytes.copyOf(5) })
|
||||||
|
|||||||
Reference in New Issue
Block a user