Skip to content

Commit

Permalink
correct state ordering issue
Browse files Browse the repository at this point in the history
  • Loading branch information
qimiko committed Jan 5, 2024
1 parent 56b5578 commit 950a80a
Showing 1 changed file with 17 additions and 18 deletions.
35 changes: 17 additions & 18 deletions app/src/main/java/com/geode/launcher/utils/ReleaseManager.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.geode.launcher.utils

import android.content.Context
import com.geode.launcher.api.Asset
import com.geode.launcher.api.Release
import com.geode.launcher.api.ReleaseRepository
import kotlinx.coroutines.CoroutineScope
Expand Down Expand Up @@ -77,8 +78,6 @@ class ReleaseManager private constructor(
}

private suspend fun getLatestRelease(): Release? {
uiState.value = ReleaseManagerState.InUpdateCheck

val sharedPreferences = PreferenceUtils.get(applicationContext)
val useNightly = sharedPreferences.getBoolean(PreferenceUtils.Key.RELEASE_CHANNEL)

Expand All @@ -93,18 +92,7 @@ class ReleaseManager private constructor(
return latestRelease
}

private suspend fun performUpdate(release: Release) {
val releaseAsset = release.getAndroidDownload()
if (releaseAsset == null) {
val noAssetException = Exception("missing Android download")
sendError(noAssetException)

return
}

// set an initial download size
uiState.value = ReleaseManagerState.InDownload(0, releaseAsset.size.toLong())

private suspend fun performUpdate(release: Release, releaseAsset: Asset) {
try {
val file = performDownload(releaseAsset.browserDownloadUrl)
performExtraction(file)
Expand All @@ -119,7 +107,7 @@ class ReleaseManager private constructor(
}

// cancels the previous update and begins a new one
private suspend fun beginUpdate(release: Release) {
private suspend fun beginUpdate(release: Release, releaseAsset: Asset) {
if (release.getDescriptor() == currentUpdate) {
return
}
Expand All @@ -129,7 +117,7 @@ class ReleaseManager private constructor(
currentUpdate = release.getDescriptor()
updateJob = coroutineScope {
launch {
performUpdate(release)
performUpdate(release, releaseAsset)
}
}
}
Expand All @@ -138,7 +126,7 @@ class ReleaseManager private constructor(
val release = try {
getLatestRelease()
} catch (e: Exception) {
sendError(e)
updateFlow.value = ReleaseManagerState.Failure(e)
return
}

Expand All @@ -158,6 +146,17 @@ class ReleaseManager private constructor(
return
}

val releaseAsset = release.getAndroidDownload()
if (releaseAsset == null) {
val noAssetException = Exception("missing Android download")
updateFlow.value = ReleaseManagerState.Failure(noAssetException)

return
}

// set an initial download size
uiState.value = ReleaseManagerState.InDownload(0, releaseAsset.size.toLong())

// final act... sync update flow to uiState
if (updateScope?.isActive == true) {
updateScope.launch {
Expand All @@ -167,7 +166,7 @@ class ReleaseManager private constructor(
}
}

beginUpdate(release)
beginUpdate(release, releaseAsset)
}

private suspend fun updatePreferences(release: Release) {
Expand Down

0 comments on commit 950a80a

Please sign in to comment.