-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPushNotificationSettingsViewModel.swift
More file actions
156 lines (146 loc) · 5.51 KB
/
PushNotificationSettingsViewModel.swift
File metadata and controls
156 lines (146 loc) · 5.51 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
//
// PushNotificationSettingsViewModel.swift
// DevLog
//
// Created by 최윤진 on 1/18/26.
//
import Foundation
@Observable
final class PushNotificationSettingsViewModel: Store {
struct State: Equatable {
var pushNotificationEnable: Bool = false
var viewPushNotificationTime: Date = .init()
var sheetPushNotificationTime: Date = .init()
var showTimePicker: Bool = false
var isLoading: Bool = false
var sheetHeight: CGFloat = .pi
var showSheet: Bool = false
var showAlert: Bool = false
var alertTitle: String = ""
var alertMessage: String = ""
var pushNotificationHour: Int {
Calendar.current.component(.hour, from: viewPushNotificationTime)
}
var pushNotificationMinute: Int {
Calendar.current.component(.minute, from: viewPushNotificationTime)
}
}
enum Action {
case fetchSettings
case setAlert(Bool)
case setLoading(Bool)
case setPushNotificationEnable(Bool)
case setPushNotificationTime(view: Date? = nil, sheet: Date? = nil)
case setShowTimePicker(Bool)
case setSheetHeight(CGFloat)
case selectPresetTime(Date)
case confirmUpdate
case rollbackUpdate
}
enum SideEffect {
case fetchPushNotificationSettings
case updatePushNotificationSettings
}
private(set) var state: State = .init()
private let calendar = Calendar.current
private let fetchPushSettingsUseCase: FetchPushSettingsUseCase
private let updatePushSettingsUseCase: UpdatePushSettingsUseCase
init(
fetchPushSettingsUseCase: FetchPushSettingsUseCase,
updatePushSettingsUseCase: UpdatePushSettingsUseCase
) {
self.fetchPushSettingsUseCase = fetchPushSettingsUseCase
self.updatePushSettingsUseCase = updatePushSettingsUseCase
}
func reduce(with action: Action) -> [SideEffect] {
var state = self.state
var effects: [SideEffect] = []
switch action {
case .fetchSettings:
effects = [.fetchPushNotificationSettings]
case .setAlert(let isPresented):
setAlert(&state, isPresented: isPresented)
case .setLoading(let value):
state.isLoading = value
case .setPushNotificationEnable(let value):
state.pushNotificationEnable = value
effects = [.updatePushNotificationSettings]
case .setPushNotificationTime(let view, let sheet):
if let value = view {
state.viewPushNotificationTime = value
}
if let value = sheet {
state.sheetPushNotificationTime = value
}
case .setShowTimePicker(let value):
state.showTimePicker = value
if !value {
state.sheetPushNotificationTime = state.viewPushNotificationTime
}
case .setSheetHeight(let value):
state.sheetHeight = value
case .selectPresetTime(let date):
state.viewPushNotificationTime = date
state.sheetPushNotificationTime = date
effects = [.updatePushNotificationSettings]
case .confirmUpdate:
state.showTimePicker = false
state.viewPushNotificationTime = state.sheetPushNotificationTime
effects = [.updatePushNotificationSettings]
case .rollbackUpdate:
state.showTimePicker = false
state.sheetPushNotificationTime = state.viewPushNotificationTime
}
if self.state != state { self.state = state }
return effects
}
func run(_ effect: SideEffect) {
switch effect {
case .fetchPushNotificationSettings:
Task {
do {
defer { send(.setLoading(false)) }
send(.setLoading(true))
let settings = try await fetchPushSettingsUseCase.execute()
self.send(.setPushNotificationEnable(settings.isEnabled))
if let hour = settings.scheduledTime.hour,
let minute = settings.scheduledTime.minute,
let date = calendar.date(bySettingHour: hour, minute: minute, second: 0, of: Date()) {
self.send(.setPushNotificationTime(view: date, sheet: date))
}
} catch {
send(.setAlert(true))
}
}
case .updatePushNotificationSettings:
Task {
do {
defer { send(.setLoading(false)) }
send(.setLoading(true))
let dateComponents = calendar.dateComponents(
[.hour, .minute],
from: state.sheetPushNotificationTime
)
let settings = PushNotificationSettings(
isEnabled: state.pushNotificationEnable,
scheduledTime: dateComponents
)
try await updatePushSettingsUseCase.execute(settings)
} catch {
send(.setAlert(true))
send(.fetchSettings)
}
}
}
}
}
extension PushNotificationSettingsViewModel {
func setAlert(
_ state: inout State,
isPresented: Bool
) {
state.alertTitle = "오류"
state.alertMessage = "문제가 발생했습니다. 잠시 후 다시 시도해주세요."
state.showAlert = isPresented
}
}