Skip to content

Commit

Permalink
Merge pull request #129 from uswLectureEvaluation/fix/#127-openmajor-…
Browse files Browse the repository at this point in the history
…lecture-filter

Fix/#127 개설학과 필터링 버그 해결
  • Loading branch information
jinukeu authored Feb 1, 2024
2 parents 1a84a22 + 465e418 commit 8cf04f3
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 59 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.collections.immutable.toPersistentList
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.flow.lastOrNull
import kotlinx.coroutines.sync.Mutex
import kotlinx.coroutines.sync.withLock
import org.orbitmvi.orbit.Container
import org.orbitmvi.orbit.ContainerHost
import org.orbitmvi.orbit.annotation.OrbitExperimental
Expand All @@ -27,6 +29,8 @@ class LectureEvaluationViewModel @Inject constructor(
override val container: Container<LectureEvaluationState, LectureEvaluationSideEffect> =
container(LectureEvaluationState())

private val mutex: Mutex = Mutex()

private val currentState
get() = container.stateFlow.value

Expand Down Expand Up @@ -81,35 +85,37 @@ class LectureEvaluationViewModel @Inject constructor(
majorType: String = currentState.selectedOpenMajor,
needClear: Boolean,
) = intent {
val currentList = if (needClear) {
page = 1
reduce { state.copy(isLoading = true) }
emptyList()
} else {
state.lectureEvaluationList
}

getLectureEvaluationListUseCase(
RetrieveLectureEvaluationAverageListUseCase.Param(
search = search,
option = LectureAlign.entries[alignPosition].query,
page = page,
majorType = majorType,
),
).onSuccess { newList ->
handleGetLectureEvaluationListSuccess(
alignPosition = alignPosition,
majorType = majorType,
currentList = currentList,
newList = newList,
)
}.onFailure {
postSideEffect(LectureEvaluationSideEffect.HandleException(it))
}

if (needClear) {
postSideEffect(LectureEvaluationSideEffect.ScrollToTop)
reduce { state.copy(isLoading = false) }
mutex.withLock {
val currentList = if (needClear) {
page = 1
reduce { state.copy(isLoading = true) }
emptyList()
} else {
state.lectureEvaluationList
}

getLectureEvaluationListUseCase(
RetrieveLectureEvaluationAverageListUseCase.Param(
search = search,
option = LectureAlign.entries[alignPosition].query,
page = page,
majorType = majorType,
),
).onSuccess { newList ->
handleGetLectureEvaluationListSuccess(
alignPosition = alignPosition,
majorType = majorType,
currentList = currentList,
newList = newList,
)
}.onFailure {
postSideEffect(LectureEvaluationSideEffect.HandleException(it))
}

if (needClear) {
postSideEffect(LectureEvaluationSideEffect.ScrollToTop)
reduce { state.copy(isLoading = false) }
}
}
}

Expand Down Expand Up @@ -146,6 +152,7 @@ class LectureEvaluationViewModel @Inject constructor(
postSideEffect(LectureEvaluationSideEffect.NavigateLectureEvaluationDetail(id))
}
}

private fun showOnboardingBottomSheet() = intent { reduce { state.copy(showOnboardingBottomSheet = true) } }
fun hideOnboardingBottomSheet() = intent { reduce { state.copy(showOnboardingBottomSheet = false) } }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import com.suwiki.feature.timetable.navigation.argument.toCellEditorArgument
import com.suwiki.feature.timetable.openlecture.model.SchoolLevel
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.collections.immutable.toPersistentList
import kotlinx.coroutines.sync.Mutex
import kotlinx.coroutines.sync.withLock
import org.orbitmvi.orbit.Container
import org.orbitmvi.orbit.ContainerHost
import org.orbitmvi.orbit.annotation.OrbitExperimental
Expand All @@ -29,6 +31,8 @@ class OpenLectureViewModel @Inject constructor(
private val insertTimetableCellUseCase: InsertTimetableCellUseCase,
) : ViewModel(), ContainerHost<OpenLectureState, OpenLectureSideEffect> {

private val mutex: Mutex = Mutex()

override val container: Container<OpenLectureState, OpenLectureSideEffect> = container(
OpenLectureState(),
)
Expand Down Expand Up @@ -144,39 +148,41 @@ class OpenLectureViewModel @Inject constructor(
majorType: String = currentState.selectedOpenMajor,
needClear: Boolean,
) = intent {
val currentList = when {
needClear -> {
reduce { state.copy(isLoading = true) }
cursorId = 0
isLast = false
emptyList()
}
mutex.withLock {
val currentList = when {
needClear -> {
reduce { state.copy(isLoading = true) }
cursorId = 0
isLast = false
emptyList()
}

isLast -> return@intent
else -> state.openLectureList
}
isLast -> return@intent
else -> state.openLectureList
}

getOpenLectureListUseCase(
GetOpenLectureListUseCase.Param(
cursorId = cursorId,
keyword = search,
major = if (majorType == "전체") null else majorType,
grade = schoolLevel.query,
),
).onSuccess { newData ->
handleGetOpenLectureListSuccess(
schoolLevel = schoolLevel,
majorType = majorType,
currentList = currentList,
newData = newData,
)
}.onFailure {
postSideEffect(OpenLectureSideEffect.HandleException(it))
}
getOpenLectureListUseCase(
GetOpenLectureListUseCase.Param(
cursorId = cursorId,
keyword = search,
major = if (majorType == "전체") null else majorType,
grade = schoolLevel.query,
),
).onSuccess { newData ->
handleGetOpenLectureListSuccess(
schoolLevel = schoolLevel,
majorType = majorType,
currentList = currentList,
newData = newData,
)
}.onFailure {
postSideEffect(OpenLectureSideEffect.HandleException(it))
}

if (needClear) {
postSideEffect(OpenLectureSideEffect.ScrollToTop)
reduce { state.copy(isLoading = false) }
if (needClear) {
postSideEffect(OpenLectureSideEffect.ScrollToTop)
reduce { state.copy(isLoading = false) }
}
}
}

Expand Down

0 comments on commit 8cf04f3

Please sign in to comment.