From b26300653e4f07514ff29cd1b02834ca3fc19c30 Mon Sep 17 00:00:00 2001 From: Carlos Precioso <511681+cprecioso@users.noreply.github.com> Date: Wed, 14 Jul 2021 10:50:33 +0200 Subject: [PATCH] refresh currencies every day --- Sources/alfred-soulvercore/main.swift | 7 +++- Sources/alfred-soulvercore/prefs.swift | 13 +++++++ Sources/alfred-soulvercore/refresh.swift | 44 ++++++++++++++++++++++++ 3 files changed, 63 insertions(+), 1 deletion(-) create mode 100644 Sources/alfred-soulvercore/prefs.swift create mode 100644 Sources/alfred-soulvercore/refresh.swift diff --git a/Sources/alfred-soulvercore/main.swift b/Sources/alfred-soulvercore/main.swift index c8aea95..1635094 100644 --- a/Sources/alfred-soulvercore/main.swift +++ b/Sources/alfred-soulvercore/main.swift @@ -7,7 +7,12 @@ import SoulverCore -print(calc() ?? "") +refreshIfNeeded { + print(calc() ?? "") + exit(0) +} + +RunLoop.current.run() func calc() -> String? { let input = CommandLine.arguments.dropFirst().joined(separator: " ") diff --git a/Sources/alfred-soulvercore/prefs.swift b/Sources/alfred-soulvercore/prefs.swift new file mode 100644 index 0000000..861230f --- /dev/null +++ b/Sources/alfred-soulvercore/prefs.swift @@ -0,0 +1,13 @@ +import Foundation + +let BundleIdentifier = "design.precioso.alfred-soulvercore" + +let userDefaults = UserDefaults(suiteName: BundleIdentifier)! + +let cacheFolder = try? FileManager.default.url( + for: .cachesDirectory, in: .userDomainMask, appropriateFor: nil, create: true +).appendingPathComponent(BundleIdentifier) + +let LastRefreshKey = "last-refresh" +let UpdatePeriod: TimeInterval = 60 * 60 * 24 // 1 day in seconds +let CurrencyDataURL = cacheFolder?.appendingPathComponent("currencies.json") diff --git a/Sources/alfred-soulvercore/refresh.swift b/Sources/alfred-soulvercore/refresh.swift new file mode 100644 index 0000000..774fff4 --- /dev/null +++ b/Sources/alfred-soulvercore/refresh.swift @@ -0,0 +1,44 @@ +import Foundation +import SoulverCore + +func refreshIfNeeded(completionHandler: @escaping (() -> Void)) { + let lastRefresh = loadData() + let nextRefresh = lastRefresh.addingTimeInterval(UpdatePeriod) + let currentDate = Date() + + if nextRefresh < currentDate { + refresh { result in + if result { + saveData() + } + completionHandler() + } + } else { + completionHandler() + } +} + +func refresh(completionHandler: @escaping ((Bool) -> Void)) { + CurrencyList.shared.defaultCurrencySet = .popular + CurrencyList.shared.refreshRates(completionHandler: completionHandler) +} + +func loadData() -> Date { + var returnDate: Date? = nil + + if let url = CurrencyDataURL, + (try? CurrencyList.shared.loadCurrenciesFrom(url: url)) != nil + { + returnDate = userDefaults.object(forKey: LastRefreshKey) as? Date + } + + return returnDate ?? Date.distantPast +} + +func saveData() { + if let url = CurrencyDataURL, + (try? FileManager.default.createDirectory(at: url.deletingLastPathComponent(), withIntermediateDirectories: true)) != nil { + CurrencyList.shared.saveCurrenciesTo(url: url) + userDefaults.set(Date(), forKey: LastRefreshKey) + } +}