-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPushNotificationSettingsViewModel.swift
More file actions
105 lines (96 loc) · 3.59 KB
/
Copy pathPushNotificationSettingsViewModel.swift
File metadata and controls
105 lines (96 loc) · 3.59 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
//
// PushNotificationSettingsViewModel.swift
// DevLog
//
// Created by 최윤진 on 1/18/26.
//
import Foundation
final class PushNotificationSettingsViewModel: Store {
struct State {
var pushNotificationEnable = false
var pushNotificationTime = Date()
var showTimePicker = false
var sheetHeight = CGFloat.pi
var pushNotificationHour: Int {
Calendar.current.component(.hour, from: pushNotificationTime)
}
var pushNotificationMinute: Int {
Calendar.current.component(.minute, from: pushNotificationTime)
}
}
enum Action {
case onAppear
case setPushNotificationEnable(Bool)
case setPushNotificationHour(Int)
case setPushNotificationTime(Date)
case setShowTimePicker(Bool)
case setSheetHeight(CGFloat)
}
enum SideEffect {
case fetchPushNotificationSettings
case updatePushNotificationSettings
}
private let calendar = Calendar.current
@Published private(set) var state: State = .init()
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
switch action {
case .onAppear:
return [.fetchPushNotificationSettings]
case .setPushNotificationEnable(let value):
self.state.pushNotificationEnable = value
return [.updatePushNotificationSettings]
case .setPushNotificationHour(let value):
// 시간만 변경
if let newDate = calendar.date(
bySettingHour: value,
minute: 0, second: 0,
of: state.pushNotificationTime
) {
self.state.pushNotificationTime = newDate
return [.updatePushNotificationSettings]
}
case .setPushNotificationTime(let value):
self.state.pushNotificationTime = value
return [.updatePushNotificationSettings]
case .setShowTimePicker(let value):
state.showTimePicker = value
case .setSheetHeight(let value):
state.sheetHeight = value
}
self.state = state
return []
}
func run(_ effect: SideEffect) {
switch effect {
case .fetchPushNotificationSettings:
Task {
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(date))
}
}
case .updatePushNotificationSettings:
Task {
let dateComponents = calendar.dateComponents([.hour, .minute], from: state.pushNotificationTime)
let settings = PushNotificationSettings(
isEnabled: state.pushNotificationEnable,
scheduledTime: dateComponents
)
try await updatePushSettingsUseCase.execute(settings)
}
}
}
}