Расположение ядра в MVP - PullRequest
0 голосов
/ 22 марта 2019

В моем проекте у меня есть класс LocationService , который соответствует протоколу CLLocationManagerDelegate для определения местоположения текущего пользователя.

class LocationService: NSObject, CLLocationManagerDelegate {

    fileprivate let locationManager = CLLocationManager()
    var location: Location?  // Location(lat: Double, lon: Double)

    override init() {
        super.init()

        locationManager.delegate = self
        locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters

    }

    func getCurrentLocation() -> Location? {
        locationManager.startUpdatingLocation()
         // how can I catch a location?
        return location
    }


    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        if let location = locations.last {
            if location.horizontalAccuracy > 0 {
                locationManager.stopUpdatingLocation()
                self.location = Location(lat: location.coordinate.latitude, lon: location.coordinate.latitude)
            }
        }
    }

    func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
        print(error.localizedDescription)
    }

}

Я хочу, чтобы мой WeatherPresenter запускал обновления местоположения в LocationService и получал результат, как только местоположение было найдено.Есть ли способ сделать это?

class WeatherPresenter {

    unowned let delegate: WeatherViewDelegate
    let weatherService = WeatherService()
    let locationService = LocationService()

    init(with delegate: WeatherViewDelegate) {
        self.delegate = delegate
    }

    func getWeatherForCurrentLocation() {
        if let location = locationService.getCurrentLocation() {
            //...
        }
    }
}

1 Ответ

1 голос
/ 22 марта 2019

Вы можете использовать Делегат для уведомления WeatherPresenter об изменениях с LocationService

protocol LocationServiceDelegate: class { // Delegate protocol
    func didUpdateLocation()
}

class LocationService: NSObject, CLLocationManagerDelegate {

    weak var delegate: LocationServiceDelegate?

    fileprivate let locationManager = CLLocationManager()
    var location: Location?  // Location(lat: Double, lon: Double)

    override init() {
        super.init()

        locationManager.delegate = self
        locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters

    }

    func startUpdatingLocation() { // Start updating called from presenter
        locationManager.startUpdatingLocation()
    }

    func getCurrentLocation() -> Location? {
        // how can I catch a location?
        return location
    }


    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        if let location = locations.last {
            if location.horizontalAccuracy > 0 {
                locationManager.stopUpdatingLocation()
                self.location = Location(lat: location.coordinate.latitude, lon: location.coordinate.latitude)
                self.delegate?.didUpdateLocation() // Notify delegate on change
            }
        }
    }

    func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
        print(error.localizedDescription)
    }

}


class WeatherPresenter: LocationServiceDelegate {
    unowned let delegate: WeatherViewDelegate
    let weatherService = WeatherService()
    let locationService = LocationService()

    init(with delegate: WeatherViewDelegate) {
        self.delegate = delegate
        self.locationService.delegate = self // Set self as delegate
        self.locationService.startUpdatingLocation()  // Requests start updating location
    }

    func didUpdateLocation() { // This will be called on location change
        self.getWeatherForCurrentLocation()
    }

    func getWeatherForCurrentLocation() {
        if let location = locationService.getCurrentLocation() {
            //...
        }
    }
}
...