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 ->
var isChecked by rememberSaveable(module) { mutableStateOf(module.enabled) }
val scope = rememberCoroutineScope()
val updateUrl by produceState(initialValue = "") {
viewModel.checkUpdate(module) { value = it.orEmpty() }
val updatedModule by produceState(initialValue = "" to "") {
scope.launch(Dispatchers.IO) {
value = viewModel.checkUpdate(module)
}
}
val downloadingText = stringResource(R.string.module_downloading)
val startDownloadingText = stringResource(R.string.module_start_downloading)
ModuleItem(module, isChecked, updateUrl, onUninstall = {
ModuleItem(module, isChecked, updatedModule.first, onUninstall = {
scope.launch { onModuleUninstall(module) }
}, onCheckChanged = {
scope.launch {
@@ -271,8 +273,8 @@ private fun ModuleList(
val downloading = downloadingText.format(module.name)
download(
context,
updateUrl,
"${module.name}-${module.version}.zip",
updatedModule.first,
"${module.name}-${updatedModule.second}.zip",
downloading,
onDownloaded = onInstallModule,
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) {
callback(null)
return
return empty
}
viewModelScope.launch(Dispatchers.IO) {
// download updateJson
val result = kotlin.runCatching {
val url = m.updateJson
Log.i(TAG, "checkUpdate url: $url")
val response = okhttp3.OkHttpClient()
.newCall(
// download updateJson
val result = kotlin.runCatching {
val url = m.updateJson
Log.i(TAG, "checkUpdate url: $url")
val response = okhttp3.OkHttpClient()
.newCall(
okhttp3.Request.Builder()
.url(url)
.build()
).execute()
Log.d(TAG, "checkUpdate code: ${response.code}")
if (response.isSuccessful) {
response.body?.string() ?: ""
} else {
""
}
}.getOrDefault("")
Log.i(TAG, "checkUpdate result: $result")
if (result.isEmpty()) {
callback(null)
return@launch
Log.d(TAG, "checkUpdate code: ${response.code}")
if (response.isSuccessful) {
response.body?.string() ?: ""
} else {
""
}
}.getOrDefault("")
Log.i(TAG, "checkUpdate result: $result")
val updateJson = kotlin.runCatching {
JSONObject(result)
}.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)
if (result.isEmpty()) {
return empty
}
}
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
}
}