-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainFeature.swift
More file actions
162 lines (142 loc) · 4.75 KB
/
Copy pathMainFeature.swift
File metadata and controls
162 lines (142 loc) · 4.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
//
// MainFeature.swift
// DevLogPresentation
//
// Created by opfic on 6/16/26.
//
import Combine
import ComposableArchitecture
import DevLogCore
import DevLogDomain
import Foundation
import UserNotifications
@Reducer
struct MainFeature {
@ObservableState
struct State: Equatable {
@Presents var alert: AlertState<Never>?
var unreadPushCount = 0
var isObservingUnreadPushCount = false
}
enum Action: Equatable {
case alert(PresentationAction<Never>)
case view(ViewAction)
case store(StoreAction)
enum ViewAction: Equatable {
case onAppear
case selectedTabChanged(MainTab)
}
enum StoreAction: Equatable {
case setUnreadPushCount(Int)
case setAlert
}
}
private enum CancelID: Hashable {
case unreadPushCount
}
@Dependency(\.observeUnreadPushCountUseCase) var observeUnreadPushCountUseCase
@Dependency(\.trackAnalyticsEventUseCase) var trackAnalyticsEventUseCase
@Dependency(\.setApplicationBadgeCount) var setApplicationBadgeCount
var body: some ReducerOf<Self> {
Reduce { state, action in
switch action {
case .alert:
break
case .view(.onAppear):
guard !state.isObservingUnreadPushCount else { break }
state.isObservingUnreadPushCount = true
return observeUnreadPushCountEffect()
case .view(.selectedTabChanged(let tab)):
guard let screenName = tab.analyticsScreenName else { break }
return trackScreenViewEffect(screenName)
case .store(.setUnreadPushCount(let count)):
state.unreadPushCount = count
return updateBadgeCountEffect(count)
case .store(.setAlert):
state.alert = Self.alertState()
}
return .none
}
.ifLet(\.$alert, action: \.alert)
}
}
extension DependencyValues {
var observeUnreadPushCountUseCase: ObserveUnreadPushCountUseCase {
get { self[ObserveUnreadPushCountUseCaseKey.self] }
set { self[ObserveUnreadPushCountUseCaseKey.self] = newValue }
}
var setApplicationBadgeCount: @Sendable (Int) async throws -> Void {
get { self[SetApplicationBadgeCountKey.self] }
set { self[SetApplicationBadgeCountKey.self] = newValue }
}
}
private enum ObserveUnreadPushCountUseCaseKey: DependencyKey {
static var liveValue: ObserveUnreadPushCountUseCase {
preconditionFailure("ObserveUnreadPushCountUseCase must be provided.")
}
static var testValue: ObserveUnreadPushCountUseCase {
liveValue
}
}
private enum SetApplicationBadgeCountKey: DependencyKey {
static let liveValue: @Sendable (Int) async throws -> Void = { count in
try await UNUserNotificationCenter.current().setBadgeCount(count)
}
static var testValue: @Sendable (Int) async throws -> Void {
liveValue
}
}
private extension MainFeature {
func observeUnreadPushCountEffect() -> Effect<Action> {
.run { [observeUnreadPushCountUseCase] send in
do {
let publisher = try observeUnreadPushCountUseCase.observe()
for try await count in publisher.values {
await send(.store(.setUnreadPushCount(count)))
}
} catch {
await send(.store(.setAlert))
}
}
.cancellable(id: CancelID.unreadPushCount, cancelInFlight: true)
}
func trackScreenViewEffect(_ screenName: String) -> Effect<Action> {
.run { [trackAnalyticsEventUseCase] _ in
trackAnalyticsEventUseCase.execute(.screenView(screenName))
}
}
func updateBadgeCountEffect(_ count: Int) -> Effect<Action> {
.run { [setApplicationBadgeCount] _ in
do {
try await setApplicationBadgeCount(count)
} catch {
Logger(category: "MainFeature").error("Failed to update application badge count", error: error)
}
}
}
static func alertState() -> AlertState<Never> {
AlertState {
TextState(String(localized: "common_error_title"))
} actions: {
ButtonState(role: .cancel) {
TextState(String(localized: "common_close"))
}
} message: {
TextState(String(localized: "main_alert_badge_error_message"))
}
}
}
private extension MainTab {
var analyticsScreenName: String? {
switch self {
case .home:
return "home"
case .today:
return "today"
case .notification:
return nil
case .profile:
return "profile"
}
}
}