Skip to content

Commit 23090a2

Browse files
committed
feat: 시트 내 변동 값으로 서버에 업데이트 하고, 시트가 닫힐 때 적절한 값으로 부모 뷰 값 업데이트
1 parent 291cc1d commit 23090a2

2 files changed

Lines changed: 42 additions & 25 deletions

File tree

DevLog/Presentation/ViewModel/PushNotificationSettingsViewModel.swift

Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ import Foundation
1010
final class PushNotificationSettingsViewModel: Store {
1111
struct State {
1212
var pushNotificationEnable: Bool = false
13-
var pushNotificationTime: Date = .init()
13+
var viewPushNotificationTime: Date = .init()
14+
var sheetPushNotificationTime: Date = .init()
1415
var showTimePicker: Bool = false
1516
var isLoading: Bool = false
1617
var sheetHeight: CGFloat = .pi
@@ -19,10 +20,10 @@ final class PushNotificationSettingsViewModel: Store {
1920
var alertTitle: String = ""
2021
var alertMessage: String = ""
2122
var pushNotificationHour: Int {
22-
Calendar.current.component(.hour, from: pushNotificationTime)
23+
Calendar.current.component(.hour, from: viewPushNotificationTime)
2324
}
2425
var pushNotificationMinute: Int {
25-
Calendar.current.component(.minute, from: pushNotificationTime)
26+
Calendar.current.component(.minute, from: viewPushNotificationTime)
2627
}
2728
}
2829

@@ -32,18 +33,20 @@ final class PushNotificationSettingsViewModel: Store {
3233
case setLoading(Bool)
3334
case setPushNotificationEnable(Bool)
3435
case setPushNotificationHour(Int)
35-
case setPushNotificationTime(Date)
36+
case setPushNotificationTime(view: Date? = nil, sheet: Date? = nil)
3637
case setShowTimePicker(Bool)
3738
case setSheetHeight(CGFloat)
39+
case confirmUpdate
40+
case rollbackUpdate
3841
}
3942

4043
enum SideEffect {
4144
case fetchPushNotificationSettings
4245
case updatePushNotificationSettings
4346
}
4447

45-
private let calendar = Calendar.current
4648
@Published private(set) var state: State = .init()
49+
private let calendar = Calendar.current
4750
private let fetchPushSettingsUseCase: FetchPushSettingsUseCase
4851
private let updatePushSettingsUseCase: UpdatePushSettingsUseCase
4952

@@ -57,6 +60,7 @@ final class PushNotificationSettingsViewModel: Store {
5760

5861
func reduce(with action: Action) -> [SideEffect] {
5962
var state = self.state
63+
var effects: [SideEffect] = []
6064
switch action {
6165
case .onAppear:
6266
return [.fetchPushNotificationSettings]
@@ -65,28 +69,37 @@ final class PushNotificationSettingsViewModel: Store {
6569
case .setLoading(let value):
6670
state.isLoading = value
6771
case .setPushNotificationEnable(let value):
68-
self.state.pushNotificationEnable = value
69-
return [.updatePushNotificationSettings]
72+
state.pushNotificationEnable = value
7073
case .setPushNotificationHour(let value):
7174
// 시간만 변경
7275
if let newDate = calendar.date(
7376
bySettingHour: value,
7477
minute: 0, second: 0,
75-
of: state.pushNotificationTime
78+
of: state.viewPushNotificationTime
7679
) {
77-
self.state.pushNotificationTime = newDate
78-
return [.updatePushNotificationSettings]
80+
state.viewPushNotificationTime = newDate
81+
}
82+
case .setPushNotificationTime(let view, let sheet):
83+
if let value = view {
84+
state.viewPushNotificationTime = value
85+
}
86+
if let value = sheet {
87+
state.sheetPushNotificationTime = value
7988
}
80-
case .setPushNotificationTime(let value):
81-
self.state.pushNotificationTime = value
82-
return [.updatePushNotificationSettings]
8389
case .setShowTimePicker(let value):
8490
state.showTimePicker = value
8591
case .setSheetHeight(let value):
8692
state.sheetHeight = value
93+
case .confirmUpdate:
94+
state.showTimePicker = false
95+
state.viewPushNotificationTime = state.sheetPushNotificationTime
96+
effects = [.updatePushNotificationSettings]
97+
case .rollbackUpdate:
98+
state.showTimePicker = false
99+
state.sheetPushNotificationTime = state.viewPushNotificationTime
87100
}
88101
self.state = state
89-
return []
102+
return effects
90103
}
91104

92105
func run(_ effect: SideEffect) {
@@ -101,7 +114,7 @@ final class PushNotificationSettingsViewModel: Store {
101114
if let hour = settings.scheduledTime.hour,
102115
let minute = settings.scheduledTime.minute,
103116
let date = calendar.date(bySettingHour: hour, minute: minute, second: 0, of: Date()) {
104-
self.send(.setPushNotificationTime(date))
117+
self.send(.setPushNotificationTime(view: date, sheet: date))
105118
}
106119
} catch {
107120
send(.setAlert(true))
@@ -112,7 +125,10 @@ final class PushNotificationSettingsViewModel: Store {
112125
do {
113126
defer { send(.setLoading(false)) }
114127
send(.setLoading(true))
115-
let dateComponents = calendar.dateComponents([.hour, .minute], from: state.pushNotificationTime)
128+
let dateComponents = calendar.dateComponents(
129+
[.hour, .minute],
130+
from: state.sheetPushNotificationTime
131+
)
116132
let settings = PushNotificationSettings(
117133
isEnabled: state.pushNotificationEnable,
118134
scheduledTime: dateComponents

DevLog/UI/Setting/PushNotificationSettingsView.swift

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,15 @@ struct PushNotificationSettingsView: View {
3737
}
3838
.contentShape(Rectangle())
3939
.onTapGesture {
40-
viewModel.send(.setPushNotificationHour(hour))
40+
viewModel.send(.setPushNotificationTime(sheet: date))
41+
viewModel.send(.confirmUpdate)
4142
}
4243
}
4344
}
4445
HStack {
4546
Text("사용자 설정")
4647
Spacer()
47-
Text(formattedTimeString(viewModel.state.pushNotificationTime))
48+
Text(formattedTimeString(viewModel.state.viewPushNotificationTime))
4849
.foregroundStyle(.secondary)
4950
if viewModel.state.pushNotificationMinute != 0 {
5051
Image(systemName: "checkmark")
@@ -71,14 +72,14 @@ struct PushNotificationSettingsView: View {
7172
}
7273
.sheet(isPresented: Binding(
7374
get: { viewModel.state.showTimePicker },
74-
set: { _ in viewModel.send(.setShowTimePicker(false)) }
75+
set: { viewModel.send(.setShowTimePicker($0)) }
7576
)) {
7677
NavigationStack {
7778
DatePicker(
7879
"",
7980
selection: Binding(
80-
get: { viewModel.state.pushNotificationTime },
81-
set: { viewModel.send(.setPushNotificationTime($0)) }
81+
get: { viewModel.state.sheetPushNotificationTime },
82+
set: { viewModel.send(.setPushNotificationTime(sheet: $0)) }
8283
),
8384
displayedComponents: .hourAndMinute
8485
)
@@ -105,27 +106,27 @@ struct PushNotificationSettingsView: View {
105106
if #available(iOS 26.0, *) {
106107
ToolbarItem(placement: .topBarLeading) {
107108
Button(role: .cancel) {
108-
109+
viewModel.send(.rollbackUpdate)
109110
}
110111
}
111112

112113
ToolbarItem(placement: .topBarTrailing) {
113114
Button(role: .confirm) {
114-
115+
viewModel.send(.confirmUpdate)
115116
}
116117
}
117118
} else {
118119
ToolbarItem(placement: .topBarLeading) {
119120
Button {
120-
121+
viewModel.send(.rollbackUpdate)
121122
} label: {
122123
Text("취소")
123124
}
124125
}
125126

126127
ToolbarItem(placement: .topBarTrailing) {
127128
Button {
128-
129+
viewModel.send(.confirmUpdate)
129130
} label: {
130131
Text("확인")
131132
.bold()

0 commit comments

Comments
 (0)