Add a toggle for access

This commit is contained in:
Joren 2024-04-30 11:59:20 +02:00
parent 8c69116171
commit 8690ed006e
Signed by untrusted user who does not match committer: Joren
GPG Key ID: 280E33DFBC0F1B55
2 changed files with 40 additions and 3 deletions

View File

@ -16,6 +16,8 @@ import com.ti.mobpo.ui.viewmodels.FavouritesViewModel
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.grid.items
import androidx.compose.material3.AlertDialog
import androidx.compose.material3.Button
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.ui.Modifier
@ -41,6 +43,31 @@ fun Favorites(favoritesViewModel: FavouritesViewModel) {
) {
Spacer(modifier = Modifier.height(16.dp))
// Show AlertDialog if access check failed
if (favoritesViewModel.accessCheckFailed.value) {
AlertDialog(
onDismissRequest = {
// Dismiss the dialog
favoritesViewModel.accessCheckFailed.value = false
},
title = {
Text("Access Denied")
},
text = {
Text("You do not have access to this feature.")
},
confirmButton = {
Button(
onClick = {
favoritesViewModel.accessCheckFailed.value = false
}
) {
Text("OK")
}
}
)
}
favorites?.let { favoritesList ->
if (favoritesList.isNotEmpty()) {
LazyVerticalGrid(

View File

@ -1,5 +1,7 @@
package com.ti.mobpo.ui.viewmodels
import androidx.compose.runtime.MutableState
import androidx.compose.runtime.mutableStateOf
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.ti.mobpo.data.Favourite
@ -17,15 +19,23 @@ import kotlinx.coroutines.flow.stateIn
import kotlinx.coroutines.launch
import java.io.IOException
class FavouritesViewModel(private val favouritesRepository: FavouritesRepository, featureManager: FeatureManager) : ViewModel() {
private val service = PokeApi.retrofitService
class FavouritesViewModel(private val favouritesRepository: FavouritesRepository,
private val featureManager: FeatureManager
) : ViewModel() {
private val service = PokeApi.retrofitService
private val _pokemonDetails = MutableStateFlow<List<PokemonDetails>?>(null)
val pokemonDetails: StateFlow<List<PokemonDetails>?> = _pokemonDetails.asStateFlow()
val accessCheckFailed: MutableState<Boolean> = mutableStateOf(false)
fun loadFavourites() {
// featureManager.setPaidFeatureEnabled(false) enable and disable acccess
// Ugly workaround to make sure all the favourites are loaded before displaying them
viewModelScope.launch {
if(!featureManager.hasAccessToPaidFeature()) {
accessCheckFailed.value = true
return@launch
}
try {
val favouritesList: StateFlow<FavouriteUiState> =
favouritesRepository.getAllItems().map { FavouriteUiState(it) }