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

Commit

Permalink
replaced runBlocking methods with coroutinescope
Browse files Browse the repository at this point in the history
  • Loading branch information
X1nto committed Aug 7, 2020
1 parent 90f5d3a commit e9c3e7a
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 41 deletions.
13 changes: 6 additions & 7 deletions app/src/main/java/com/vanced/manager/ui/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@ import com.vanced.manager.utils.AppUtils.isInstallationRunning
import com.vanced.manager.utils.InternetTools
import com.vanced.manager.utils.PackageHelper
import com.vanced.manager.utils.ThemeHelper.setFinalTheme
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking

class MainActivity : AppCompatActivity() {

Expand Down Expand Up @@ -143,12 +144,10 @@ class MainActivity : AppCompatActivity() {
}

private fun checkUpdates() {
runBlocking {
launch {
if (InternetTools.isUpdateAvailable()) {
val fm = supportFragmentManager
UpdateCheckFragment().show(fm, "UpdateCheck")
}
CoroutineScope(Dispatchers.IO).launch {
if (InternetTools.isUpdateAvailable()) {
val fm = supportFragmentManager
UpdateCheckFragment().show(fm, "UpdateCheck")
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ class HomeFragment : Fragment(), View.OnClickListener {

}.attach()
}
viewModel.fetchData()

}

override fun onClick(v: View?) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,10 @@ class VancedLanguageSelectionFragment : Fragment() {
@ExperimentalStdlibApi
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
langs = runBlocking { getArrayFromJson("${PreferenceManager.getDefaultSharedPreferences(activity).getString("install_url", baseUrl)}/vanced.json", "langs") }
CoroutineScope(Dispatchers.IO).launch {
langs = getArrayFromJson("${PreferenceManager.getDefaultSharedPreferences(activity).getString("install_url", baseUrl)}/vanced.json", "langs")
}
loadBoxes(view.findViewById(R.id.lang_button_ll))

view.findViewById<MaterialButton>(R.id.vanced_install_finish).setOnClickListener {
val chosenLangs = mutableListOf("en")
if (!langs.contains("null"))
Expand Down
50 changes: 29 additions & 21 deletions app/src/main/java/com/vanced/manager/ui/viewmodels/HomeViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import android.content.Intent
import android.graphics.drawable.Drawable
import android.net.Uri
import android.os.Build
import android.util.Log
import android.widget.Toast
import androidx.browser.customtabs.CustomTabsIntent
import androidx.core.content.ContextCompat
Expand All @@ -19,8 +20,9 @@ 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 kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking

open class HomeViewModel(application: Application): AndroidViewModel(application) {

Expand Down Expand Up @@ -57,31 +59,33 @@ open class HomeViewModel(application: Application): AndroidViewModel(application

val fetching = ObservableField<Boolean>()

val shouldBeDisabled = ObservableField<Boolean>()
private val shouldBeDisabled = ObservableField<Boolean>()

//this too
fun fetchData() {
runBlocking {
launch {
fetching.set(true)
CoroutineScope(Dispatchers.IO).launch {
fetching.set(true)
try {
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()))
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()))
fetching.set(false)
} catch (e: Exception) {
Log.d("VMLocalisation", "Error: ", e)
}
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()))
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()))
fetching.set(false)
}
}

Expand Down Expand Up @@ -162,4 +166,8 @@ open class HomeViewModel(application: Application): AndroidViewModel(application
}
}

init {
fetchData()
}

}
16 changes: 6 additions & 10 deletions app/src/main/java/com/vanced/manager/utils/AppUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@ import com.vanced.manager.R
import com.vanced.manager.core.downloader.*
import com.vanced.manager.core.installer.*
import com.vanced.manager.ui.fragments.HomeFragment
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.*

object AppUtils {

Expand All @@ -36,13 +34,11 @@ object AppUtils {

fun sendFailure(status: Int, context: Context) {
//Delay error broadcast until activity (and fragment) get back to the screen
runBlocking {
launch {
delay(500)
val intent = Intent(HomeFragment.INSTALL_FAILED)
intent.putExtra("errorMsg", getErrorMessage(status, context))
LocalBroadcastManager.getInstance(context).sendBroadcast(intent)
}
CoroutineScope(Dispatchers.IO).launch {
delay(500)
val intent = Intent(HomeFragment.INSTALL_FAILED)
intent.putExtra("errorMsg", getErrorMessage(status, context))
LocalBroadcastManager.getInstance(context).sendBroadcast(intent)
}
}

Expand Down

0 comments on commit e9c3e7a

Please sign in to comment.