diff --git a/app/src/main/java/gq/kirmanak/mealient/ui/recipes/RecipesListFragment.kt b/app/src/main/java/gq/kirmanak/mealient/ui/recipes/RecipesListFragment.kt index 9dd5ba0..6188633 100644 --- a/app/src/main/java/gq/kirmanak/mealient/ui/recipes/RecipesListFragment.kt +++ b/app/src/main/java/gq/kirmanak/mealient/ui/recipes/RecipesListFragment.kt @@ -57,9 +57,15 @@ class RecipesListFragment : Fragment(R.layout.fragment_recipes_list) { checkedMenuItemId = R.id.recipes_list ) } - viewModel.showFavoriteIcon.observe(viewLifecycleOwner) { showFavoriteIcon -> + collectWhenViewResumed(viewModel.showFavoriteIcon) { showFavoriteIcon -> setupRecipeAdapter(showFavoriteIcon) } + collectWhenViewResumed(viewModel.deleteRecipeResult) { + logger.d { "Delete recipe result is $it" } + if (it.isFailure) { + showLongToast(R.string.fragment_recipes_delete_recipe_failed) + } + } hideKeyboardOnScroll() } @@ -151,12 +157,7 @@ class RecipesListFragment : Fragment(R.layout.fragment_recipes_list) { R.string.fragment_recipes_delete_recipe_confirm_dialog_message, entity.name ) val onPositiveClick = DialogInterface.OnClickListener { _, _ -> - viewModel.onDeleteConfirm(entity).observe(viewLifecycleOwner) { - logger.d { "onDeleteClick: result is $it" } - if (it.isFailure) { - showLongToast(R.string.fragment_recipes_delete_recipe_failed) - } - } + viewModel.onDeleteConfirm(entity) } val positiveBtnResId = R.string.fragment_recipes_delete_recipe_confirm_dialog_positive_btn val titleResId = R.string.fragment_recipes_delete_recipe_confirm_dialog_title diff --git a/app/src/main/java/gq/kirmanak/mealient/ui/recipes/RecipesListViewModel.kt b/app/src/main/java/gq/kirmanak/mealient/ui/recipes/RecipesListViewModel.kt index a8a3b4a..028a27c 100644 --- a/app/src/main/java/gq/kirmanak/mealient/ui/recipes/RecipesListViewModel.kt +++ b/app/src/main/java/gq/kirmanak/mealient/ui/recipes/RecipesListViewModel.kt @@ -2,9 +2,9 @@ package gq.kirmanak.mealient.ui.recipes import androidx.lifecycle.LiveData import androidx.lifecycle.ViewModel -import androidx.lifecycle.asLiveData import androidx.lifecycle.liveData import androidx.lifecycle.viewModelScope +import androidx.paging.PagingData import androidx.paging.cachedIn import dagger.hilt.android.lifecycle.HiltViewModel import gq.kirmanak.mealient.data.auth.AuthRepo @@ -12,8 +12,16 @@ import gq.kirmanak.mealient.data.recipes.RecipeRepo import gq.kirmanak.mealient.database.recipe.entity.RecipeSummaryEntity import gq.kirmanak.mealient.extensions.valueUpdatesOnly import gq.kirmanak.mealient.logging.Logger +import kotlinx.coroutines.channels.BufferOverflow +import kotlinx.coroutines.flow.Flow +import kotlinx.coroutines.flow.MutableSharedFlow +import kotlinx.coroutines.flow.SharedFlow +import kotlinx.coroutines.flow.SharingStarted +import kotlinx.coroutines.flow.StateFlow import kotlinx.coroutines.flow.launchIn import kotlinx.coroutines.flow.onEach +import kotlinx.coroutines.flow.stateIn +import kotlinx.coroutines.launch import javax.inject.Inject @HiltViewModel @@ -23,8 +31,18 @@ class RecipesListViewModel @Inject constructor( private val logger: Logger, ) : ViewModel() { - val pagingData = recipeRepo.createPager().flow.cachedIn(viewModelScope) - val showFavoriteIcon = authRepo.isAuthorizedFlow.asLiveData() + val pagingData: Flow> = recipeRepo.createPager().flow + .cachedIn(viewModelScope) + + val showFavoriteIcon: StateFlow = authRepo.isAuthorizedFlow + .stateIn(viewModelScope, SharingStarted.Eagerly, false) + + private val _deleteRecipeResult = MutableSharedFlow>( + replay = 0, + extraBufferCapacity = 1, + onBufferOverflow = BufferOverflow.DROP_OLDEST + ) + val deleteRecipeResult: SharedFlow> get() = _deleteRecipeResult init { authRepo.isAuthorizedFlow.valueUpdatesOnly().onEach { hasAuthorized -> @@ -50,8 +68,11 @@ class RecipesListViewModel @Inject constructor( ).also { emit(it) } } - fun onDeleteConfirm(recipeSummaryEntity: RecipeSummaryEntity) = liveData { + fun onDeleteConfirm(recipeSummaryEntity: RecipeSummaryEntity) { logger.v { "onDeleteConfirm() called with: recipeSummaryEntity = $recipeSummaryEntity" } - recipeRepo.deleteRecipe(recipeSummaryEntity.slug).also { emit(it) } + viewModelScope.launch { + val result = recipeRepo.deleteRecipe(recipeSummaryEntity.slug) + _deleteRecipeResult.emit(result) + } } } \ No newline at end of file