manager: Fix incorrect version of downloaded module.

This commit is contained in:
weishu
2023-09-10 18:16:18 +08:00
parent 1fb2aad893
commit 52234d040f
2 changed files with 39 additions and 47 deletions

View File

@@ -227,14 +227,16 @@ private fun ModuleList(
items(viewModel.moduleList) { module -> items(viewModel.moduleList) { module ->
var isChecked by rememberSaveable(module) { mutableStateOf(module.enabled) } var isChecked by rememberSaveable(module) { mutableStateOf(module.enabled) }
val scope = rememberCoroutineScope() val scope = rememberCoroutineScope()
val updateUrl by produceState(initialValue = "") { val updatedModule by produceState(initialValue = "" to "") {
viewModel.checkUpdate(module) { value = it.orEmpty() } scope.launch(Dispatchers.IO) {
value = viewModel.checkUpdate(module)
}
} }
val downloadingText = stringResource(R.string.module_downloading) val downloadingText = stringResource(R.string.module_downloading)
val startDownloadingText = stringResource(R.string.module_start_downloading) val startDownloadingText = stringResource(R.string.module_start_downloading)
ModuleItem(module, isChecked, updateUrl, onUninstall = { ModuleItem(module, isChecked, updatedModule.first, onUninstall = {
scope.launch { onModuleUninstall(module) } scope.launch { onModuleUninstall(module) }
}, onCheckChanged = { }, onCheckChanged = {
scope.launch { scope.launch {
@@ -271,8 +273,8 @@ private fun ModuleList(
val downloading = downloadingText.format(module.name) val downloading = downloadingText.format(module.name)
download( download(
context, context,
updateUrl, updatedModule.first,
"${module.name}-${module.version}.zip", "${module.name}-${updatedModule.second}.zip",
downloading, downloading,
onDownloaded = onInstallModule, onDownloaded = onInstallModule,
onDownloading = { onDownloading = {

View File

@@ -115,12 +115,11 @@ class ModuleViewModel : ViewModel() {
} }
} }
fun checkUpdate(m: ModuleInfo, callback: (String?) -> Unit) { fun checkUpdate(m: ModuleInfo): Pair<String, String> {
val empty = "" to ""
if (m.updateJson.isEmpty() || m.remove || m.update || !m.enabled) { if (m.updateJson.isEmpty() || m.remove || m.update || !m.enabled) {
callback(null) return empty
return
} }
viewModelScope.launch(Dispatchers.IO) {
// download updateJson // download updateJson
val result = kotlin.runCatching { val result = kotlin.runCatching {
val url = m.updateJson val url = m.updateJson
@@ -141,30 +140,21 @@ class ModuleViewModel : ViewModel() {
Log.i(TAG, "checkUpdate result: $result") Log.i(TAG, "checkUpdate result: $result")
if (result.isEmpty()) { if (result.isEmpty()) {
callback(null) return empty
return@launch
} }
val updateJson = kotlin.runCatching { val updateJson = kotlin.runCatching {
JSONObject(result) JSONObject(result)
}.getOrNull() }.getOrNull() ?: return empty
if (updateJson == null) {
callback(null)
return@launch
}
val version = updateJson.optString("version", "") val version = updateJson.optString("version", "")
val versionCode = updateJson.optInt("versionCode", 0) val versionCode = updateJson.optInt("versionCode", 0)
val zipUrl = updateJson.optString("zipUrl", "") val zipUrl = updateJson.optString("zipUrl", "")
val changelog = updateJson.optString("changelog", "") val changelog = updateJson.optString("changelog", "")
if (versionCode <= m.versionCode || zipUrl.isEmpty()) { if (versionCode <= m.versionCode || zipUrl.isEmpty()) {
callback(null) return empty
return@launch
} }
callback(zipUrl) return zipUrl to version
} }
} }
}