Skip to content

Commit e5f9490

Browse files
committed
A little refactoring
1 parent db93974 commit e5f9490

File tree

1 file changed

+22
-18
lines changed

1 file changed

+22
-18
lines changed

GeofenceTester/RegionsListViewController.swift

+22-18
Original file line numberDiff line numberDiff line change
@@ -63,24 +63,14 @@ class RegionsListViewController: UIViewController {
6363

6464
let center = self.mapView.userLocation.coordinate
6565

66-
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound]) { granted, error in
66+
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound]) { [self] granted, error in
6767

6868
if let error = error {
69-
self.logger.log(level: .error, "Error in requestAuthorization: \(error.localizedDescription)")
69+
self.handleError("Error in requestAuthorization: \(error.localizedDescription)")
7070
return
7171
}
7272

73-
guard CLLocationManager.isMonitoringAvailable(for: CLCircularRegion.self) else {
74-
return
75-
}
76-
// Register the region.
77-
let region = CLCircularRegion(center: center,
78-
radius: 10,
79-
identifier: identifier)
80-
region.notifyOnEntry = true
81-
region.notifyOnExit = true
82-
83-
self.locationManager.startMonitoring(for: region)
73+
self.registerRegion(identifier: identifier, center: center)
8474
DispatchQueue.main.async {
8575
self.updateMap()
8676
}
@@ -90,7 +80,22 @@ class RegionsListViewController: UIViewController {
9080
self.present(alertController, animated: true)
9181
}
9282

93-
func updateMap() {
83+
private func registerRegion(identifier: String, center: CLLocationCoordinate2D) {
84+
guard CLLocationManager.isMonitoringAvailable(for: CLCircularRegion.self) else {
85+
self.handleError("Circular Region Monitoring not available")
86+
return
87+
}
88+
// Register the region.
89+
let region = CLCircularRegion(center: center,
90+
radius: 10,
91+
identifier: identifier)
92+
region.notifyOnEntry = true
93+
region.notifyOnExit = true
94+
95+
self.locationManager.startMonitoring(for: region)
96+
}
97+
98+
private func updateMap() {
9499
guard mapView != nil else {
95100
return
96101
}
@@ -116,12 +121,12 @@ class RegionsListViewController: UIViewController {
116121
mapView.showAnnotations(mapAnnotations, animated: true)
117122
}
118123

119-
func handleError (_ error: Error) {
124+
private func handleError (_ error: Error) {
120125
handleError(error.localizedDescription)
121126
}
122127

123-
func handleError (_ error: String) {
124-
print (error)
128+
private func handleError (_ error: String) {
129+
logger.error("\(error)")
125130
}
126131

127132
// MARK: - Navigation
@@ -180,7 +185,6 @@ extension RegionsListViewController: MKMapViewDelegate {
180185
calloutAccessoryControlTapped control: UIControl){
181186
self.performSegue(withIdentifier: "RegionDetail", sender: view.annotation)
182187
}
183-
184188
}
185189

186190
// MARK: - CLLocationManagerDelegate

0 commit comments

Comments
 (0)