@@ -10,7 +10,8 @@ import Foundation
1010final 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
0 commit comments