From 730f511af4ea511e860f5cd84ab8b873e3e0627d Mon Sep 17 00:00:00 2001 From: hasibprince Date: Thu, 2 Mar 2023 13:54:19 +0600 Subject: [PATCH 1/2] loading hompage data asynchronously --- .../e/apps/api/fused/FusedAPIImpl.kt | 57 +++++++++++++------ 1 file changed, 39 insertions(+), 18 deletions(-) diff --git a/app/src/main/java/foundation/e/apps/api/fused/FusedAPIImpl.kt b/app/src/main/java/foundation/e/apps/api/fused/FusedAPIImpl.kt index 889cbb999..c77f4900e 100644 --- a/app/src/main/java/foundation/e/apps/api/fused/FusedAPIImpl.kt +++ b/app/src/main/java/foundation/e/apps/api/fused/FusedAPIImpl.kt @@ -21,7 +21,6 @@ package foundation.e.apps.api.fused import android.content.Context import android.text.format.Formatter import androidx.lifecycle.LiveData -import androidx.lifecycle.LiveDataScope import androidx.lifecycle.liveData import androidx.lifecycle.map import com.aurora.gplayapi.Constants @@ -62,8 +61,7 @@ import foundation.e.apps.utils.enums.Type import foundation.e.apps.utils.enums.isUnFiltered import foundation.e.apps.utils.modules.PWAManagerModule import foundation.e.apps.utils.modules.PreferenceManagerModule -import kotlinx.coroutines.TimeoutCancellationException -import kotlinx.coroutines.withTimeout +import kotlinx.coroutines.* import timber.log.Timber import javax.inject.Inject import javax.inject.Singleton @@ -122,19 +120,37 @@ class FusedAPIImpl @Inject constructor( ): LiveData>> { val list = mutableListOf() + var resultGplay: Deferred>>? = null + var resultOpenSource: Deferred>>? = null + var resultPWA: Deferred>>? = null return liveData { - if (preferenceManagerModule.isGplaySelected()) { - loadHomeData(list, Source.GPLAY, authData, this) - } + coroutineScope { - if (preferenceManagerModule.isOpenSourceSelected()) { - loadHomeData(list, Source.OPEN, authData, this) - } + if (preferenceManagerModule.isGplaySelected()) { + resultGplay = async { loadHomeData(list, Source.GPLAY, authData) } + } + + if (preferenceManagerModule.isOpenSourceSelected()) { + resultOpenSource = async { loadHomeData(list, Source.OPEN, authData) } + } + + if (preferenceManagerModule.isPWASelected()) { + resultPWA = async { loadHomeData(list, Source.PWA, authData) } + } + + resultGplay?.await()?.let { + emit(it) + } + resultOpenSource?.await()?.let { + emit(it) + } + resultPWA?.await()?.let { + emit(it) + } - if (preferenceManagerModule.isPWASelected()) { - loadHomeData(list, Source.PWA, authData, this) } + } } @@ -142,11 +158,9 @@ class FusedAPIImpl @Inject constructor( priorList: MutableList, source: Source, authData: AuthData, - scope: LiveDataScope>>, - ) { + ): ResultSupreme> { val apiStatus = when (source) { - Source.GPLAY -> runCodeBlockWithTimeout({ priorList.addAll(fetchGPlayHome(authData)) }) @@ -173,8 +187,14 @@ class FusedAPIImpl @Inject constructor( } setHomeErrorMessage(apiStatus, source) - - scope.emit(ResultSupreme.create(apiStatus, priorList)) + priorList.sortByDescending { + when (it.source) { + APP_TYPE_OPEN -> 2 + APP_TYPE_PWA -> 1 + else -> 3 + } + } + return ResultSupreme.create(apiStatus, priorList) } private fun setHomeErrorMessage(apiStatus: ResultStatus, source: Source) { @@ -1052,7 +1072,7 @@ class FusedAPIImpl @Inject constructor( private fun getCategoryIconName(category: FusedCategory): String { var categoryTitle = if (category.tag.getOperationalTag() - .contentEquals(AppTag.GPlay().getOperationalTag()) + .contentEquals(AppTag.GPlay().getOperationalTag()) ) category.id else category.title if (categoryTitle.contains(CATEGORY_TITLE_REPLACEABLE_CONJUNCTION)) { @@ -1190,7 +1210,8 @@ class FusedAPIImpl @Inject constructor( query: String, authData: AuthData ): LiveData, Boolean>> { - val searchResults = gPlayAPIRepository.getSearchResults(query, authData, ::replaceWithFDroid) + val searchResults = + gPlayAPIRepository.getSearchResults(query, authData, ::replaceWithFDroid) return searchResults.map { Pair( it.first, -- GitLab From bb532e4e9987d953e057b11b22a7150bbf3d0f26 Mon Sep 17 00:00:00 2001 From: hasibprince Date: Fri, 3 Mar 2023 09:51:33 +0600 Subject: [PATCH 2/2] fixed: lint issues --- .../java/foundation/e/apps/MainActivity.kt | 1 - .../foundation/e/apps/MainActivityViewModel.kt | 11 +++++------ .../e/apps/api/fused/FusedAPIImpl.kt | 18 +++++++++++------- .../utils/parentFragment/TimeoutFragment.kt | 15 +++++++++------ 4 files changed, 25 insertions(+), 20 deletions(-) diff --git a/app/src/main/java/foundation/e/apps/MainActivity.kt b/app/src/main/java/foundation/e/apps/MainActivity.kt index 67efd606d..e9a8adfb5 100644 --- a/app/src/main/java/foundation/e/apps/MainActivity.kt +++ b/app/src/main/java/foundation/e/apps/MainActivity.kt @@ -55,7 +55,6 @@ import foundation.e.apps.utils.eventBus.AppEvent import foundation.e.apps.utils.eventBus.EventBus import foundation.e.apps.utils.exceptions.GPlayValidationException import foundation.e.apps.utils.modules.CommonUtilsFunctions -import foundation.e.apps.utils.modules.CommonUtilsModule import kotlinx.coroutines.flow.collectLatest import kotlinx.coroutines.flow.filter import kotlinx.coroutines.launch diff --git a/app/src/main/java/foundation/e/apps/MainActivityViewModel.kt b/app/src/main/java/foundation/e/apps/MainActivityViewModel.kt index 901d86692..e769ba46b 100644 --- a/app/src/main/java/foundation/e/apps/MainActivityViewModel.kt +++ b/app/src/main/java/foundation/e/apps/MainActivityViewModel.kt @@ -53,15 +53,14 @@ import foundation.e.apps.utils.enums.Type import foundation.e.apps.utils.enums.User import foundation.e.apps.utils.enums.isInitialized import foundation.e.apps.utils.enums.isUnFiltered -import foundation.e.apps.utils.modules.CommonUtilsModule import foundation.e.apps.utils.modules.DataStoreModule import foundation.e.apps.utils.modules.PWAManagerModule -import java.io.ByteArrayOutputStream -import javax.inject.Inject import kotlinx.coroutines.channels.ProducerScope import kotlinx.coroutines.channels.awaitClose import kotlinx.coroutines.flow.callbackFlow import kotlinx.coroutines.launch +import java.io.ByteArrayOutputStream +import javax.inject.Inject @HiltViewModel class MainActivityViewModel @Inject constructor( @@ -394,9 +393,9 @@ class MainActivityViewModel @Inject constructor( val capabilities = connectivityManager.getNetworkCapabilities(connectivityManager.activeNetwork) val hasInternet = - capabilities != null - && capabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET) - && capabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_VALIDATED) + capabilities != null && + capabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET) && + capabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_VALIDATED) trySend(hasInternet) } diff --git a/app/src/main/java/foundation/e/apps/api/fused/FusedAPIImpl.kt b/app/src/main/java/foundation/e/apps/api/fused/FusedAPIImpl.kt index c77f4900e..33e061c16 100644 --- a/app/src/main/java/foundation/e/apps/api/fused/FusedAPIImpl.kt +++ b/app/src/main/java/foundation/e/apps/api/fused/FusedAPIImpl.kt @@ -61,11 +61,17 @@ import foundation.e.apps.utils.enums.Type import foundation.e.apps.utils.enums.isUnFiltered import foundation.e.apps.utils.modules.PWAManagerModule import foundation.e.apps.utils.modules.PreferenceManagerModule -import kotlinx.coroutines.* +import kotlinx.coroutines.Deferred +import kotlinx.coroutines.TimeoutCancellationException +import kotlinx.coroutines.async +import kotlinx.coroutines.coroutineScope +import kotlinx.coroutines.withTimeout import timber.log.Timber import javax.inject.Inject import javax.inject.Singleton +typealias FusedHomeDeferred = Deferred>> + @Singleton class FusedAPIImpl @Inject constructor( private val cleanAPKRepository: CleanAPKRepository, @@ -120,9 +126,9 @@ class FusedAPIImpl @Inject constructor( ): LiveData>> { val list = mutableListOf() - var resultGplay: Deferred>>? = null - var resultOpenSource: Deferred>>? = null - var resultPWA: Deferred>>? = null + var resultGplay: FusedHomeDeferred? = null + var resultOpenSource: FusedHomeDeferred? = null + var resultPWA: FusedHomeDeferred? = null return liveData { coroutineScope { @@ -148,9 +154,7 @@ class FusedAPIImpl @Inject constructor( resultPWA?.await()?.let { emit(it) } - } - } } @@ -1072,7 +1076,7 @@ class FusedAPIImpl @Inject constructor( private fun getCategoryIconName(category: FusedCategory): String { var categoryTitle = if (category.tag.getOperationalTag() - .contentEquals(AppTag.GPlay().getOperationalTag()) + .contentEquals(AppTag.GPlay().getOperationalTag()) ) category.id else category.title if (categoryTitle.contains(CATEGORY_TITLE_REPLACEABLE_CONJUNCTION)) { diff --git a/app/src/main/java/foundation/e/apps/utils/parentFragment/TimeoutFragment.kt b/app/src/main/java/foundation/e/apps/utils/parentFragment/TimeoutFragment.kt index 870e5e9a1..447720787 100644 --- a/app/src/main/java/foundation/e/apps/utils/parentFragment/TimeoutFragment.kt +++ b/app/src/main/java/foundation/e/apps/utils/parentFragment/TimeoutFragment.kt @@ -99,14 +99,17 @@ abstract class TimeoutFragment(@LayoutRes layoutId: Int) : Fragment(layoutId) { * is disconnected and reconnected multiple times. */ private fun LiveData.loadDataOnce(lifecycleOwner: LifecycleOwner, observer: Observer) { - observe(lifecycleOwner, object : Observer { - override fun onChanged(t: Boolean) { - observer.onChanged(t) - if (t) { - removeObserver(this) + observe( + lifecycleOwner, + object : Observer { + override fun onChanged(t: Boolean) { + observer.onChanged(t) + if (t) { + removeObserver(this) + } } } - }) + ) } /** -- GitLab