Skip to content

Commit 6f95fa8

Browse files
committed
refactor: refreshable fetch 흐름 분리
1 parent b29ec97 commit 6f95fa8

4 files changed

Lines changed: 29 additions & 17 deletions

File tree

Application/DevLogPresentation/Sources/Home/List/TodoListView.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ struct TodoListView: View {
171171
}
172172
.offset(y: headerOffset)
173173
}
174-
.refreshable { store.send(.view(.refresh)) }
174+
.refreshable { await store.send(.view(.refresh)).finish() }
175175
.scrollDisabled(visibleTodos.isEmpty || store.state.isLoading)
176176

177177
if store.state.isLoading {

Application/DevLogPresentation/Sources/PushNotification/PushNotificationListFeature.swift

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ struct PushNotificationListFeature {
5858
case loading(LoadingFeature.Action)
5959

6060
enum ViewAction: Equatable {
61+
case refresh
6162
case fetchNotifications
6263
case loadNextPage
6364
case deleteNotification(PushNotificationItem)
@@ -83,7 +84,6 @@ struct PushNotificationListFeature {
8384
case syncNotifications([PushNotificationItem], nextCursor: PushNotificationCursor?, hasMore: Bool)
8485
case setNotificationHidden(String, Bool)
8586
case setNotificationRead(String, Bool)
86-
case observeNotifications(PushNotificationQuery, Int)
8787
}
8888
}
8989

@@ -151,6 +151,9 @@ private extension PushNotificationListFeature {
151151
state: inout State
152152
) -> Effect<Action> {
153153
switch action {
154+
case .refresh:
155+
state.nextCursor = nil
156+
return fetchNotificationsPageEffect(query: state.query, cursor: nil)
154157
case .fetchNotifications:
155158
state.nextCursor = nil
156159
return fetchNotificationsEffect(query: state.query, cursor: nil, existingCount: 0)
@@ -252,8 +255,6 @@ private extension PushNotificationListFeature {
252255
if let index = state.notifications.firstIndex(where: { $0.id == notificationId }) {
253256
state.notifications[index].isRead = isRead
254257
}
255-
case .observeNotifications(let query, let limit):
256-
return observeNotificationsEffect(query: query, limit: limit)
257258
}
258259

259260
return .none
@@ -272,7 +273,28 @@ private extension PushNotificationListFeature {
272273
existingCount: Int
273274
) -> Effect<Action> {
274275
let limit = max(query.pageSize, existingCount)
275-
let fetchEffect: Effect<Action> = .run { [fetchPushNotificationsUseCase] send in
276+
let fetchEffect = fetchNotificationsPageEffect(query: query, cursor: cursor)
277+
let observeEffect = observeNotificationsEffect(
278+
query: query,
279+
limit: max(limit, existingCount + query.pageSize)
280+
)
281+
282+
if cursor == nil {
283+
return .concatenate(
284+
.cancel(id: CancelID.observeNotifications),
285+
fetchEffect,
286+
observeEffect
287+
)
288+
}
289+
290+
return fetchEffect
291+
}
292+
293+
func fetchNotificationsPageEffect(
294+
query: PushNotificationQuery,
295+
cursor: PushNotificationCursor?
296+
) -> Effect<Action> {
297+
.run { [fetchPushNotificationsUseCase] send in
276298
await send(.loading(.begin(target: .default, mode: .delayed)))
277299
do {
278300
let page = try await fetchPushNotificationsUseCase.execute(query, cursor: cursor)
@@ -286,23 +308,13 @@ private extension PushNotificationListFeature {
286308
))
287309
)
288310
await send(.store(.setHasMore(page.items.count == query.pageSize && page.nextCursor != nil)))
289-
await send(.store(.observeNotifications(query, max(limit, existingCount + page.items.count))))
290311
await send(.loading(.end(target: .default, mode: .delayed)))
291312
} catch {
292313
await send(.loading(.end(target: .default, mode: .delayed)))
293314
await send(.store(.setAlert))
294315
}
295316
}
296317
.cancellable(id: CancelID.fetchNotifications, cancelInFlight: true)
297-
298-
if cursor == nil {
299-
return .concatenate(
300-
.cancel(id: CancelID.observeNotifications),
301-
fetchEffect
302-
)
303-
}
304-
305-
return fetchEffect
306318
}
307319

308320
func observeNotificationsEffect(

Application/DevLogPresentation/Sources/PushNotification/PushNotificationListView.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ struct PushNotificationListView: View {
3838
headerOffset = max(0, -offset)
3939
}
4040
.safeAreaInset(edge: .top) { safeAreaHeader }
41-
.refreshable { store.send(.view(.fetchNotifications)) }
41+
.refreshable { await store.send(.view(.refresh)).finish() }
4242
.navigationTitle(String(localized: "nav_push_notifications"))
4343
.listStyle(.plain)
4444
}

Application/DevLogPresentation/Sources/Today/TodayView.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ struct TodayView: View {
3939
.navigationTitle(String(localized: "nav_today"))
4040
.toolbar { toolbarContent }
4141
.background(NavigationBarConfigurator())
42-
.refreshable { store.send(.refresh) }
42+
.refreshable { await store.send(.refresh).finish() }
4343
.alert($store.scope(state: \.alert, action: \.alert))
4444
.overlay {
4545
if store.isLoading {

0 commit comments

Comments
 (0)