From f637d2eef8ca59a76e87636433f77572201bf8a3 Mon Sep 17 00:00:00 2001 From: X1nto Date: Sat, 19 Dec 2020 12:35:07 +0400 Subject: [PATCH] added isFetching LiveData --- .../com/vanced/manager/ui/MainActivity.kt | 2 +- .../manager/ui/dialogs/URLChangeDialog.kt | 4 ++-- .../manager/ui/fragments/HomeFragment.kt | 2 ++ .../manager/ui/viewmodels/HomeViewModel.kt | 8 +------ .../com/vanced/manager/utils/Extensions.kt | 23 ------------------- .../com/vanced/manager/utils/InternetTools.kt | 17 ++++++++------ 6 files changed, 16 insertions(+), 40 deletions(-) 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 b891ae0e..0c2aa96a 100644 --- a/app/src/main/java/com/vanced/manager/ui/MainActivity.kt +++ b/app/src/main/java/com/vanced/manager/ui/MainActivity.kt @@ -16,8 +16,8 @@ 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.BuildConfig.VERSION_CODE import com.vanced.manager.R import com.vanced.manager.databinding.ActivityMainBinding import com.vanced.manager.ui.dialogs.DialogContainer diff --git a/app/src/main/java/com/vanced/manager/ui/dialogs/URLChangeDialog.kt b/app/src/main/java/com/vanced/manager/ui/dialogs/URLChangeDialog.kt index b328a45a..12d774b8 100644 --- a/app/src/main/java/com/vanced/manager/ui/dialogs/URLChangeDialog.kt +++ b/app/src/main/java/com/vanced/manager/ui/dialogs/URLChangeDialog.kt @@ -11,8 +11,8 @@ import androidx.lifecycle.lifecycleScope import androidx.preference.PreferenceManager.getDefaultSharedPreferences import com.vanced.manager.core.ui.base.BindingDialogFragment import com.vanced.manager.databinding.DialogCustomUrlBinding -import com.vanced.manager.utils.Extensions.fetchData import com.vanced.manager.utils.InternetTools.baseUrl +import com.vanced.manager.utils.InternetTools.loadJson import kotlinx.coroutines.launch class URLChangeDialog : BindingDialogFragment() { @@ -60,7 +60,7 @@ class URLChangeDialog : BindingDialogFragment() { private fun saveUrl(url: String) { lifecycleScope.launch { getDefaultSharedPreferences(requireActivity()).edit { putString("install_url", url) } - requireActivity().fetchData() + loadJson(requireActivity()) dismiss() } } 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 26a5095c..2adc0776 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 @@ -28,6 +28,7 @@ import com.vanced.manager.databinding.FragmentHomeBinding import com.vanced.manager.ui.dialogs.DialogContainer.installAlertBuilder import com.vanced.manager.ui.viewmodels.HomeViewModel import com.vanced.manager.ui.viewmodels.HomeViewModelFactory +import com.vanced.manager.utils.InternetTools.isFetching open class HomeFragment : BindingFragment() { @@ -59,6 +60,7 @@ open class HomeFragment : BindingFragment() { setHasOptionsMenu(true) with (binding) { homeRefresh.setOnRefreshListener { viewModel.fetchData() } + isFetching.observe(viewLifecycleOwner) { homeRefresh.isRefreshing = it } tooltip = ViewTooltip .on(recyclerAppList) .position(ViewTooltip.Position.TOP) 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 ac9a188b..2c8a4a47 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 @@ -27,8 +27,6 @@ import com.vanced.manager.utils.AppUtils.musicPkg import com.vanced.manager.utils.AppUtils.musicRootPkg import com.vanced.manager.utils.AppUtils.vancedPkg import com.vanced.manager.utils.AppUtils.vancedRootPkg -import com.vanced.manager.utils.Extensions.fetchData -import com.vanced.manager.utils.Extensions.setRefreshing import com.vanced.manager.utils.Extensions.show import com.vanced.manager.utils.InternetTools import com.vanced.manager.utils.InternetTools.loadJson @@ -52,10 +50,8 @@ open class HomeViewModel(private val activity: FragmentActivity): ViewModel() { fun fetchData() { viewModelScope.launch { - activity.setRefreshing(true) loadJson(activity) Crowdin.forceUpdate(activity) - activity.setRefreshing(false) } } @@ -134,20 +130,18 @@ open class HomeViewModel(private val activity: FragmentActivity): ViewModel() { fun uninstallPackage(pkg: String) { if (prefs.getString("vanced_variant", "nonroot") == "root" && uninstallRootApk(pkg)) { - viewModelScope.launch { activity.fetchData() } + viewModelScope.launch { loadJson(activity) } } else { uninstallApk(pkg, activity) } } init { - activity.setRefreshing(true) vanced.value = DataModel(InternetTools.vanced, activity, vancedPkg, activity.getString(R.string.vanced), AppCompatResources.getDrawable(activity, R.drawable.ic_vanced)) vancedRoot.value = DataModel(InternetTools.vanced, activity, vancedRootPkg, activity.getString(R.string.vanced), AppCompatResources.getDrawable(activity, R.drawable.ic_vanced)) music.value = DataModel(InternetTools.music, activity, musicPkg, activity.getString(R.string.music), AppCompatResources.getDrawable(activity, R.drawable.ic_music)) musicRoot.value = DataModel(InternetTools.music, activity, musicRootPkg, activity.getString(R.string.music), AppCompatResources.getDrawable(activity, R.drawable.ic_music)) microg.value = DataModel(InternetTools.microg, activity, microgPkg, activity.getString(R.string.microg), AppCompatResources.getDrawable(activity, R.drawable.ic_microg)) manager.value = DataModel(InternetTools.manager, activity, managerPkg, activity.getString(R.string.app_name), AppCompatResources.getDrawable(activity, R.mipmap.ic_launcher)) - activity.setRefreshing(false) } } diff --git a/app/src/main/java/com/vanced/manager/utils/Extensions.kt b/app/src/main/java/com/vanced/manager/utils/Extensions.kt index fded9167..a363ee9e 100644 --- a/app/src/main/java/com/vanced/manager/utils/Extensions.kt +++ b/app/src/main/java/com/vanced/manager/utils/Extensions.kt @@ -1,6 +1,5 @@ package com.vanced.manager.utils -import android.app.Activity import android.content.Context import android.content.ContextWrapper import android.content.DialogInterface @@ -11,13 +10,11 @@ import androidx.fragment.app.DialogFragment import androidx.fragment.app.FragmentActivity import androidx.lifecycle.LifecycleOwner import androidx.preference.PreferenceManager.getDefaultSharedPreferences -import androidx.swiperefreshlayout.widget.SwipeRefreshLayout import com.google.android.material.dialog.MaterialAlertDialogBuilder import com.google.android.material.progressindicator.LinearProgressIndicator import com.google.android.material.radiobutton.MaterialRadioButton import com.vanced.manager.R import com.vanced.manager.utils.InternetTools.baseUrl -import com.vanced.manager.utils.InternetTools.loadJson import com.vanced.manager.utils.ThemeHelper.accentColor import com.vanced.manager.utils.ThemeHelper.defAccentColor import java.util.* @@ -32,26 +29,6 @@ object Extensions { show(activity.supportFragmentManager, "") } - suspend fun Activity.fetchData() { - val refreshLayout = findViewById(R.id.home_refresh) - setRefreshing(true, refreshLayout) - loadJson(this) - setRefreshing(false, refreshLayout) - } - - fun Activity.setRefreshing(isRefreshing: Boolean) { - val refreshLayout = findViewById(R.id.home_refresh) - if (refreshLayout != null) { - refreshLayout.isRefreshing = isRefreshing - } - } - - fun Activity.setRefreshing(isRefreshing: Boolean, refreshLayout: SwipeRefreshLayout?) { - if (refreshLayout != null) { - refreshLayout.isRefreshing = isRefreshing - } - } - fun Context.getDefaultPrefs(): SharedPreferences = getDefaultSharedPreferences(this) //Not sure how much this can affect performance 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 67a3103c..1224c8c2 100644 --- a/app/src/main/java/com/vanced/manager/utils/InternetTools.kt +++ b/app/src/main/java/com/vanced/manager/utils/InternetTools.kt @@ -23,13 +23,15 @@ object InternetTools { private const val TAG = "VMNetTools" - var vanced = MutableLiveData() - var music = MutableLiveData() - var microg = MutableLiveData() - var manager = MutableLiveData() + val vanced = MutableLiveData() + val music = MutableLiveData() + val microg = MutableLiveData() + val manager = MutableLiveData() - var vancedVersions = MutableLiveData>() - var musicVersions = MutableLiveData>() + val vancedVersions = MutableLiveData>() + val musicVersions = MutableLiveData>() + + val isFetching = MutableLiveData() //var braveTiers = MutableLiveData() @@ -49,6 +51,7 @@ object InternetTools { fun getFileNameFromUrl(url: String) = url.substring(url.lastIndexOf('/') + 1, url.length) suspend fun loadJson(context: Context) = withContext(Dispatchers.IO) { + isFetching.postValue(true) val installUrl = context.getDefaultPrefs().getString("install_url", baseUrl) val calendar = Calendar.getInstance() val hour = calendar.get(Calendar.HOUR_OF_DAY) @@ -68,7 +71,7 @@ object InternetTools { musicVersions.postValue(versions?.array("music")) microg.postValue(latest?.obj("microg")) manager.postValue(latest?.obj("manager")) - + isFetching.postValue(false) } private suspend fun getJsonString(file: String, obj: String, context: Context): String {