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,56 +115,46 @@ 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 Log.i(TAG, "checkUpdate url: $url")
Log.i(TAG, "checkUpdate url: $url") val response = okhttp3.OkHttpClient()
val response = okhttp3.OkHttpClient() .newCall(
.newCall(
okhttp3.Request.Builder() okhttp3.Request.Builder()
.url(url) .url(url)
.build() .build()
).execute() ).execute()
Log.d(TAG, "checkUpdate code: ${response.code}") Log.d(TAG, "checkUpdate code: ${response.code}")
if (response.isSuccessful) { if (response.isSuccessful) {
response.body?.string() ?: "" response.body?.string() ?: ""
} else { } else {
"" ""
}
}.getOrDefault("")
Log.i(TAG, "checkUpdate result: $result")
if (result.isEmpty()) {
callback(null)
return@launch
} }
}.getOrDefault("")
Log.i(TAG, "checkUpdate result: $result")
val updateJson = kotlin.runCatching { if (result.isEmpty()) {
JSONObject(result) return empty
}.getOrNull()
if (updateJson == null) {
callback(null)
return@launch
}
val version = updateJson.optString("version", "")
val versionCode = updateJson.optInt("versionCode", 0)
val zipUrl = updateJson.optString("zipUrl", "")
val changelog = updateJson.optString("changelog", "")
if (versionCode <= m.versionCode || zipUrl.isEmpty()) {
callback(null)
return@launch
}
callback(zipUrl)
} }
}
val updateJson = kotlin.runCatching {
JSONObject(result)
}.getOrNull() ?: return empty
val version = updateJson.optString("version", "")
val versionCode = updateJson.optInt("versionCode", 0)
val zipUrl = updateJson.optString("zipUrl", "")
val changelog = updateJson.optString("changelog", "")
if (versionCode <= m.versionCode || zipUrl.isEmpty()) {
return empty
}
return zipUrl to version
}
} }