Commit 5cb58cd4 by Demid Merzlyakov

Location.lastTimeUpdated renamed to lastWeatherUpdateDate and made optional.

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