Skip to content

Commit a617391

Browse files
authored
[#486] TodayViewModel 생성 및 생명주기를 개선한다 (#496)
* fix: Today 데이터 fetch 시점을 탭 선택 상태 기준으로 변경 * fix: 초기 탭 선택 상태의 데이터 fetch 누락 방지 * refactor: Home 데이터 fetch 액션명 정리
1 parent 9d33d26 commit a617391

6 files changed

Lines changed: 16 additions & 11 deletions

File tree

Application/DevLogPresentation/Sources/Home/Home/HomeViewCoordinator.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ final class HomeViewCoordinator {
4545
)
4646
}
4747

48-
func loadInitialData() {
49-
viewModel.send(.loadInitialData)
48+
func fetchData() {
49+
viewModel.send(.fetchData)
5050
}
5151

5252
func makeTodoManageViewModel() -> TodoManageViewModel {

Application/DevLogPresentation/Sources/Home/Home/HomeViewModel.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ final class HomeViewModel: Store {
3838
}
3939

4040
enum Action {
41-
case loadInitialData
41+
case fetchData
4242
case networkStatusChanged(Bool)
4343
case setPresentation(Presentation, Bool)
4444
case setAlert(isPresented: Bool, type: AlertType? = nil)
@@ -145,7 +145,7 @@ final class HomeViewModel: Store {
145145
switch action {
146146
case .networkStatusChanged(let isConnected):
147147
state.isNetworkConnected = isConnected
148-
case .loadInitialData, .setPresentation, .setAlert, .setToast, .refreshWebPages,
148+
case .fetchData, .setPresentation, .setAlert, .setToast, .refreshWebPages,
149149
.tapTodoCategory, .orderTodoCategory, .addTodo, .updateWebPageURLInput,
150150
.addWebPage, .deleteWebPage, .undoDeleteWebPage:
151151
effects = reduceByView(action, state: &state)
@@ -274,7 +274,7 @@ private extension HomeViewModel {
274274
// swiftlint:disable cyclomatic_complexity
275275
func reduceByView(_ action: Action, state: inout State) -> [SideEffect] {
276276
switch action {
277-
case .loadInitialData:
277+
case .fetchData:
278278
return [.fetchTodoCategoryPreferences, .fetchRecentTodos, .fetchWebPages]
279279
case .refreshWebPages:
280280
return [.fetchWebPages]

Application/DevLogPresentation/Sources/Main/MainView.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,11 @@ struct MainView: View {
4141
.onAppear {
4242
coordinator.mainViewModel.send(.onAppear)
4343
}
44-
.onChange(of: selectedTab) { _, newValue in
44+
.onChange(of: selectedTab, initial: true) { _, newValue in
4545
if newValue == .home {
46-
homeViewCoordinator.loadInitialData()
46+
homeViewCoordinator.fetchData()
47+
} else if newValue == .today {
48+
todayViewCoordinator.fetchData()
4749
}
4850
}
4951
.alert(

Application/DevLogPresentation/Sources/Today/TodayView.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ struct TodayView: View {
2929
.toolbar { toolbarContent }
3030
.background(NavigationBarConfigurator())
3131
.refreshable { coordinator.viewModel.send(.refresh) }
32-
.onAppear { coordinator.viewModel.send(.onAppear) }
3332
.alert(
3433
coordinator.viewModel.state.alertTitle,
3534
isPresented: Binding(

Application/DevLogPresentation/Sources/Today/TodayViewCoordinator.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,8 @@ final class TodayViewCoordinator {
2424
updateTodayDisplayOptionsUseCase: container.resolve(UpdateTodayDisplayOptionsUseCase.self)
2525
)
2626
}
27+
28+
func fetchData() {
29+
viewModel.send(.fetchData)
30+
}
2731
}

Application/DevLogPresentation/Sources/Today/TodayViewModel.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ final class TodayViewModel: Store {
6262
case resetDisplayOptions
6363
case completeTodo(TodayTodoItem)
6464
case togglePinned(TodayTodoItem)
65-
case onAppear
65+
case fetchData
6666
case fetchTodos([TodayTodoItem])
6767
case setLoading(Bool)
6868
case updateTodo(TodayTodoItem)
@@ -178,7 +178,7 @@ final class TodayViewModel: Store {
178178
case .refresh, .setAlert, .setSectionScope, .setDueDateVisibility, .setFocusVisibility,
179179
.resetDisplayOptions, .completeTodo, .togglePinned:
180180
effects = reduceByUser(action, state: &state)
181-
case .onAppear:
181+
case .fetchData:
182182
effects = reduceByView(action, state: &state)
183183
case .fetchTodos, .setLoading, .updateTodo, .removeTodo:
184184
effects = reduceByRun(action, state: &state)
@@ -305,7 +305,7 @@ private extension TodayViewModel {
305305

306306
func reduceByView(_ action: Action, state: inout State) -> [SideEffect] {
307307
switch action {
308-
case .onAppear:
308+
case .fetchData:
309309
return [.fetchTodos]
310310
default:
311311
break

0 commit comments

Comments
 (0)