-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
63 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} | ||
} |