compose: details: Setup and use app state properly

Signed-off-by: Aayush Gupta <aayushgupta219@gmail.com>
This commit is contained in:
Aayush Gupta
2025-08-30 19:59:56 +08:00
parent 0dde304938
commit 10b9e5a182
3 changed files with 40 additions and 42 deletions

View File

@@ -109,14 +109,13 @@ fun AppDetailsScreen(
LaunchedEffect(key1 = packageName) { viewModel.fetchAppDetails(packageName) } LaunchedEffect(key1 = packageName) { viewModel.fetchAppDetails(packageName) }
with(app) { when (state) {
when { is AppState.Loading -> ScreenContentLoading(onNavigateUp = onNavigateUp)
this != null -> { is AppState.Error -> ScreenContentError(onNavigateUp = onNavigateUp)
if (this.packageName.isBlank()) {
ScreenContentLoading(onNavigateUp = onNavigateUp) else -> {
} else {
ScreenContentApp( ScreenContentApp(
app = this, app = app!!,
featuredReviews = featuredReviews, featuredReviews = featuredReviews,
suggestions = suggestions, suggestions = suggestions,
isFavorite = favorite, isFavorite = favorite,
@@ -127,9 +126,9 @@ fun AppDetailsScreen(
exodusReport = exodusReport, exodusReport = exodusReport,
onNavigateUp = onNavigateUp, onNavigateUp = onNavigateUp,
onNavigateToAppDetails = onNavigateToAppDetails, onNavigateToAppDetails = onNavigateToAppDetails,
onDownload = { viewModel.purchase(this) }, onDownload = { viewModel.purchase(app!!) },
onFavorite = { viewModel.toggleFavourite(this) }, onFavorite = { viewModel.toggleFavourite(app!!) },
onCancelDownload = { viewModel.cancelDownload(this) }, onCancelDownload = { viewModel.cancelDownload(app!!) },
onUninstall = { AppInstaller.uninstall(context, packageName) }, onUninstall = { AppInstaller.uninstall(context, packageName) },
onOpen = { onOpen = {
try { try {
@@ -147,11 +146,6 @@ fun AppDetailsScreen(
) )
} }
} }
// TODO: Deal with different kind of errors
else -> ScreenContentError(onNavigateUp = onNavigateUp)
}
}
} }
/** /**

View File

@@ -60,6 +60,8 @@ sealed class AppState {
data object Archived : AppState() data object Archived : AppState()
data object Updatable : AppState() data object Updatable : AppState()
data object Unavailable : AppState() data object Unavailable : AppState()
data object Loading : AppState()
data object Error : AppState()
/** /**
* Whether there is some sort of ongoing process related to the app * Whether there is some sort of ongoing process related to the app

View File

@@ -72,10 +72,10 @@ class AppDetailsViewModel @Inject constructor(
private val TAG = AppDetailsViewModel::class.java.simpleName private val TAG = AppDetailsViewModel::class.java.simpleName
private val _app = MutableStateFlow<App?>(App("")) private val _app = MutableStateFlow<App?>(null)
val app = _app.asStateFlow() val app = _app.asStateFlow()
private val _state = MutableStateFlow(defaultAppState) private val _state = MutableStateFlow<AppState>(AppState.Loading)
val state = _state.asStateFlow() val state = _state.asStateFlow()
private val _suggestions = MutableStateFlow<List<App>>(emptyList()) private val _suggestions = MutableStateFlow<List<App>>(emptyList())
@@ -149,9 +149,11 @@ class AppDetailsViewModel @Inject constructor(
_app.value = appDetailsHelper.getAppByPackageName(packageName).copy( _app.value = appDetailsHelper.getAppByPackageName(packageName).copy(
isInstalled = PackageUtil.isInstalled(context, packageName) isInstalled = PackageUtil.isInstalled(context, packageName)
) )
_state.value = defaultAppState
} catch (exception: Exception) { } catch (exception: Exception) {
Log.e(TAG, "Failed to fetch app details", exception) Log.e(TAG, "Failed to fetch app details", exception)
_app.value = null _app.value = null
_state.value = AppState.Error
} }
}.invokeOnCompletion { throwable -> }.invokeOnCompletion { throwable ->
// Only proceed if there was no error while fetching the app details // Only proceed if there was no error while fetching the app details