From bce55bf83a511e7f006e1e2d12b92ef9f6e28cd9 Mon Sep 17 00:00:00 2001 From: Sayantan Roychowdhury Date: Thu, 3 Nov 2022 17:20:13 +0530 Subject: [PATCH 1/9] [WIP] Issue 5508: func signatures - LiveData>> --- .../e/apps/api/fused/FusedAPIImpl.kt | 8 ++- .../e/apps/api/fused/FusedAPIRepository.kt | 2 +- .../foundation/e/apps/home/HomeFragment.kt | 11 +++-- .../foundation/e/apps/home/HomeViewModel.kt | 49 ++++++++++--------- 4 files changed, 37 insertions(+), 33 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 afdb7e5ee..1a27015db 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 @@ -101,9 +101,8 @@ class FusedAPIImpl @Inject constructor( * https://gitlab.e.foundation/e/backlog/-/issues/5404 * https://gitlab.e.foundation/e/backlog/-/issues/5413 */ - suspend fun getHomeScreenData(authData: AuthData): Pair, ResultStatus> { - val preferredApplicationType = preferenceManagerModule.preferredApplicationType() - return getHomeScreenDataBasedOnApplicationType(authData, preferredApplicationType) + suspend fun getHomeScreenData(authData: AuthData): LiveData>> { + return getHomeScreenDataBasedOnApplicationType(authData) } /** @@ -130,8 +129,7 @@ class FusedAPIImpl @Inject constructor( */ private suspend fun getHomeScreenDataBasedOnApplicationType( authData: AuthData, - applicationType: String - ): Pair, ResultStatus> { + ): LiveData>> { val list = mutableListOf() val apiStatus = runCodeBlockWithTimeout({ diff --git a/app/src/main/java/foundation/e/apps/api/fused/FusedAPIRepository.kt b/app/src/main/java/foundation/e/apps/api/fused/FusedAPIRepository.kt index 6e99241b9..270f65a9e 100644 --- a/app/src/main/java/foundation/e/apps/api/fused/FusedAPIRepository.kt +++ b/app/src/main/java/foundation/e/apps/api/fused/FusedAPIRepository.kt @@ -78,7 +78,7 @@ class FusedAPIRepository @Inject constructor(private val fusedAPIImpl: FusedAPII var hasNextStreamCluster = false private set - suspend fun getHomeScreenData(authData: AuthData): Pair, ResultStatus> { + suspend fun getHomeScreenData(authData: AuthData): LiveData>> { return fusedAPIImpl.getHomeScreenData(authData) } diff --git a/app/src/main/java/foundation/e/apps/home/HomeFragment.kt b/app/src/main/java/foundation/e/apps/home/HomeFragment.kt index 31dc53311..12146e480 100644 --- a/app/src/main/java/foundation/e/apps/home/HomeFragment.kt +++ b/app/src/main/java/foundation/e/apps/home/HomeFragment.kt @@ -33,6 +33,7 @@ import foundation.e.apps.AppInfoFetchViewModel import foundation.e.apps.AppProgressViewModel import foundation.e.apps.MainActivityViewModel import foundation.e.apps.R +import foundation.e.apps.api.ResultSupreme import foundation.e.apps.api.fused.FusedAPIInterface import foundation.e.apps.api.fused.data.FusedApp import foundation.e.apps.api.fused.data.FusedHome @@ -94,7 +95,7 @@ class HomeFragment : TimeoutFragment(R.layout.fragment_home), FusedAPIInterface private fun observeHomeScreenData() { homeViewModel.homeScreenData.observe(viewLifecycleOwner) { stopLoadingUI() - if (it.second != ResultStatus.OK) { + if (!it.isSuccess()) { return@observe } @@ -102,7 +103,7 @@ class HomeFragment : TimeoutFragment(R.layout.fragment_home), FusedAPIInterface return@observe } - homeParentRVAdapter?.setData(it.first) + homeParentRVAdapter?.setData(it.data!!) } } @@ -147,9 +148,9 @@ class HomeFragment : TimeoutFragment(R.layout.fragment_home), FusedAPIInterface ).show(childFragmentManager, "HomeFragment") } - private fun isHomeDataUpdated(homeScreenResult: Pair, ResultStatus>) = + private fun isHomeDataUpdated(homeScreenResult: ResultSupreme>) = homeParentRVAdapter?.currentList?.isEmpty() == true || homeViewModel.isHomeDataUpdated( - homeScreenResult.first, + homeScreenResult.data!!, homeParentRVAdapter?.currentList as List ) @@ -195,7 +196,7 @@ class HomeFragment : TimeoutFragment(R.layout.fragment_home), FusedAPIInterface } override fun loadData(authObjectList: List) { - homeViewModel.loadData(authObjectList) { _ -> + homeViewModel.loadData(authObjectList, viewLifecycleOwner) { _ -> clearAndRestartGPlayLogin() true } diff --git a/app/src/main/java/foundation/e/apps/home/HomeViewModel.kt b/app/src/main/java/foundation/e/apps/home/HomeViewModel.kt index 25cb14b7a..89f8faf85 100644 --- a/app/src/main/java/foundation/e/apps/home/HomeViewModel.kt +++ b/app/src/main/java/foundation/e/apps/home/HomeViewModel.kt @@ -18,10 +18,12 @@ package foundation.e.apps.home +import androidx.lifecycle.LifecycleOwner import androidx.lifecycle.MutableLiveData import androidx.lifecycle.viewModelScope import com.aurora.gplayapi.data.models.AuthData import dagger.hilt.android.lifecycle.HiltViewModel +import foundation.e.apps.api.ResultSupreme import foundation.e.apps.api.fused.FusedAPIRepository import foundation.e.apps.api.fused.data.FusedApp import foundation.e.apps.api.fused.data.FusedHome @@ -44,47 +46,50 @@ class HomeViewModel @Inject constructor( * * Issue: https://gitlab.e.foundation/e/backlog/-/issues/5404 */ - var homeScreenData: MutableLiveData, ResultStatus>> = MutableLiveData() + var homeScreenData: MutableLiveData>> = MutableLiveData() fun loadData( authObjectList: List, + lifecycleOwner: LifecycleOwner, retryBlock: (failedObjects: List) -> Boolean, ) { super.onLoadData(authObjectList, { successAuthList, _ -> successAuthList.find { it is AuthObject.GPlayAuth }?.run { - getHomeScreenData(result.data!! as AuthData) + getHomeScreenData(result.data!! as AuthData, lifecycleOwner) return@onLoadData } successAuthList.find { it is AuthObject.CleanApk }?.run { - getHomeScreenData(AuthData("", "")) + getHomeScreenData(AuthData("", ""), lifecycleOwner) return@onLoadData } }, retryBlock) } - fun getHomeScreenData(authData: AuthData) { + fun getHomeScreenData( + authData: AuthData, + lifecycleOwner: LifecycleOwner, + ) { viewModelScope.launch { - val screenData = fusedAPIRepository.getHomeScreenData(authData) - homeScreenData.postValue(screenData) - - val status = screenData.second - - if (status != ResultStatus.OK) { - val exception = - if (authData.aasToken.isNotBlank() || authData.authToken.isNotBlank()) - GPlayException( - screenData.second == ResultStatus.TIMEOUT, - status.message.ifBlank { "Data load error" } + fusedAPIRepository.getHomeScreenData(authData).observe(lifecycleOwner) { + homeScreenData.postValue(it) + + if (!it.isSuccess()) { + val exception = + if (authData.aasToken.isNotBlank() || authData.authToken.isNotBlank()) + GPlayException( + it.isTimeout(), + it.message.ifBlank { "Data load error" } + ) + else CleanApkException( + it.isTimeout(), + it.message.ifBlank { "Data load error" } ) - else CleanApkException( - screenData.second == ResultStatus.TIMEOUT, - status.message.ifBlank { "Data load error" } - ) - exceptionsList.add(exception) - exceptionsLiveData.postValue(exceptionsList) + exceptionsList.add(exception) + exceptionsLiveData.postValue(exceptionsList) + } } } } @@ -94,7 +99,7 @@ class HomeViewModel @Inject constructor( } fun isFusedHomesEmpty(): Boolean { - return homeScreenData.value?.first?.let { + return homeScreenData.value?.data?.let { fusedAPIRepository.isFusedHomesEmpty(it) } ?: true } -- GitLab From 48b71b3dc4545165cc967aafd5d5074ce2aa17f6 Mon Sep 17 00:00:00 2001 From: Sayantan Roychowdhury Date: Thu, 3 Nov 2022 19:49:39 +0530 Subject: [PATCH 2/9] Issue 5508: Fetch home data and emit in livedata scope --- .../e/apps/api/fused/FusedAPIImpl.kt | 45 +++++++++++-------- 1 file changed, 27 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 1a27015db..5f93dc392 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 @@ -130,34 +130,43 @@ class FusedAPIImpl @Inject constructor( private suspend fun getHomeScreenDataBasedOnApplicationType( authData: AuthData, ): LiveData>> { + val list = mutableListOf() - val apiStatus = runCodeBlockWithTimeout({ + return liveData { if (preferenceManagerModule.isGplaySelected()) { - list.addAll(fetchGPlayHome(authData)) + val apiStatus = runCodeBlockWithTimeout({ + list.addAll(fetchGPlayHome(authData)) + }) + emit(ResultSupreme.create(apiStatus, list)) } if (preferenceManagerModule.isOpenSourceSelected()) { - val response = cleanAPKRepository.getHomeScreenData( - CleanAPKInterface.APP_TYPE_ANY, - CleanAPKInterface.APP_SOURCE_FOSS - ).body() - response?.home?.let { - list.addAll(generateCleanAPKHome(it, APP_TYPE_OPEN)) - } + val apiStatus = runCodeBlockWithTimeout({ + val response = cleanAPKRepository.getHomeScreenData( + CleanAPKInterface.APP_TYPE_ANY, + CleanAPKInterface.APP_SOURCE_FOSS + ).body() + response?.home?.let { + list.addAll(generateCleanAPKHome(it, APP_TYPE_OPEN)) + } + }) + emit(ResultSupreme.create(apiStatus, list)) } if (preferenceManagerModule.isPWASelected()) { - val response = cleanAPKRepository.getHomeScreenData( - CleanAPKInterface.APP_TYPE_PWA, - CleanAPKInterface.APP_SOURCE_ANY - ).body() - response?.home?.let { - list.addAll(generateCleanAPKHome(it, APP_TYPE_PWA)) - } + val apiStatus = runCodeBlockWithTimeout({ + val response = cleanAPKRepository.getHomeScreenData( + CleanAPKInterface.APP_TYPE_PWA, + CleanAPKInterface.APP_SOURCE_ANY + ).body() + response?.home?.let { + list.addAll(generateCleanAPKHome(it, APP_TYPE_PWA)) + } + }) + emit(ResultSupreme.create(apiStatus, list)) } - }) - return Pair(list, apiStatus) + } } /* -- GitLab From b97f556bf5ebe15e1916dae1693cb2503ad6bc8d Mon Sep 17 00:00:00 2001 From: Sayantan Roychowdhury Date: Thu, 3 Nov 2022 19:52:33 +0530 Subject: [PATCH 3/9] Issue 5508: refactor, remove useless method --- .../e/apps/api/fused/FusedAPIImpl.kt | 20 +------------------ 1 file changed, 1 insertion(+), 19 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 5f93dc392..deea77fc8 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 @@ -93,18 +93,6 @@ class FusedAPIImpl @Inject constructor( private var TAG = FusedAPIImpl::class.java.simpleName - /** - * Pass list of FusedHome and status. - * Second argument can be of [ResultStatus.TIMEOUT] to indicate timeout. - * - * Issue: - * https://gitlab.e.foundation/e/backlog/-/issues/5404 - * https://gitlab.e.foundation/e/backlog/-/issues/5413 - */ - suspend fun getHomeScreenData(authData: AuthData): LiveData>> { - return getHomeScreenDataBasedOnApplicationType(authData) - } - /** * Check if list in all the FusedHome is empty. * If any list is not empty, send false. @@ -121,13 +109,7 @@ class FusedAPIImpl @Inject constructor( return preferenceManagerModule.preferredApplicationType() } - /* - * Offload fetching application to a different method to dynamically fallback to a different - * app source if the user selected app source times out. - * - * Issue: https://gitlab.e.foundation/e/backlog/-/issues/5404 - */ - private suspend fun getHomeScreenDataBasedOnApplicationType( + suspend fun getHomeScreenData( authData: AuthData, ): LiveData>> { -- GitLab From daf012a3121d67599dac629e953bdaf38baa331f Mon Sep 17 00:00:00 2001 From: Sayantan Roychowdhury Date: Thu, 3 Nov 2022 20:49:35 +0530 Subject: [PATCH 4/9] Issue 5508: ktlint fixes --- .../java/foundation/e/apps/application/ApplicationFragment.kt | 2 +- app/src/main/java/foundation/e/apps/home/HomeFragment.kt | 1 - app/src/main/java/foundation/e/apps/home/HomeViewModel.kt | 1 - 3 files changed, 1 insertion(+), 3 deletions(-) diff --git a/app/src/main/java/foundation/e/apps/application/ApplicationFragment.kt b/app/src/main/java/foundation/e/apps/application/ApplicationFragment.kt index c575c1464..5fac54cdf 100644 --- a/app/src/main/java/foundation/e/apps/application/ApplicationFragment.kt +++ b/app/src/main/java/foundation/e/apps/application/ApplicationFragment.kt @@ -713,7 +713,7 @@ class ApplicationFragment : TimeoutFragment(R.layout.fragment_application) { return } val downloadedSize = "${ - Formatter.formatFileSize(requireContext(), progressResult.second).substringBefore(" MB") + Formatter.formatFileSize(requireContext(), progressResult.second).substringBefore(" MB") }/${Formatter.formatFileSize(requireContext(), progressResult.first)}" val progressPercentage = ((progressResult.second / progressResult.first.toDouble()) * 100f).toInt() diff --git a/app/src/main/java/foundation/e/apps/home/HomeFragment.kt b/app/src/main/java/foundation/e/apps/home/HomeFragment.kt index 12146e480..db1a5a3fb 100644 --- a/app/src/main/java/foundation/e/apps/home/HomeFragment.kt +++ b/app/src/main/java/foundation/e/apps/home/HomeFragment.kt @@ -44,7 +44,6 @@ import foundation.e.apps.home.model.HomeParentRVAdapter import foundation.e.apps.login.AuthObject import foundation.e.apps.manager.download.data.DownloadProgress import foundation.e.apps.manager.pkg.PkgManagerModule -import foundation.e.apps.utils.enums.ResultStatus import foundation.e.apps.utils.enums.Status import foundation.e.apps.utils.exceptions.GPlayException import foundation.e.apps.utils.exceptions.GPlayLoginException diff --git a/app/src/main/java/foundation/e/apps/home/HomeViewModel.kt b/app/src/main/java/foundation/e/apps/home/HomeViewModel.kt index 89f8faf85..3fa06ba15 100644 --- a/app/src/main/java/foundation/e/apps/home/HomeViewModel.kt +++ b/app/src/main/java/foundation/e/apps/home/HomeViewModel.kt @@ -28,7 +28,6 @@ import foundation.e.apps.api.fused.FusedAPIRepository import foundation.e.apps.api.fused.data.FusedApp import foundation.e.apps.api.fused.data.FusedHome import foundation.e.apps.login.AuthObject -import foundation.e.apps.utils.enums.ResultStatus import foundation.e.apps.utils.exceptions.CleanApkException import foundation.e.apps.utils.exceptions.GPlayException import foundation.e.apps.utils.parentFragment.LoadingViewModel -- GitLab From 483b00f355c73a010f1bbaf3b809686534a949ea Mon Sep 17 00:00:00 2001 From: Sayantan Roychowdhury Date: Thu, 3 Nov 2022 21:42:34 +0530 Subject: [PATCH 5/9] Issue 5508: ResultSupreme - allow adding error message for timeout as well. --- app/src/main/java/foundation/e/apps/api/ResultSupreme.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/src/main/java/foundation/e/apps/api/ResultSupreme.kt b/app/src/main/java/foundation/e/apps/api/ResultSupreme.kt index d64afd821..856674c71 100644 --- a/app/src/main/java/foundation/e/apps/api/ResultSupreme.kt +++ b/app/src/main/java/foundation/e/apps/api/ResultSupreme.kt @@ -140,7 +140,7 @@ sealed class ResultSupreme { if (isUnknownError()) { this.data = data } else { - this.message = message + this.message = message.ifBlank { status.message } this.exception = exception } } -- GitLab From 19db21f4f62a325686f5831075770f0feb0ff071 Mon Sep 17 00:00:00 2001 From: Sayantan Roychowdhury Date: Thu, 3 Nov 2022 21:44:08 +0530 Subject: [PATCH 6/9] Issue 5508: Add clearer logging if home data from a particular source fails. --- .../java/foundation/e/apps/api/fused/FusedAPIImpl.kt | 9 +++++++++ 1 file changed, 9 insertions(+) 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 deea77fc8..5b8e63e53 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 @@ -120,6 +120,9 @@ class FusedAPIImpl @Inject constructor( val apiStatus = runCodeBlockWithTimeout({ list.addAll(fetchGPlayHome(authData)) }) + if (apiStatus != ResultStatus.OK) { + apiStatus.message = ("GPlay home loading error\n" + apiStatus.message).trim() + } emit(ResultSupreme.create(apiStatus, list)) } @@ -133,6 +136,9 @@ class FusedAPIImpl @Inject constructor( list.addAll(generateCleanAPKHome(it, APP_TYPE_OPEN)) } }) + if (apiStatus != ResultStatus.OK) { + apiStatus.message = ("Open Source home loading error\n" + apiStatus.message).trim() + } emit(ResultSupreme.create(apiStatus, list)) } @@ -146,6 +152,9 @@ class FusedAPIImpl @Inject constructor( list.addAll(generateCleanAPKHome(it, APP_TYPE_PWA)) } }) + if (apiStatus != ResultStatus.OK) { + apiStatus.message = ("PWA home loading error\n" + apiStatus.message).trim() + } emit(ResultSupreme.create(apiStatus, list)) } } -- GitLab From 3d57be7e8a9875fe884b049d8213d3a401ab9131 Mon Sep 17 00:00:00 2001 From: Sayantan Roychowdhury Date: Fri, 4 Nov 2022 12:54:13 +0530 Subject: [PATCH 7/9] Issue 5508: resolve one nested if block --- .../foundation/e/apps/home/HomeViewModel.kt | 22 +++++++++---------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/app/src/main/java/foundation/e/apps/home/HomeViewModel.kt b/app/src/main/java/foundation/e/apps/home/HomeViewModel.kt index 3fa06ba15..d42e4dce0 100644 --- a/app/src/main/java/foundation/e/apps/home/HomeViewModel.kt +++ b/app/src/main/java/foundation/e/apps/home/HomeViewModel.kt @@ -74,21 +74,21 @@ class HomeViewModel @Inject constructor( fusedAPIRepository.getHomeScreenData(authData).observe(lifecycleOwner) { homeScreenData.postValue(it) - if (!it.isSuccess()) { - val exception = - if (authData.aasToken.isNotBlank() || authData.authToken.isNotBlank()) - GPlayException( - it.isTimeout(), - it.message.ifBlank { "Data load error" } - ) - else CleanApkException( + if (it.isSuccess()) return@observe + + val exception = + if (authData.aasToken.isNotBlank() || authData.authToken.isNotBlank()) + GPlayException( it.isTimeout(), it.message.ifBlank { "Data load error" } ) + else CleanApkException( + it.isTimeout(), + it.message.ifBlank { "Data load error" } + ) - exceptionsList.add(exception) - exceptionsLiveData.postValue(exceptionsList) - } + exceptionsList.add(exception) + exceptionsLiveData.postValue(exceptionsList) } } } -- GitLab From 98a5876270f75b6e9937f81d63234234764f965f Mon Sep 17 00:00:00 2001 From: Sayantan Roychowdhury Date: Fri, 4 Nov 2022 15:19:42 +0530 Subject: [PATCH 8/9] Issue 5508: Create Source enum class. Use switch case to load home data. --- .../e/apps/api/fused/FusedAPIImpl.kt | 81 +++++++++++-------- .../foundation/e/apps/utils/enums/Source.kt | 24 ++++++ 2 files changed, 73 insertions(+), 32 deletions(-) create mode 100644 app/src/main/java/foundation/e/apps/utils/enums/Source.kt 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 5b8e63e53..5e0ac928d 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,6 +21,7 @@ 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 @@ -54,6 +55,7 @@ import foundation.e.apps.utils.enums.AppTag import foundation.e.apps.utils.enums.FilterLevel import foundation.e.apps.utils.enums.Origin import foundation.e.apps.utils.enums.ResultStatus +import foundation.e.apps.utils.enums.Source import foundation.e.apps.utils.enums.Status import foundation.e.apps.utils.enums.Type import foundation.e.apps.utils.enums.isUnFiltered @@ -117,47 +119,62 @@ class FusedAPIImpl @Inject constructor( return liveData { if (preferenceManagerModule.isGplaySelected()) { - val apiStatus = runCodeBlockWithTimeout({ - list.addAll(fetchGPlayHome(authData)) - }) - if (apiStatus != ResultStatus.OK) { - apiStatus.message = ("GPlay home loading error\n" + apiStatus.message).trim() - } - emit(ResultSupreme.create(apiStatus, list)) + loadHomeData(list, Source.GPLAY, authData, this) } if (preferenceManagerModule.isOpenSourceSelected()) { - val apiStatus = runCodeBlockWithTimeout({ - val response = cleanAPKRepository.getHomeScreenData( - CleanAPKInterface.APP_TYPE_ANY, - CleanAPKInterface.APP_SOURCE_FOSS - ).body() - response?.home?.let { - list.addAll(generateCleanAPKHome(it, APP_TYPE_OPEN)) - } - }) - if (apiStatus != ResultStatus.OK) { - apiStatus.message = ("Open Source home loading error\n" + apiStatus.message).trim() - } - emit(ResultSupreme.create(apiStatus, list)) + loadHomeData(list, Source.OPEN, authData, this) } if (preferenceManagerModule.isPWASelected()) { - val apiStatus = runCodeBlockWithTimeout({ - val response = cleanAPKRepository.getHomeScreenData( - CleanAPKInterface.APP_TYPE_PWA, - CleanAPKInterface.APP_SOURCE_ANY - ).body() - response?.home?.let { - list.addAll(generateCleanAPKHome(it, APP_TYPE_PWA)) - } - }) - if (apiStatus != ResultStatus.OK) { - apiStatus.message = ("PWA home loading error\n" + apiStatus.message).trim() + loadHomeData(list, Source.PWA, authData, this) + } + } + } + + private suspend fun loadHomeData( + priorList: MutableList, + source: Source, + authData: AuthData, + scope: LiveDataScope>>, + ) { + + val apiStatus = when (source) { + + Source.GPLAY -> runCodeBlockWithTimeout({ + priorList.addAll(fetchGPlayHome(authData)) + }) + + Source.OPEN -> runCodeBlockWithTimeout({ + val response = cleanAPKRepository.getHomeScreenData( + CleanAPKInterface.APP_TYPE_ANY, + CleanAPKInterface.APP_SOURCE_FOSS + ).body() + response?.home?.let { + priorList.addAll(generateCleanAPKHome(it, APP_TYPE_OPEN)) + } + }) + + Source.PWA -> runCodeBlockWithTimeout({ + val response = cleanAPKRepository.getHomeScreenData( + CleanAPKInterface.APP_TYPE_PWA, + CleanAPKInterface.APP_SOURCE_ANY + ).body() + response?.home?.let { + priorList.addAll(generateCleanAPKHome(it, APP_TYPE_PWA)) } - emit(ResultSupreme.create(apiStatus, list)) + }) + } + + if (apiStatus != ResultStatus.OK) { + apiStatus.message = when (source) { + Source.GPLAY -> ("GPlay home loading error\n" + apiStatus.message).trim() + Source.OPEN -> ("Open Source home loading error\n" + apiStatus.message).trim() + Source.PWA -> ("PWA home loading error\n" + apiStatus.message).trim() } } + + scope.emit(ResultSupreme.create(apiStatus, priorList)) } /* diff --git a/app/src/main/java/foundation/e/apps/utils/enums/Source.kt b/app/src/main/java/foundation/e/apps/utils/enums/Source.kt new file mode 100644 index 000000000..07780704e --- /dev/null +++ b/app/src/main/java/foundation/e/apps/utils/enums/Source.kt @@ -0,0 +1,24 @@ +/* + * Copyright (C) 2019-2022 MURENA SAS + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package foundation.e.apps.utils.enums + +enum class Source { + GPLAY, + OPEN, + PWA, +} -- GitLab From c27dbb6cb5f09df88c39ddcb369a4be227c76e85 Mon Sep 17 00:00:00 2001 From: Sayantan Roychowdhury Date: Mon, 7 Nov 2022 14:30:45 +0530 Subject: [PATCH 9/9] Issue 5508: Move home error message to a separate function. --- .../main/java/foundation/e/apps/api/fused/FusedAPIImpl.kt | 8 ++++++-- 1 file changed, 6 insertions(+), 2 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 5e0ac928d..564e62631 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 @@ -166,6 +166,12 @@ class FusedAPIImpl @Inject constructor( }) } + setHomeErrorMessage(apiStatus, source) + + scope.emit(ResultSupreme.create(apiStatus, priorList)) + } + + private fun setHomeErrorMessage(apiStatus: ResultStatus, source: Source) { if (apiStatus != ResultStatus.OK) { apiStatus.message = when (source) { Source.GPLAY -> ("GPlay home loading error\n" + apiStatus.message).trim() @@ -173,8 +179,6 @@ class FusedAPIImpl @Inject constructor( Source.PWA -> ("PWA home loading error\n" + apiStatus.message).trim() } } - - scope.emit(ResultSupreme.create(apiStatus, priorList)) } /* -- GitLab