diff --git a/app/src/main/java/foundation/e/apps/ui/search/SearchFragment.kt b/app/src/main/java/foundation/e/apps/ui/search/SearchFragment.kt index db775616c063b5931a3e19c29381e26e950090d1..9b00e52614b78c266f7aca0886c8b8f14e469871 100644 --- a/app/src/main/java/foundation/e/apps/ui/search/SearchFragment.kt +++ b/app/src/main/java/foundation/e/apps/ui/search/SearchFragment.kt @@ -313,9 +313,7 @@ class SearchFragment : private fun initiateSearch() { showLoadingUI() - searchViewModel.loadData(searchText) { - true - } + searchViewModel.loadData(searchText) } private fun showLoadingUI() { diff --git a/app/src/main/java/foundation/e/apps/ui/search/SearchViewModel.kt b/app/src/main/java/foundation/e/apps/ui/search/SearchViewModel.kt index b4b04c8ae9c19eabec200042f2b5d99031fa1690..e07d3400716bf3063b693768e1d2c90e4b9f47dc 100644 --- a/app/src/main/java/foundation/e/apps/ui/search/SearchViewModel.kt +++ b/app/src/main/java/foundation/e/apps/ui/search/SearchViewModel.kt @@ -21,6 +21,7 @@ package foundation.e.apps.ui.search import androidx.annotation.GuardedBy import androidx.lifecycle.LiveData import androidx.lifecycle.MutableLiveData +import androidx.lifecycle.ViewModel import androidx.lifecycle.viewModelScope import com.aurora.gplayapi.SearchSuggestEntry import dagger.hilt.android.lifecycle.HiltViewModel @@ -31,14 +32,9 @@ import foundation.e.apps.data.application.search.SearchResult import foundation.e.apps.data.enums.Source import foundation.e.apps.data.exodus.repositories.IAppPrivacyInfoRepository import foundation.e.apps.data.exodus.repositories.PrivacyScoreRepository -import foundation.e.apps.data.login.AuthObject -import foundation.e.apps.data.login.AuthenticatorRepository -import foundation.e.apps.ui.parentFragment.LoadingViewModel -import kotlinx.coroutines.Dispatchers.IO -import kotlinx.coroutines.Dispatchers.Main +import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.async import kotlinx.coroutines.awaitAll -import kotlinx.coroutines.delay import kotlinx.coroutines.launch import kotlinx.coroutines.sync.Mutex import kotlinx.coroutines.sync.withLock @@ -50,9 +46,8 @@ import javax.inject.Inject class SearchViewModel @Inject constructor( private val applicationRepository: ApplicationRepository, private val privacyScoreRepository: PrivacyScoreRepository, - private val appPrivacyInfoRepository: IAppPrivacyInfoRepository, - private val authenticatorRepository: AuthenticatorRepository -) : LoadingViewModel() { + private val appPrivacyInfoRepository: IAppPrivacyInfoRepository +) : ViewModel() { val searchSuggest: MutableLiveData?> = MutableLiveData() @@ -60,8 +55,6 @@ class SearchViewModel @Inject constructor( MutableLiveData() val searchResult: LiveData = _searchResult - private var lastAuthObjects: List? = null - private var isLoading: Boolean = false @GuardedBy("mutex") @@ -72,10 +65,6 @@ class SearchViewModel @Inject constructor( private var flagOpenSource: Boolean = false private var flagPWA: Boolean = false - companion object { - private const val PREVENT_HTTP_429_DELAY_IN_MS = 1000L - } - fun setFilterFlags( flagNoTrackers: Boolean = false, flagOpenSource: Boolean = false, @@ -85,50 +74,36 @@ class SearchViewModel @Inject constructor( this.flagOpenSource = flagOpenSource this.flagPWA = flagPWA - viewModelScope.launch(IO) { + viewModelScope.launch(Dispatchers.IO) { emitFilteredResults(null) } } fun getSearchSuggestions(query: String) { - viewModelScope.launch(IO) { + viewModelScope.launch(Dispatchers.IO) { searchSuggest.postValue( applicationRepository.getSearchSuggestions(query) ) } } - fun loadData( - query: String, - retryBlock: (failedObjects: List) -> Boolean - ) { - viewModelScope.launch { - if (query.isBlank()) return@launch + fun loadData(query: String) { + if (query.isBlank()) return - val authObjects = authenticatorRepository.getAuthObjects() - - lastAuthObjects = authObjects - - super.onLoadData(authObjects, { successObjects, failedObjects -> - viewModelScope.launch { - mutex.withLock { - accumulatedList.clear() - } - } - - successObjects.find { it is AuthObject.CleanApk }?.run { - fetchCleanApkData(query) - } + viewModelScope.launch { + mutex.withLock { + accumulatedList.clear() + } + } - successObjects.find { it is AuthObject.GPlayAuth }?.run { - fetchGplayData(query) - } + _searchResult.postValue( + ResultSupreme.Success( + Pair(emptyList(), false) + ) + ) - failedObjects.find { it is AuthObject.GPlayAuth }?.run { - fetchGplayData(query) - } - }, retryBlock) - } + fetchCleanApkData(query) + fetchGplayData(query) } /* @@ -140,27 +115,18 @@ class SearchViewModel @Inject constructor( private fun fetchCleanApkData( query: String ) { - viewModelScope.launch(IO) { + viewModelScope.launch(Dispatchers.IO) { val searchResultSupreme = applicationRepository.getCleanApkSearchResults(query) - emitFilteredResults(searchResultSupreme) - - if (!searchResultSupreme.isSuccess()) { - searchResultSupreme.exception?.let { handleException(it) } - } } } - fun loadMore(query: String, autoTriggered: Boolean = false) { - viewModelScope.launch(Main) { + fun loadMore(query: String) { + viewModelScope.launch(Dispatchers.Main) { if (isLoading) { Timber.d("Search result is loading....") return@launch } - - if (autoTriggered) { - delay(PREVENT_HTTP_429_DELAY_IN_MS) - } fetchGplayData(query) } } @@ -170,18 +136,12 @@ class SearchViewModel @Inject constructor( } private fun fetchGplayData(query: String) { - viewModelScope.launch(IO) { + viewModelScope.launch(Dispatchers.IO) { isLoading = true val gplaySearchResult = applicationRepository.getGplaySearchResults(query) - if (!gplaySearchResult.isSuccess()) { - gplaySearchResult.exception?.let { - handleException(it) - } - } - val currentAppList = updateCurrentAppList(gplaySearchResult) val finalResult = ResultSupreme.Success( @@ -204,11 +164,6 @@ class SearchViewModel @Inject constructor( return currentAppList.distinctBy { it.package_name } } - private fun handleException(exception: Exception) { - exceptionsList.add(exception) - exceptionsLiveData.postValue(exceptionsList) - } - /** * @return returns true if there is changes in data, otherwise false */ @@ -217,10 +172,6 @@ class SearchViewModel @Inject constructor( oldApplications: List ) = applicationRepository.isAnyFusedAppUpdated(newApplications, oldApplications) - fun isAuthObjectListSame(authObjectList: List?): Boolean { - return lastAuthObjects == authObjectList - } - private fun hasTrackers(app: Application): Boolean { return when { app.privacyScore == 0 -> true // Manually blocked apps (Facebook etc.) @@ -236,7 +187,7 @@ class SearchViewModel @Inject constructor( } } - private suspend fun getFilteredList(): List = withContext(IO) { + private suspend fun getFilteredList(): List = withContext(Dispatchers.IO) { if (flagNoTrackers) { mutex.withLock { val deferredCheck = accumulatedList.map {