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

88 lines
3.3 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
2020-11-08 12:46:17 +00:00
import androidx.databinding.Observable
import androidx.databinding.ObservableField
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
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: ObservableField<JsonObject?>,
2020-10-31 19:45:39 +00:00
private val context: Context,
val appPkg: String,
val appName: String,
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
2020-09-20 14:41:28 +00:00
fun fetch() = CoroutineScope(Dispatchers.IO).launch {
val jobj = jsonObject.get()
2020-11-23 20:33:24 +00:00
isAppInstalled.postValue(isPackageInstalled(appPkg, context.packageManager))
versionName.postValue(
jobj?.string("version")?.removeSuffix("-vanced") ?: context.getString(
R.string.unavailable
)
)
installedVersionName.postValue(getPkgVersionName(isAppInstalled.value, appPkg))
versionCode.postValue(jobj?.int("versionCode") ?: 0)
installedVersionCode.postValue(getPkgVersionCode(isAppInstalled.value, appPkg))
buttonTxt.postValue(compareInt(installedVersionCode.value, versionCode.value))
changelog.postValue(jobj?.string("changelog") ?: context.getString(R.string.unavailable))
2020-08-31 16:12:20 +00:00
}
init {
fetch()
jsonObject.addOnPropertyChangedCallback(object : Observable.OnPropertyChangedCallback() {
override fun onPropertyChanged(sender: Observable?, propertyId: Int) {
fetch()
}
})
}
2020-11-23 20:33:24 +00:00
private fun getPkgVersionName(toCheck: Boolean?, pkg: String): String {
val pm = context.packageManager
2020-11-23 20:33:24 +00:00
return if (toCheck == true) {
2020-10-02 20:30:02 +00:00
pm.getPackageInfo(pkg, 0).versionName.removeSuffix("-vanced")
2020-08-26 11:50:02 +00:00
} else {
context.getString(R.string.unavailable)
}
}
@Suppress("DEPRECATION")
2020-11-23 20:33:24 +00:00
private fun getPkgVersionCode(toCheck: Boolean?, pkg: String): Int {
return if (toCheck == true) {
2020-08-26 11:50:02 +00:00
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P)
2020-11-23 20:33:24 +00:00
context.packageManager.getPackageInfo(pkg, 0).longVersionCode.and(0xFFFFFFFF)
.toInt()
2020-08-26 11:50:02 +00:00
else
2020-08-26 17:21:37 +00:00
context.packageManager.getPackageInfo(pkg, 0).versionCode
2020-08-26 11:50:02 +00:00
} else 0
}
2020-11-23 20:33:24 +00:00
private fun compareInt(int1: Int?, int2: Int?): String {
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
}