Commit 5cb58cd4 by Demid Merzlyakov

Location.lastTimeUpdated renamed to lastWeatherUpdateDate and made optional.

parent db349ab7
...@@ -21,7 +21,6 @@ public class LocationManager { ...@@ -21,7 +21,6 @@ public class LocationManager {
private let weatherUpdateSource: WeatherSource private let weatherUpdateSource: WeatherSource
private let defaultLocation = Location(deviceLocation: false, private let defaultLocation = Location(deviceLocation: false,
lastTimeUpdated: Date(),
coordinates: .init(latitude: 37.3230, longitude: -122.0322), // Cupertino coordinates: .init(latitude: 37.3230, longitude: -122.0322), // Cupertino
timeZone: TimeZone(abbreviation: "PST")!) timeZone: TimeZone(abbreviation: "PST")!)
...@@ -93,10 +92,12 @@ public class LocationManager { ...@@ -93,10 +92,12 @@ public class LocationManager {
log.warning("Update weather: no location.") log.warning("Update weather: no location.")
return return
} }
guard Date().timeIntervalSince(location.lastTimeUpdated) >= weatherUpdateSource.weatherUpdateInterval else { if let lastTimeUpdated = location.lastWeatherUpdateDate {
log.info("Update weather: fresh enough (last updated at \(location.lastTimeUpdated)), skip update.") guard Date().timeIntervalSince(lastTimeUpdated) >= weatherUpdateSource.weatherUpdateInterval else {
log.info("Update weather: fresh enough (last updated at \(location.lastWeatherUpdateDate)), skip update.")
return return
} }
}
log.info("Update weather for location: \(location)") log.info("Update weather for location: \(location)")
weatherUpdateSource.updateWeather(for: location) { [weak self] (updatedLocation, error) in weatherUpdateSource.updateWeather(for: location) { [weak self] (updatedLocation, error) in
...@@ -207,7 +208,7 @@ public class LocationManager { ...@@ -207,7 +208,7 @@ public class LocationManager {
log.error("Geo lookup: no coordinates present: \(partialLocation)") log.error("Geo lookup: no coordinates present: \(partialLocation)")
var location: Location? = nil var location: Location? = nil
if partialLocation.deviceLocation { if partialLocation.deviceLocation {
location = Location(deviceLocation: true, lastTimeUpdated: Date(timeIntervalSince1970: 0), timeZone: TimeZone.current) location = Location(deviceLocation: true, timeZone: TimeZone.current)
} }
completion(location) completion(location)
return return
...@@ -236,10 +237,7 @@ public class LocationManager { ...@@ -236,10 +237,7 @@ public class LocationManager {
return return
} }
//TODO: come up with something for the date, or just make it optional (probably makes the most sense) result = Location(deviceLocation: partialLocation.deviceLocation, timeZone: timeZone)
result = Location(deviceLocation: partialLocation.deviceLocation,
lastTimeUpdated: Date(timeIntervalSince1970: 0),
timeZone: timeZone)
result?.coordinates = CLLocationCoordinate2D(latitude: lat, longitude: lon) result?.coordinates = CLLocationCoordinate2D(latitude: lat, longitude: lon)
result?.cityName = partialLocation.cityName ?? placemark.locality result?.cityName = partialLocation.cityName ?? placemark.locality
result?.countryName = partialLocation.countryName ?? placemark.country result?.countryName = partialLocation.countryName ?? placemark.country
......
...@@ -12,7 +12,7 @@ public struct Location { ...@@ -12,7 +12,7 @@ public struct Location {
// MARK: - Data fields // MARK: - Data fields
/// True if this location came from the Device GPS (user's current location). False if it was added in some other way (search, push notification, popular cities, etc.) /// True if this location came from the Device GPS (user's current location). False if it was added in some other way (search, push notification, popular cities, etc.)
public let deviceLocation: Bool public let deviceLocation: Bool
public var lastTimeUpdated: Date public var lastWeatherUpdateDate: Date?
public var coordinates: CLLocationCoordinate2D? public var coordinates: CLLocationCoordinate2D?
public var imageName: String? = "ny_bridge" //we'll possibly need to switch to URL public var imageName: String? = "ny_bridge" //we'll possibly need to switch to URL
...@@ -106,7 +106,7 @@ extension Location: CustomStringConvertible { ...@@ -106,7 +106,7 @@ extension Location: CustomStringConvertible {
extension Location: UpdatableModelObject { extension Location: UpdatableModelObject {
public func mergedWith(incrementalChanges: Location) -> Location { public func mergedWith(incrementalChanges: Location) -> Location {
var result = self var result = self
result.lastTimeUpdated = incrementalChanges.lastTimeUpdated result.lastWeatherUpdateDate = incrementalChanges.lastWeatherUpdateDate
// The preference is given to self values rather than incrementalChanges in this class // The preference is given to self values rather than incrementalChanges in this class
result.coordinates = result.coordinates ?? incrementalChanges.coordinates result.coordinates = result.coordinates ?? incrementalChanges.coordinates
result.imageName = result.imageName ?? incrementalChanges.imageName result.imageName = result.imageName ?? incrementalChanges.imageName
......
...@@ -51,7 +51,7 @@ struct WdtLocation: Codable { ...@@ -51,7 +51,7 @@ struct WdtLocation: Codable {
} }
return Location(deviceLocation: false, return Location(deviceLocation: false,
lastTimeUpdated: updatedAt, lastWeatherUpdateDate: updatedAt,
coordinates: coordinates, coordinates: coordinates,
imageName: nil, imageName: nil,
countryCode: nil, countryCode: nil,
......
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