diff --git a/app/src/main/java/foundation/e/apps/ui/updates/UpdatesFragment.kt b/app/src/main/java/foundation/e/apps/ui/updates/UpdatesFragment.kt index 9dc547e16e21e52db8abbd670b1e161734aea3f9..1319f9a9790225cd3c6d35dcce002ba92bccd814 100644 --- a/app/src/main/java/foundation/e/apps/ui/updates/UpdatesFragment.kt +++ b/app/src/main/java/foundation/e/apps/ui/updates/UpdatesFragment.kt @@ -84,6 +84,10 @@ class UpdatesFragment : TimeoutFragment(R.layout.fragment_updates), ApplicationI private var isDownloadObserverAdded = false + companion object { + private const val SCROLL_TO_TOP_DELAY_MILLIS = 100L + } + override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) _binding = FragmentUpdatesBinding.bind(view) @@ -93,9 +97,6 @@ class UpdatesFragment : TimeoutFragment(R.layout.fragment_updates), ApplicationI authObjects.observe(viewLifecycleOwner) { if (it == null) return@observe - if (!updatesViewModel.updatesList.value?.first.isNullOrEmpty()) { - return@observe - } loadDataWhenNetworkAvailable(it) } @@ -152,6 +153,11 @@ class UpdatesFragment : TimeoutFragment(R.layout.fragment_updates), ApplicationI stopLoadingUI() + binding.recyclerView.postDelayed( + { binding.recyclerView.scrollToPosition(0) }, + SCROLL_TO_TOP_DELAY_MILLIS + ) + Timber.d("===>> observeupdate list called") if (resultStatus != ResultStatus.OK) { val exception = GPlayException(resultStatus == ResultStatus.TIMEOUT) @@ -279,12 +285,14 @@ class UpdatesFragment : TimeoutFragment(R.layout.fragment_updates), ApplicationI } override fun loadData(authObjectList: List) { - showLoadingUI() - updatesViewModel.loadData(authObjectList) { - clearAndRestartGPlayLogin() - true + if (updatesViewModel.haveSourcesChanged()) { + showLoadingUI() + updatesViewModel.loadData(authObjectList) { + clearAndRestartGPlayLogin() + true + } + initUpdateAllButton() } - initUpdateAllButton() } private fun initUpdateAllButton() { diff --git a/app/src/main/java/foundation/e/apps/ui/updates/UpdatesViewModel.kt b/app/src/main/java/foundation/e/apps/ui/updates/UpdatesViewModel.kt index 42045a30fd3e760d228f3418d97703c3e25ebce5..0c15b27cb0ec1711c91b139084c60deb5c2edb1f 100644 --- a/app/src/main/java/foundation/e/apps/ui/updates/UpdatesViewModel.kt +++ b/app/src/main/java/foundation/e/apps/ui/updates/UpdatesViewModel.kt @@ -23,14 +23,16 @@ import androidx.lifecycle.viewModelScope import androidx.work.WorkInfo import com.aurora.gplayapi.data.models.AuthData import dagger.hilt.android.lifecycle.HiltViewModel +import foundation.e.apps.data.StoreRepository +import foundation.e.apps.data.Stores import foundation.e.apps.data.enums.ResultStatus import foundation.e.apps.data.enums.Status import foundation.e.apps.data.application.ApplicationRepository import foundation.e.apps.data.application.data.Application +import foundation.e.apps.data.enums.Source import foundation.e.apps.data.login.AuthObject import foundation.e.apps.data.login.exceptions.CleanApkException import foundation.e.apps.data.login.exceptions.GPlayException -import foundation.e.apps.data.preference.AppLoungePreference import foundation.e.apps.data.updates.UpdatesManagerRepository import foundation.e.apps.ui.parentFragment.LoadingViewModel import kotlinx.coroutines.launch @@ -40,11 +42,13 @@ import javax.inject.Inject class UpdatesViewModel @Inject constructor( private val updatesManagerRepository: UpdatesManagerRepository, private val applicationRepository: ApplicationRepository, - private val appLoungePreference: AppLoungePreference + private val stores: Stores ) : LoadingViewModel() { val updatesList: MutableLiveData, ResultStatus?>> = MutableLiveData() + private var previousStores = mapOf() + fun loadData( authObjectList: List, retryBlock: (failedObjects: List) -> Boolean, @@ -63,11 +67,24 @@ class UpdatesViewModel @Inject constructor( }, retryBlock) } + fun haveSourcesChanged(): Boolean { + val newStores = stores.getStores() + if (newStores == previousStores) { + return false + } + + previousStores = newStores.toMutableMap() + return true + } + private fun getUpdates(authData: AuthData?) { viewModelScope.launch { - val updatesResult = if (authData != null) + val updatesResult = if (authData != null) { updatesManagerRepository.getUpdates(authData) - else updatesManagerRepository.getUpdatesOSS() + } else { + updatesManagerRepository.getUpdatesOSS() + } + updatesList.postValue(updatesResult) val status = updatesResult.second @@ -131,6 +148,4 @@ class UpdatesViewModel @Inject constructor( ) return updatesList.value?.first?.any { pendingStatesForUpdate.contains(it.status) } == true } - - fun getUpdateInterval() = appLoungePreference.getUpdateInterval() }