Skip to content
This repository has been archived by the owner on Mar 15, 2022. It is now read-only.

Commit

Permalink
Model class goes brrrrr
Browse files Browse the repository at this point in the history
  • Loading branch information
X1nto committed Aug 26, 2020
1 parent 3a3624e commit b1e0db8
Show file tree
Hide file tree
Showing 6 changed files with 127 additions and 142 deletions.
75 changes: 75 additions & 0 deletions app/src/main/java/com/vanced/manager/model/DataModel.kt
Original file line number Diff line number Diff line change
@@ -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)
}
}

}
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
package com.vanced.manager.ui.fragments

import androidx.fragment.app.Fragment

class HomeFragment : Fragment() {
Expand Down
105 changes: 12 additions & 93 deletions app/src/main/java/com/vanced/manager/ui/viewmodels/HomeViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<Int>()
private val microgInstalledVersionCode = ObservableField<Int>()

private val vancedVersionCode = ObservableField<Int>()
private val microgVersionCode = ObservableField<Int>()

//this is fucking retarded
val vancedInstallButtonTxt = ObservableField<String>()
val vancedInstallButtonIcon = ObservableField<Drawable>()
val microgInstalled = ObservableField<Boolean>()
val vancedInstalled = ObservableField<Boolean>()
val vancedInstalledVersion = ObservableField<String>()
val microgInstalledVersion = ObservableField<String>()
val vancedVersion = ObservableField<String>()
val microgVersion = ObservableField<String>()
val microgInstallButtonTxt = ObservableField<String>()
val microgInstallButtonIcon = ObservableField<Drawable>()

val nonrootModeSelected: Boolean = variant == "nonroot"

//val variant = getDefaultSharedPreferences(application).getString("vanced_variant", "nonroot")

val fetching = ObservableField<Boolean>()

private val shouldBeDisabled = ObservableField<Boolean>()

//this too

val vanced = ObservableField<DataModel>()
val microg = ObservableField<DataModel>()
val music = ObservableField<DataModel>()

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)
}
}
Expand Down Expand Up @@ -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()
Expand Down
12 changes: 6 additions & 6 deletions app/src/main/res/layout/include_microg.xml
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@
<com.google.android.material.button.MaterialButton
android:id="@+id/microg_installbtn"
style="@style/ButtonStyle"
android:text="@{viewModel.microgInstallButtonTxt}"
app:icon="@{viewModel.microgInstallButtonIcon}"
android:text="@{viewModel.microg.buttonTxt}"
app:icon="@{viewModel.microg.buttonIcon}"
app:layout_constraintBottom_toTopOf="@id/microg_uninstallbtn"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
Expand All @@ -75,7 +75,7 @@
<TextView
android:id="@+id/microg_latest_version"
style="@style/AppVer.Bold"
android:text="@{viewModel.microgVersion}" />
android:text="@{viewModel.microg.versionName}" />

</LinearLayout>

Expand All @@ -94,7 +94,7 @@
<TextView
android:id="@+id/microg_installed_version"
style="@style/AppVer.Bold"
android:text="@{viewModel.microgInstalledVersion}" />
android:text="@{viewModel.microg.installedVersionCode}" />

</LinearLayout>

Expand All @@ -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"
Expand All @@ -112,7 +112,7 @@
<ImageView
android:id="@+id/microg_uninstallbtn"
style="@style/ClickableImageWidget.Red"
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_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@id/microg_title_buttons_barrier"
Expand Down
Loading

0 comments on commit b1e0db8

Please sign in to comment.