Replace LiveData with Flow for delete result
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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<PagingData<RecipeSummaryEntity>> = recipeRepo.createPager().flow
|
||||
.cachedIn(viewModelScope)
|
||||
|
||||
val showFavoriteIcon: StateFlow<Boolean> = authRepo.isAuthorizedFlow
|
||||
.stateIn(viewModelScope, SharingStarted.Eagerly, false)
|
||||
|
||||
private val _deleteRecipeResult = MutableSharedFlow<Result<Unit>>(
|
||||
replay = 0,
|
||||
extraBufferCapacity = 1,
|
||||
onBufferOverflow = BufferOverflow.DROP_OLDEST
|
||||
)
|
||||
val deleteRecipeResult: SharedFlow<Result<Unit>> 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)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user