@@ -50,6 +50,7 @@ class LocationManager: NSObject, ObservableObject {
5050// span: MKCoordinateSpan(latitudeDelta: 0.005, longitudeDelta: 0.005)
5151// )
5252 @Published var isAlways : Bool = false
53+ var challengeType : Main . ChallengeType ?
5354
5455 var geofence : CLCircularRegion ? {
5556 willSet {
@@ -64,7 +65,6 @@ class LocationManager: NSObject, ObservableObject {
6465 }
6566 }
6667 }
67-
6868
6969 @Published var status : CLAuthorizationStatus ? {
7070 willSet { self . objectWillChange. send ( ) }
@@ -73,20 +73,20 @@ class LocationManager: NSObject, ObservableObject {
7373 @Published var location = CLLocation ( ) {
7474 willSet {
7575 print ( " LocationManager - location \( newValue) willSet " )
76- print ( " LocationManager - 1 : \( ( self . geofence? . contains ( newValue. coordinate) ?? false ) ) " )
76+ print ( " LocationManager - 1 : \( self . geofence? . contains ( newValue. coordinate) ?? false ) " )
7777 print ( " LocationManager - 2 : \( !( self . geofence? . contains ( self . location. coordinate) ?? false ) ) " )
78- print ( " LocationManager - 3 : \( !( geofence? . contains ( newValue. coordinate) ?? false ) ) " )
78+ print ( " LocationManager - 3 : \( !( self . geofence? . contains ( newValue. coordinate) ?? false ) ) " )
7979 print ( " LocationManager - 4 : \( self . geofence? . contains ( self . location. coordinate) ?? false ) " )
80-
81- if ( self . geofence? . contains ( newValue. coordinate) ?? false ) , !( self . geofence? . contains ( self . location. coordinate) ?? false ) {
80+
81+ if self . geofence? . contains ( newValue. coordinate) ?? false , !( self . geofence? . contains ( self . location. coordinate) ?? false ) {
8282 // newValue내에 존재 && 이전Value내에 존재하지 X
8383 print ( " LocationManager - geofence enter " )
8484 NotificationCenter . default. post ( name: . enterGeofence, object: nil )
85- } else if !( geofence? . contains ( newValue. coordinate) ?? false ) , ( self . geofence? . contains ( self . location. coordinate) ?? false ) {
85+ } else if !( self . geofence? . contains ( newValue. coordinate) ?? false ) , self . geofence? . contains ( self . location. coordinate) ?? false {
8686 print ( " LocationManager - geofence exit " )
8787 NotificationCenter . default. post ( name: . exitGeofence, object: nil )
8888 }
89-
89+
9090 self . objectWillChange. send ( )
9191 }
9292 }
@@ -142,7 +142,7 @@ extension LocationManager: CLLocationManagerDelegate {
142142 didUpdateLocations locations: [ CLLocation ]
143143 ) {
144144 guard let location = locations. last else {
145- print ( " 변경된 location이 없습니다 " )
145+ print ( " LocationManager - 변경된 location이 없습니다" )
146146 return
147147 }
148148 self . location = location
@@ -166,7 +166,7 @@ extension LocationManager: CLLocationManagerDelegate {
166166 챌린지 종료 카운트다운 초기화
167167 */
168168 func locationManager( _ manager: CLLocationManager , didEnterRegion region: CLRegion ) {
169- print ( " Enter: \( self . geofence? . center) " )
169+ print ( " LocationManager - Enter: \( self . geofence? . center) " )
170170 NotificationCenter . default. post ( name: . enterGeofence, object: nil )
171171 }
172172
@@ -175,23 +175,28 @@ extension LocationManager: CLLocationManagerDelegate {
175175 - 현재 위치에서 벗어남을 감지하고 n초 후 챌린지 종료 카운트 다운 및 경고
176176 */
177177 func locationManager( _ manager: CLLocationManager , didExitRegion region: CLRegion ) {
178- print ( " Exit: \( self . geofence? . center) " )
178+ print ( " LocationManager - Exit: \( self . geofence? . center) " )
179179 NotificationCenter . default. post ( name: . exitGeofence, object: nil )
180180 }
181181
182182 /** 현재 위치를 지오펜싱으로 영역 처리
183183 */
184+
185+ func resetGeofence( ) {
186+ if let reset_geofence = self . geofence {
187+ scheduleNotification_exit ( region: reset_geofence)
188+ }
189+ }
190+
184191 func setGeofenceMyHome( region: MKCoordinateRegion ) {
185192 let _geofenceExit = CLCircularRegion (
186193 center: region. center,
187194 radius: 100 , // 100m
188195 identifier: " MyHomeRegion1 "
189196 )
190197
191- _geofenceExit. notifyOnExit = true
192- _geofenceExit. notifyOnEntry = true
193198 self . geofence = _geofenceExit
194- scheduleNotification_exit ( region: _geofenceExit)
199+ // scheduleNotification_exit(region: _geofenceExit)
195200// self.locationManager.startMonitoring(for: _geofenceExit)
196201 }
197202}
@@ -201,53 +206,38 @@ extension LocationManager: CLLocationManagerDelegate {
201206extension LocationManager : UNUserNotificationCenterDelegate {
202207 func userNotificationCenter( _ center: UNUserNotificationCenter , openSettingsFor notification: UNNotification ? ) { }
203208
204- func scheduleNotification_enter( region: CLCircularRegion ) {
205- print ( " asd " )
206- let center = UNUserNotificationCenter . current ( )
207-
208- center. removeAllPendingNotificationRequests ( ) // deletes pending scheduled notifications, there is a schedule limit qty
209-
210- let content = UNMutableNotificationContent ( )
211- content. title = " 들어감 "
212- content. body = " Run! This zone is dangerous! :o "
213- content. categoryIdentifier = " alarm "
214- content. sound = UNNotificationSound . default
215-
216- // Ex. Trigger within a timeInterval
217- // let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 5, repeats: false)
218-
219- let trigger = UNLocationNotificationTrigger ( region: region, repeats: true )
220-
221- let request = UNNotificationRequest ( identifier: UUID ( ) . uuidString, content: content, trigger: trigger)
222- self . notificationCenter. add ( request)
223- }
224-
225209 func scheduleNotification_exit( region: CLCircularRegion ) {
226- print ( " asd " )
227210 let center = UNUserNotificationCenter . current ( )
228211
229212 center. removeAllPendingNotificationRequests ( ) // deletes pending scheduled notifications, there is a schedule limit qty
230213
231214 let content = UNMutableNotificationContent ( )
232- content. title = " 나가기 "
233- content. body = " Run! This zone is dangerous! :o "
215+ content. title = " 나갔다 "
216+ content. body = " ????? "
234217 content. categoryIdentifier = " alarm "
235218 content. sound = UNNotificationSound . default
236219
220+ region. notifyOnEntry = false
221+ region. notifyOnExit = true
237222 // Ex. Trigger within a timeInterval
238223 // let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 5, repeats: false)
239224
240- let trigger = UNLocationNotificationTrigger ( region: region, repeats: true )
225+ let trigger = UNLocationNotificationTrigger ( region: region, repeats: false )
241226
242227 let request = UNNotificationRequest ( identifier: UUID ( ) . uuidString, content: content, trigger: trigger)
243- self . notificationCenter . add ( request)
228+ center . add ( request)
244229 }
245230
246231 func userNotificationCenter(
247232 _ center: UNUserNotificationCenter ,
248233 willPresent notification: UNNotification ,
249234 withCompletionHandler completionHandler: @escaping ( UNNotificationPresentationOptions ) -> Void
250235 ) {
251- completionHandler ( [ . list, . badge, . sound] )
236+ if self . challengeType == . running {
237+ print ( " 로케이션 completion \( self . challengeType) " )
238+ completionHandler ( [ . banner, . list, . sound] )
239+ } else {
240+ print ( " 로케이션 \( self . challengeType) " )
241+ }
252242 }
253243}
0 commit comments