Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/#127 개설학과 필터링 버그 해결 #129

Merged
merged 3 commits into from
Feb 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading