Commit 7c4248a5 by Demid Merzlyakov

Legacy migration: migrate old locations without reverse geocoding when possible.

parent 45366f35
...@@ -63,13 +63,9 @@ internal class DeviceLocationMonitor: NSObject { ...@@ -63,13 +63,9 @@ internal class DeviceLocationMonitor: NSObject {
} }
public struct DeviceLocation: PartialLocation { public struct DeviceLocation: PartialLocation {
public var nickname: String? { public var countryCode: String?
return nil public let nickname: String? = nil
} public let selected: Bool? = nil
public var selected: Bool? {
return nil
}
private static let coordinatesStringAccuracy = 6 // 6 digits after the dot private static let coordinatesStringAccuracy = 6 // 6 digits after the dot
private let location: CLLocation? private let location: CLLocation?
...@@ -77,9 +73,7 @@ internal class DeviceLocationMonitor: NSObject { ...@@ -77,9 +73,7 @@ internal class DeviceLocationMonitor: NSObject {
self.location = location self.location = location
} }
public var deviceLocation: Bool { public let deviceLocation: Bool = true
return true
}
public var lat: String? { public var lat: String? {
guard let coordinate = location?.coordinate else { guard let coordinate = location?.coordinate else {
...@@ -95,30 +89,16 @@ internal class DeviceLocationMonitor: NSObject { ...@@ -95,30 +89,16 @@ internal class DeviceLocationMonitor: NSObject {
return String(format: "%.\(DeviceLocation.coordinatesStringAccuracy)f", coordinate.longitude) return String(format: "%.\(DeviceLocation.coordinatesStringAccuracy)f", coordinate.longitude)
} }
public var countryName: String? { public let countryName: String? = nil
nil public let region: String? = nil
} public let cityName: String? = nil
public var region: String? {
nil
}
public var cityName: String? {
nil
}
public var nameForDisplay: String { public var nameForDisplay: String {
// this should never be called. // this should never be called.
return "" return ""
} }
public let fipsCode: String? = nil
public var fipsCode: String? { public let optionalCityId: String? = nil
nil public let timeZoneIdentifier: String? = nil
}
public var optionalCityId: String? {
return nil
}
} }
public typealias CurrentLocationCompletion = (LocationRequestResult) -> () public typealias CurrentLocationCompletion = (LocationRequestResult) -> ()
......
...@@ -482,13 +482,14 @@ public class LocationManager { ...@@ -482,13 +482,14 @@ public class LocationManager {
public func addIfNeeded(partialLocation: PartialLocation, selectLocation: Bool, completion: @escaping (Bool) -> ()) { public func addIfNeeded(partialLocation: PartialLocation, selectLocation: Bool, completion: @escaping (Bool) -> ()) {
if let location = partialLocation as? Location { if let location = partialLocation as? Location {
addIfNeeded(location: location, selectLocation: selectLocation) addIfNeeded(location: location, selectLocation: selectLocation)
completion(true)
} }
else { else {
makeLocation(from: partialLocation) { (location) in makeLocation(from: partialLocation) { (location) in
onMain { [weak self] in onMain { [weak self] in
if let location = location { if let location = location {
completion(true)
self?.addIfNeeded(location: location, selectLocation: selectLocation) self?.addIfNeeded(location: location, selectLocation: selectLocation)
completion(true)
} }
else { else {
completion(false) completion(false)
...@@ -572,6 +573,24 @@ public class LocationManager { ...@@ -572,6 +573,24 @@ public class LocationManager {
return return
} }
let coordinates = CLLocationCoordinate2D(latitude: lat, longitude: lon)
if let tzId = partialLocation.timeZoneIdentifier,
let timeZone = TimeZone(identifier: tzId),
partialLocation.cityName != nil || partialLocation.countryName != nil || partialLocation.region != nil {
var location = Location(deviceLocation: partialLocation.deviceLocation, timeZone: timeZone)
location.cityName = partialLocation.cityName
location.countryName = partialLocation.countryName
location.countryCode = partialLocation.countryCode
location.fipsCode = partialLocation.fipsCode
location.coordinates = coordinates
location.region = partialLocation.region
location.nickname = partialLocation.nickname
log.debug("Geo lookup: migrated legacy location without geocoding: \(location)")
completion(location)
return
}
let location = CLLocation(latitude: lat, longitude: lon) let location = CLLocation(latitude: lat, longitude: lon)
let geocodeCompletion: CLGeocodeCompletionHandler = { [weak self] (placemarks, error) in let geocodeCompletion: CLGeocodeCompletionHandler = { [weak self] (placemarks, error) in
...@@ -597,7 +616,7 @@ public class LocationManager { ...@@ -597,7 +616,7 @@ public class LocationManager {
} }
result = Location(deviceLocation: partialLocation.deviceLocation, timeZone: timeZone) result = Location(deviceLocation: partialLocation.deviceLocation, timeZone: timeZone)
result?.coordinates = CLLocationCoordinate2D(latitude: lat, longitude: lon) result?.coordinates = coordinates
result?.cityName = partialLocation.cityName ?? placemark.locality result?.cityName = partialLocation.cityName ?? placemark.locality
result?.countryName = partialLocation.countryName ?? placemark.country result?.countryName = partialLocation.countryName ?? placemark.country
result?.countryCode = placemark.isoCountryCode result?.countryCode = placemark.isoCountryCode
......
...@@ -104,7 +104,6 @@ extension GeoNamesPlace: PartialLocation { ...@@ -104,7 +104,6 @@ extension GeoNamesPlace: PartialLocation {
return nil return nil
} }
var nameForDisplay: String { var nameForDisplay: String {
//TODO: refactor this //TODO: refactor this
var sb = String() var sb = String()
...@@ -162,4 +161,8 @@ extension GeoNamesPlace: PartialLocation { ...@@ -162,4 +161,8 @@ extension GeoNamesPlace: PartialLocation {
var cityName: String? { var cityName: String? {
return city return city
} }
var timeZoneIdentifier: String? {
return nil
}
} }
...@@ -230,6 +230,10 @@ extension Location: PartialLocation { ...@@ -230,6 +230,10 @@ extension Location: PartialLocation {
} }
} }
public var timeZoneIdentifier: String? {
timeZone.identifier
}
public var optionalCityId: String? { public var optionalCityId: String? {
cityId cityId
} }
......
...@@ -13,12 +13,14 @@ public protocol PartialLocation { ...@@ -13,12 +13,14 @@ public protocol PartialLocation {
var lat: String? { get} var lat: String? { get}
var lon: String? { get } var lon: String? { get }
var countryName: String? { get } var countryName: String? { get }
var countryCode: String? { get }
var region: String? { get } var region: String? { get }
var cityName: String? { get } var cityName: String? { get }
var fipsCode: String? { get } var fipsCode: String? { get }
var optionalCityId: String? { get } var optionalCityId: String? { get }
var nickname: String? { get } var nickname: String? { get }
var selected: Bool? { get } var selected: Bool? { get }
var timeZoneIdentifier: String? { get }
var nameForDisplay: String { get } var nameForDisplay: String { get }
} }
...@@ -41,12 +41,14 @@ class LegacyMigrationManager { ...@@ -41,12 +41,14 @@ class LegacyMigrationManager {
var countryName: String? var countryName: String?
var region: String? var region: String?
var cityName: String? var cityName: String?
var countryCode: String?
var fipsCode: String? var fipsCode: String?
let optionalCityId: String? = nil let optionalCityId: String? = nil
var nickname: String? var nickname: String?
var nameForDisplay: String { var nameForDisplay: String {
"\(countryName ?? ""):\(region ?? ""):\(cityName ?? "") (\(nickname ?? "")) – @\(lat ?? "");\(lon ?? ""); isDevice: \(deviceLocation)" "\(countryName ?? ""):\(region ?? ""):\(cityName ?? "") (\(nickname ?? "")) – @\(lat ?? "");\(lon ?? ""); isDevice: \(deviceLocation)"
} }
var timeZoneIdentifier: String?
} }
private var lastMigrationResult: MigrationResult? { private var lastMigrationResult: MigrationResult? {
...@@ -132,10 +134,12 @@ class LegacyMigrationManager { ...@@ -132,10 +134,12 @@ class LegacyMigrationManager {
converted.lat = String(format: "%.8f", legacyLocation.geoPointLat) converted.lat = String(format: "%.8f", legacyLocation.geoPointLat)
converted.lon = String(format: "%.8f", legacyLocation.geoPointLong) converted.lon = String(format: "%.8f", legacyLocation.geoPointLong)
converted.countryName = legacyLocation.countryName converted.countryName = legacyLocation.countryName
converted.countryCode = legacyLocation.country
converted.region = legacyLocation.region converted.region = legacyLocation.region
converted.cityName = legacyLocation.city converted.cityName = legacyLocation.city
converted.fipsCode = legacyLocation.fips converted.fipsCode = legacyLocation.fips
converted.selected = legacyLocation.selectedLocation converted.selected = legacyLocation.selectedLocation
converted.timeZoneIdentifier = legacyLocation.timeZone
result.append(converted) result.append(converted)
} }
self.lastMigrationResult = .success self.lastMigrationResult = .success
......
...@@ -21,6 +21,7 @@ class LegacyWdtLocation: NSObject, NSCoding { ...@@ -21,6 +21,7 @@ class LegacyWdtLocation: NSObject, NSCoding {
var geoPointLat: Double = -1 var geoPointLat: Double = -1
var geoPointLong: Double = -1 var geoPointLong: Double = -1
var fips: String? var fips: String?
var timeZone: String?
class var documentsFolder: String { class var documentsFolder: String {
...@@ -55,6 +56,7 @@ class LegacyWdtLocation: NSObject, NSCoding { ...@@ -55,6 +56,7 @@ class LegacyWdtLocation: NSObject, NSCoding {
self.myLocation = decoder.decodeBool(forKey: "myLocation") self.myLocation = decoder.decodeBool(forKey: "myLocation")
self.selectedLocation = decoder.decodeBool(forKey: "selectedLocation") self.selectedLocation = decoder.decodeBool(forKey: "selectedLocation")
self.fips = decoder.decodeObject(forKey: "fips") as? String self.fips = decoder.decodeObject(forKey: "fips") as? String
self.timeZone = decoder.decodeObject(forKey: "timezone") as? String
} }
func encode(with coder: NSCoder) { func encode(with coder: NSCoder) {
...@@ -70,5 +72,6 @@ class LegacyWdtLocation: NSObject, NSCoding { ...@@ -70,5 +72,6 @@ class LegacyWdtLocation: NSObject, NSCoding {
coder.encode(self.myLocation, forKey: "myLocation") coder.encode(self.myLocation, forKey: "myLocation")
coder.encode(self.selectedLocation, forKey: "selectedLocation") coder.encode(self.selectedLocation, forKey: "selectedLocation")
coder.encode(self.fips, forKey: "fips") coder.encode(self.fips, forKey: "fips")
coder.encode(self.timeZone, forKey: "timezone")
} }
} }
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