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