249 lines
8.9 KiB
Swift
249 lines
8.9 KiB
Swift
//
|
|
// RefreshTask.swift
|
|
// App
|
|
//
|
|
// Created by Marcus Handte on 22.07.22.
|
|
//
|
|
|
|
import Foundation
|
|
import class MoWeStaApi.DefaultAPI
|
|
import class MoWeStaApi.City
|
|
import class MoWeStaApi.State
|
|
import class MoWeStaApi.Country
|
|
import class MoWeStaApi.WeatherCondition
|
|
import class MoWeStaApi.WeatherForecast
|
|
import class MoWeStaApi.WeatherStation
|
|
import class MoWeStaApi.Geometry
|
|
|
|
/// Loads the location and weather info for all favorites.
|
|
public class RefreshTask: BaseTask {
|
|
|
|
// The timeout to prevent frequent location downloads
|
|
public static let LOCATION_TIMEOUT = 24 * 60 * 60 * 1000
|
|
|
|
// The timeout to prevent too frequent weather downloads
|
|
public static let WEATHER_TIMEOUT = 5 * 60 * 1000
|
|
|
|
/// The persistence context
|
|
let context = PersistenceController.shared.container.newBackgroundContext()
|
|
|
|
override func executeAsync() -> Bool {
|
|
context.performAndWait {
|
|
let fetch = Favorite.fetchRequest()
|
|
if let favorites = try? fetch.execute() {
|
|
print("Syncing \(favorites.count) favorites.")
|
|
let group = DispatchGroup()
|
|
let queue = DispatchQueue(label: "refresh", attributes: .concurrent)
|
|
let semaphore = DispatchSemaphore(value: 3)
|
|
for fav in favorites {
|
|
if isCanceled() {
|
|
print("Sync was cancelled.")
|
|
break
|
|
} else {
|
|
print("Refresh start: \(fav.identifier)")
|
|
if !fav.removed {
|
|
semaphore.wait()
|
|
group.enter()
|
|
queue.async {
|
|
let favref = FavoriteRefresher(self, fav)
|
|
favref.refresh {
|
|
print("Refresh complete: \(fav.identifier)")
|
|
semaphore.signal()
|
|
group.leave()
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
group.wait()
|
|
print("Saving refreshed context.")
|
|
try? context.save()
|
|
}
|
|
}
|
|
return !isCanceled()
|
|
}
|
|
}
|
|
|
|
/// A utility class to download the data for a favorite
|
|
class FavoriteRefresher {
|
|
|
|
let task: RefreshTask
|
|
let favorite: Favorite
|
|
let now = Int64(NSDate().timeIntervalSince1970 * 1000)
|
|
var remaining = 0
|
|
var lock = NSRecursiveLock()
|
|
var city: City? = nil
|
|
var location: Any? = nil
|
|
var boundary: Geometry? = nil
|
|
var forecast: WeatherForecast? = nil
|
|
var condition: WeatherCondition? = nil
|
|
var station: WeatherStation? = nil
|
|
|
|
init(_ task: RefreshTask, _ favorite: Favorite) {
|
|
self.favorite = favorite
|
|
self.task = task
|
|
}
|
|
|
|
private func isLocationIncomplete() -> Bool {
|
|
return favorite.location == nil || favorite.forecast == nil
|
|
}
|
|
|
|
private func isWeatherIncomplete() -> Bool {
|
|
return favorite.condition == nil || favorite.forecast == nil
|
|
}
|
|
|
|
|
|
public func refresh(completion: @escaping () -> Void) {
|
|
lock.lock()
|
|
if !task.isCanceled() {
|
|
if now - favorite.locationTime > RefreshTask.LOCATION_TIMEOUT || isLocationIncomplete() {
|
|
switch favorite.getLocationType() {
|
|
case .country:
|
|
remaining += 2
|
|
DefaultAPI.getCountry(id: favorite.identifier) {
|
|
response, error in
|
|
if let r = response {
|
|
self.location = r
|
|
}
|
|
self.complete(completion: completion)
|
|
}
|
|
|
|
DefaultAPI.getCountryCities(id: favorite.identifier, pageOffset: 0, pageSize: 1) {
|
|
response, error in
|
|
if let r = response, let f = r.elements.first {
|
|
self.city = f
|
|
self.refresh(f, true, completion)
|
|
}
|
|
self.complete(completion: completion)
|
|
}
|
|
/** This is too much data.
|
|
DefaultAPI.getCountryBoundary(id: favorite.identifier) {
|
|
response, error in
|
|
if let r = response {
|
|
self.boundary = r
|
|
}
|
|
self.complete(completion: completion)
|
|
}
|
|
*/
|
|
case .state:
|
|
remaining += 3
|
|
DefaultAPI.getState(id: favorite.identifier) {
|
|
response, error in
|
|
if let r = response {
|
|
self.location = r
|
|
}
|
|
self.complete(completion: completion)
|
|
}
|
|
DefaultAPI.getStateCities(id: favorite.identifier, pageOffset: 0, pageSize: 1) {
|
|
response, error in
|
|
if let r = response, let f = r.elements.first {
|
|
self.city = f
|
|
self.refresh(f, true, completion)
|
|
}
|
|
self.complete(completion: completion)
|
|
}
|
|
DefaultAPI.getStateBoundary(id: favorite.identifier) {
|
|
response, error in
|
|
if let r = response {
|
|
self.boundary = r
|
|
}
|
|
self.complete(completion: completion)
|
|
}
|
|
case .city:
|
|
remaining += 2
|
|
DefaultAPI.getCity(id: favorite.identifier) {
|
|
response, error in
|
|
if let r = response {
|
|
self.city = r
|
|
self.location = r
|
|
self.refresh(r, true, completion)
|
|
}
|
|
self.complete(completion: completion)
|
|
}
|
|
DefaultAPI.getCityBoundary(id: favorite.identifier) {
|
|
response, error in
|
|
if let r = response {
|
|
self.boundary = r
|
|
}
|
|
self.complete(completion: completion)
|
|
}
|
|
}
|
|
} else if now - favorite.weatherTime > RefreshTask.WEATHER_TIMEOUT || isWeatherIncomplete() {
|
|
if let c = favorite.cityValue {
|
|
refresh(c, false, completion)
|
|
}
|
|
}
|
|
}
|
|
if remaining == 0 {
|
|
DispatchQueue.main.async {
|
|
completion()
|
|
}
|
|
}
|
|
lock.unlock()
|
|
}
|
|
|
|
/// refreshes the weather for a city, optionally with location
|
|
func refresh(_ city: City, _ full: Bool, _ completion: @escaping () -> Void) {
|
|
if !task.isCanceled() {
|
|
lock.lock()
|
|
if full {
|
|
remaining += 3
|
|
} else {
|
|
remaining += 2
|
|
}
|
|
lock.unlock()
|
|
if full {
|
|
DefaultAPI.getCityStation(id: city.id!) {
|
|
response, error in
|
|
if let r = response {
|
|
self.station = r
|
|
}
|
|
self.complete(completion: completion)
|
|
}
|
|
}
|
|
DefaultAPI.getCityForecast(id: city.id!) {
|
|
response, error in
|
|
if let r = response {
|
|
self.forecast = r
|
|
}
|
|
self.complete(completion: completion)
|
|
}
|
|
DefaultAPI.getCityCondition(id: city.id!) {
|
|
response, error in
|
|
if let r = response {
|
|
self.condition = r
|
|
}
|
|
self.complete(completion: completion)
|
|
}
|
|
}
|
|
}
|
|
|
|
/// complete the task and sets the variables
|
|
func complete(completion: @escaping () -> Void) {
|
|
lock.lock()
|
|
remaining -= 1
|
|
if remaining == 0 {
|
|
if let c = city, let l = location {
|
|
if let b = boundary {
|
|
favorite.boundaryValue = b
|
|
}
|
|
if let s = station {
|
|
favorite.stationValue = s
|
|
}
|
|
favorite.locationValue = l
|
|
favorite.cityValue = c
|
|
favorite.locationTime = now
|
|
}
|
|
if let f = forecast, let c = condition {
|
|
favorite.forecastValue = f
|
|
favorite.conditionValue = c
|
|
favorite.weatherTime = now
|
|
}
|
|
DispatchQueue.main.async {
|
|
completion()
|
|
}
|
|
}
|
|
lock.unlock()
|
|
}
|
|
}
|