replaced runBlocking methods with coroutinescope

This commit is contained in:
X1nto 2020-08-07 23:24:35 +04:00
parent 90f5d3a337
commit e9c3e7aad5
5 changed files with 45 additions and 41 deletions

View File

@ -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() {
@ -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")
}
}
}

View File

@ -88,7 +88,7 @@ class HomeFragment : Fragment(), View.OnClickListener {
}.attach()
}
viewModel.fetchData()
}
override fun onClick(v: View?) {

View File

@ -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"))

View File

@ -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
@ -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) {
@ -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)
}
}
@ -162,4 +166,8 @@ open class HomeViewModel(application: Application): AndroidViewModel(application
}
}
init {
fetchData()
}
}

View File

@ -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 {
@ -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)
}
}