Skip to content

Commit

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

Fix/#131 개설강좌 필터링 문제 해결
  • Loading branch information
jinukeu authored Feb 9, 2024
2 parents ac7cbcc + 111469c commit a23aa5f
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import kotlinx.coroutines.sync.withLock
import org.orbitmvi.orbit.Container
import org.orbitmvi.orbit.ContainerHost
import org.orbitmvi.orbit.annotation.OrbitExperimental
import org.orbitmvi.orbit.syntax.simple.SimpleSyntax
import org.orbitmvi.orbit.syntax.simple.blockingIntent
import org.orbitmvi.orbit.syntax.simple.intent
import org.orbitmvi.orbit.syntax.simple.postSideEffect
Expand Down Expand Up @@ -51,15 +52,26 @@ class LectureEvaluationViewModel @Inject constructor(

fun updateSelectedOpenMajor(openMajor: String) = intent {
if (openMajor == state.selectedOpenMajor) return@intent

reduce {
state.copy(
selectedOpenMajor = openMajor,
)
}

getLectureEvaluationList(
majorType = openMajor,
needClear = true,
)
}

fun updateAlignItem(position: Int) {
fun updateAlignItem(position: Int) = intent {
reduce {
state.copy(
selectedAlignPosition = position,
)
}

getLectureEvaluationList(
alignPosition = position,
needClear = true,
)
}
Expand All @@ -81,8 +93,6 @@ class LectureEvaluationViewModel @Inject constructor(

fun getLectureEvaluationList(
search: String = searchQuery,
alignPosition: Int = currentState.selectedAlignPosition,
majorType: String = currentState.selectedOpenMajor,
needClear: Boolean,
) = intent {
mutex.withLock {
Expand All @@ -97,14 +107,12 @@ class LectureEvaluationViewModel @Inject constructor(
getLectureEvaluationListUseCase(
RetrieveLectureEvaluationAverageListUseCase.Param(
search = search,
option = LectureAlign.entries[alignPosition].query,
option = LectureAlign.entries[currentState.selectedAlignPosition].query,
page = page,
majorType = majorType,
majorType = currentState.selectedOpenMajor,
),
).onSuccess { newList ->
handleGetLectureEvaluationListSuccess(
alignPosition = alignPosition,
majorType = majorType,
currentList = currentList,
newList = newList,
)
Expand All @@ -119,23 +127,17 @@ class LectureEvaluationViewModel @Inject constructor(
}
}

private fun handleGetLectureEvaluationListSuccess(
alignPosition: Int,
majorType: String,
private suspend fun SimpleSyntax<LectureEvaluationState, LectureEvaluationSideEffect>.handleGetLectureEvaluationListSuccess(
currentList: List<LectureEvaluationAverage?>,
newList: List<LectureEvaluationAverage?>,
) = intent {
reduce {
page++
state.copy(
selectedAlignPosition = alignPosition,
selectedOpenMajor = majorType,
lectureEvaluationList = currentList
.plus(newList)
.distinctBy { it?.id }
.toPersistentList(),
)
}
) = reduce {
page++
state.copy(
lectureEvaluationList = currentList
.plus(newList)
.distinctBy { it?.id }
.toPersistentList(),
)
}

private suspend fun checkLoggedIn() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import kotlinx.coroutines.sync.withLock
import org.orbitmvi.orbit.Container
import org.orbitmvi.orbit.ContainerHost
import org.orbitmvi.orbit.annotation.OrbitExperimental
import org.orbitmvi.orbit.syntax.simple.SimpleSyntax
import org.orbitmvi.orbit.syntax.simple.blockingIntent
import org.orbitmvi.orbit.syntax.simple.intent
import org.orbitmvi.orbit.syntax.simple.postSideEffect
Expand Down Expand Up @@ -52,6 +53,7 @@ class OpenLectureViewModel @Inject constructor(
OpenLectureSideEffect.NavigateCellEditor(openLecture.toCellEditorArgument()),
)
}

fun navigateAddCustomCell() = intent { postSideEffect(OpenLectureSideEffect.NavigateAddCustomTimetableCell) }

fun insertTimetable() = intent {
Expand Down Expand Up @@ -128,24 +130,33 @@ class OpenLectureViewModel @Inject constructor(
}

fun updateSchoolLevelPosition(schoolLevel: SchoolLevel) = intent {
reduce {
state.copy(
schoolLevel = schoolLevel,
)
}

getOpenLectureList(
schoolLevel = schoolLevel,
needClear = true,
)
}

fun updateSelectedOpenMajor(openMajor: String) = intent {
if (openMajor == state.selectedOpenMajor) return@intent

reduce {
state.copy(
selectedOpenMajor = openMajor,
)
}

getOpenLectureList(
majorType = openMajor,
needClear = true,
)
}

fun getOpenLectureList(
search: String = searchQuery,
schoolLevel: SchoolLevel = currentState.schoolLevel,
majorType: String = currentState.selectedOpenMajor,
needClear: Boolean,
) = intent {
mutex.withLock {
Expand All @@ -165,13 +176,11 @@ class OpenLectureViewModel @Inject constructor(
GetOpenLectureListUseCase.Param(
cursorId = cursorId,
keyword = search,
major = if (majorType == "전체") null else majorType,
grade = schoolLevel.query,
major = if (currentState.selectedOpenMajor == "전체") null else currentState.selectedOpenMajor,
grade = currentState.schoolLevel.query,
),
).onSuccess { newData ->
handleGetOpenLectureListSuccess(
schoolLevel = schoolLevel,
majorType = majorType,
currentList = currentList,
newData = newData,
)
Expand All @@ -186,24 +195,18 @@ class OpenLectureViewModel @Inject constructor(
}
}

private fun handleGetOpenLectureListSuccess(
schoolLevel: SchoolLevel,
majorType: String,
private suspend fun SimpleSyntax<OpenLectureState, OpenLectureSideEffect>.handleGetOpenLectureListSuccess(
currentList: List<OpenLecture>,
newData: OpenLectureData,
) = intent {
reduce {
isLast = newData.isLast
cursorId = newData.content.lastOrNull()?.id ?: 0L
state.copy(
schoolLevel = schoolLevel,
selectedOpenMajor = majorType,
openLectureList = currentList
.plus(newData.content)
.distinctBy { it.id }
.toPersistentList(),
)
}
) = reduce {
isLast = newData.isLast
cursorId = newData.content.lastOrNull()?.id ?: 0L
state.copy(
openLectureList = currentList
.plus(newData.content)
.distinctBy { it.id }
.toPersistentList(),
)
}

fun showGradeBottomSheet() = intent { reduce { state.copy(showSchoolLevelBottomSheet = true) } }
Expand Down

0 comments on commit a23aa5f

Please sign in to comment.