diff --git a/app/src/main/java/foundation/e/apps/MainActivity.kt b/app/src/main/java/foundation/e/apps/MainActivity.kt index 67efd606d0b8c73967cec33124aaf5f3b83729d8..e9a8adfb505916bce94b435db6c3af15456a63bd 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 901d86692e6323930589e03a99219c7a0bc4a94d..e769ba46b2e4ee62aaad3787b5b843a342694aff 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 889cbb9993a6a285b02b47e9e803dc5b37bbde12..33e061c162aea417085482e4cf9d3c863095ea23 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,12 +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.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, @@ -122,18 +126,34 @@ class FusedAPIImpl @Inject constructor( ): LiveData>> { val list = mutableListOf() + var resultGplay: FusedHomeDeferred? = null + var resultOpenSource: FusedHomeDeferred? = null + var resultPWA: FusedHomeDeferred? = 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.isPWASelected()) { - loadHomeData(list, Source.PWA, authData, this) + 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) + } } } } @@ -142,11 +162,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 +191,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) { @@ -1190,7 +1214,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, 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 870e5e9a13c6a981f217dfc29d0d63785b930020..447720787ce2503ed834086552a4217900ba7d08 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) + } } } - }) + ) } /**