From ec92a8b415d4a6b29cb3a6d8d08537b7a9220960 Mon Sep 17 00:00:00 2001 From: X1nto Date: Wed, 16 Dec 2020 17:21:45 +0400 Subject: [PATCH] replaced observable fields with livedata --- .../vanced/manager/adapter/AppListAdapter.kt | 7 ++- .../core/downloader/MicrogDownloader.kt | 2 +- .../core/downloader/MusicDownloader.kt | 4 +- .../core/downloader/VancedDownloader.kt | 4 +- .../com/vanced/manager/model/DataModel.kt | 36 ++++++------ .../com/vanced/manager/ui/MainActivity.kt | 18 +++--- .../manager/ui/dialogs/ManagerUpdateDialog.kt | 5 +- .../ui/dialogs/MusicPreferencesDialog.kt | 2 +- .../dialogs/VancedLanguageSelectionDialog.kt | 2 +- .../ui/dialogs/VancedPreferencesDialog.kt | 2 +- .../ui/dialogs/VancedThemeSelectorDialog.kt | 2 +- .../manager/ui/fragments/AboutFragment.kt | 2 +- .../manager/ui/fragments/HomeFragment.kt | 4 +- .../com/vanced/manager/utils/InternetTools.kt | 56 ++++++------------- .../vanced/manager/utils/LanguageHelper.kt | 2 +- .../com/vanced/manager/utils/PackageHelper.kt | 7 +-- 16 files changed, 69 insertions(+), 86 deletions(-) diff --git a/app/src/main/java/com/vanced/manager/adapter/AppListAdapter.kt b/app/src/main/java/com/vanced/manager/adapter/AppListAdapter.kt index 0efba9fe..a411c109 100644 --- a/app/src/main/java/com/vanced/manager/adapter/AppListAdapter.kt +++ b/app/src/main/java/com/vanced/manager/adapter/AppListAdapter.kt @@ -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 } diff --git a/app/src/main/java/com/vanced/manager/core/downloader/MicrogDownloader.kt b/app/src/main/java/com/vanced/manager/core/downloader/MicrogDownloader.kt index 1e8ac5a8..96365d42 100644 --- a/app/src/main/java/com/vanced/manager/core/downloader/MicrogDownloader.kt +++ b/app/src/main/java/com/vanced/manager/core/downloader/MicrogDownloader.kt @@ -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 = { diff --git a/app/src/main/java/com/vanced/manager/core/downloader/MusicDownloader.kt b/app/src/main/java/com/vanced/manager/core/downloader/MusicDownloader.kt index 2d76b792..c63217b9 100644 --- a/app/src/main/java/com/vanced/manager/core/downloader/MusicDownloader.kt +++ b/app/src/main/java/com/vanced/manager/core/downloader/MusicDownloader.kt @@ -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" diff --git a/app/src/main/java/com/vanced/manager/core/downloader/VancedDownloader.kt b/app/src/main/java/com/vanced/manager/core/downloader/VancedDownloader.kt index 4bf9361f..a913cc34 100644 --- a/app/src/main/java/com/vanced/manager/core/downloader/VancedDownloader.kt +++ b/app/src/main/java/com/vanced/manager/core/downloader/VancedDownloader.kt @@ -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) } diff --git a/app/src/main/java/com/vanced/manager/model/DataModel.kt b/app/src/main/java/com/vanced/manager/model/DataModel.kt index d762680d..61490a7b 100644 --- a/app/src/main/java/com/vanced/manager/model/DataModel.kt +++ b/app/src/main/java/com/vanced/manager/model/DataModel.kt @@ -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, + private val jsonObject: LiveData, private val context: Context, val appPkg: String, val appName: String, @@ -30,28 +30,32 @@ open class DataModel( val buttonTxt = MutableLiveData() val changelog = MutableLiveData() - 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 { diff --git a/app/src/main/java/com/vanced/manager/ui/MainActivity.kt b/app/src/main/java/com/vanced/manager/ui/MainActivity.kt index 14704707..b891ae0e 100644 --- a/app/src/main/java/com/vanced/manager/ui/MainActivity.kt +++ b/app/src/main/java/com/vanced/manager/ui/MainActivity.kt @@ -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") - } } } diff --git a/app/src/main/java/com/vanced/manager/ui/dialogs/ManagerUpdateDialog.kt b/app/src/main/java/com/vanced/manager/ui/dialogs/ManagerUpdateDialog.kt index 609724fc..77dfbef4 100644 --- a/app/src/main/java/com/vanced/manager/ui/dialogs/ManagerUpdateDialog.kt +++ b/app/src/main/java/com/vanced/manager/ui/dialogs/ManagerUpdateDialog.kt @@ -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() { @@ -90,7 +91,7 @@ class ManagerUpdateDialog : BindingDialogFragment() } 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 { diff --git a/app/src/main/java/com/vanced/manager/ui/dialogs/MusicPreferencesDialog.kt b/app/src/main/java/com/vanced/manager/ui/dialogs/MusicPreferencesDialog.kt index 91fd6870..5152c4cd 100644 --- a/app/src/main/java/com/vanced/manager/ui/dialogs/MusicPreferencesDialog.kt +++ b/app/src/main/java/com/vanced/manager/ui/dialogs/MusicPreferencesDialog.kt @@ -34,7 +34,7 @@ class MusicPreferencesDialog : BindingBottomSheetDialogFragment("langs")?.value + private val langs = vanced.value?.array("langs")?.value private val prefs by lazy { requireActivity().getSharedPreferences("installPrefs", Context.MODE_PRIVATE) } override fun binding( diff --git a/app/src/main/java/com/vanced/manager/ui/dialogs/VancedPreferencesDialog.kt b/app/src/main/java/com/vanced/manager/ui/dialogs/VancedPreferencesDialog.kt index adb165fc..65d7bc82 100644 --- a/app/src/main/java/com/vanced/manager/ui/dialogs/VancedPreferencesDialog.kt +++ b/app/src/main/java/com/vanced/manager/ui/dialogs/VancedPreferencesDialog.kt @@ -44,7 +44,7 @@ class VancedPreferencesDialog : BindingBottomSheetDialogFragment("themes")?.value?.map {theme -> + private fun loadButtons() = vanced.value?.array("themes")?.value?.map {theme -> ThemedMaterialRadioButton(requireActivity()).apply { text = theme.convertToAppTheme(requireActivity()) tag = theme diff --git a/app/src/main/java/com/vanced/manager/ui/fragments/AboutFragment.kt b/app/src/main/java/com/vanced/manager/ui/fragments/AboutFragment.kt index b22b397a..9aeb90c2 100644 --- a/app/src/main/java/com/vanced/manager/ui/fragments/AboutFragment.kt +++ b/app/src/main/java/com/vanced/manager/ui/fragments/AboutFragment.kt @@ -42,7 +42,7 @@ class AboutFragment : BindingFragment() { 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") ) ) } diff --git a/app/src/main/java/com/vanced/manager/ui/fragments/HomeFragment.kt b/app/src/main/java/com/vanced/manager/ui/fragments/HomeFragment.kt index 21b97abb..26a5095c 100644 --- a/app/src/main/java/com/vanced/manager/ui/fragments/HomeFragment.kt +++ b/app/src/main/java/com/vanced/manager/ui/fragments/HomeFragment.kt @@ -57,7 +57,7 @@ open class HomeFragment : BindingFragment() { 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() { 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() { diff --git a/app/src/main/java/com/vanced/manager/utils/InternetTools.kt b/app/src/main/java/com/vanced/manager/utils/InternetTools.kt index 056f9d1a..67a3103c 100644 --- a/app/src/main/java/com/vanced/manager/utils/InternetTools.kt +++ b/app/src/main/java/com/vanced/manager/utils/InternetTools.kt @@ -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() - var music = ObservableField() - var microg = ObservableField() - var manager = ObservableField() + var vanced = MutableLiveData() + var music = MutableLiveData() + var microg = MutableLiveData() + var manager = MutableLiveData() - var vancedVersions = ObservableField>() - var musicVersions = ObservableField>() + var vancedVersions = MutableLiveData>() + var musicVersions = MutableLiveData>() - //var braveTiers = ObservableField() + //var braveTiers = MutableLiveData() 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) } diff --git a/app/src/main/java/com/vanced/manager/utils/LanguageHelper.kt b/app/src/main/java/com/vanced/manager/utils/LanguageHelper.kt index d26d1ad9..08c1fd52 100644 --- a/app/src/main/java/com/vanced/manager/utils/LanguageHelper.kt +++ b/app/src/main/java/com/vanced/manager/utils/LanguageHelper.kt @@ -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() sysLocales.forEach { sysLocale -> diff --git a/app/src/main/java/com/vanced/manager/utils/PackageHelper.kt b/app/src/main/java/com/vanced/manager/utils/PackageHelper.kt index b3d119fc..cfb56141 100644 --- a/app/src/main/java/com/vanced/manager/utils/PackageHelper.kt +++ b/app/src/main/java/com/vanced/manager/utils/PackageHelper.kt @@ -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" }