Read navigation argumens in ViewModel
This commit is contained in:
@@ -7,7 +7,6 @@ import android.view.View
|
|||||||
import android.view.ViewGroup
|
import android.view.ViewGroup
|
||||||
import androidx.core.view.isVisible
|
import androidx.core.view.isVisible
|
||||||
import androidx.fragment.app.viewModels
|
import androidx.fragment.app.viewModels
|
||||||
import androidx.navigation.fragment.navArgs
|
|
||||||
import by.kirich1409.viewbindingdelegate.viewBinding
|
import by.kirich1409.viewbindingdelegate.viewBinding
|
||||||
import com.google.android.material.bottomsheet.BottomSheetDialog
|
import com.google.android.material.bottomsheet.BottomSheetDialog
|
||||||
import com.google.android.material.bottomsheet.BottomSheetDialogFragment
|
import com.google.android.material.bottomsheet.BottomSheetDialogFragment
|
||||||
@@ -22,7 +21,6 @@ import javax.inject.Inject
|
|||||||
class RecipeInfoFragment : BottomSheetDialogFragment() {
|
class RecipeInfoFragment : BottomSheetDialogFragment() {
|
||||||
|
|
||||||
private val binding by viewBinding(FragmentRecipeInfoBinding::bind)
|
private val binding by viewBinding(FragmentRecipeInfoBinding::bind)
|
||||||
private val arguments by navArgs<RecipeInfoFragmentArgs>()
|
|
||||||
private val viewModel by viewModels<RecipeInfoViewModel>()
|
private val viewModel by viewModels<RecipeInfoViewModel>()
|
||||||
private val ingredientsAdapter by lazy { recipeIngredientsAdapterFactory.build() }
|
private val ingredientsAdapter by lazy { recipeIngredientsAdapterFactory.build() }
|
||||||
private val instructionsAdapter by lazy { recipeInstructionsAdapterFactory.build() }
|
private val instructionsAdapter by lazy { recipeInstructionsAdapterFactory.build() }
|
||||||
@@ -58,7 +56,6 @@ class RecipeInfoFragment : BottomSheetDialogFragment() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
with(viewModel) {
|
with(viewModel) {
|
||||||
loadRecipeInfo(arguments.recipeId, arguments.recipeSlug)
|
|
||||||
uiState.observe(viewLifecycleOwner, ::onUiStateChange)
|
uiState.observe(viewLifecycleOwner, ::onUiStateChange)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,39 +1,40 @@
|
|||||||
package gq.kirmanak.mealient.ui.recipes.info
|
package gq.kirmanak.mealient.ui.recipes.info
|
||||||
|
|
||||||
import androidx.lifecycle.LiveData
|
import androidx.lifecycle.LiveData
|
||||||
import androidx.lifecycle.MutableLiveData
|
import androidx.lifecycle.SavedStateHandle
|
||||||
import androidx.lifecycle.ViewModel
|
import androidx.lifecycle.ViewModel
|
||||||
import androidx.lifecycle.viewModelScope
|
import androidx.lifecycle.liveData
|
||||||
import dagger.hilt.android.lifecycle.HiltViewModel
|
import dagger.hilt.android.lifecycle.HiltViewModel
|
||||||
import gq.kirmanak.mealient.data.recipes.RecipeRepo
|
import gq.kirmanak.mealient.data.recipes.RecipeRepo
|
||||||
import gq.kirmanak.mealient.datasource.runCatchingExceptCancel
|
import gq.kirmanak.mealient.datasource.runCatchingExceptCancel
|
||||||
import gq.kirmanak.mealient.logging.Logger
|
import gq.kirmanak.mealient.logging.Logger
|
||||||
import kotlinx.coroutines.launch
|
|
||||||
import javax.inject.Inject
|
import javax.inject.Inject
|
||||||
|
|
||||||
@HiltViewModel
|
@HiltViewModel
|
||||||
class RecipeInfoViewModel @Inject constructor(
|
class RecipeInfoViewModel @Inject constructor(
|
||||||
private val recipeRepo: RecipeRepo,
|
private val recipeRepo: RecipeRepo,
|
||||||
private val logger: Logger,
|
private val logger: Logger,
|
||||||
|
savedStateHandle: SavedStateHandle,
|
||||||
) : ViewModel() {
|
) : ViewModel() {
|
||||||
|
|
||||||
private val _uiState = MutableLiveData(RecipeInfoUiState())
|
private val args = RecipeInfoFragmentArgs.fromSavedStateHandle(savedStateHandle)
|
||||||
val uiState: LiveData<RecipeInfoUiState> get() = _uiState
|
|
||||||
|
|
||||||
fun loadRecipeInfo(recipeId: String, recipeSlug: String) {
|
val uiState: LiveData<RecipeInfoUiState> = liveData {
|
||||||
logger.v { "loadRecipeInfo() called with: recipeId = $recipeId, recipeSlug = $recipeSlug" }
|
logger.v { "Initializing UI state with args = $args" }
|
||||||
_uiState.value = RecipeInfoUiState()
|
emit(RecipeInfoUiState())
|
||||||
viewModelScope.launch {
|
runCatchingExceptCancel {
|
||||||
runCatchingExceptCancel { recipeRepo.loadRecipeInfo(recipeId, recipeSlug) }
|
recipeRepo.loadRecipeInfo(args.recipeId, args.recipeSlug)
|
||||||
.onSuccess {
|
}.onSuccess {
|
||||||
logger.d { "loadRecipeInfo: received recipe info = $it" }
|
logger.d { "loadRecipeInfo: received recipe info = $it" }
|
||||||
_uiState.value = RecipeInfoUiState(
|
val newState = RecipeInfoUiState(
|
||||||
areIngredientsVisible = it.recipeIngredients.isNotEmpty(),
|
areIngredientsVisible = it.recipeIngredients.isNotEmpty(),
|
||||||
areInstructionsVisible = it.recipeInstructions.isNotEmpty(),
|
areInstructionsVisible = it.recipeInstructions.isNotEmpty(),
|
||||||
recipeInfo = it,
|
recipeInfo = it,
|
||||||
)
|
)
|
||||||
}
|
emit(newState)
|
||||||
.onFailure { logger.e(it) { "loadRecipeInfo: can't load recipe info" } }
|
}.onFailure {
|
||||||
|
logger.e(it) { "loadRecipeInfo: can't load recipe info" }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user