-
Notifications
You must be signed in to change notification settings - Fork 1
/
CurrencyRefresher.swift
38 lines (34 loc) · 1.04 KB
/
CurrencyRefresher.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import Foundation
import SoulverCore
actor CurrencyRefresher {
private init() {
do {
try CurrencyList.shared.loadCurrenciesFrom(url: CurrencyDataURL)
} catch {}
}
static let shared = CurrencyRefresher()
enum Error: Swift.Error {
case errorRefreshingCurrency
}
func refreshIfNeeded() async throws {
if await PreferencesManager.shared.needsRefresh() {
try await refresh()
}
}
func refresh() async throws {
CurrencyList.shared.defaultCurrencySet = .popular
try await withCheckedThrowingContinuation { (c: CheckedContinuation<Void, Swift.Error>) in
CurrencyList.shared.refreshRates { success in
if success {
c.resume(returning: ())
} else {
c.resume(throwing: Error.errorRefreshingCurrency)
}
}
}
try FileManager.default.createDirectory(
at: CurrencyDataURL.deletingLastPathComponent(), withIntermediateDirectories: true)
CurrencyList.shared.saveCurrenciesTo(url: CurrencyDataURL)
await PreferencesManager.shared.markRefreshed()
}
}