mirror of
https://github.com/YTVanced/VancedManager
synced 2024-11-26 21:13:01 +00:00
replaced observable fields with livedata
This commit is contained in:
parent
620c96f83b
commit
ec92a8b415
16 changed files with 69 additions and 86 deletions
|
@ -31,11 +31,11 @@ class AppListAdapter(
|
|||
|
||||
inner class ListViewHolder(private val binding: ViewAppBinding) : RecyclerView.ViewHolder(binding.root) {
|
||||
val appCard = binding.appCard
|
||||
|
||||
fun bind(position: Int) {
|
||||
val dataModel = if (isRoot) rootDataModels[position] else dataModels[position]
|
||||
with(binding) {
|
||||
appName.text = dataModel?.appName
|
||||
if (dataModel?.buttonTxt?.value != null) appInstallButton.text = dataModel.buttonTxt.value
|
||||
dataModel?.buttonTxt?.observe(this@AppListAdapter.lifecycleOwner) {
|
||||
appInstallButton.text = it
|
||||
}
|
||||
|
@ -46,9 +46,14 @@ class AppListAdapter(
|
|||
dataModel?.appPkg?.let { it1 -> viewModel.uninstallPackage(it1) }
|
||||
}
|
||||
appUninstall.isVisible = dataModel?.isAppInstalled?.value == true
|
||||
dataModel?.isAppInstalled?.observe(lifecycleOwner) {
|
||||
appUninstall.isVisible = it
|
||||
}
|
||||
if (dataModel?.versionName?.value != null) appRemoteVersion.text = dataModel.versionName.value
|
||||
dataModel?.versionName?.observe(this@AppListAdapter.lifecycleOwner) {
|
||||
appRemoteVersion.text = it
|
||||
}
|
||||
if (dataModel?.installedVersionName?.value != null) appInstalledVersion.text = dataModel.installedVersionName.value
|
||||
dataModel?.installedVersionName?.observe(this@AppListAdapter.lifecycleOwner) {
|
||||
appInstalledVersion.text = it
|
||||
}
|
||||
|
|
|
@ -13,7 +13,7 @@ object MicrogDownloader {
|
|||
private const val folderName = "microg"
|
||||
|
||||
fun downloadMicrog(context: Context) {
|
||||
val url = microg.get()?.string("url") ?: ""
|
||||
val url = microg.value?.string("url") ?: ""
|
||||
fuelDownload(url, folderName, fileName, context, onDownloadComplete = {
|
||||
startMicrogInstall(context)
|
||||
}, onError = {
|
||||
|
|
|
@ -29,8 +29,8 @@ object MusicDownloader {
|
|||
|
||||
fun downloadMusic(context: Context) {
|
||||
val prefs = getDefaultSharedPreferences(context)
|
||||
version = prefs.getString("music_version", "latest")?.getLatestAppVersion(musicVersions.get()?.value ?: listOf(""))
|
||||
versionCode = music.get()?.int("versionCode")
|
||||
version = prefs.getString("music_version", "latest")?.getLatestAppVersion(musicVersions.value?.value ?: listOf(""))
|
||||
versionCode = music.value?.int("versionCode")
|
||||
variant = prefs.getString("vanced_variant", "nonroot")
|
||||
baseurl = "${prefs.getInstallUrl()}/music/v$version"
|
||||
folderName = "music/$variant"
|
||||
|
|
|
@ -56,14 +56,14 @@ object VancedDownloader {
|
|||
lang = it.split(", ").toMutableList()
|
||||
}
|
||||
theme = prefs.getString("theme", "dark")
|
||||
vancedVersion = defPrefs.getString("vanced_version", "latest")?.getLatestAppVersion(vancedVersions.get()?.value ?: listOf(""))
|
||||
vancedVersion = defPrefs.getString("vanced_version", "latest")?.getLatestAppVersion(vancedVersions.value?.value ?: listOf(""))
|
||||
themePath = "$installUrl/apks/v$vancedVersion/$variant/Theme"
|
||||
hashUrl = "apks/v$vancedVersion/$variant/Theme/hash.json"
|
||||
//newInstaller = defPrefs.getBoolean("new_installer", false)
|
||||
arch = getArch()
|
||||
count = 0
|
||||
|
||||
vancedVersionCode = vanced.get()?.int("versionCode") ?: 0
|
||||
vancedVersionCode = vanced.value?.int("versionCode") ?: 0
|
||||
downloadSplits(context)
|
||||
}
|
||||
|
||||
|
|
|
@ -3,18 +3,18 @@ package com.vanced.manager.model
|
|||
import android.content.Context
|
||||
import android.graphics.drawable.Drawable
|
||||
import android.os.Build
|
||||
import androidx.databinding.Observable
|
||||
import androidx.databinding.ObservableField
|
||||
import androidx.lifecycle.LiveData
|
||||
import androidx.lifecycle.MutableLiveData
|
||||
import com.beust.klaxon.JsonObject
|
||||
import com.vanced.manager.R
|
||||
import com.vanced.manager.utils.Extensions.lifecycleOwner
|
||||
import com.vanced.manager.utils.PackageHelper.isPackageInstalled
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.launch
|
||||
|
||||
open class DataModel(
|
||||
private val jsonObject: ObservableField<JsonObject?>,
|
||||
private val jsonObject: LiveData<JsonObject?>,
|
||||
private val context: Context,
|
||||
val appPkg: String,
|
||||
val appName: String,
|
||||
|
@ -30,28 +30,32 @@ open class DataModel(
|
|||
val buttonTxt = MutableLiveData<String>()
|
||||
val changelog = MutableLiveData<String>()
|
||||
|
||||
fun fetch() = CoroutineScope(Dispatchers.IO).launch {
|
||||
val jobj = jsonObject.get()
|
||||
private fun fetch() = CoroutineScope(Dispatchers.IO).launch {
|
||||
val jobj = jsonObject.value
|
||||
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))
|
||||
versionName.postValue(jobj?.string("version")?.removeSuffix("-vanced") ?: context.getString(
|
||||
R.string.unavailable
|
||||
))
|
||||
installedVersionName.postValue(getPkgVersionName(isAppInstalled.value, appPkg))
|
||||
changelog.postValue(jobj?.string("changelog") ?: context.getString(R.string.unavailable))
|
||||
}
|
||||
|
||||
init {
|
||||
fetch()
|
||||
jsonObject.addOnPropertyChangedCallback(object : Observable.OnPropertyChangedCallback() {
|
||||
override fun onPropertyChanged(sender: Observable?, propertyId: Int) {
|
||||
fetch()
|
||||
with(context.lifecycleOwner()) {
|
||||
this?.let {
|
||||
jsonObject.observe(it) {
|
||||
fetch()
|
||||
}
|
||||
}
|
||||
})
|
||||
this?.let { versionCode.observe(it) { versionCode ->
|
||||
installedVersionCode.observe(it) { installedVersionCode ->
|
||||
buttonTxt.value = compareInt(installedVersionCode, versionCode)
|
||||
}
|
||||
}}
|
||||
}
|
||||
}
|
||||
|
||||
private fun getPkgVersionName(toCheck: Boolean?, pkg: String): String {
|
||||
|
|
|
@ -16,6 +16,7 @@ import androidx.preference.PreferenceManager.getDefaultSharedPreferences
|
|||
import com.crowdin.platform.Crowdin
|
||||
import com.crowdin.platform.LoadingStateListener
|
||||
import com.google.firebase.messaging.FirebaseMessaging
|
||||
import com.vanced.manager.BuildConfig.VERSION_CODE
|
||||
import com.vanced.manager.BuildConfig.ENABLE_CROWDIN_AUTH
|
||||
import com.vanced.manager.R
|
||||
import com.vanced.manager.databinding.ActivityMainBinding
|
||||
|
@ -25,13 +26,14 @@ import com.vanced.manager.ui.dialogs.URLChangeDialog
|
|||
import com.vanced.manager.ui.fragments.HomeFragmentDirections
|
||||
import com.vanced.manager.ui.fragments.SettingsFragmentDirections
|
||||
import com.vanced.manager.utils.Extensions.show
|
||||
import com.vanced.manager.utils.InternetTools
|
||||
import com.vanced.manager.utils.InternetTools.manager
|
||||
import com.vanced.manager.utils.LanguageContextWrapper
|
||||
import com.vanced.manager.utils.LanguageHelper.authCrowdin
|
||||
import com.vanced.manager.utils.LanguageHelper.onActivityResult
|
||||
import com.vanced.manager.utils.PackageHelper
|
||||
import com.vanced.manager.utils.ThemeHelper.setFinalTheme
|
||||
|
||||
|
||||
class MainActivity : AppCompatActivity() {
|
||||
|
||||
lateinit var binding: ActivityMainBinding
|
||||
|
@ -70,6 +72,11 @@ class MainActivity : AppCompatActivity() {
|
|||
}
|
||||
|
||||
initDialogs(intent.getBooleanExtra("firstLaunch", false))
|
||||
manager.observe(this) {
|
||||
if (manager.value?.int("versionCode") ?: 0 > VERSION_CODE) {
|
||||
ManagerUpdateDialog.newInstance(false).show(this)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override fun onBackPressed() {
|
||||
|
@ -106,7 +113,6 @@ class MainActivity : AppCompatActivity() {
|
|||
navHost.navigate(HomeFragmentDirections.toSettingsFragment())
|
||||
return true
|
||||
}
|
||||
|
||||
R.id.toolbar_update_manager -> {
|
||||
ManagerUpdateDialog.newInstance(false).show(supportFragmentManager, "manager_update")
|
||||
}
|
||||
|
@ -175,14 +181,6 @@ class MainActivity : AppCompatActivity() {
|
|||
)
|
||||
}
|
||||
}
|
||||
|
||||
checkUpdates()
|
||||
}
|
||||
|
||||
private fun checkUpdates() {
|
||||
if (InternetTools.isUpdateAvailable()) {
|
||||
ManagerUpdateDialog.newInstance(false).show(supportFragmentManager, "UpdateCheck")
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -11,12 +11,13 @@ import android.view.LayoutInflater
|
|||
import android.view.ViewGroup
|
||||
import androidx.core.view.isVisible
|
||||
import androidx.localbroadcastmanager.content.LocalBroadcastManager
|
||||
import com.vanced.manager.BuildConfig.VERSION_CODE
|
||||
import com.vanced.manager.R
|
||||
import com.vanced.manager.core.ui.base.BindingDialogFragment
|
||||
import com.vanced.manager.databinding.DialogManagerUpdateBinding
|
||||
import com.vanced.manager.utils.DownloadHelper.downloadManager
|
||||
import com.vanced.manager.utils.DownloadHelper.downloadProgress
|
||||
import com.vanced.manager.utils.InternetTools.isUpdateAvailable
|
||||
import com.vanced.manager.utils.InternetTools.manager
|
||||
|
||||
class ManagerUpdateDialog : BindingDialogFragment<DialogManagerUpdateBinding>() {
|
||||
|
||||
|
@ -90,7 +91,7 @@ class ManagerUpdateDialog : BindingDialogFragment<DialogManagerUpdateBinding>()
|
|||
}
|
||||
|
||||
private fun checkUpdates() {
|
||||
if (isUpdateAvailable()) {
|
||||
if (manager.value?.int("versionCode") ?: 0 > VERSION_CODE) {
|
||||
binding.managerUpdatePatient.text = requireActivity().getString(R.string.please_be_patient)
|
||||
downloadManager(requireActivity())
|
||||
} else {
|
||||
|
|
|
@ -34,7 +34,7 @@ class MusicPreferencesDialog : BindingBottomSheetDialogFragment<DialogMusicPrefe
|
|||
|
||||
private fun bindData() {
|
||||
with(binding) {
|
||||
val musicVersionsConv = musicVersions.get()?.value?.reversed()?.convertToAppVersions()
|
||||
val musicVersionsConv = musicVersions.value?.value?.reversed()?.convertToAppVersions()
|
||||
musicInstallTitle.text = getString(R.string.app_installation_preferences, getString(R.string.music))
|
||||
musicVersion.text = getString(R.string.chosen_version, prefs.getString("music_version", "latest"))
|
||||
openVersionSelector.setOnClickListener {
|
||||
|
|
|
@ -30,7 +30,7 @@ class VancedLanguageSelectionDialog : BindingBottomSheetDialogFragment<DialogVan
|
|||
}
|
||||
}
|
||||
|
||||
private val langs = vanced.get()?.array<String>("langs")?.value
|
||||
private val langs = vanced.value?.array<String>("langs")?.value
|
||||
private val prefs by lazy { requireActivity().getSharedPreferences("installPrefs", Context.MODE_PRIVATE) }
|
||||
|
||||
override fun binding(
|
||||
|
|
|
@ -44,7 +44,7 @@ class VancedPreferencesDialog : BindingBottomSheetDialogFragment<DialogVancedPre
|
|||
val loc = Locale(lang)
|
||||
showLang.add(loc.getDisplayLanguage(loc).capitalize(Locale.ROOT))
|
||||
}
|
||||
val vancedVersionsConv = vancedVersions.get()?.value?.reversed()?.convertToAppVersions()
|
||||
val vancedVersionsConv = vancedVersions.value?.value?.reversed()?.convertToAppVersions()
|
||||
vancedInstallTitle.text = getString(R.string.app_installation_preferences, getString(R.string.vanced))
|
||||
vancedTheme.text = getString(R.string.chosen_theme, installPrefs.getString("theme", "dark")?.convertToAppTheme(requireActivity()))
|
||||
vancedVersion.text = getString(R.string.chosen_version, defPrefs.getString("vanced_version", "latest"))
|
||||
|
|
|
@ -60,7 +60,7 @@ class VancedThemeSelectorDialog : BindingBottomSheetDialogFragment<DialogBottomR
|
|||
}
|
||||
}
|
||||
|
||||
private fun loadButtons() = vanced.get()?.array<String>("themes")?.value?.map {theme ->
|
||||
private fun loadButtons() = vanced.value?.array<String>("themes")?.value?.map {theme ->
|
||||
ThemedMaterialRadioButton(requireActivity()).apply {
|
||||
text = theme.convertToAppTheme(requireActivity())
|
||||
tag = theme
|
||||
|
|
|
@ -42,7 +42,7 @@ class AboutFragment : BindingFragment<FragmentAboutBinding>() {
|
|||
AppInfoDialog.newInstance(
|
||||
appName = getString(R.string.app_name),
|
||||
appIcon = AppCompatResources.getDrawable(requireActivity(), R.mipmap.ic_launcher),
|
||||
changelog = manager.get()?.string("changelog")
|
||||
changelog = manager.value?.string("changelog")
|
||||
)
|
||||
)
|
||||
}
|
||||
|
|
|
@ -57,7 +57,7 @@ open class HomeFragment : BindingFragment<FragmentHomeBinding>() {
|
|||
private fun bindData() {
|
||||
requireActivity().title = getString(R.string.title_home)
|
||||
setHasOptionsMenu(true)
|
||||
with(binding) {
|
||||
with (binding) {
|
||||
homeRefresh.setOnRefreshListener { viewModel.fetchData() }
|
||||
tooltip = ViewTooltip
|
||||
.on(recyclerAppList)
|
||||
|
@ -108,13 +108,11 @@ open class HomeFragment : BindingFragment<FragmentHomeBinding>() {
|
|||
super.onPause()
|
||||
localBroadcastManager.unregisterReceiver(broadcastReceiver)
|
||||
tooltip.close()
|
||||
//binding.mainTablayout.removeOnTabSelectedListener(tabListener)
|
||||
}
|
||||
|
||||
override fun onResume() {
|
||||
super.onResume()
|
||||
registerReceivers()
|
||||
// binding.mainTablayout.addOnTabSelectedListener(tabListener)
|
||||
}
|
||||
|
||||
private val broadcastReceiver: BroadcastReceiver = object : BroadcastReceiver() {
|
||||
|
|
|
@ -7,33 +7,31 @@ import androidx.browser.customtabs.CustomTabColorSchemeParams
|
|||
import androidx.browser.customtabs.CustomTabsIntent
|
||||
import androidx.core.content.ContextCompat
|
||||
import androidx.core.net.toUri
|
||||
import androidx.databinding.ObservableField
|
||||
import androidx.lifecycle.MutableLiveData
|
||||
import androidx.preference.PreferenceManager.getDefaultSharedPreferences
|
||||
import com.beust.klaxon.JsonArray
|
||||
import com.beust.klaxon.JsonObject
|
||||
import com.vanced.manager.BuildConfig
|
||||
import com.vanced.manager.R
|
||||
import com.vanced.manager.utils.AppUtils.generateChecksum
|
||||
import com.vanced.manager.utils.Extensions.getDefaultPrefs
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.withContext
|
||||
import java.io.File
|
||||
import java.text.SimpleDateFormat
|
||||
import java.util.*
|
||||
|
||||
object InternetTools {
|
||||
|
||||
private const val TAG = "VMNetTools"
|
||||
|
||||
var vanced = ObservableField<JsonObject?>()
|
||||
var music = ObservableField<JsonObject?>()
|
||||
var microg = ObservableField<JsonObject?>()
|
||||
var manager = ObservableField<JsonObject?>()
|
||||
var vanced = MutableLiveData<JsonObject?>()
|
||||
var music = MutableLiveData<JsonObject?>()
|
||||
var microg = MutableLiveData<JsonObject?>()
|
||||
var manager = MutableLiveData<JsonObject?>()
|
||||
|
||||
var vancedVersions = ObservableField<JsonArray<String>>()
|
||||
var musicVersions = ObservableField<JsonArray<String>>()
|
||||
var vancedVersions = MutableLiveData<JsonArray<String>>()
|
||||
var musicVersions = MutableLiveData<JsonArray<String>>()
|
||||
|
||||
//var braveTiers = ObservableField<JsonObject?>()
|
||||
//var braveTiers = MutableLiveData<JsonObject?>()
|
||||
|
||||
fun openUrl(url: String, color: Int, context: Context) {
|
||||
val customTabPrefs = getDefaultSharedPreferences(context).getBoolean("use_custom_tabs", true)
|
||||
|
@ -56,32 +54,20 @@ object InternetTools {
|
|||
val hour = calendar.get(Calendar.HOUR_OF_DAY)
|
||||
val minute = calendar.get(Calendar.MINUTE)
|
||||
val second = calendar.get(Calendar.SECOND)
|
||||
val latest = JsonHelper.getJson("$installUrl/latest.json?fetchTime=$hour$minute$second")
|
||||
Log.d("test", "$installUrl/latest.json?fetchTime=$hour$minute$second")
|
||||
val versions = JsonHelper.getJson("$installUrl/versions.json?fetchTime=${SimpleDateFormat("HHmmss", Locale.ROOT)}")
|
||||
val fetchTime = "fetchTime=$hour$minute$second"
|
||||
val latest = JsonHelper.getJson("$installUrl/latest.json?$fetchTime")
|
||||
val versions = JsonHelper.getJson("$installUrl/versions.json?$fetchTime")
|
||||
// braveTiers.apply {
|
||||
// set(getJson("$installUrl/sponsor.json"))
|
||||
// notifyChange()
|
||||
// }
|
||||
|
||||
vanced.apply {
|
||||
set(latest?.obj("vanced"))
|
||||
notifyChange()
|
||||
}
|
||||
vancedVersions.set(versions?.array("vanced"))
|
||||
music.apply {
|
||||
set(latest?.obj("music"))
|
||||
notifyChange()
|
||||
}
|
||||
musicVersions.set(versions?.array("music"))
|
||||
microg.apply {
|
||||
set(latest?.obj("microg"))
|
||||
notifyChange()
|
||||
}
|
||||
manager.apply {
|
||||
set(latest?.obj("manager"))
|
||||
notifyChange()
|
||||
}
|
||||
vanced.postValue(latest?.obj("vanced"))
|
||||
vancedVersions.postValue(versions?.array("vanced") )
|
||||
music.postValue(latest?.obj("music"))
|
||||
musicVersions.postValue(versions?.array("music"))
|
||||
microg.postValue(latest?.obj("microg"))
|
||||
manager.postValue(latest?.obj("manager"))
|
||||
|
||||
}
|
||||
|
||||
|
@ -95,14 +81,6 @@ object InternetTools {
|
|||
}
|
||||
}
|
||||
|
||||
fun isUpdateAvailable(): Boolean {
|
||||
while (true) {
|
||||
if (manager.get() != null) {
|
||||
return manager.get()?.int("versionCode") ?: 0 > BuildConfig.VERSION_CODE
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun getSha256(hashUrl: String, obj: String, context: Context): String {
|
||||
return getJsonString(hashUrl, obj, context)
|
||||
}
|
||||
|
|
|
@ -36,7 +36,7 @@ object LanguageHelper {
|
|||
|
||||
@Suppress("DEPRECATION")
|
||||
fun getDefaultVancedLanguages(): String {
|
||||
val serverLangs = vanced.get()?.array("langs") ?: mutableListOf("")
|
||||
val serverLangs = vanced.value?.array("langs") ?: mutableListOf("")
|
||||
val sysLocales = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) Resources.getSystem().configuration.locales.toLangTags() else arrayOf(Resources.getSystem().configuration.locale.language)
|
||||
val finalLangs = mutableListOf<String>()
|
||||
sysLocales.forEach { sysLocale ->
|
||||
|
|
|
@ -31,7 +31,6 @@ import java.util.regex.Pattern
|
|||
import kotlin.collections.ArrayList
|
||||
import kotlin.collections.HashMap
|
||||
|
||||
|
||||
object PackageHelper {
|
||||
|
||||
private const val apkInstallPath = "/data/adb"
|
||||
|
@ -167,7 +166,7 @@ object PackageHelper {
|
|||
return false
|
||||
}
|
||||
|
||||
fun installRootApp(context: Context, app: String, appVerCode: Int, pkg: String, modApkBool: (fileName: String) -> Boolean) = CoroutineScope(Dispatchers.IO).launch {
|
||||
private fun installRootApp(context: Context, app: String, appVerCode: Int, pkg: String, modApkBool: (fileName: String) -> Boolean) = CoroutineScope(Dispatchers.IO).launch {
|
||||
Shell.getShell {
|
||||
val apkFilesPath = context.getExternalFilesDir("$app/root")?.path
|
||||
val fileInfoList = apkFilesPath?.let { it1 -> getFileInfoList(it1) }
|
||||
|
@ -195,7 +194,7 @@ object PackageHelper {
|
|||
installRootApp(
|
||||
context,
|
||||
"music",
|
||||
music.get()?.int("versionCode")!!,
|
||||
music.value?.int("versionCode")!!,
|
||||
musicRootPkg
|
||||
) {
|
||||
it == "root.apk"
|
||||
|
@ -206,7 +205,7 @@ object PackageHelper {
|
|||
installRootApp(
|
||||
context,
|
||||
"vanced",
|
||||
vanced.get()?.int("versionCode")!!,
|
||||
vanced.value?.int("versionCode")!!,
|
||||
vancedRootPkg
|
||||
) { fileName ->
|
||||
vancedThemes.any { fileName == "$it.apk" }
|
||||
|
|
Loading…
Reference in a new issue