Skip to content

Commit e20842b

Browse files
committed
refactor: PushNotification Account toast 표시 이전
1 parent a7794f5 commit e20842b

5 files changed

Lines changed: 31 additions & 63 deletions

File tree

Application/DevLogPresentation/Sources/PushNotification/PushNotificationListView.swift

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -47,18 +47,6 @@ struct PushNotificationListView: View {
4747
} message: {
4848
Text(viewModel.state.alertMessage)
4949
}
50-
.toast(
51-
isPresented: Binding(
52-
get: { viewModel.state.showToast },
53-
set: { viewModel.send(.setToast(isPresented: $0)) }),
54-
duration: 5,
55-
action: { viewModel.send(.undoDelete) }
56-
) {
57-
Label(viewModel.state.toastMessage, systemImage: "arrow.uturn.left")
58-
.font(.caption)
59-
.multilineTextAlignment(.center)
60-
.lineLimit(3)
61-
}
6250
.sheet(item: Binding(
6351
get: { isCompactLayout ? coordinator.todoIdToPresent : nil },
6452
set: { item in

Application/DevLogPresentation/Sources/PushNotification/PushNotificationListViewModel.swift

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,8 @@ final class PushNotificationListViewModel: Store {
1515
struct State: Equatable {
1616
var notifications: [PushNotificationItem] = []
1717
var showAlert: Bool = false
18-
var showToast: Bool = false
1918
var alertTitle: String = ""
2019
var alertMessage: String = ""
21-
var toastMessage: String = ""
2220
var isLoading: Bool = false
2321
var hasMore: Bool = false
2422
var nextCursor: PushNotificationCursor?
@@ -33,8 +31,8 @@ final class PushNotificationListViewModel: Store {
3331
case deleteNotification(PushNotificationItem)
3432
case toggleRead(PushNotificationItem)
3533
case undoDelete
34+
case finishDeleteToast(String)
3635
case setAlert(isPresented: Bool)
37-
case setToast(isPresented: Bool)
3836
case setLoading(Bool)
3937
case appendNotifications([PushNotificationItem], nextCursor: PushNotificationCursor?)
4038
case resetPagination
@@ -98,11 +96,11 @@ final class PushNotificationListViewModel: Store {
9896
var effects: [SideEffect] = []
9997

10098
switch action {
101-
case .deleteNotification, .toggleRead, .undoDelete, .setAlert, .toggleSortOption,
99+
case .deleteNotification, .toggleRead, .undoDelete, .finishDeleteToast, .setAlert, .toggleSortOption,
102100
.setTimeFilter, .toggleUnreadOnly, .resetFilters, .selectNotification:
103101
effects = reduceByUser(action, state: &state)
104102

105-
case .fetchNotifications, .setToast, .loadNextPage:
103+
case .fetchNotifications, .loadNextPage:
106104
effects = reduceByView(action, state: &state)
107105

108106
case .setLoading, .appendNotifications, .resetPagination, .setHasMore,
@@ -187,7 +185,7 @@ private extension PushNotificationListViewModel {
187185
if state.notifications.contains(where: { $0.id == item.id }) {
188186
self.undoNotificationId = item.id
189187
setNotificationHidden(&state, notificationId: item.id, isHidden: true)
190-
setToast(&state, isPresented: true)
188+
presentDeleteNotificationToast(item.id)
191189
return [.delete(item)]
192190
}
193191
return []
@@ -201,6 +199,11 @@ private extension PushNotificationListViewModel {
201199
setNotificationHidden(&state, notificationId: undoNotificationId, isHidden: false)
202200
self.undoNotificationId = nil
203201
return [.undoDelete(undoNotificationId)]
202+
case .finishDeleteToast(let notificationId):
203+
state.notifications.removeAll { $0.id == notificationId && $0.isHidden }
204+
if self.undoNotificationId == notificationId {
205+
self.undoNotificationId = nil
206+
}
204207
case .setAlert(let isPresented):
205208
setAlert(&state, isPresented: isPresented)
206209
case .toggleSortOption:
@@ -253,12 +256,6 @@ private extension PushNotificationListViewModel {
253256
case .loadNextPage:
254257
guard state.hasMore, !state.isLoading else { return [] }
255258
return [.fetchNotifications(state.query, cursor: state.nextCursor)]
256-
case .setToast(let isPresented):
257-
setToast(&state, isPresented: isPresented)
258-
if !isPresented {
259-
state.notifications.removeAll { $0.isHidden }
260-
self.undoNotificationId = nil
261-
}
262259
default:
263260
break
264261
}
@@ -306,12 +303,21 @@ private extension PushNotificationListViewModel {
306303
state.showAlert = isPresented
307304
}
308305

309-
func setToast(
310-
_ state: inout State,
311-
isPresented: Bool
312-
) {
313-
state.toastMessage = String(localized: "common_undo")
314-
state.showToast = isPresented
306+
func presentDeleteNotificationToast(_ notificationId: String) {
307+
ToastPresenter.present(
308+
message: String(localized: "common_undo"),
309+
systemImage: "arrow.uturn.left",
310+
duration: 5,
311+
font: .caption,
312+
multilineTextAlignment: .center,
313+
lineLimit: 3,
314+
action: { [weak self] in
315+
self?.send(.undoDelete)
316+
},
317+
onDismiss: { [weak self] in
318+
self?.send(.finishDeleteToast(notificationId))
319+
}
320+
)
315321
}
316322

317323
func setNotificationHidden(

Application/DevLogPresentation/Sources/Settings/AccountView.swift

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,6 @@ struct AccountView: View {
6363
} message: {
6464
Text(viewModel.state.alertMessage)
6565
}
66-
.toast(isPresented: Binding(
67-
get: { viewModel.state.showToast },
68-
set: { viewModel.send(.setToast(isPresented: $0)) }
69-
)) {
70-
Text(viewModel.state.toastMessage)
71-
}
7266
.overlay {
7367
if viewModel.state.isLoading {
7468
LoadingView()

Application/DevLogPresentation/Sources/Settings/AccountViewModel.swift

Lines changed: 2 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,6 @@ final class AccountViewModel: Store {
1818
var alertTitle: String = ""
1919
var alertType: AlertType?
2020
var alertMessage: String = ""
21-
var showToast: Bool = false
22-
var toastType: ToastType?
23-
var toastMessage: String = ""
2421
var isLoading: Bool = false
2522
}
2623

@@ -29,7 +26,6 @@ final class AccountViewModel: Store {
2926
case linkWithProvider(AuthProvider)
3027
case unlinkFromProvider(AuthProvider)
3128
case setAlert(isPresented: Bool, type: AlertType? = nil)
32-
case setToast(isPresented: Bool, type: ToastType? = nil)
3329
case setLoading(Bool)
3430
case updateProviders(currentProvider: AuthProvider?, allProviders: [AuthProvider])
3531
}
@@ -47,11 +43,6 @@ final class AccountViewModel: Store {
4743
case error
4844
}
4945

50-
enum ToastType {
51-
case linkSuccess
52-
case unlinkSuccess
53-
}
54-
5546
private(set) var state: State = .init()
5647
private let fetchProvidersUseCase: FetchAuthProvidersUseCase
5748
private let linkProviderUseCase: LinkAuthProviderUseCase
@@ -81,8 +72,6 @@ final class AccountViewModel: Store {
8172
effects = [.unlink(value)]
8273
case .setAlert(let presented, let type):
8374
setAlert(&state, isPresented: presented, type: type)
84-
case .setToast(let presented, let type):
85-
setToast(&state, isPresented: presented, type: type)
8675
case .setLoading(let value):
8776
state.isLoading = value
8877
case .updateProviders(let currentProvider, let allProviders):
@@ -113,7 +102,7 @@ final class AccountViewModel: Store {
113102
do {
114103
defer { endLoading(.delayed) }
115104
try await linkProviderUseCase.execute(provider)
116-
send(.setToast(isPresented: true, type: .linkSuccess))
105+
ToastPresenter.present(message: String(localized: "account_toast_link_success"))
117106

118107
let (currentProvider, allProviders) = try await fetchProvidersUseCase.execute()
119108
send(.updateProviders(currentProvider: currentProvider, allProviders: allProviders))
@@ -128,7 +117,7 @@ final class AccountViewModel: Store {
128117
do {
129118
defer { endLoading(.delayed) }
130119
try await unlinkProviderUseCase.execute(provider)
131-
send(.setToast(isPresented: true, type: .unlinkSuccess))
120+
ToastPresenter.present(message: String(localized: "account_toast_unlink_success"))
132121

133122
let (currentProvider, allProviders) = try await fetchProvidersUseCase.execute()
134123
send(.updateProviders(currentProvider: currentProvider, allProviders: allProviders))
@@ -180,19 +169,6 @@ private extension AccountViewModel {
180169
state.alertType = type
181170
}
182171

183-
func setToast(_ state: inout State, isPresented: Bool, type: ToastType?) {
184-
switch type {
185-
case .linkSuccess:
186-
state.toastMessage = String(localized: "account_toast_link_success")
187-
case .unlinkSuccess:
188-
state.toastMessage = String(localized: "account_toast_unlink_success")
189-
case .none:
190-
state.toastMessage = ""
191-
}
192-
state.showToast = isPresented
193-
state.toastType = type
194-
}
195-
196172
private func beginLoading(_ mode: LoadingState.Mode) {
197173
loadingState.begin(mode: mode) { [weak self] isLoading in
198174
self?.send(.setLoading(isLoading))

Application/DevLogPresentation/Tests/PushNotification/DeletePushNotificationTests.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ import DevLogDomain
1515
struct DeletePushNotificationTests {
1616
@Test("삭제하면 항목이 즉시 숨겨지고 되돌리기 토스트가 표시되며 삭제 유스케이스가 호출된다")
1717
func 삭제하면_항목이_즉시_숨겨지고_되돌리기_토스트가_표시되며_삭제_유스케이스가_호출된다() async throws {
18+
ToastPresenter.reset()
19+
1820
let fetchPushNotificationsUseCaseSpy = FetchPushNotificationsUseCaseSpy(
1921
pushNotificationPage: PushNotificationPage(
2022
items: [
@@ -56,7 +58,7 @@ struct DeletePushNotificationTests {
5658
pushNotificationListViewModel.send(.deleteNotification(pushNotificationItem))
5759

5860
#expect(pushNotificationListViewModel.state.notifications.filter { !$0.isHidden }.isEmpty)
59-
#expect(pushNotificationListViewModel.state.showToast)
61+
#expect(ToastPresenter.item?.message == String(localized: "common_undo"))
6062

6163
await waitUntil {
6264
deletePushNotificationUseCaseSpy.calledNotificationIds == ["notification-1"]
@@ -67,6 +69,8 @@ struct DeletePushNotificationTests {
6769

6870
@Test("삭제를 되돌리면 되돌리기 유스케이스가 호출되고 숨김 상태가 해제된다")
6971
func 삭제를_되돌리면_되돌리기_유스케이스가_호출되고_숨김_상태가_해제된다() async throws {
72+
ToastPresenter.reset()
73+
7074
let fetchPushNotificationsUseCaseSpy = FetchPushNotificationsUseCaseSpy(
7175
pushNotificationPage: PushNotificationPage(
7276
items: [

0 commit comments

Comments
 (0)