Skip to content

Commit

Permalink
feat(reciperecommend): 무한스크롤 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
SeongHoonC committed Jun 4, 2024
1 parent 251251d commit 989a87d
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package com.sundaegukbap.banchango.feature.reciperecommend

import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.sundaegukbap.banchango.Recipe
import com.sundaegukbap.banchango.core.data.repository.api.RecipeRepository
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.MutableStateFlow
Expand All @@ -20,14 +19,33 @@ class RecipeRecommendViewModel @Inject constructor(
MutableStateFlow(RecipeRecommendUiState.Loading)
val uiState: StateFlow<RecipeRecommendUiState> = _uiState.asStateFlow()

init {
viewModelScope.launch {
recipeRepository.getRecipeRecommendation().onSuccess {
_uiState.value =
RecipeRecommendUiState
.Success((it + it + it + it + it)
.mapIndexed { index, recipe ->
RecipeRecommendItemUiState(
recipe = recipe.copy(name = recipe.name + index.toString()),
isHated = false,
isLiked = false,
)
})
}.onFailure { throwable ->
throwable.printStackTrace()
}
}
}

fun getRecipeRecommendation() {
if (_uiState.value is RecipeRecommendUiState.Success) return
val successUiState = _uiState.value as? RecipeRecommendUiState.Success ?: return

viewModelScope.launch {
recipeRepository.getRecipeRecommendation().onSuccess {
_uiState.value =
RecipeRecommendUiState
.Success((it + it + it + it + it + it + it)
RecipeRecommendUiState.Success(
successUiState.recipes + (it + it + it + it + it)
.mapIndexed { index, recipe ->
RecipeRecommendItemUiState(
recipe = recipe.copy(name = recipe.name + index.toString()),
Expand All @@ -44,6 +62,7 @@ class RecipeRecommendViewModel @Inject constructor(
fun hateRecipe(page: Int) {
val successRecipes = _uiState.value as? RecipeRecommendUiState.Success ?: return
_uiState.value = RecipeRecommendUiState.Success(
// TODO : Hate
successRecipes.recipes.toMutableList().apply { removeAt(page) }
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ fun RecipeRecommendRoute(
RecipeRecommendContent(
padding = padding,
uiState = uiState,
onHateClick = { viewModel.hateRecipe(it) },
onHateClick = viewModel::hateRecipe,
onLastPageVisible = viewModel::getRecipeRecommendation,
)
}

Expand All @@ -51,6 +52,7 @@ fun RecipeRecommendContent(
uiState: RecipeRecommendUiState,
onHateClick: (Int) -> Unit = {},
onLikeClick: (Int) -> Unit = {},
onLastPageVisible: () -> Unit = {},
) {

when (uiState) {
Expand All @@ -64,6 +66,7 @@ fun RecipeRecommendContent(
padding = padding,
onHateClick = onHateClick,
onLikeClick = onLikeClick,
onLastPageVisible = onLastPageVisible,
)
}
}
Expand All @@ -83,6 +86,7 @@ private fun RecipeRecommendScreen(
recipeRecommends: List<RecipeRecommendItemUiState>,
onHateClick: (Int) -> Unit,
onLikeClick: (Int) -> Unit,
onLastPageVisible: () -> Unit = {},
) {
val pagerState = rememberPagerState(pageCount = { recipeRecommends.size })
val coroutineScope = rememberCoroutineScope()
Expand All @@ -96,14 +100,18 @@ private fun RecipeRecommendScreen(

var visible by remember { mutableStateOf(true) }

if (page >= recipeRecommends.size - 2) {
onLastPageVisible()
}

RecipePage(
visible = visible,
page = page,
recipeRecommends = recipeRecommends,
onLikeClick = {
coroutineScope.launch {
onLikeClick(it)
pagerState.animateScrollToPage(it)
pagerState.animateScrollToPage(it + 1)
}
},
onHateClick = { visible = false },
Expand Down

0 comments on commit 989a87d

Please sign in to comment.