Commit ba2e8e60 by Demid Merzlyakov

Minor model changes.

parent 3ed817e8
......@@ -74,6 +74,8 @@
CEDE4E8425EEFD56007457E9 /* WdtDailySummariesArray.swift in Sources */ = {isa = PBXBuildFile; fileRef = CEDE4E8025EEFD56007457E9 /* WdtDailySummariesArray.swift */; };
CEDE4E8525EEFD56007457E9 /* WdtHourlySummariesArray.swift in Sources */ = {isa = PBXBuildFile; fileRef = CEDE4E8125EEFD56007457E9 /* WdtHourlySummariesArray.swift */; };
CEDE4E8925EEFFEF007457E9 /* WdtDayNight.swift in Sources */ = {isa = PBXBuildFile; fileRef = CEDE4E8825EEFFEF007457E9 /* WdtDayNight.swift */; };
CEDE4F0B25EFA3A7007457E9 /* UpdatableModelObject.swift in Sources */ = {isa = PBXBuildFile; fileRef = CEDE4F0A25EFA3A7007457E9 /* UpdatableModelObject.swift */; };
CEDE4F0F25EFA3B4007457E9 /* UpdatableModelObjectInTime.swift in Sources */ = {isa = PBXBuildFile; fileRef = CEDE4F0E25EFA3B4007457E9 /* UpdatableModelObjectInTime.swift */; };
/* End PBXBuildFile section */
/* Begin PBXFileReference section */
......@@ -148,6 +150,8 @@
CEDE4E8025EEFD56007457E9 /* WdtDailySummariesArray.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = WdtDailySummariesArray.swift; sourceTree = "<group>"; };
CEDE4E8125EEFD56007457E9 /* WdtHourlySummariesArray.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = WdtHourlySummariesArray.swift; sourceTree = "<group>"; };
CEDE4E8825EEFFEF007457E9 /* WdtDayNight.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WdtDayNight.swift; sourceTree = "<group>"; };
CEDE4F0A25EFA3A7007457E9 /* UpdatableModelObject.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UpdatableModelObject.swift; sourceTree = "<group>"; };
CEDE4F0E25EFA3B4007457E9 /* UpdatableModelObjectInTime.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UpdatableModelObjectInTime.swift; sourceTree = "<group>"; };
DF826CF4702D9DCCB9A9DD71 /* Pods-1Weather.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-1Weather.release.xcconfig"; path = "Target Support Files/Pods-1Weather/Pods-1Weather.release.xcconfig"; sourceTree = "<group>"; };
/* End PBXFileReference section */
......@@ -421,6 +425,8 @@
CEAFF08125DFC66F00DF4EBF /* Model */ = {
isa = PBXGroup;
children = (
CEAFF09125DFC71D00DF4EBF /* HelperTypes.swift */,
CEDE4F0925EFA376007457E9 /* Protocols */,
CEAFF0A025E0FEF100DF4EBF /* ModelObjects */,
CEAFF0A225E0FF0800DF4EBF /* LocationManager.swift */,
);
......@@ -456,7 +462,6 @@
CEAFF08825DFC6B200DF4EBF /* CurrentWeather.swift */,
CEAFF08B25DFC6BC00DF4EBF /* DailyWeather.swift */,
CEAFF08E25DFC6ED00DF4EBF /* HourlyWeather.swift */,
CEAFF09125DFC71D00DF4EBF /* HelperTypes.swift */,
);
path = ModelObjects;
sourceTree = "<group>";
......@@ -473,6 +478,15 @@
path = HelperObjects;
sourceTree = "<group>";
};
CEDE4F0925EFA376007457E9 /* Protocols */ = {
isa = PBXGroup;
children = (
CEDE4F0A25EFA3A7007457E9 /* UpdatableModelObject.swift */,
CEDE4F0E25EFA3B4007457E9 /* UpdatableModelObjectInTime.swift */,
);
path = Protocols;
sourceTree = "<group>";
};
DBFD169AA2AA6A3CB5B68BB5 /* Frameworks */ = {
isa = PBXGroup;
children = (
......@@ -602,6 +616,7 @@
CEAFF08925DFC6B200DF4EBF /* CurrentWeather.swift in Sources */,
CEC5270325E7BB4000DA58A5 /* WdtSurfaceObservation.swift in Sources */,
CEAFF08C25DFC6BD00DF4EBF /* DailyWeather.swift in Sources */,
CEDE4F0B25EFA3A7007457E9 /* UpdatableModelObject.swift in Sources */,
CDD0F1E82572429E00CF5017 /* AppFont.swift in Sources */,
CDC6124F25E7964700188DA7 /* CityDayTimesCell.swift in Sources */,
CD17C5FB25D15B6B00EE884E /* AppCoordinator.swift in Sources */,
......@@ -648,6 +663,7 @@
CDC6126625E9085600188DA7 /* GraphLine.swift in Sources */,
CD86246525E66E8A0097F3FB /* CityPrecipCell.swift in Sources */,
CD80917B2578E4A8003541A4 /* UIViewController+Alert.swift in Sources */,
CEDE4F0F25EFA3B4007457E9 /* UpdatableModelObjectInTime.swift in Sources */,
CD17C5F625D15B4400EE884E /* TodayViewController.swift in Sources */,
CD86245E25E646350097F3FB /* SunUvView.swift in Sources */,
CEAFF08325DFC67F00DF4EBF /* Location.swift in Sources */,
......
......@@ -28,9 +28,45 @@ public struct CurrentWeather {
public var sunrise: Time?
public var sunset: Time?
public var sunState: CelestialState = .normal
public var sunState: CelestialState? = .normal
public var moonrise: Time?
public var moonset: Time?
public var moonState: CelestialState = .normal
public var moonPhase: MoonPhase = .unknown
public var moonState: CelestialState? = .normal
public var moonPhase: MoonPhase? = .unknown
}
extension CurrentWeather: UpdatableModelObjectInTime {
public func mergedWith(incrementalChanges: CurrentWeather) -> CurrentWeather {
var result = self
result.date = incrementalChanges.date
result.timeZone = incrementalChanges.timeZone
result.weekDay = incrementalChanges.weekDay
result.type = incrementalChanges.type
result.isDay = incrementalChanges.isDay
// Since this class is meant to contain frequently updated info, the preference is given to incrementalChanges data.
result.minTemp = incrementalChanges.minTemp ?? result.minTemp
result.maxTemp = incrementalChanges.maxTemp ?? result.maxTemp
result.windSpeed = incrementalChanges.windSpeed ?? result.windSpeed
result.windDirection = incrementalChanges.windDirection ?? result.windDirection
result.precipitationProbability = incrementalChanges.precipitationProbability ?? result.precipitationProbability
result.temp = incrementalChanges.temp ?? result.temp
result.apparentTemp = incrementalChanges.apparentTemp ?? result.apparentTemp
result.humidity = incrementalChanges.humidity ?? result.humidity
result.visibility = incrementalChanges.visibility ?? result.visibility
result.pressure = incrementalChanges.pressure ?? result.pressure
result.sunrise = incrementalChanges.sunrise ?? result.sunrise
result.sunset = incrementalChanges.sunset ?? result.sunset
result.sunState = incrementalChanges.sunState ?? result.sunState
result.moonrise = incrementalChanges.moonrise ?? result.moonrise
result.moonset = incrementalChanges.moonset ?? result.moonset
result.moonState = incrementalChanges.moonState ?? result.moonState
result.moonPhase = incrementalChanges.moonPhase ?? result.moonPhase
return result
}
}
......@@ -19,11 +19,38 @@ public struct DailyWeather {
public var windDirection: WindDirection?
public var precipitationProbability: Percent?
public var sunrise: Date
public var sunset: Date
public var sunState: CelestialState = .normal
public var moonrise: Date
public var moonset: Date
public var moonState: CelestialState = .normal
public var moonPhase: MoonPhase = .unknown
public var sunrise: Date?
public var sunset: Date?
public var sunState: CelestialState? = .normal
public var moonrise: Date?
public var moonset: Date?
public var moonState: CelestialState? = .normal
public var moonPhase: MoonPhase? = .unknown
}
extension DailyWeather: UpdatableModelObjectInTime {
public func mergedWith(incrementalChanges: DailyWeather) -> DailyWeather {
var result = self
result.date = incrementalChanges.date
result.timeZone = incrementalChanges.timeZone
result.weekDay = incrementalChanges.weekDay
result.type = incrementalChanges.type
// Since this class is meant to contain frequently updated info, the preference is given to incrementalChanges data.
result.minTemp = incrementalChanges.minTemp ?? result.minTemp
result.maxTemp = incrementalChanges.maxTemp ?? result.maxTemp
result.windSpeed = incrementalChanges.windSpeed ?? result.windSpeed
result.windDirection = incrementalChanges.windDirection ?? result.windDirection
result.precipitationProbability = incrementalChanges.precipitationProbability ?? result.precipitationProbability
result.sunrise = incrementalChanges.sunrise ?? result.sunrise
result.sunset = incrementalChanges.sunset ?? result.sunset
result.sunState = incrementalChanges.sunState ?? result.sunState
result.moonrise = incrementalChanges.moonrise ?? result.moonrise
result.moonset = incrementalChanges.moonset ?? result.moonset
result.moonState = incrementalChanges.moonState ?? result.moonState
result.moonPhase = incrementalChanges.moonPhase ?? result.moonPhase
return result
}
}
......@@ -23,3 +23,24 @@ public struct HourlyWeather {
public var humidity: Percent?
}
extension HourlyWeather: UpdatableModelObjectInTime {
public func mergedWith(incrementalChanges: HourlyWeather) -> HourlyWeather {
var result = self
result.date = incrementalChanges.date
result.timeZone = incrementalChanges.timeZone
result.weekDay = incrementalChanges.weekDay
result.type = incrementalChanges.type
result.isDay = incrementalChanges.isDay
// Since this class is meant to contain frequently updated info, the preference is given to incrementalChanges data.
result.temp = incrementalChanges.temp ?? result.temp
result.apparentTemp = incrementalChanges.apparentTemp ?? result.apparentTemp
result.windSpeed = incrementalChanges.windSpeed ?? result.windSpeed
result.windDirection = incrementalChanges.windDirection ?? result.windDirection
result.precipitationProbability = incrementalChanges.precipitationProbability ?? result.precipitationProbability
result.humidity = incrementalChanges.humidity ?? result.humidity
return result
}
}
......@@ -11,7 +11,7 @@ import CoreLocation
public struct Location: CustomStringConvertible, Equatable {
// MARK: - Data fields
public var coordinates: CLLocationCoordinate2D?
public var imageName = "ny_bridge" //we'll possibly need to switch to URL
public var imageName: String? = "ny_bridge" //we'll possibly need to switch to URL
public var countryCode: String? {
didSet {
......@@ -72,3 +72,26 @@ public struct Location: CustomStringConvertible, Equatable {
#endif
}
}
// MARK: - UpdatableModelObject implementation
extension Location: UpdatableModelObject {
public func mergedWith(incrementalChanges: Location) -> Location {
var result = self
// The preferense is given to self values rather than incrementalChanges in this class
result.coordinates = result.coordinates ?? incrementalChanges.coordinates
result.imageName = result.imageName ?? incrementalChanges.imageName
result.countryCode = result.countryCode ?? incrementalChanges.countryCode
result.countryName = result.countryName ?? incrementalChanges.countryName
result.region = result.region ?? incrementalChanges.region
result.cityName = result.cityName ?? incrementalChanges.cityName
result.nickname = result.nickname ?? incrementalChanges.nickname
result.zip = result.zip ?? incrementalChanges.zip
result.fipsCode = result.fipsCode ?? incrementalChanges.fipsCode
result.today = result.today?.mergedWith(incrementalChanges: incrementalChanges.today) ?? incrementalChanges.today
result.daily = result.daily.mergedWith(incrementalChanges: incrementalChanges.daily)
result.hourly = result.hourly.mergedWith(incrementalChanges: incrementalChanges.hourly)
return result
}
}
//
// UpdatableModelObject.swift
// 1Weather
//
// Created by Demid Merzlyakov on 03.03.2021.
//
import Foundation
public protocol UpdatableModelObject {
func mergedWith(incrementalChanges: Self) -> Self
func mergedWith(incrementalChanges: Self?) -> Self
}
public extension UpdatableModelObject {
func mergedWith(incrementalChanges: Self?) -> Self {
if let incrementalChanges = incrementalChanges {
return self.mergedWith(incrementalChanges: incrementalChanges)
}
else {
return self
}
}
}
extension Array: UpdatableModelObject where Element: UpdatableModelObjectInTime {
public func mergedWith(incrementalChanges: Array<Element>) -> Array<Element> {
#warning("Not implemented!")
//TODO: Implement!
return incrementalChanges
}
}
//
// UpdatableModelObjectInTime.swift
// 1Weather
//
// Created by Demid Merzlyakov on 03.03.2021.
//
import Foundation
public protocol UpdatableModelObjectInTime: UpdatableModelObject {
var date: Date { get }
}
......@@ -19,11 +19,11 @@ struct WdtDailySummary: Codable {
public var precipitationProbability: String?
public var sunriseUtc: String?
public var sunsetUtc: String?
public var sunState: CelestialState? // if nil, then normal
public var sunState: CelestialState? // may be nil if micro update
public var moonriseUtc: String?
public var moonsetUtc: String?
public var moonState: CelestialState? // if nil then normal
public var moonPhase: MoonPhase? //if nil then unknown
public var moonState: CelestialState? // may be nil if micro update
public var moonPhase: MoonPhase? // may be nil if micro update
private enum CodingKeys: String, CodingKey {
case dateLocal = "summary_date"
......
......@@ -72,25 +72,25 @@ public class WdtWeatherSource: WeatherSource {
}
func update(location: Location, from locationResponse: WdtLocationResponse) -> Location? {
guard let wdtLocation = locationResponse.locations.first else {
return nil
}
// guard let wdtLocation = locationResponse.locations.first else {
// return nil
// }
var updatedLocation = location
if updatedLocation.coordinates == nil {
if let lat = CLLocationDegrees(wdtLocation.lat ?? ""), let lon = CLLocationDegrees(wdtLocation.lon ?? "") {
location.coordinates = CLLocationCoordinate2D(latitude: lat, longitude: lon)
}
}
if updatedLocation.cityName == nil {
updatedLocation.cityName = wdtLocation.city
}
if updatedLocation.countryName == nil {
updatedLocation.countryName = wdtLocation.country
}
if updatedLocation.region == nil {
updatedLocation.
}
// if updatedLocation.coordinates == nil {
// if let lat = CLLocationDegrees(wdtLocation.lat ?? ""), let lon = CLLocationDegrees(wdtLocation.lon ?? "") {
// location.coordinates = CLLocationCoordinate2D(latitude: lat, longitude: lon)
// }
// }
// if updatedLocation.cityName == nil {
// updatedLocation.cityName = wdtLocation.city
// }
// if updatedLocation.countryName == nil {
// updatedLocation.countryName = wdtLocation.country
// }
// if updatedLocation.region == nil {
// updatedLocation.
// }
//
// TOOD: implement
#warning("Not implemented!")
......
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