Skip to content

Commit

Permalink
Merge pull request #650 from Syn-McJ/feat/rates-cache
Browse files Browse the repository at this point in the history
fix: report cached rates on init
  • Loading branch information
Syn-McJ authored May 7, 2024
2 parents 818b7d0 + 5e025d4 commit 60e779b
Showing 1 changed file with 15 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,15 +70,21 @@ final class BaseRatesProvider: NSObject, RatesProvider {
private let kRefreshTimeInterval: TimeInterval = 60
private let kPriceByCodeKey = "DS_PRICEMANAGER_PRICESBYCODE"

var updateHandler: (([RateObject]) -> Void)?
var updateHandler: (([RateObject]) -> Void)? {
didSet {
let plainPricesByCode = UserDefaults.standard.object(forKey: kPriceByCodeKey) as! [String : NSNumber]
updateHandler?(plainPricesByCode.map { code, rate in
RateObject(code: code, name: currencyName(fromCode: code), price: rate.decimalValue)
})
}
}

private var lastPriceSourceInfo: String!

private let operationQueue: DSOperationQueue

override init() {
operationQueue = DSOperationQueue()

super.init()
}

Expand Down Expand Up @@ -110,4 +116,11 @@ final class BaseRatesProvider: NSObject, RatesProvider {

operationQueue.addOperation(priceOperation)
}

func currencyName(fromCode code: String) -> String {
let locale = Locale.current
let currencyName = locale.localizedString(forCurrencyCode: code)

return currencyName ?? code
}
}

0 comments on commit 60e779b

Please sign in to comment.