From b1e0db85986be97b5a337892521f0ac13bb57cc7 Mon Sep 17 00:00:00 2001 From: X1nto Date: Wed, 26 Aug 2020 15:50:02 +0400 Subject: [PATCH] Model class goes brrrrr --- .../com/vanced/manager/model/DataModel.kt | 75 +++++++++++++ .../manager/ui/fragments/MainFragment.kt | 2 + .../manager/ui/viewmodels/HomeViewModel.kt | 105 ++---------------- app/src/main/res/layout/include_microg.xml | 12 +- app/src/main/res/layout/include_music.xml | 63 +++++------ app/src/main/res/layout/include_vanced.xml | 12 +- 6 files changed, 127 insertions(+), 142 deletions(-) create mode 100644 app/src/main/java/com/vanced/manager/model/DataModel.kt diff --git a/app/src/main/java/com/vanced/manager/model/DataModel.kt b/app/src/main/java/com/vanced/manager/model/DataModel.kt new file mode 100644 index 0000000000..17f86a1f25 --- /dev/null +++ b/app/src/main/java/com/vanced/manager/model/DataModel.kt @@ -0,0 +1,75 @@ +package com.vanced.manager.model + +import android.content.Context +import android.graphics.drawable.Drawable +import androidx.preference.PreferenceManager.getDefaultSharedPreferences +import com.vanced.manager.utils.InternetTools.getJsonInt +import com.vanced.manager.utils.InternetTools.getJsonString +import com.vanced.manager.utils.PackageHelper.isPackageInstalled + +open class DataModel( + private val jsonName: String, + private val context: Context +) { + + private val variant = getDefaultSharedPreferences(context).getString("vanced_variant") + + private val appPkg = + when (jsonName) { + "vanced" -> if (variant == "root") "com.google.android.youtube" else "com.vanced.android.youtube" + "microg" -> "com.mgoogle.android.gms" + "music" -> "com.vanced.android.youtube.music" + } + + open fun isAppInstalled(): Boolean = isPackageInstalled(appPkg, context.packageManager) + + open fun getVersionName(): String = getJsonString("$jsonName.json", "version") + + open fun getVersionCode(): Int = getJsonInt("$jsonName.json", "versionCode") + + open fun getInstalledVersionName(): String = getPkgVersion(isAppInstalled(), pkgPkg) + + open fun getInstalledVersionCode(): Int = getJsonInt("$jsonName.json", "versionCode") + + open fun getButtonTxt(): String = compareInt(getInstalledVersionCode(), getVersionCode()) + + open fun getButtonIcon(): Drawable = compareIntDrawable(getInstalledVersionCode(), getVersionCode()) + + private fun getPkgVersion(toCheck: Boolean, pkg: String): String { + return if (toCheck) { + pm.getPackageInfo(pkg, 0).versionName + } else { + context.getString(R.string.unavailable) + } + } + + @Suppress("DEPRECATION") + private fun getPkgVerCode(toCheck: Boolean, pkg: String): Int { + return if (toCheck) { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) + pm.getPackageInfo(pkg, 0).longVersionCode.and(0xFFFFFFFF).toInt() + else + pm.getPackageInfo(pkg, 0).versionCode + } else 0 + } + + private fun compareInt(int1: Int, int2: Int): String { + 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) + } + + } + + private fun compareIntDrawable(int1: Int, int2: Int): Drawable? { + return when { + int1 == 0 -> context.getDrawable(R.drawable.ic_download) + int2 > int1 -> context.getDrawable(R.drawable.ic_update) + int2 == int1 -> context.getDrawable(R.drawable.ic_done) + else -> context.getDrawable(R.drawable.ic_download) + } + } + +} diff --git a/app/src/main/java/com/vanced/manager/ui/fragments/MainFragment.kt b/app/src/main/java/com/vanced/manager/ui/fragments/MainFragment.kt index 744a5e8ffd..e2a4185729 100644 --- a/app/src/main/java/com/vanced/manager/ui/fragments/MainFragment.kt +++ b/app/src/main/java/com/vanced/manager/ui/fragments/MainFragment.kt @@ -1,3 +1,5 @@ +package com.vanced.manager.ui.fragments + import androidx.fragment.app.Fragment class HomeFragment : Fragment() { diff --git a/app/src/main/java/com/vanced/manager/ui/viewmodels/HomeViewModel.kt b/app/src/main/java/com/vanced/manager/ui/viewmodels/HomeViewModel.kt index 41f41cfc50..e18f2423c7 100644 --- a/app/src/main/java/com/vanced/manager/ui/viewmodels/HomeViewModel.kt +++ b/app/src/main/java/com/vanced/manager/ui/viewmodels/HomeViewModel.kt @@ -4,7 +4,6 @@ import android.app.Application import android.content.ActivityNotFoundException import android.content.ComponentName import android.content.Intent -import android.graphics.drawable.Drawable import android.net.Uri import android.os.Build import android.util.Log @@ -17,71 +16,28 @@ import androidx.lifecycle.AndroidViewModel import androidx.preference.PreferenceManager.getDefaultSharedPreferences import com.crowdin.platform.Crowdin import com.vanced.manager.R -import com.vanced.manager.utils.InternetTools.getJsonInt -import com.vanced.manager.utils.InternetTools.getJsonString -import com.vanced.manager.utils.PackageHelper.isPackageInstalled +import com.vanced.manager.model.DataModel import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.launch open class HomeViewModel(application: Application): AndroidViewModel(application) { - private val variant = getDefaultSharedPreferences(application).getString("vanced_variant", "nonroot") - - private val vancedPkgName: String = - if (variant == "root") { - "com.google.android.youtube" - } else { - "com.vanced.android.youtube" - } - - private val pm = application.packageManager - - private val vancedInstalledVersionCode = ObservableField() - private val microgInstalledVersionCode = ObservableField() - - private val vancedVersionCode = ObservableField() - private val microgVersionCode = ObservableField() - - //this is fucking retarded - val vancedInstallButtonTxt = ObservableField() - val vancedInstallButtonIcon = ObservableField() - val microgInstalled = ObservableField() - val vancedInstalled = ObservableField() - val vancedInstalledVersion = ObservableField() - val microgInstalledVersion = ObservableField() - val vancedVersion = ObservableField() - val microgVersion = ObservableField() - val microgInstallButtonTxt = ObservableField() - val microgInstallButtonIcon = ObservableField() - - val nonrootModeSelected: Boolean = variant == "nonroot" - + //val variant = getDefaultSharedPreferences(application).getString("vanced_variant", "nonroot") + val fetching = ObservableField() - - private val shouldBeDisabled = ObservableField() - - //this too + + val vanced = ObservableField() + val microg = ObservableField() + val music = ObservableField() + fun fetchData() { CoroutineScope(Dispatchers.IO).launch { fetching.set(true) - //if (Build.VERSION.SDK_INT < Build.VERSION_CODES.R) - Crowdin.forceUpdate(getApplication()) - vancedVersion.set(getJsonString("vanced.json", "version", getApplication())) - microgVersion.set(getJsonString("microg.json", "version", getApplication())) - microgInstalled.set(isPackageInstalled("com.mgoogle.android.gms", pm)) - vancedInstalled.set(isPackageInstalled(vancedPkgName, pm)) - vancedInstalledVersion.set(getPkgInfo(vancedInstalled.get()!!, vancedPkgName, getApplication())) - microgInstalledVersion.set(getPkgInfo(microgInstalled.get()!!, "com.mgoogle.android.gms", getApplication()).removeSuffix("-vanced")) - vancedVersionCode.set(getJsonInt("vanced.json", "versionCode", getApplication())) - microgVersionCode.set(getJsonInt("microg.json", "versionCode", getApplication())) - vancedInstalledVersionCode.set(getPkgVerCode(vancedInstalled.get()!!, vancedPkgName)) - microgInstalledVersionCode.set(getPkgVerCode(microgInstalled.get()!!, "com.mgoogle.android.gms")) - microgInstallButtonTxt.set(compareInt(microgInstalledVersionCode.get()!!, microgVersionCode.get()!!, getApplication())) - microgInstallButtonIcon.set(compareIntDrawable(microgInstalledVersionCode.get()!!, microgVersionCode.get()!!, getApplication())) - shouldBeDisabled.set(nonrootModeSelected && !microgInstalled.get()!!) - vancedInstallButtonIcon.set(compareIntDrawable(vancedInstalledVersionCode.get()!!, vancedVersionCode.get()!!, getApplication())) - vancedInstallButtonTxt.set(compareInt(vancedInstalledVersionCode.get()!!, vancedVersionCode.get()!!, getApplication())) + Crowdin.forceUpdate(getApplication()) + vanced.set(DataModel("vanced.json", getApplication())) + microg.set(DataModel("microg.json", getApplication())) + music.set(DataModel("music.json", getApplication())) fetching.set(false) } } @@ -126,43 +82,6 @@ open class HomeViewModel(application: Application): AndroidViewModel(application } } - private fun getPkgInfo(toCheck: Boolean, pkg: String, application: Application): String { - return if (toCheck) { - pm.getPackageInfo(pkg, 0).versionName - } else { - application.getString(R.string.unavailable) - } - } - - @Suppress("DEPRECATION") - private fun getPkgVerCode(toCheck: Boolean, pkg: String): Int { - return if (toCheck) { - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) - pm.getPackageInfo(pkg, 0).longVersionCode.and(0xFFFFFFFF).toInt() - else - pm.getPackageInfo(pkg, 0).versionCode - } else 0 - } - - private fun compareInt(int1: Int, int2: Int, application: Application): String { - return when { - int1 == 0 -> application.getString(R.string.install) - int2 > int1 -> application.getString(R.string.update) - int2 == int1 || int1 > int2 -> application.getString(R.string.button_reinstall) - else -> application.getString(R.string.install) - } - - } - - private fun compareIntDrawable(int1: Int, int2: Int, application: Application): Drawable? { - return when { - int1 == 0 -> application.getDrawable(R.drawable.ic_download) - int2 > int1 -> application.getDrawable(R.drawable.ic_update) - int2 == int1 -> application.getDrawable(R.drawable.ic_done) - else -> application.getDrawable(R.drawable.ic_download) - } - } - init { fetching.set(false) fetchData() diff --git a/app/src/main/res/layout/include_microg.xml b/app/src/main/res/layout/include_microg.xml index 720d493fd8..6bcf0d1f9a 100644 --- a/app/src/main/res/layout/include_microg.xml +++ b/app/src/main/res/layout/include_microg.xml @@ -47,8 +47,8 @@ @@ -75,7 +75,7 @@ + android:text="@{viewModel.microg.versionName}" /> @@ -94,7 +94,7 @@ + android:text="@{viewModel.microg.installedVersionCode}" /> @@ -103,7 +103,7 @@ style="@style/ClickableImageWidget" android:layout_marginEnd="4dp" android:onClick="@{()-> viewModel.openMicrogSettings()}" - android:visibility="@{viewModel.microgInstalled ? View.VISIBLE : View.GONE}" + android:visibility="@{viewModel.microg.appInstalled ? View.VISIBLE : View.GONE}" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toStartOf="@id/microg_uninstallbtn" app:layout_constraintTop_toBottomOf="@id/microg_title_buttons_barrier" @@ -112,7 +112,7 @@ + app:srcCompat="@drawable/ic_music" /> + app:constraint_referenced_ids="music_installbtn" /> + app:layout_constraintTop_toBottomOf="@+id/music_title_buttons_barrier"> + android:text="@{viewModel.music.versionName}" /> @@ -85,50 +85,39 @@ android:layout_height="wrap_content" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintStart_toStartOf="parent" - app:layout_constraintTop_toBottomOf="@id/microg_remote_latest_version"> + app:layout_constraintTop_toBottomOf="@id/music_remote_latest_version"> + android:text="@{viewModel.music.installedVersion}" /> - - @@ -65,7 +65,7 @@ + android:text="@{viewModel.vanced.versionName}" /> @@ -104,7 +104,7 @@ + android:text="@{viewModel.vanced.installedVersionName}" /> @@ -136,4 +136,4 @@ - \ No newline at end of file +