Commit 45366f35 by Demid Merzlyakov

Error handling for failed reverse geocoding.

parent 1b2e8abb
......@@ -92,7 +92,11 @@ class DeeplinksRouter {
log.debug("Parsed path: \(parsedComponent.rawValue)")
if let location = parseLocation(from: url) {
log.debug("Location found: \(location)")
locationManager.addIfNeeded(partialLocation: location, selectLocation: true)
locationManager.addIfNeeded(partialLocation: location, selectLocation: true) { success in
if !success {
self.log.error("Failed to add / switch the location")
}
}
}
else {
log.debug("No location.")
......
......@@ -227,7 +227,11 @@ public class LocationManager {
if self.deviceLocationMonitor.hasLocationPermissions {
if locations.first?.deviceLocation != true {
if let lastKnownLocation = self.deviceLocationMonitor.lastKnownLocation {
self.addIfNeeded(partialLocation: lastKnownLocation, selectLocation: false)
self.addIfNeeded(partialLocation: lastKnownLocation, selectLocation: false) { success in
if !success {
self.log.error("Failed to add / switch the location")
}
}
}
}
}
......@@ -252,7 +256,11 @@ public class LocationManager {
}
for location: PartialLocation in legacyLocations {
self.addIfNeeded(partialLocation: location, selectLocation: location.selected ?? false)
self.addIfNeeded(partialLocation: location, selectLocation: location.selected ?? false) { success in
if !success {
self.log.error("Failed to migrate location: \(location.nameForDisplay)")
}
}
}
self.loadedLocations = true
......@@ -471,7 +479,7 @@ public class LocationManager {
updateEverythingIfNeeded()
}
public func addIfNeeded(partialLocation: PartialLocation, selectLocation: Bool) {
public func addIfNeeded(partialLocation: PartialLocation, selectLocation: Bool, completion: @escaping (Bool) -> ()) {
if let location = partialLocation as? Location {
addIfNeeded(location: location, selectLocation: selectLocation)
}
......@@ -479,8 +487,12 @@ public class LocationManager {
makeLocation(from: partialLocation) { (location) in
onMain { [weak self] in
if let location = location {
completion(true)
self?.addIfNeeded(location: location, selectLocation: selectLocation)
}
else {
completion(false)
}
}
}
}
......@@ -513,7 +525,18 @@ public class LocationManager {
deviceLocationMonitor.useCurrentLocation(requestedFrom: viewController) { [weak self] (result) in
switch result {
case .success(let location):
self?.addIfNeeded(partialLocation: location, selectLocation: true)
self?.addIfNeeded(partialLocation: location, selectLocation: true) { success in
if !success {
self?.log.error("Failed to add / switch the location to the current one. Will try again in a second")
DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
self?.addIfNeeded(partialLocation: location, selectLocation: true, completion: { success in
if !success {
self?.log.error("Still failed to add the current location.")
}
})
}
}
}
completion(.success)
case .denied:
completion(.denied)
......@@ -580,6 +603,7 @@ public class LocationManager {
result?.countryCode = placemark.isoCountryCode
result?.region = partialLocation.region ?? placemark.administrativeArea
result?.zip = placemark.postalCode
result?.nickname = partialLocation.nickname
self.log.info("Geo lookup got location: \(result?.description ?? "-")")
}
......@@ -598,7 +622,11 @@ public class LocationManager {
extension LocationManager: DeviceLocationMonitorDelegate {
func deviceLocationMonitor(_ monitor: DeviceLocationMonitor, didUpdateLocation newLocation: PartialLocation) {
log.debug("device location updated: (\(newLocation.lat ?? "-"); \(newLocation.lon ?? "-"))")
addIfNeeded(partialLocation: newLocation, selectLocation: false)
addIfNeeded(partialLocation: newLocation, selectLocation: false) { success in
if !success {
self.log.error("Failed to update the current location.")
}
}
}
func deviceLocationMonitor(_ monitor: DeviceLocationMonitor, authorizationStatusChanged locationIsAllowed: Bool) {
......
......@@ -134,7 +134,11 @@ extension PushNotificationsManager: UNUserNotificationCenterDelegate {
newLoc.longitude = lon
newLoc.optionalCityId = cityId
newLoc.fipsCode = fipsCode
LocationManager.shared.addIfNeeded(partialLocation: newLoc, selectLocation: true)
LocationManager.shared.addIfNeeded(partialLocation: newLoc, selectLocation: true) { success in
if !success {
self.log.error("Failed to add / switch the location")
}
}
log.info("MoEngage push: location found: \(newLoc)")
}
......
......@@ -138,6 +138,8 @@
"search.error.maxLocationWarning" = "To keep your 1Weather running in tip-top shape, please limit the number of locations to %@";
"search.error.deleteError" = "Failed to delete location.";
"search.accessibility.tapToSelectLocation" = "Tap to select this location";
"search.error.title" = "Error";
"search.error.addError.message" = "An error occurred while adding the location. Please, make sure you have an internet connection and try again.";
//Location
"location.servicesAreTurnedOff" = "System location services are turned off.";
......
......@@ -44,7 +44,9 @@ class LegacyMigrationManager {
var fipsCode: String?
let optionalCityId: String? = nil
var nickname: String?
let nameForDisplay: String = ""
var nameForDisplay: String {
"\(countryName ?? ""):\(region ?? ""):\(cityName ?? "") (\(nickname ?? "")) – @\(lat ?? "");\(lon ?? ""); isDevice: \(deviceLocation)"
}
}
private var lastMigrationResult: MigrationResult? {
......
......@@ -197,8 +197,15 @@ public class LocationsViewModel {
self.delegate?.viewModelError(model: self, title: nil, message: "search.error.maxLocationWarning".localized())
return
}
locationManager.addIfNeeded(partialLocation: city, selectLocation: true)
self.delegate?.viewModelDidSelectCity(model: self)
locationManager.addIfNeeded(partialLocation: city, selectLocation: true) { success in
if success {
self.delegate?.viewModelDidSelectCity(model: self)
}
else {
self.delegate?.viewModelError(model: self, title: "search.error.title".localized(), message: "search.error.addError.message".localized())
}
}
}
func delete(city: PartialLocation) {
......@@ -207,7 +214,7 @@ public class LocationsViewModel {
return
}
if !locationManager.remove(location: location) {
self.delegate?.viewModelError(model: self, title: "Error", message: "search.error.deleteError".localized())
self.delegate?.viewModelError(model: self, title: "search.error.title".localized(), message: "search.error.deleteError".localized())
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment