mirror of
https://github.com/geode-sdk/android-launcher.git
synced 2026-10-25 17:09:22 +00:00
isolate launcher download from release fetch
This commit is contained in:
3 files changed
+22
-27
No files matched your search
@@ -114,7 +114,7 @@ object LauncherUpdater {
|
|||||||
_uiState.emit(LauncherUpdateState.Inactive)
|
_uiState.emit(LauncherUpdateState.Inactive)
|
||||||
}
|
}
|
||||||
|
|
||||||
suspend fun downloadUpdate(context: Context): File? {
|
suspend fun downloadUpdate(context: Context): Result<File> {
|
||||||
_uiState.emit(LauncherUpdateState.Downloading)
|
_uiState.emit(LauncherUpdateState.Downloading)
|
||||||
|
|
||||||
return withContext(Dispatchers.IO) {
|
return withContext(Dispatchers.IO) {
|
||||||
@@ -188,8 +188,17 @@ object LauncherUpdater {
|
|||||||
|
|
||||||
suspend fun installLauncherUpdate(context: Context) {
|
suspend fun installLauncherUpdate(context: Context) {
|
||||||
val launcherDownload = downloadUpdate(context)
|
val launcherDownload = downloadUpdate(context)
|
||||||
|
.getOrElse { e ->
|
||||||
|
e.printStackTrace()
|
||||||
|
Toast.makeText(
|
||||||
|
context, context.getString(
|
||||||
|
R.string.launcher_update_download_failed,
|
||||||
|
e.cause
|
||||||
|
), Toast.LENGTH_SHORT
|
||||||
|
).show()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
if (launcherDownload != null) {
|
|
||||||
if (!launcherDownload.exists()) {
|
if (!launcherDownload.exists()) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -205,17 +214,12 @@ suspend fun installLauncherUpdate(context: Context) {
|
|||||||
|
|
||||||
LauncherUpdater.resetState()
|
LauncherUpdater.resetState()
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
Toast.makeText(context, context.getString(R.string.release_fetch_no_releases), Toast.LENGTH_SHORT).show()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Composable
|
@Composable
|
||||||
fun LauncherUpdateDialog(isCancelling: Boolean, onDismiss: () -> Unit) {
|
fun LauncherUpdateDialog(isCancelling: Boolean, onDismiss: () -> Unit) {
|
||||||
val updateState by LauncherUpdater.uiState.collectAsState()
|
val updateState by LauncherUpdater.uiState.collectAsState()
|
||||||
|
|
||||||
val cancellable = updateState != LauncherUpdater.LauncherUpdateState.Active
|
|
||||||
|
|
||||||
LaunchedEffect(updateState) {
|
LaunchedEffect(updateState) {
|
||||||
if (updateState == LauncherUpdater.LauncherUpdateState.Finished) {
|
if (updateState == LauncherUpdater.LauncherUpdateState.Finished) {
|
||||||
onDismiss()
|
onDismiss()
|
||||||
@@ -242,7 +246,7 @@ fun LauncherUpdateDialog(isCancelling: Boolean, onDismiss: () -> Unit) {
|
|||||||
LinearProgressIndicator()
|
LinearProgressIndicator()
|
||||||
},
|
},
|
||||||
confirmButton = {
|
confirmButton = {
|
||||||
TextButton(onClick = onDismiss, enabled = cancellable && !isCancelling) {
|
TextButton(onClick = onDismiss, enabled = !isCancelling) {
|
||||||
Text(stringResource(R.string.message_box_cancel))
|
Text(stringResource(R.string.message_box_cancel))
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -21,6 +21,7 @@ import okhttp3.OkHttpClient
|
|||||||
import okio.buffer
|
import okio.buffer
|
||||||
import okio.source
|
import okio.source
|
||||||
import java.io.File
|
import java.io.File
|
||||||
|
import java.io.FileNotFoundException
|
||||||
import java.io.InterruptedIOException
|
import java.io.InterruptedIOException
|
||||||
import kotlin.time.Clock
|
import kotlin.time.Clock
|
||||||
import kotlin.time.Duration.Companion.minutes
|
import kotlin.time.Duration.Companion.minutes
|
||||||
@@ -136,38 +137,27 @@ class ReleaseManager private constructor(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private suspend fun downloadLauncherUpdate(release: Downloadable): File? {
|
private suspend fun downloadLauncherUpdate(release: Downloadable): Result<File> {
|
||||||
val download = release.getDownload() ?: return null
|
val download = release.getDownload()
|
||||||
|
?: return Result.failure(FileNotFoundException())
|
||||||
|
|
||||||
val outputDirectory = applicationContext.filesDir
|
val outputDirectory = applicationContext.filesDir
|
||||||
val outputFile = File(outputDirectory, download.filename)
|
val outputFile = File(outputDirectory, download.filename)
|
||||||
|
|
||||||
/*
|
|
||||||
if (outputFile.exists()) {
|
|
||||||
// only download the apk once
|
|
||||||
return
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
|
|
||||||
_uiState.value = ReleaseManagerState.InDownload(0, download.size)
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
DownloadUtils.downloadStream(
|
DownloadUtils.downloadStream(
|
||||||
httpClient,
|
httpClient,
|
||||||
download.url
|
download.url
|
||||||
) { progress, outOf ->
|
).use { fileStream ->
|
||||||
_uiState.value = ReleaseManagerState.InDownload(progress, outOf)
|
|
||||||
}.use { fileStream ->
|
|
||||||
outputFile.outputStream().use {
|
outputFile.outputStream().use {
|
||||||
fileStream.copyTo(it)
|
fileStream.copyTo(it)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
sendError(e)
|
return Result.failure(e)
|
||||||
return null
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return outputFile
|
return Result.success(outputFile)
|
||||||
}
|
}
|
||||||
|
|
||||||
private suspend fun performUpdate(release: Downloadable) {
|
private suspend fun performUpdate(release: Downloadable) {
|
||||||
@@ -275,10 +265,10 @@ class ReleaseManager private constructor(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
suspend fun downloadLatestLauncherUpdate(): File? {
|
suspend fun downloadLatestLauncherUpdate(): Result<File> {
|
||||||
val update = _availableLauncherUpdateDetails.value
|
val update = _availableLauncherUpdateDetails.value
|
||||||
?: getLatestLauncherUpdate()
|
?: getLatestLauncherUpdate()
|
||||||
?: return null
|
?: return Result.failure(FileNotFoundException())
|
||||||
|
|
||||||
return downloadLauncherUpdate(update)
|
return downloadLauncherUpdate(update)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -70,6 +70,7 @@
|
|||||||
<string name="launcher_self_update_failed_title">Update failed</string>
|
<string name="launcher_self_update_failed_title">Update failed</string>
|
||||||
|
|
||||||
<string name="launcher_self_update_failed">Failed to install launcher update: %1$s.</string>
|
<string name="launcher_self_update_failed">Failed to install launcher update: %1$s.</string>
|
||||||
|
<string name="launcher_update_download_failed">Downloading update failed: %1$s</string>
|
||||||
<string name="launcher_self_update_progress">Installing update…</string>
|
<string name="launcher_self_update_progress">Installing update…</string>
|
||||||
|
|
||||||
<string name="launcher_starting_game">Starting Geometry Dash…</string>
|
<string name="launcher_starting_game">Starting Geometry Dash…</string>
|
||||||
|
|||||||
Reference in new issue
Block a user