Skip to content

Commit a98fa85

Browse files
committed
Refactor: RoutineListViewModel 초기화 및 데이터 구독 로직 수정
1 parent 0edc72f commit a98fa85

1 file changed

Lines changed: 17 additions & 12 deletions

File tree

presentation/src/main/java/com/threegap/bitnagil/presentation/screen/routinelist/RoutineListViewModel.kt

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,15 @@ class RoutineListViewModel @Inject constructor(
2626
private val deleteRoutineForDayUseCase: DeleteRoutineForDayUseCase,
2727
) : ContainerHost<RoutineListState, RoutineListSideEffect>, ViewModel() {
2828

29-
override val container: Container<RoutineListState, RoutineListSideEffect> = container(initialState = RoutineListState.INIT)
29+
override val container: Container<RoutineListState, RoutineListSideEffect> =
30+
container(
31+
initialState = RoutineListState.INIT,
32+
buildSettings = { repeatOnSubscribedStopTimeout = 5_000L },
33+
onCreate = {
34+
updateDate(selectedDate)
35+
observeWeeklyRoutines()
36+
}
37+
)
3038

3139
private val selectedDate = savedStateHandle.get<String>("selectedDate")
3240
?.takeIf { it.isNotBlank() }
@@ -35,11 +43,6 @@ class RoutineListViewModel @Inject constructor(
3543
}
3644
?: LocalDate.now()
3745

38-
init {
39-
updateDate(selectedDate)
40-
observeWeeklyRoutines()
41-
}
42-
4346
fun updateDate(selectedDate: LocalDate) {
4447
intent {
4548
reduce { state.copy(selectedDate = selectedDate) }
@@ -72,12 +75,14 @@ class RoutineListViewModel @Inject constructor(
7275

7376
private fun observeWeeklyRoutines() {
7477
intent {
75-
reduce { state.copy(isLoading = true) }
76-
val weekDays = state.selectedDate.getCurrentWeekDays()
77-
observeWeeklyRoutinesUseCase(weekDays.first().toString(), weekDays.last().toString())
78-
.collect { schedule ->
79-
reduce { state.copy(isLoading = false, routines = schedule.toUiModel()) }
80-
}
78+
repeatOnSubscription {
79+
reduce { state.copy(isLoading = true) }
80+
val weekDays = state.selectedDate.getCurrentWeekDays()
81+
observeWeeklyRoutinesUseCase(weekDays.first().toString(), weekDays.last().toString())
82+
.collect { schedule ->
83+
reduce { state.copy(isLoading = false, routines = schedule.toUiModel()) }
84+
}
85+
}
8186
}
8287
}
8388

0 commit comments

Comments
 (0)