Files
mowesta/ios-app/app/Model/FavoriteExtensions.swift
T
handte 4fdbf10054
LocationBasedServices/mowesta/pipeline/head This commit looks good
Add 2-way (background) synchronization and favorites.
2022-07-23 16:58:25 +02:00

196 lines
5.6 KiB
Swift

//
// Favorite.swift
// App
//
// Created by Marcus Handte on 15.07.22.
//
import Foundation
import MoWeStaApi
import AnyCodable
/// Extends the favorite entity generated by core data with additional properties (implemented via associated objects)
/// to simplify the access to json encoded data stored in the database and to avoid repeated loading and unloading.
extension Favorite {
/// The location which should be a city, state or country
public var locationValue: Any? {
get {
if let loc = location, let data = loc.data(using: .utf8) {
switch getLocationType() {
case .city:
return try? JSONDecoder().decode(City.self, from: data)
case .country:
return try? JSONDecoder().decode(Country.self, from: data)
case .state:
return try? JSONDecoder().decode(State.self, from: data)
}
}
return nil
}
set {
if let nv = newValue as? City {
if let data = try? JSONEncoder().encode(nv) {
location = String(decoding: data, as: UTF8.self)
} else {
location = nil
}
} else if let nv = newValue as? State {
if let data = try? JSONEncoder().encode(nv) {
location = String(decoding: data, as: UTF8.self)
} else {
location = nil
}
} else if let nv = newValue as? Country {
if let data = try? JSONEncoder().encode(nv) {
location = String(decoding: data, as: UTF8.self)
} else {
location = nil
}
} else {
location = nil
}
}
}
/// The city
public var cityValue: City? {
get {
if let c = city, let data = c.data(using: .utf8) {
return try? JSONDecoder().decode(City.self, from: data)
}
return nil
}
set {
if let v = newValue, let data = try? JSONEncoder().encode(v) {
city = String(decoding: data, as: UTF8.self)
} else {
city = nil
}
}
}
/// The weather condition.
public var conditionValue: WeatherCondition? {
get {
if let c = condition, let data = c.data(using: .utf8) {
return try? JSONDecoder().decode(WeatherCondition.self, from: data)
} else {
return nil
}
}
set {
if let v = newValue, let data = try? JSONEncoder().encode(v) {
condition = String(decoding: data, as: UTF8.self)
} else {
condition = nil
}
}
}
/// The weather forecast.
public var forecastValue: WeatherForecast? {
get {
if let f = forecast, let data = f.data(using: .utf8) {
return try? JSONDecoder().decode(WeatherForecast.self, from: data)
}
return nil
}
set {
if let v = newValue, let data = try? JSONEncoder().encode(v) {
forecast = String(decoding: data, as: UTF8.self)
} else {
forecast = nil
}
}
}
/// The weatehr station
public var stationValue: WeatherStation? {
get {
if let s = station, let data = s.data(using: .utf8) {
return try? JSONDecoder().decode(WeatherStation.self, from: data)
}
return nil
}
set {
if let v = newValue, let data = try? JSONEncoder().encode(v) {
station = String(decoding: data, as: UTF8.self)
} else {
station = nil
}
}
}
/// The boundary
public var boundaryValue: Geometry? {
get {
if let b = boundary, let data = b.data(using: .utf8) {
return try? JSONDecoder().decode(Geometry.self, from: data)
}
return nil
}
set {
if let v = newValue, let data = try? JSONEncoder().encode(v) {
boundary = String(decoding: data, as: UTF8.self)
} else {
boundary = nil
}
}
}
/// Returns the loation type
public func getLocationType() -> LocationHierarchy.ModelType {
if (self.locationType == 2) {
return .country
} else if (self.locationType == 1) {
return .state
} else {
return .city
}
}
/// Sets the location type.
public func setLocationType(type: LocationHierarchy.ModelType) {
switch (type) {
case .city:
locationType = 0
case .state:
locationType = 1
case .country:
locationType = 2
}
}
/// Returns the loation type
public func getLocationReferenceType() -> LocationReference.ModelType {
if (self.locationType == 2) {
return .country
} else if (self.locationType == 1) {
return .state
} else {
return .city
}
}
/// Sets the location type.
public func setLocationReferenceType(type: LocationReference.ModelType) {
switch (type) {
case .city:
locationType = 0
case .state:
locationType = 1
case .country:
locationType = 2
}
}
}