Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 10 additions & 12 deletions app/src/main/java/me/bmax/apatch/ui/screen/APM.kt
Original file line number Diff line number Diff line change
Expand Up @@ -399,17 +399,13 @@ private fun ModuleList(
items(modules) { module ->
var isChecked by rememberSaveable(module) { mutableStateOf(module.enabled) }
val scope = rememberCoroutineScope()
val updatedModule by produceState(initialValue = Triple("", "", "")) {
scope.launch(Dispatchers.IO) {
value = viewModel.checkUpdate(module)
}
}
val updateInfo = module.updateInfo

ModuleItem(
navigator,
module,
isChecked,
updatedModule.first,
updateInfo?.zipUrl ?: "",
onUninstall = {
scope.launch { onModuleUninstall(module) }
},
Expand Down Expand Up @@ -440,12 +436,14 @@ private fun ModuleList(
},
onUpdate = {
scope.launch {
onModuleUpdate(
module,
updatedModule.third,
updatedModule.first,
"${module.name}-${updatedModule.second}.zip"
)
updateInfo?.let { info ->
onModuleUpdate(
module,
info.changelog,
info.zipUrl,
"${module.name}-${info.version}.zip"
)
}
}
},
onClick = {
Expand Down
24 changes: 16 additions & 8 deletions app/src/main/java/me/bmax/apatch/ui/viewmodel/APModuleViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class APModuleViewModel : ViewModel() {
private var modules by mutableStateOf<List<ModuleInfo>>(emptyList())
}

class ModuleInfo(
data class ModuleInfo(
val id: String,
val name: String,
val author: String,
Expand All @@ -36,6 +36,7 @@ class APModuleViewModel : ViewModel() {
val updateJson: String,
val hasWebUi: Boolean,
val hasActionScript: Boolean,
val updateInfo: ModuleUpdateInfo? = null,
)

data class ModuleUpdateInfo(
Expand Down Expand Up @@ -97,6 +98,14 @@ class APModuleViewModel : ViewModel() {
obj.optBoolean("action")
)
}.toList()
viewModelScope.launch(Dispatchers.IO) {
val updatedModules = modules.map { module ->
if (module.enabled && module.updateJson.isNotEmpty() && !module.update && !module.remove) {
module.copy(updateInfo = runCatching { checkUpdate(module) }.getOrNull())
} else module
}
modules = updatedModules
}
isNeedRefresh = false
}.onFailure { e ->
Log.e(TAG, "fetchModuleList: ", e)
Expand All @@ -117,10 +126,9 @@ class APModuleViewModel : ViewModel() {
return version.replace(Regex("[^a-zA-Z0-9.\\-_]"), "_")
}

fun checkUpdate(m: ModuleInfo): Triple<String, String, String> {
val empty = Triple("", "", "")
fun checkUpdate(m: ModuleInfo): ModuleUpdateInfo? {
if (m.updateJson.isEmpty() || m.remove || m.update || !m.enabled) {
return empty
return null
}
// download updateJson
val result = kotlin.runCatching {
Expand All @@ -142,21 +150,21 @@ class APModuleViewModel : ViewModel() {
Log.i(TAG, "checkUpdate result: $result")

if (result.isEmpty()) {
return empty
return null
}

val updateJson = kotlin.runCatching {
JSONObject(result)
}.getOrNull() ?: return empty
}.getOrNull() ?: return null

val version = sanitizeVersionString(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 null
}

return Triple(zipUrl, version, changelog)
return ModuleUpdateInfo(version, versionCode, zipUrl, changelog)
}
}