Remove lifecycleOwner parameter from DataModel

This commit is contained in:
Tiger Oakes 2021-10-24 13:13:03 -07:00
parent 25f8f800ea
commit 4af47e8de0
4 changed files with 33 additions and 62 deletions

View File

@ -88,6 +88,7 @@ class ExpandableAppListAdapter(
apps[position]
)
}
appDownload.setIconResource(buttonTag.image)
contentDescription = activity.getString(
when (buttonTag) {
ButtonTag.UPDATE -> R.string.accessibility_update
@ -108,11 +109,6 @@ class ExpandableAppListAdapter(
dataModel?.installedVersionName?.observe(activity) {
appVersionInstalled.text = it
}
dataModel?.buttonImage?.observe(activity) {
if (it != null) {
appDownload.icon = it
}
}
}
}
}

View File

@ -1,5 +1,10 @@
package com.vanced.manager.model
enum class ButtonTag {
INSTALL, UPDATE, REINSTALL
import androidx.annotation.DrawableRes
import com.vanced.manager.R
enum class ButtonTag(@DrawableRes val image: Int) {
INSTALL(R.drawable.ic_app_download),
UPDATE(R.drawable.ic_app_update),
REINSTALL(R.drawable.ic_app_reinstall)
}

View File

@ -5,63 +5,46 @@ import android.graphics.drawable.Drawable
import android.os.Build
import androidx.annotation.DrawableRes
import androidx.core.content.ContextCompat
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.*
import com.beust.klaxon.JsonObject
import com.vanced.manager.R
import com.vanced.manager.core.CombinedLiveData
import com.vanced.manager.utils.PackageHelper.isPackageInstalled
open class DataModel(
private val jsonObject: LiveData<JsonObject?>,
private val context: Context,
lifecycleOwner: LifecycleOwner,
jsonObject: LiveData<JsonObject?>,
context: Context,
val appPkg: String,
val appName: String,
val appDescription: String,
@DrawableRes val appIcon: Int
) {
private val versionCode = MutableLiveData<Int>()
private val installedVersionCode = MutableLiveData<Int>()
val isAppInstalled = Transformations.map(jsonObject) { isAppInstalled(appPkg) }
private val versionCode = Transformations.map(jsonObject) { jobj ->
jobj?.int("versionCode") ?: 0
}
private val installedVersionCode = Transformations.map(isAppInstalled) {
getPkgVersionCode(appPkg, it)
}
private val unavailable = context.getString(R.string.unavailable)
private val pm = context.packageManager
val isAppInstalled = MutableLiveData<Boolean>()
val versionName = MutableLiveData<String>()
val installedVersionName = MutableLiveData<String>()
val buttonTag = MutableLiveData<ButtonTag>()
val buttonImage = MutableLiveData<Drawable>()
val changelog = MutableLiveData<String>()
private fun fetch() {
val jobj = jsonObject.value
isAppInstalled.value = isAppInstalled(appPkg)
versionCode.value = jobj?.int("versionCode") ?: 0
versionName.value = jobj?.string("version") ?: unavailable
changelog.value = jobj?.string("changelog") ?: unavailable
val versionName = Transformations.map(jsonObject) { jobj ->
jobj?.string("version") ?: unavailable
}
val changelog = Transformations.map(jsonObject) { jobj ->
jobj?.string("changelog") ?: unavailable
}
val installedVersionName = Transformations.map(isAppInstalled) {
getPkgVersionName(appPkg, it)
}
val buttonTag = CombinedLiveData(versionCode, installedVersionCode) { versionCode, installedVersionCode ->
compareInt(installedVersionCode, versionCode)
}
init {
fetch()
with(lifecycleOwner) {
jsonObject.observe(this) {
fetch()
}
isAppInstalled.observe(this) {
installedVersionCode.value = getPkgVersionCode(appPkg, it)
installedVersionName.value = getPkgVersionName(appPkg, it)
}
versionCode.observe(this) { versionCode ->
installedVersionCode.observe(this) { installedVersionCode ->
buttonTag.value = compareInt(installedVersionCode, versionCode)
buttonImage.value = compareIntDrawable(installedVersionCode, versionCode)
}
}
}
}
open fun isAppInstalled(pkg: String): Boolean = isPackageInstalled(pkg, context.packageManager)
open fun isAppInstalled(pkg: String): Boolean = isPackageInstalled(pkg, pm)
private fun getPkgVersionName(pkg: String, isAppInstalled: Boolean): String {
return if (isAppInstalled) {
@ -95,16 +78,4 @@ open class DataModel(
}
return ButtonTag.INSTALL
}
private fun compareIntDrawable(int1: Int?, int2: Int?): Drawable {
if (int2 != null && int1 != null) {
return when {
int1 == 0 -> ContextCompat.getDrawable(context, R.drawable.ic_app_download)!!
int2 > int1 -> ContextCompat.getDrawable(context, R.drawable.ic_app_update)!!
int1 >= int2 -> ContextCompat.getDrawable(context, R.drawable.ic_app_reinstall)!!
else -> ContextCompat.getDrawable(context, R.drawable.ic_app_download)!!
}
}
return ContextCompat.getDrawable(context, R.drawable.ic_app_download)!!
}
}

View File

@ -10,7 +10,6 @@ import com.vanced.manager.utils.PackageHelper
class RootDataModel(
jsonObject: LiveData<JsonObject?>,
context: Context,
lifecycleOwner: LifecycleOwner,
appPkg: String,
appName: String,
appDescription: String,
@ -22,7 +21,7 @@ class RootDataModel(
//Ironic, isn't it?
private val scriptName: String?
) : DataModel(
jsonObject, context, lifecycleOwner, appPkg, appName, appDescription, appIcon
jsonObject, context, appPkg, appName, appDescription, appIcon
) {
override fun isAppInstalled(pkg: String): Boolean {