Add a toggle for access
This commit is contained in:
parent
8c69116171
commit
8690ed006e
@ -16,6 +16,8 @@ import com.ti.mobpo.ui.viewmodels.FavouritesViewModel
|
|||||||
import androidx.compose.foundation.layout.height
|
import androidx.compose.foundation.layout.height
|
||||||
import androidx.compose.foundation.layout.padding
|
import androidx.compose.foundation.layout.padding
|
||||||
import androidx.compose.foundation.lazy.grid.items
|
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.runtime.LaunchedEffect
|
||||||
import androidx.compose.ui.Modifier
|
import androidx.compose.ui.Modifier
|
||||||
|
|
||||||
@ -41,6 +43,31 @@ fun Favorites(favoritesViewModel: FavouritesViewModel) {
|
|||||||
) {
|
) {
|
||||||
Spacer(modifier = Modifier.height(16.dp))
|
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 ->
|
favorites?.let { favoritesList ->
|
||||||
if (favoritesList.isNotEmpty()) {
|
if (favoritesList.isNotEmpty()) {
|
||||||
LazyVerticalGrid(
|
LazyVerticalGrid(
|
||||||
|
@ -1,5 +1,7 @@
|
|||||||
package com.ti.mobpo.ui.viewmodels
|
package com.ti.mobpo.ui.viewmodels
|
||||||
|
|
||||||
|
import androidx.compose.runtime.MutableState
|
||||||
|
import androidx.compose.runtime.mutableStateOf
|
||||||
import androidx.lifecycle.ViewModel
|
import androidx.lifecycle.ViewModel
|
||||||
import androidx.lifecycle.viewModelScope
|
import androidx.lifecycle.viewModelScope
|
||||||
import com.ti.mobpo.data.Favourite
|
import com.ti.mobpo.data.Favourite
|
||||||
@ -17,15 +19,23 @@ import kotlinx.coroutines.flow.stateIn
|
|||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
import java.io.IOException
|
import java.io.IOException
|
||||||
|
|
||||||
class FavouritesViewModel(private val favouritesRepository: FavouritesRepository, featureManager: FeatureManager) : ViewModel() {
|
class FavouritesViewModel(private val favouritesRepository: FavouritesRepository,
|
||||||
private val service = PokeApi.retrofitService
|
private val featureManager: FeatureManager
|
||||||
|
) : ViewModel() {
|
||||||
|
|
||||||
|
private val service = PokeApi.retrofitService
|
||||||
private val _pokemonDetails = MutableStateFlow<List<PokemonDetails>?>(null)
|
private val _pokemonDetails = MutableStateFlow<List<PokemonDetails>?>(null)
|
||||||
val pokemonDetails: StateFlow<List<PokemonDetails>?> = _pokemonDetails.asStateFlow()
|
val pokemonDetails: StateFlow<List<PokemonDetails>?> = _pokemonDetails.asStateFlow()
|
||||||
|
val accessCheckFailed: MutableState<Boolean> = mutableStateOf(false)
|
||||||
|
|
||||||
fun loadFavourites() {
|
fun loadFavourites() {
|
||||||
|
// featureManager.setPaidFeatureEnabled(false) enable and disable acccess
|
||||||
// Ugly workaround to make sure all the favourites are loaded before displaying them
|
// Ugly workaround to make sure all the favourites are loaded before displaying them
|
||||||
viewModelScope.launch {
|
viewModelScope.launch {
|
||||||
|
if(!featureManager.hasAccessToPaidFeature()) {
|
||||||
|
accessCheckFailed.value = true
|
||||||
|
return@launch
|
||||||
|
}
|
||||||
try {
|
try {
|
||||||
val favouritesList: StateFlow<FavouriteUiState> =
|
val favouritesList: StateFlow<FavouriteUiState> =
|
||||||
favouritesRepository.getAllItems().map { FavouriteUiState(it) }
|
favouritesRepository.getAllItems().map { FavouriteUiState(it) }
|
||||||
|
Loading…
x
Reference in New Issue
Block a user