Skip to content

Commit 40c9196

Browse files
committed
refactor: AlertState 적용
1 parent e4d6807 commit 40c9196

3 files changed

Lines changed: 41 additions & 40 deletions

File tree

Application/DevLogPresentation/Sources/Main/MainFeature.swift

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,13 @@ import UserNotifications
1616
struct MainFeature {
1717
@ObservableState
1818
struct State: Equatable {
19+
@Presents var alert: AlertState<Never>?
1920
var unreadPushCount = 0
20-
var showAlert = false
21-
var alertTitle = ""
22-
var alertMessage = ""
2321
var isObservingUnreadPushCount = false
2422
}
2523

2624
enum Action: Equatable {
25+
case alert(PresentationAction<Never>)
2726
case view(ViewAction)
2827
case store(StoreAction)
2928

@@ -49,6 +48,8 @@ struct MainFeature {
4948
var body: some ReducerOf<Self> {
5049
Reduce { state, action in
5150
switch action {
51+
case .alert:
52+
break
5253
case .view(.onAppear):
5354
guard !state.isObservingUnreadPushCount else { break }
5455
state.isObservingUnreadPushCount = true
@@ -65,6 +66,7 @@ struct MainFeature {
6566

6667
return .none
6768
}
69+
.ifLet(\.$alert, action: \.alert)
6870
}
6971
}
7072

@@ -145,9 +147,19 @@ private extension MainFeature {
145147
_ state: inout State,
146148
isPresented: Bool
147149
) {
148-
state.alertTitle = String(localized: "common_error_title")
149-
state.alertMessage = String(localized: "main_alert_badge_error_message")
150-
state.showAlert = isPresented
150+
state.alert = isPresented ? alertState() : nil
151+
}
152+
153+
static func alertState() -> AlertState<Never> {
154+
AlertState {
155+
TextState(String(localized: "common_error_title"))
156+
} actions: {
157+
ButtonState(role: .cancel) {
158+
TextState(String(localized: "common_close"))
159+
}
160+
} message: {
161+
TextState(String(localized: "main_alert_badge_error_message"))
162+
}
151163
}
152164
}
153165

Application/DevLogPresentation/Sources/Main/MainView.swift

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
//
77

88
import SwiftUI
9+
import ComposableArchitecture
910
import DevLogCore
1011
import DevLogDomain
1112

@@ -38,6 +39,8 @@ struct MainView: View {
3839
}
3940

4041
var body: some View {
42+
@Bindable var store = coordinator.store
43+
4144
Group {
4245
if isCompactLayout {
4346
tabView
@@ -63,14 +66,7 @@ struct MainView: View {
6366
profileViewCoordinator.fetchData()
6467
}
6568
}
66-
.alert(
67-
coordinator.store.alertTitle,
68-
isPresented: mainAlertPresented
69-
) {
70-
Button(String(localized: "common_close"), role: .cancel) { }
71-
} message: {
72-
Text(coordinator.store.alertMessage)
73-
}
69+
.alert($store.scope(state: \.alert, action: \.alert))
7470
.toastHost()
7571
}
7672

@@ -381,13 +377,6 @@ private extension MainView {
381377
horizontalSizeClass == .compact
382378
}
383379

384-
var mainAlertPresented: Binding<Bool> {
385-
Binding(
386-
get: { coordinator.store.showAlert },
387-
set: { coordinator.store.send(.store(.setAlert($0))) }
388-
)
389-
}
390-
391380
var sidebarSelection: Binding<MainTab?> {
392381
Binding(
393382
get: { selectedTab },

Application/DevLogPresentation/Tests/Main/MainFeatureTests.swift

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -81,29 +81,23 @@ struct MainFeatureTests {
8181
}
8282

8383
@Test("MainFeature는 기존 Main 상태관리처럼 alert 표시 여부와 문구를 함께 갱신한다")
84-
func MainFeature는_기존_Main_상태관리처럼_alert_표시_여부와_문구를_함께_갱신한다() async {
84+
func MainFeature는_기존_Main_상태관리처럼_alert_state를_갱신한다() async {
8585
let reference = MainStateManagementReference()
8686
let store = makeStore()
8787

8888
let presentEffects = reference.reduce(.setAlert(true))
8989
await store.send(.store(.setAlert(true))) {
90-
$0.showAlert = reference.state.showAlert
91-
$0.alertTitle = reference.state.alertTitle
92-
$0.alertMessage = reference.state.alertMessage
90+
$0.alert = reference.state.alert
9391
}
9492

9593
let dismissEffects = reference.reduce(.setAlert(false))
9694
await store.send(.store(.setAlert(false))) {
97-
$0.showAlert = reference.state.showAlert
98-
$0.alertTitle = reference.state.alertTitle
99-
$0.alertMessage = reference.state.alertMessage
95+
$0.alert = reference.state.alert
10096
}
10197

10298
#expect(presentEffects.isEmpty)
10399
#expect(dismissEffects.isEmpty)
104-
#expect(!reference.state.showAlert)
105-
#expect(reference.state.alertTitle == String(localized: "common_error_title"))
106-
#expect(reference.state.alertMessage == String(localized: "main_alert_badge_error_message"))
100+
#expect(reference.state.alert == nil)
107101
}
108102

109103
@Test("MainFeature는 기존 Main 상태관리처럼 unread count 관찰 시작 실패 시 alert를 표시한다")
@@ -118,9 +112,7 @@ struct MainFeatureTests {
118112
$0.isObservingUnreadPushCount = true
119113
}
120114
await store.receive(.store(.setAlert(true))) {
121-
$0.showAlert = reference.state.showAlert
122-
$0.alertTitle = reference.state.alertTitle
123-
$0.alertMessage = reference.state.alertMessage
115+
$0.alert = reference.state.alert
124116
}
125117
}
126118

@@ -178,10 +170,8 @@ private func makeStore(
178170
@MainActor
179171
private final class MainStateManagementReference {
180172
struct State: Equatable {
173+
var alert: AlertState<Never>?
181174
var unreadPushCount = 0
182-
var showAlert = false
183-
var alertTitle = ""
184-
var alertMessage = ""
185175
}
186176

187177
enum Action {
@@ -222,9 +212,19 @@ private final class MainStateManagementReference {
222212
}
223213

224214
private func setAlert(_ isPresented: Bool) {
225-
state.alertTitle = String(localized: "common_error_title")
226-
state.alertMessage = String(localized: "main_alert_badge_error_message")
227-
state.showAlert = isPresented
215+
state.alert = isPresented ? expectedMainErrorAlert() : nil
216+
}
217+
}
218+
219+
private func expectedMainErrorAlert() -> AlertState<Never> {
220+
AlertState {
221+
TextState(String(localized: "common_error_title"))
222+
} actions: {
223+
ButtonState(role: .cancel) {
224+
TextState(String(localized: "common_close"))
225+
}
226+
} message: {
227+
TextState(String(localized: "main_alert_badge_error_message"))
228228
}
229229
}
230230

0 commit comments

Comments
 (0)