Skip to content

Commit

Permalink
[feature/withdraw] 식당리스트 화면 리뷰 분기처리 (#96)
Browse files Browse the repository at this point in the history
  • Loading branch information
SsongSik committed Feb 8, 2024
1 parent d902932 commit 8d08a69
Show file tree
Hide file tree
Showing 10 changed files with 77 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ import kotlinx.coroutines.flow.Flow
interface ReviewDataSource {

suspend fun getPagingStoreReviews(
offset: Int,
limit: Int,
order: String?,
group: String?,
grade: Int?,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ class ReviewDataSourceImpl @Inject constructor(
private val storeReviewApi: StoreReviewApi,
) : ReviewDataSource {
override suspend fun getPagingStoreReviews(
offset: Int,
limit: Int,
order: String?,
group: String?,
grade: Int?,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,12 @@ class DefaultReviewRepository @Inject constructor(
private val reviewDataSource: ReviewDataSource,
) : ReviewRepository {
override suspend fun getPagingStoreReviews(
offset: Int,
limit: Int,
order: String?,
group: String?,
grade: Int?,
campusIdx: Int
): Flow<PagingData<StoreReviewEntity>> {
return reviewDataSource.getPagingStoreReviews(
offset,
limit,
order,
group,
grade,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ import kotlinx.coroutines.flow.Flow

interface ReviewRepository {
suspend fun getPagingStoreReviews(
offset: Int,
limit: Int,
order: String?,
group: String?,
grade: Int?,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,11 @@ class GetStoreReviewUseCase @Inject constructor(
private val reviewRepository: ReviewRepository
) {
suspend operator fun invoke(
offset: Int,
limit: Int,
order: String?,
group: String?,
grade: Int?,
campusIdx: Int
) : Flow<PagingData<StoreReviewEntity>> {
return reviewRepository.getPagingStoreReviews(offset, limit, order, group, grade, campusIdx)
return reviewRepository.getPagingStoreReviews(order, group, grade, campusIdx)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@ class DetailContract {
val reportCategoryType: ReportCategoryType = ReportCategoryType.NONE,
val restaurantCategoryType: RestaurantCategoryType = RestaurantCategoryType.NONE,
val rating: Int = 0,
val isReviewScreen: Boolean = false
) : ViewState

sealed class DetailEvent : ViewEvent {
object InitDetailScreen : DetailEvent()
data class InitDetailScreen(val isReviewScreen: Boolean = false) : DetailEvent()
data class SortBottomSheetStateChange(val sortBottomSheetState: Boolean) : DetailEvent()
data class MealRatingBottomSheetStateChange(val mealRatingBottomSheetState: Boolean) : DetailEvent()
data class ReportBottomSheetStateChange(val reportBottomSheetState: Boolean) : DetailEvent()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,13 @@ import androidx.hilt.navigation.compose.hiltViewModel
import androidx.paging.compose.LazyPagingItems
import androidx.paging.compose.collectAsLazyPagingItems
import com.everymeal.domain.model.restaurant.RestaurantDataEntity
import com.everymeal.domain.model.review.StoreReviewEntity
import com.everymeal.presentation.R
import com.everymeal.presentation.components.EveryMealCategoryRatingBottomSheetDialog
import com.everymeal.presentation.components.EveryMealDetailReportBottomSheetDialog
import com.everymeal.presentation.components.EveryMealReportBottomSheetDialog
import com.everymeal.presentation.components.EveryMealRestaurantItem
import com.everymeal.presentation.components.EveryMealReviewItem
import com.everymeal.presentation.components.EveryMealSortCategoryBottomSheetDialog
import com.everymeal.presentation.ui.save.SaveTopBar
import com.everymeal.presentation.ui.theme.Grey2
Expand All @@ -55,8 +57,11 @@ fun DetailListScreen(
val pagingRestaurantList: LazyPagingItems<RestaurantDataEntity> =
detailListViewModel.restaurantItems.collectAsLazyPagingItems()

val pagingReviewList: LazyPagingItems<StoreReviewEntity> =
detailListViewModel.restaurantReviews.collectAsLazyPagingItems()

LaunchedEffect(Unit) {
detailListViewModel.setEvent(DetailContract.DetailEvent.InitDetailScreen)
detailListViewModel.setEvent(DetailContract.DetailEvent.InitDetailScreen(title == "리뷰"))
}

LaunchedEffect(key1 = detailListViewModel.effect) {
Expand Down Expand Up @@ -258,6 +263,19 @@ fun DetailListScreen(
Spacer(modifier = Modifier.padding(16.dp))
}
}

items(pagingReviewList.itemCount) { index ->
val item = pagingReviewList[index]
item?.let {
EveryMealReviewItem(
review = it,
onDetailRestaurantClick = { restaurantId ->
detailListViewModel.setEvent(DetailContract.DetailEvent.OnRestaurantDetailClick(restaurantId))
}
)
Spacer(modifier = Modifier.padding(16.dp))
}
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ import androidx.lifecycle.viewModelScope
import androidx.paging.PagingData
import androidx.paging.cachedIn
import com.everymeal.domain.model.restaurant.RestaurantDataEntity
import com.everymeal.domain.model.review.StoreReviewEntity
import com.everymeal.domain.usecase.local.GetUniversityIndexUseCase
import com.everymeal.domain.usecase.restaurant.GetUnivRestaurantUseCase
import com.everymeal.domain.usecase.review.GetStoreReviewUseCase
import com.everymeal.presentation.base.BaseViewModel
import com.everymeal.presentation.ui.detail.DetailContract.DetailEvent
import com.everymeal.presentation.ui.detail.DetailContract.DetailState
Expand All @@ -21,23 +23,38 @@ import javax.inject.Inject
@HiltViewModel
class DetailListViewModel @Inject constructor(
private val getUnivRestaurantUseCase: GetUnivRestaurantUseCase,
private val getUniversityIndexUseCase: GetUniversityIndexUseCase
private val getUniversityIndexUseCase: GetUniversityIndexUseCase,
private val getStoreReviewUseCase: GetStoreReviewUseCase
): BaseViewModel<DetailState, DetailEffect, DetailEvent>(
DetailState()
) {
private val _restaurantItems : MutableStateFlow<PagingData<RestaurantDataEntity>> = MutableStateFlow(value = PagingData.empty())
val restaurantItems : StateFlow<PagingData<RestaurantDataEntity>> = _restaurantItems.asStateFlow()

private val _restaurantReviews : MutableStateFlow<PagingData<StoreReviewEntity>> = MutableStateFlow(value = PagingData.empty())
val restaurantReviews : StateFlow<PagingData<StoreReviewEntity>> = _restaurantReviews.asStateFlow()

override fun handleEvents(event: DetailEvent) {
when (event) {
is DetailEvent.InitDetailScreen -> {
getRestaurantList()
reflectUpdateState(
isReviewScreen = event.isReviewScreen
)
if(event.isReviewScreen) {
getReviewList()
} else {
getRestaurantList()
}
}
is DetailEvent.OnClickDetailListCategoryType -> {
reflectUpdateState(
detailSortCategoryType = event.detailSortCategoryType
)
getRestaurantList()
if(viewState.value.isReviewScreen) {
getReviewList()
} else {
getRestaurantList()
}
}
is DetailEvent.SortBottomSheetStateChange -> {
reflectUpdateState(
Expand All @@ -48,7 +65,11 @@ class DetailListViewModel @Inject constructor(
reflectUpdateState(
mealRatingBottomSheetState = event.mealRatingBottomSheetState
)
getRestaurantList()
if(viewState.value.isReviewScreen) {
getReviewList()
} else {
getRestaurantList()
}
}
is DetailEvent.ReportBottomSheetStateChange -> {
reflectUpdateState(
Expand Down Expand Up @@ -79,20 +100,42 @@ class DetailListViewModel @Inject constructor(
reflectUpdateState(
restaurantCategoryType = RestaurantCategoryType.NONE
)
getRestaurantList()
if(viewState.value.isReviewScreen) {
getReviewList()
} else {
getRestaurantList()
}
}
is DetailEvent.OnDeleteClickRating -> {
reflectUpdateState(
rating = 0
)
getRestaurantList()
if(viewState.value.isReviewScreen) {
getReviewList()
} else {
getRestaurantList()
}
}
is DetailEvent.OnRestaurantDetailClick -> {
sendEffect({ DetailEffect.OnRestaurantClickEffect(event.restaurantId) })
}
}
}

private fun getReviewList() {
viewModelScope.launch {
getStoreReviewUseCase(
order = viewState.value.detailSortCategoryType.sort(),
group = viewState.value.restaurantCategoryType.sort(),
grade = if(viewState.value.rating == 0) null else viewState.value.rating,
campusIdx = getUniversityIndexUseCase().first().toInt()
).cachedIn(viewModelScope)
.collect {
_restaurantReviews.emit(it)
}
}
}

private fun getRestaurantList() {
viewModelScope.launch {
getUnivRestaurantUseCase(
Expand All @@ -116,6 +159,7 @@ class DetailListViewModel @Inject constructor(
reportCategoryType: ReportCategoryType = viewState.value.reportCategoryType,
rating: Int = viewState.value.rating,
restaurantCategoryType: RestaurantCategoryType = viewState.value.restaurantCategoryType,
isReviewScreen: Boolean = viewState.value.isReviewScreen
) {
updateState {
copy(
Expand All @@ -127,6 +171,7 @@ class DetailListViewModel @Inject constructor(
reportCategoryType = reportCategoryType,
rating = rating,
restaurantCategoryType = restaurantCategoryType,
isReviewScreen = isReviewScreen
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ enum class DetailListScreenType {
RESTAURANT,
CAFE,
DRINK,
REVIEW
}

fun String.DetailListScreenType(): DetailListScreenType {
Expand All @@ -42,6 +43,7 @@ fun String.DetailListScreenType(): DetailListScreenType {
"밥집" -> DetailListScreenType.RESTAURANT
"카페" -> DetailListScreenType.CAFE
"술집" -> DetailListScreenType.DRINK
"리뷰" -> DetailListScreenType.REVIEW
else -> DetailListScreenType.RECOMMEND
}
}
Expand All @@ -52,5 +54,6 @@ fun DetailListScreenType.title(): String {
DetailListScreenType.RESTAURANT -> "밥집"
DetailListScreenType.CAFE -> "카페"
DetailListScreenType.DRINK -> "술집"
DetailListScreenType.REVIEW -> "리뷰"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ fun HomeScreen(
EveryMealLineButton(
text = stringResource(R.string.home_restaurant_review_button_text),
onClick = {

homeViewModel.setEvent(HomeContract.HomeEvent.OnClickDetailList("리뷰".DetailListScreenType()))
},
)
}
Expand Down

0 comments on commit 8d08a69

Please sign in to comment.