Slightly change update UI logic

This commit is contained in:
topjohnwu 2022-01-21 05:37:37 -08:00
parent a73acfb9c2
commit 761a8dde65
4 changed files with 25 additions and 14 deletions

View File

@ -14,15 +14,18 @@ import java.util.*
data class LocalModule(
private val path: String,
) : Module() {
private val svc get() = ServiceLocator.networkService
override var id: String = ""
override var name: String = ""
override var version: String = ""
override var versionCode: Int = -1
var author: String = ""
var description: String = ""
var updateJson: String = ""
var updateInfo: OnlineModule? = null
var outdated = false
private var updateUrl: String = ""
private val removeFile = SuFile(path, "remove")
private val disableFile = SuFile(path, "disable")
private val updateFile = SuFile(path, "update")
@ -93,7 +96,7 @@ data class LocalModule(
"versionCode" -> versionCode = value.toInt()
"author" -> author = value
"description" -> description = value
"updateJson" -> updateJson = value
"updateJson" -> updateUrl = value
}
}
}
@ -113,20 +116,21 @@ data class LocalModule(
}
}
suspend fun load():Boolean {
if (updateJson.isEmpty()) return false
suspend fun fetch(): Boolean {
if (updateUrl.isEmpty())
return false
try {
val json = ServiceLocator.networkService.fetchModuleJson(updateJson)
if (json.versionCode > versionCode) {
updateInfo = OnlineModule(this, json)
return true
}
val json = svc.fetchModuleJson(updateUrl)
updateInfo = OnlineModule(this, json)
outdated = json.versionCode > versionCode
return true
} catch (e: IOException) {
Timber.w(e)
} catch (e: JsonDataException) {
Timber.w(e)
}
return false
}

View File

@ -54,12 +54,18 @@ class LocalModuleRvItem(
}
@get:Bindable
var updateReady: Boolean
get() = item.updateInfo != null && !isRemoved && isEnabled
set(_) = notifyPropertyChanged(BR.updateReady)
val showUpdate get() = item.updateInfo != null
@get:Bindable
val updateReady get() = item.outdated && !isRemoved && isEnabled
val isUpdated = item.updated
fun fetchedUpdateInfo() {
notifyPropertyChanged(BR.showUpdate)
notifyPropertyChanged(BR.updateReady)
}
fun delete() {
isRemoved = !isRemoved
}

View File

@ -59,7 +59,8 @@ class ModuleViewModel : BaseViewModel() {
private suspend fun loadUpdateInfo() {
withContext(Dispatchers.IO) {
itemsInstalled.forEach {
it.updateReady = it.item.load()
if (it.item.fetch())
it.fetchedUpdateInfo()
}
}
}

View File

@ -130,7 +130,7 @@
<Button
android:id="@+id/module_update"
style="@style/WidgetFoundation.Button.Text"
gone="@{item.item.updateJson.length == 0}"
goneUnless="@{item.showUpdate}"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"