Skip to content

Commit d3b0e30

Browse files
committed
replace notification center with delegate for better readability
1 parent 99c5f1e commit d3b0e30

File tree

2 files changed

+7
-3
lines changed

2 files changed

+7
-3
lines changed

GoInfoGame/GoInfoGame/LocationManagerDelegate.swift

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@ import MapKit
1010

1111
class LocationManagerDelegate: NSObject, ObservableObject, CLLocationManagerDelegate {
1212

13-
var locationManager = CLLocationManager()
13+
var locationManager = CLLocationManager()
1414
@Published var location: CLLocation?
15+
var locationUpdateHandler: ((CLLocation) -> Void)?
1516

1617
override init() {
1718
super.init()
@@ -56,7 +57,7 @@ class LocationManagerDelegate: NSObject, ObservableObject, CLLocationManagerDele
5657
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
5758
guard let mostRecentLocation = locations.last else { return }
5859
location = mostRecentLocation
59-
NotificationCenter.default.post(name: Notification.Name("userLocationDidChange"), object: nil)
60+
locationUpdateHandler?(mostRecentLocation)
6061
stopUpdatingLocation()
6162
}
6263
}

GoInfoGame/GoInfoGame/UI/Map/MapViewModel.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,10 @@ class MapViewModel: ObservableObject {
2828
locationManagerDelegate.locationManager.requestWhenInUseAuthorization()
2929
locationManagerDelegate.locationManager.startUpdatingLocation()
3030

31-
NotificationCenter.default.addObserver(self, selector: #selector(locationDidChange), name: Notification.Name("userLocationDidChange"), object: nil)
31+
locationManagerDelegate.locationUpdateHandler = { [weak self] location in
32+
guard let self = self else { return }
33+
fetchOSMDataFor(currentLocation: location)
34+
}
3235

3336
}
3437

0 commit comments

Comments
 (0)