Skip to content

Commit

Permalink
manager: Fix incorrect version of downloaded module.
Browse files Browse the repository at this point in the history
  • Loading branch information
tiann committed Sep 10, 2023
1 parent 1fb2aad commit 52234d0
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 45 deletions.
12 changes: 7 additions & 5 deletions manager/app/src/main/java/me/weishu/kernelsu/ui/screen/Module.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 (result.isEmpty()) {
return empty
}

if (updateJson == null) {
callback(null)
return@launch
}
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()) {
callback(null)
return@launch
}

callback(zipUrl)
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
}
}

0 comments on commit 52234d0

Please sign in to comment.