From 444831f6cb5590f1826ca5b97a61b10284897914 Mon Sep 17 00:00:00 2001 From: Hasib Prince Date: Wed, 11 May 2022 17:33:58 +0600 Subject: [PATCH] App Lounge: AppNotFound exception is handled --- .../java/foundation/e/apps/MainActivity.kt | 2 +- .../e/apps/application/ApplicationFragment.kt | 5 ++++ .../apps/application/ApplicationViewModel.kt | 27 +++++++++++++------ app/src/main/res/values/strings.xml | 1 + 4 files changed, 26 insertions(+), 9 deletions(-) diff --git a/app/src/main/java/foundation/e/apps/MainActivity.kt b/app/src/main/java/foundation/e/apps/MainActivity.kt index 3c6d100cd..63d0ac77e 100644 --- a/app/src/main/java/foundation/e/apps/MainActivity.kt +++ b/app/src/main/java/foundation/e/apps/MainActivity.kt @@ -258,7 +258,7 @@ class MainActivity : AppCompatActivity() { } } - private fun showSnackbarMessage(message: String) { + fun showSnackbarMessage(message: String) { Snackbar.make(binding.root, message, Snackbar.LENGTH_SHORT).show() } 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 1c896b79a..d5f360c6b 100644 --- a/app/src/main/java/foundation/e/apps/application/ApplicationFragment.kt +++ b/app/src/main/java/foundation/e/apps/application/ApplicationFragment.kt @@ -45,6 +45,7 @@ import com.google.android.material.snackbar.Snackbar import com.google.android.material.textview.MaterialTextView import dagger.hilt.android.AndroidEntryPoint import foundation.e.apps.AppInfoFetchViewModel +import foundation.e.apps.MainActivity import foundation.e.apps.MainActivityViewModel import foundation.e.apps.PrivacyInfoViewModel import foundation.e.apps.R @@ -258,6 +259,10 @@ class ApplicationFragment : Fragment(R.layout.fragment_application) { observeDownloadStatus(view) fetchAppTracker(it) } + + applicationViewModel.errorMessageLiveData.observe(viewLifecycleOwner) { + (requireActivity() as MainActivity).showSnackbarMessage(getString(it)) + } } private fun observeDownloadStatus(view: View) { diff --git a/app/src/main/java/foundation/e/apps/application/ApplicationViewModel.kt b/app/src/main/java/foundation/e/apps/application/ApplicationViewModel.kt index aa902b8f5..ea96457cb 100644 --- a/app/src/main/java/foundation/e/apps/application/ApplicationViewModel.kt +++ b/app/src/main/java/foundation/e/apps/application/ApplicationViewModel.kt @@ -22,7 +22,9 @@ import androidx.lifecycle.MutableLiveData import androidx.lifecycle.ViewModel import androidx.lifecycle.viewModelScope import com.aurora.gplayapi.data.models.AuthData +import com.aurora.gplayapi.exceptions.ApiException import dagger.hilt.android.lifecycle.HiltViewModel +import foundation.e.apps.R import foundation.e.apps.api.fused.FusedAPIRepository import foundation.e.apps.api.fused.data.FusedApp import foundation.e.apps.manager.database.fusedDownload.FusedDownload @@ -47,17 +49,25 @@ class ApplicationViewModel @Inject constructor( val fusedApp: MutableLiveData = MutableLiveData() val appStatus: MutableLiveData = MutableLiveData() val downloadProgress = downloadProgressLD + private val _errorMessageLiveData: MutableLiveData = MutableLiveData() + val errorMessageLiveData: MutableLiveData = _errorMessageLiveData fun getApplicationDetails(id: String, packageName: String, authData: AuthData, origin: Origin) { viewModelScope.launch(Dispatchers.IO) { - fusedApp.postValue( - fusedAPIRepository.getApplicationDetails( - id, - packageName, - authData, - origin + try { + fusedApp.postValue( + fusedAPIRepository.getApplicationDetails( + id, + packageName, + authData, + origin + ) ) - ) + } catch (e: ApiException.AppNotFound) { + _errorMessageLiveData.postValue(R.string.app_not_found) + } catch (e: Exception) { + _errorMessageLiveData.postValue(R.string.unknown_error) + } } } @@ -107,7 +117,8 @@ class ApplicationViewModel @Inject constructor( fusedApp.value?.let { app -> val downloadingItem = downloadList.find { it.origin == app.origin && (it.packageName == app.package_name || it.id == app.package_name) } - appStatus.value = downloadingItem?.status ?: fusedAPIRepository.getFusedAppInstallationStatus(app) + appStatus.value = + downloadingItem?.status ?: fusedAPIRepository.getFusedAppInstallationStatus(app) } } } diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index fb2863aae..3e8be0412 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -104,6 +104,7 @@ This app will be available later! Unknown Error! There is not enough space available to download this application! + Your application was not found. Update All -- GitLab