0
0
Fork 0
mirror of https://github.com/YTVanced/VancedManager synced 2024-11-15 23:35:06 +00:00
VancedManager/app/src/main/java/com/vanced/manager/model/DataModel.kt

98 lines
3.5 KiB
Kotlin
Raw Normal View History

2020-08-26 11:50:02 +00:00
package com.vanced.manager.model
import android.content.Context
import android.graphics.drawable.Drawable
2020-08-26 17:21:37 +00:00
import android.os.Build
import androidx.lifecycle.LiveData
2020-11-23 20:33:24 +00:00
import androidx.lifecycle.MutableLiveData
2020-09-16 10:40:01 +00:00
import com.beust.klaxon.JsonObject
2020-09-06 10:33:04 +00:00
import com.vanced.manager.R
2020-08-26 11:50:02 +00:00
import com.vanced.manager.utils.PackageHelper.isPackageInstalled
2021-01-16 17:00:38 +00:00
import com.vanced.manager.utils.lifecycleOwner
2020-09-20 14:41:28 +00:00
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
2020-08-26 11:50:02 +00:00
open class DataModel(
private val jsonObject: LiveData<JsonObject?>,
2020-10-31 19:45:39 +00:00
private val context: Context,
open val appPkg: String,
open val appName: String,
open val appIcon: Drawable?,
2020-08-26 11:50:02 +00:00
) {
2020-08-31 16:12:20 +00:00
2020-11-23 20:33:24 +00:00
private val versionCode = MutableLiveData<Int>()
private val installedVersionCode = MutableLiveData<Int>()
2020-09-17 10:13:16 +00:00
2020-11-23 20:33:24 +00:00
val isAppInstalled = MutableLiveData<Boolean>()
val versionName = MutableLiveData<String>()
val installedVersionName = MutableLiveData<String>()
val buttonTxt = MutableLiveData<String>()
val changelog = MutableLiveData<String>()
2020-09-17 10:13:16 +00:00
private fun fetch() = CoroutineScope(Dispatchers.IO).launch {
val jobj = jsonObject.value
2020-11-23 20:33:24 +00:00
isAppInstalled.postValue(isPackageInstalled(appPkg, context.packageManager))
versionCode.postValue(jobj?.int("versionCode") ?: 0)
versionName.postValue(jobj?.string("version")?.removeSuffix("-vanced") ?: context.getString(
R.string.unavailable
))
2020-11-23 20:33:24 +00:00
changelog.postValue(jobj?.string("changelog") ?: context.getString(R.string.unavailable))
2020-08-31 16:12:20 +00:00
}
init {
fetch()
with(context.lifecycleOwner()) {
this?.let {
jsonObject.observe(it) {
fetch()
}
}
2020-12-18 12:12:52 +00:00
this?.let {
isAppInstalled.observe(it) {
installedVersionCode.postValue(getPkgVersionCode(appPkg))
installedVersionName.postValue(getPkgVersionName(appPkg))
}
2020-12-18 12:12:52 +00:00
}
this?.let {
versionCode.observe(it) { versionCode ->
installedVersionCode.observe(it) { installedVersionCode ->
buttonTxt.value = compareInt(installedVersionCode, versionCode)
}
}
}
}
}
2020-11-23 20:33:24 +00:00
open fun getPkgVersionName(pkg: String): String {
val pm = context.packageManager
2020-12-18 12:12:52 +00:00
return if (isAppInstalled.value == true) {
2021-01-17 09:27:22 +00:00
pm?.getPackageInfo(pkg, 0)?.versionName?.removeSuffix("-vanced") ?: context.getString(R.string.unavailable)
2020-08-26 11:50:02 +00:00
} else {
context.getString(R.string.unavailable)
}
}
@Suppress("DEPRECATION")
2020-12-18 12:12:52 +00:00
private fun getPkgVersionCode(pkg: String): Int {
2021-01-17 09:27:22 +00:00
val pm = context.packageManager
2020-12-18 12:12:52 +00:00
return if (isAppInstalled.value == true) {
2020-08-26 11:50:02 +00:00
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P)
2021-01-17 09:27:22 +00:00
pm?.getPackageInfo(pkg, 0)?.longVersionCode?.and(0xFFFFFFFF)?.toInt() ?: 0
2020-08-26 11:50:02 +00:00
else
2021-01-17 09:27:22 +00:00
pm?.getPackageInfo(pkg, 0)?.versionCode ?: 0
2020-08-26 11:50:02 +00:00
} else 0
}
open fun compareInt(int1: Int?, int2: Int?): String {
2020-11-23 20:33:24 +00:00
if (int2 != null && int1 != null) {
return when {
int1 == 0 -> context.getString(R.string.install)
int2 > int1 -> context.getString(R.string.update)
int2 == int1 || int1 > int2 -> context.getString(R.string.button_reinstall)
else -> context.getString(R.string.install)
}
2020-08-26 11:50:02 +00:00
}
2020-11-23 20:33:24 +00:00
return context.getString(R.string.install)
2020-08-26 11:50:02 +00:00
}
2020-11-23 20:33:24 +00:00
}