Skip to content

Commit

Permalink
[MOD] #307 뷰모델에서 추천코스 관련 전역변수 갱신하는 로직 통일
Browse files Browse the repository at this point in the history
  • Loading branch information
leeeha committed Feb 9, 2024
1 parent 51d91cf commit fe29a6e
Showing 1 changed file with 28 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,19 +65,21 @@ class DiscoverViewModel @Inject constructor(

fun refreshDiscoverCourses() {
getMarathonCourses()

initRecommendCoursePagingData()
initRecommendCourseSortCriteria()
updateRecommendCourseSortCriteria(
criteria = DEFAULT_SORT_CRITERIA
)
getRecommendCourses()
}

private fun initRecommendCoursePagingData() {
isRecommendCoursePageEnd = false
currentPageNumber = FIRST_PAGE_NUM
private fun updateRecommendCoursePagingData(isEnd: Boolean, pageNo: Int) {
isRecommendCoursePageEnd = isEnd
currentPageNumber = pageNo
Timber.d("isEnd: ${isRecommendCoursePageEnd}, page: ${currentPageNumber}")
}

private fun initRecommendCourseSortCriteria() {
currentSortCriteria = DEFAULT_SORT_CRITERIA
private fun updateRecommendCourseSortCriteria(criteria: String) {
currentSortCriteria = criteria
Timber.d("sort: ${currentSortCriteria}")
}

private fun getDiscoverBanners() {
Expand Down Expand Up @@ -122,17 +124,22 @@ class DiscoverViewModel @Inject constructor(

courseRepository.getRecommendCourse(
pageNo = FIRST_PAGE_NUM.toString(),
sort = DEFAULT_SORT_CRITERIA
sort = currentSortCriteria
).onSuccess { pagingData ->
if (pagingData == null) {
_recommendCourseGetState.value =
UiStateV2.Failure("RECOMMEND COURSE DATA IS NULL")
return@onSuccess
}

isRecommendCoursePageEnd = pagingData.isEnd
_recommendCourseGetState.value = UiStateV2.Success(pagingData.recommendCourses)
Timber.d("RECOMMEND COURSE GET SUCCESS")

updateRecommendCoursePagingData(
isEnd = pagingData.isEnd,
pageNo = FIRST_PAGE_NUM
)

}.onFailure { exception ->
_recommendCourseGetState.value = UiStateV2.Failure(exception.message.toString())
Timber.e("RECOMMEND COURSE GET FAIL")
Expand All @@ -147,7 +154,6 @@ class DiscoverViewModel @Inject constructor(
// 다음 페이지가 없으면 요청하지 않는다.
if (isRecommendCoursePageEnd) return@launch

Timber.d("다음 페이지를 요청했어요! 정렬 기준: $currentSortCriteria")
_recommendCourseNextPageState.value = UiStateV2.Loading

courseRepository.getRecommendCourse(
Expand All @@ -161,9 +167,10 @@ class DiscoverViewModel @Inject constructor(
return@onSuccess
}

// 전역변수 업데이트
isRecommendCoursePageEnd = pagingData.isEnd
currentPageNumber++
updateRecommendCoursePagingData(
isEnd = pagingData.isEnd,
pageNo = currentPageNumber + 1
)

_recommendCourseNextPageState.value = UiStateV2.Success(pagingData.recommendCourses)
Timber.d("RECOMMEND COURSE NEXT PAGE GET SUCCESS")
Expand All @@ -177,23 +184,26 @@ class DiscoverViewModel @Inject constructor(
}

fun sortRecommendCourses(criteria: String) {
initRecommendCoursePagingData()
updateCurrentSortCriteria(criteria)
updateRecommendCourseSortCriteria(criteria)

viewModelScope.launch {
_recommendCourseSortState.value = UiStateV2.Loading

courseRepository.getRecommendCourse(
pageNo = FIRST_PAGE_NUM.toString(),
sort = criteria
sort = currentSortCriteria
).onSuccess { pagingData ->
if (pagingData == null) {
_recommendCourseSortState.value =
UiStateV2.Failure("RECOMMEND COURSE DATA IS NULL")
return@onSuccess
}

isRecommendCoursePageEnd = pagingData.isEnd
updateRecommendCoursePagingData(
isEnd = pagingData.isEnd,
pageNo = FIRST_PAGE_NUM
)

_recommendCourseSortState.value = UiStateV2.Success(pagingData.recommendCourses)
Timber.d("RECOMMEND COURSE SORT SUCCESS")
}.onFailure { exception ->
Expand All @@ -203,10 +213,6 @@ class DiscoverViewModel @Inject constructor(
}
}

private fun updateCurrentSortCriteria(criteria: String) {
currentSortCriteria = criteria
}

fun postCourseScrap(id: Int, scrapTF: Boolean) {
viewModelScope.launch {
_courseScrapState.value = UiStateV2.Loading
Expand Down

0 comments on commit fe29a6e

Please sign in to comment.