-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPushNotificationSettingsFeature.swift
More file actions
205 lines (184 loc) · 7.01 KB
/
Copy pathPushNotificationSettingsFeature.swift
File metadata and controls
205 lines (184 loc) · 7.01 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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
//
// PushNotificationSettingsFeature.swift
// DevLogPresentation
//
// Created by opfic on 6/12/26.
//
import ComposableArchitecture
import DevLogDomain
import Foundation
import SwiftUI
@Reducer
struct PushNotificationSettingsFeature {
@ObservableState
struct State: Equatable {
@Presents var alert: AlertState<Never>?
@Presents var timePicker: TimePickerState?
var pushNotificationEnable = false
var viewPushNotificationTime = Date()
var loading = LoadingFeature.State()
var isLoading: Bool {
loading.isLoading
}
var pushNotificationHour: Int {
Calendar.current.component(.hour, from: viewPushNotificationTime)
}
var pushNotificationMinute: Int {
Calendar.current.component(.minute, from: viewPushNotificationTime)
}
}
@ObservableState
struct TimePickerState: Equatable {
var time: Date
var height: CGFloat = .pi
}
enum Action: BindableAction {
case alert(PresentationAction<Never>)
case binding(BindingAction<State>)
case timePicker(PresentationAction<TimePicker>)
case fetchSettings
case setAlert
case tapCustomTime
case selectPresetTime(Date)
case loading(LoadingFeature.Action)
enum TimePicker: BindableAction, Equatable {
case binding(BindingAction<TimePickerState>)
case tapCloseButton
case tapDoneButton
}
}
@Dependency(\.fetchPushSettingsUseCase) var fetchPushSettingsUseCase
@Dependency(\.updatePushSettingsUseCase) var updatePushSettingsUseCase
var body: some ReducerOf<Self> {
Scope(state: \.loading, action: \.loading) {
LoadingFeature()
}
BindingReducer()
Reduce { state, action in
switch action {
case .alert:
break
case .binding(\.pushNotificationEnable):
return updatePushNotificationSettingsEffect(settings: settings(from: state))
case .binding(\.viewPushNotificationTime):
let time = state.viewPushNotificationTime
state.timePicker?.time = time
case .binding:
break
case .timePicker(.dismiss):
state.timePicker = nil
case .timePicker(.presented(.tapCloseButton)):
state.timePicker = nil
case .timePicker(.presented(.tapDoneButton)):
guard let time = state.timePicker?.time else { break }
state.timePicker = nil
state.viewPushNotificationTime = time
return updatePushNotificationSettingsEffect(settings: settings(from: state))
case .timePicker:
break
case .fetchSettings:
return fetchPushNotificationSettingsEffect()
case .setAlert:
state.alert = alertState()
case .tapCustomTime:
state.timePicker = TimePickerState(time: state.viewPushNotificationTime)
case .selectPresetTime(let date):
state.viewPushNotificationTime = date
state.timePicker?.time = date
return updatePushNotificationSettingsEffect(settings: settings(from: state))
case .loading:
break
}
return .none
}
.ifLet(\.$alert, action: \.alert)
.ifLet(\.$timePicker, action: \.timePicker) {
TimePickerFeature()
}
}
}
private struct TimePickerFeature: Reducer {
typealias State = PushNotificationSettingsFeature.TimePickerState
typealias Action = PushNotificationSettingsFeature.Action.TimePicker
var body: some ReducerOf<Self> {
BindingReducer()
}
}
extension DependencyValues {
var fetchPushSettingsUseCase: FetchPushSettingsUseCase {
get { self[FetchPushSettingsUseCaseKey.self] }
set { self[FetchPushSettingsUseCaseKey.self] = newValue }
}
var updatePushSettingsUseCase: UpdatePushSettingsUseCase {
get { self[UpdatePushSettingsUseCaseKey.self] }
set { self[UpdatePushSettingsUseCaseKey.self] = newValue }
}
}
private enum FetchPushSettingsUseCaseKey: DependencyKey {
static var liveValue: FetchPushSettingsUseCase {
preconditionFailure("FetchPushSettingsUseCase must be provided.")
}
static var testValue: FetchPushSettingsUseCase {
liveValue
}
}
private enum UpdatePushSettingsUseCaseKey: DependencyKey {
static var liveValue: UpdatePushSettingsUseCase {
preconditionFailure("UpdatePushSettingsUseCase must be provided.")
}
static var testValue: UpdatePushSettingsUseCase {
liveValue
}
}
private extension PushNotificationSettingsFeature {
func fetchPushNotificationSettingsEffect() -> Effect<Action> {
.run { [fetchPushSettingsUseCase] send in
await send(.loading(.begin(target: .default, mode: .delayed)))
do {
let settings = try await fetchPushSettingsUseCase.execute()
await send(.binding(.set(\.pushNotificationEnable, settings.isEnabled)))
if let hour = settings.scheduledTime.hour,
let minute = settings.scheduledTime.minute,
let date = Calendar.current.date(bySettingHour: hour, minute: minute, second: 0, of: Date()) {
await send(.binding(.set(\.viewPushNotificationTime, date)))
}
await send(.loading(.end(target: .default, mode: .delayed)))
} catch {
await send(.loading(.end(target: .default, mode: .delayed)))
await send(.setAlert)
}
}
}
func updatePushNotificationSettingsEffect(settings: PushNotificationSettings) -> Effect<Action> {
.run { [updatePushSettingsUseCase] send in
await send(.loading(.begin(target: .default, mode: .delayed)))
do {
try await updatePushSettingsUseCase.execute(settings)
await send(.loading(.end(target: .default, mode: .delayed)))
} catch {
await send(.loading(.end(target: .default, mode: .delayed)))
await send(.setAlert)
await send(.fetchSettings)
}
}
}
func settings(from state: State) -> PushNotificationSettings {
let date = state.timePicker?.time ?? state.viewPushNotificationTime
let dateComponents = Calendar.current.dateComponents([.hour, .minute], from: date)
return PushNotificationSettings(
isEnabled: state.pushNotificationEnable,
scheduledTime: dateComponents
)
}
func alertState() -> AlertState<Never> {
AlertState {
TextState(String(localized: "common_error_title"))
} actions: {
ButtonState(role: .cancel) {
TextState(String(localized: "common_close"))
}
} message: {
TextState(String(localized: "common_error_message"))
}
}
}