Skip to content

Commit

Permalink
feat: 레시피 추천 새로고침 적용
Browse files Browse the repository at this point in the history
  • Loading branch information
SeongHoonC committed Aug 1, 2024
1 parent e8ea667 commit 36f4ce4
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,10 @@ fun RecipeItem(
recipeRecommendItemUiState: RecipeRecommendItemUiState,
onRecipeClick: (Recipe) -> Unit,
onRecipeLikeClick: (Recipe) -> Unit,
modifier: Modifier =Modifier
) {
Column(
modifier = Modifier
modifier = modifier
.fillMaxWidth()
.clickable {
onRecipeClick(recipeRecommendItemUiState.recommendedRecipe.recipe)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,46 +14,44 @@ import javax.inject.Inject

@HiltViewModel
class RecipeRecommendViewModel
@Inject
constructor(
private val recipeRepository: RecipeRepository,
) : ViewModel() {
private val _uiState: MutableStateFlow<RecipeRecommendUiState> =
MutableStateFlow(RecipeRecommendUiState.Loading)
val uiState: StateFlow<RecipeRecommendUiState> = _uiState.asStateFlow()
@Inject
constructor(
private val recipeRepository: RecipeRepository,
) : ViewModel() {
private val _uiState: MutableStateFlow<RecipeRecommendUiState> =
MutableStateFlow(RecipeRecommendUiState.Loading)
val uiState: StateFlow<RecipeRecommendUiState> = _uiState.asStateFlow()

fun getRecipeRecommendation() {
val remainRecipes = (_uiState.value as? RecipeRecommendUiState.Success)?.recipes ?: listOf()
fun getRecipeRecommendation() {
_uiState.value = RecipeRecommendUiState.Loading

viewModelScope.launch {
recipeRepository
.getRecipeRecommendation()
.onSuccess { recipes ->
_uiState.value =
RecipeRecommendUiState.Success(
remainRecipes + recipes.toUiState(),
)
}.onFailure { throwable ->
throwable.printStackTrace()
}
}
}

fun likeRecipe(recipe: Recipe) {
val successUiState = _uiState.value as? RecipeRecommendUiState.Success ?: return
val likedRecipeUiStates =
successUiState.recipes.map { recipeUiState ->
if (recipeUiState.recommendedRecipe.recipe.id == recipe.id) {
recipeUiState.copy(isLiked = !recipeUiState.isLiked)
} else {
recipeUiState
}
viewModelScope.launch {
recipeRepository
.getRecipeRecommendation()
.onSuccess { recipes ->
_uiState.value =
RecipeRecommendUiState.Success(recipes.toUiState())
}.onFailure { throwable ->
throwable.printStackTrace()
}
_uiState.value = RecipeRecommendUiState.Success(likedRecipeUiStates)
}
}

private fun List<RecommendedRecipe>.toUiState(): List<RecipeRecommendItemUiState> =
map { recipe ->
RecipeRecommendItemUiState(recommendedRecipe = recipe, isLiked = false)
fun likeRecipe(recipe: Recipe) {
val successUiState = _uiState.value as? RecipeRecommendUiState.Success ?: return
val likedRecipeUiStates =
successUiState.recipes.map { recipeUiState ->
if (recipeUiState.recommendedRecipe.recipe.id == recipe.id) {
recipeUiState.copy(isLiked = !recipeUiState.isLiked)
} else {
recipeUiState
}
}
_uiState.value = RecipeRecommendUiState.Success(likedRecipeUiStates)
}

private fun List<RecommendedRecipe>.toUiState(): List<RecipeRecommendItemUiState> =
map { recipe ->
RecipeRecommendItemUiState(recommendedRecipe = recipe, isLiked = false)
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.sundaegukbap.banchango.feature.recipe.recommend

import androidx.compose.foundation.Image
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.Spacer
Expand Down Expand Up @@ -49,6 +50,7 @@ fun RecipeRecommendRoute(
RecipeRecommendContent(
padding = padding,
uiState = uiState,
onRefreshClick = { viewModel.getRecipeRecommendation() },
onLikeClick = { viewModel.likeRecipe(it) },
onRecipeClick = onRecipeClick,
onChangeSystemBarsColor = onChangeStatusBarColor,
Expand All @@ -59,6 +61,7 @@ fun RecipeRecommendRoute(
fun RecipeRecommendContent(
padding: PaddingValues,
uiState: RecipeRecommendUiState,
onRefreshClick: () -> Unit,
onLikeClick: (Recipe) -> Unit,
onRecipeClick: (Recipe) -> Unit,
onChangeSystemBarsColor: (color: Color, darkIcons: Boolean) -> Unit,
Expand All @@ -67,8 +70,9 @@ fun RecipeRecommendContent(
when (uiState) {
is RecipeRecommendUiState.Loading -> {
RecipeRecommendLoading(
modifier = Modifier.fillMaxSize(),
padding = padding,
contentAlignment = Alignment.Center,
onRefreshClick = {},
)
}

Expand All @@ -78,25 +82,38 @@ fun RecipeRecommendContent(
padding = padding,
onLikeClick = onLikeClick,
onRecipeClick = onRecipeClick,
onRefreshClick = onRefreshClick,
)
}
}
}

@Composable
fun RecipeRecommendLoading(
modifier: Modifier,
padding: PaddingValues,
contentAlignment: Alignment,
onRefreshClick: () -> Unit,
) {
Box(modifier = modifier, contentAlignment = contentAlignment) {
CircularProgressIndicator()
Box(
modifier = Modifier
.padding(padding)
.fillMaxSize()
.padding(horizontal = 16.dp),
contentAlignment = contentAlignment
) {
RecipeRecommendTopBar(onRefreshClick, Modifier.align(Alignment.TopCenter))
CircularProgressIndicator(
modifier = Modifier
.align(Alignment.Center)
)
}
}

@Composable
private fun RecipeRecommendScreen(
padding: PaddingValues,
recipeRecommends: List<RecipeRecommendItemUiState>,
onRefreshClick: () -> Unit,
onLikeClick: (Recipe) -> Unit,
onRecipeClick: (Recipe) -> Unit,
) {
Expand All @@ -107,26 +124,36 @@ private fun RecipeRecommendScreen(
modifier = Modifier.padding(horizontal = 16.dp)
) {
item {
Box(
modifier = Modifier
.padding(vertical = 20.dp)
.fillMaxWidth()
) {
Text(text = "AI 레시피 추천", fontWeight = FontWeight.Bold)
Image(
painterResource(id = R.drawable.ic_refresh),
contentDescription = "refresh",
modifier = Modifier.align(Alignment.CenterEnd)
)
}
RecipeRecommendTopBar(onRefreshClick = onRefreshClick)
}
items(recipeRecommends) { item ->
items(
items = recipeRecommends,
key = { it.recommendedRecipe.recipe.id }
) { item ->
RecipeItem(item, onRecipeClick, onLikeClick)
Spacer(modifier = Modifier.height(20.dp))
}
}
}

@Composable
private fun RecipeRecommendTopBar(onRefreshClick: () -> Unit, modifier: Modifier = Modifier) {
Box(
modifier = modifier
.padding(vertical = 20.dp)
.fillMaxWidth()
) {
Text(text = "AI 레시피 추천", fontWeight = FontWeight.Bold)
Image(
painterResource(id = R.drawable.ic_refresh),
contentDescription = "refresh",
modifier = Modifier
.align(Alignment.CenterEnd)
.clickable { onRefreshClick() }
)
}
}

@Preview(showBackground = true)
@Composable
fun RecipePagePreview() {
Expand All @@ -135,6 +162,7 @@ fun RecipePagePreview() {
padding = PaddingValues(16.dp),
onLikeClick = {},
onRecipeClick = {},
onRefreshClick = {},
onChangeSystemBarsColor = { _, _ -> },
uiState =
RecipeRecommendUiState.Success(
Expand Down

0 comments on commit 36f4ce4

Please sign in to comment.