2020-11-20 22:58:04 -05:00
|
|
|
|
//
|
|
|
|
|
// FiatUnit.swift
|
|
|
|
|
// BlueWallet
|
|
|
|
|
//
|
|
|
|
|
// Created by Marcos Rodriguez on 11/20/20.
|
|
|
|
|
// Copyright © 2020 BlueWallet. All rights reserved.
|
|
|
|
|
//
|
|
|
|
|
import Foundation
|
|
|
|
|
|
|
|
|
|
struct FiatUnit: Codable {
|
|
|
|
|
let endPointKey: String
|
|
|
|
|
let symbol: String
|
|
|
|
|
let locale: String
|
2021-03-29 18:14:24 +03:00
|
|
|
|
let source: String
|
2021-05-21 02:02:29 -04:00
|
|
|
|
|
2020-11-20 22:58:04 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func fiatUnit(currency: String) -> FiatUnit? {
|
2021-07-18 15:51:40 +02:00
|
|
|
|
return Bundle.main.decode([String: FiatUnit].self, from: "fiatUnits.json").first(where: {$1.endPointKey == currency})?.value
|
2021-05-21 02:02:29 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
extension Bundle {
|
|
|
|
|
func decode<T: Decodable>(_ type: T.Type, from file: String, dateDecodingStrategy: JSONDecoder.DateDecodingStrategy = .deferredToDate, keyDecodingStrategy: JSONDecoder.KeyDecodingStrategy = .useDefaultKeys) -> T {
|
|
|
|
|
guard let url = self.url(forResource: file, withExtension: nil) else {
|
|
|
|
|
fatalError("Failed to locate \(file) in bundle.")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
guard let data = try? Data(contentsOf: url) else {
|
|
|
|
|
fatalError("Failed to load \(file) from bundle.")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let decoder = JSONDecoder()
|
|
|
|
|
|
|
|
|
|
decoder.dateDecodingStrategy = dateDecodingStrategy
|
|
|
|
|
decoder.keyDecodingStrategy = keyDecodingStrategy
|
2020-11-20 22:58:04 -05:00
|
|
|
|
|
2021-05-21 02:02:29 -04:00
|
|
|
|
do {
|
|
|
|
|
return try decoder.decode(T.self, from: data)
|
|
|
|
|
} catch DecodingError.keyNotFound(let key, let context) {
|
|
|
|
|
fatalError("Failed to decode \(file) from bundle due to missing key '\(key.stringValue)' not found – \(context.debugDescription)")
|
|
|
|
|
} catch DecodingError.typeMismatch(_, let context) {
|
|
|
|
|
fatalError("Failed to decode \(file) from bundle due to type mismatch – \(context.debugDescription)")
|
|
|
|
|
} catch DecodingError.valueNotFound(let type, let context) {
|
|
|
|
|
fatalError("Failed to decode \(file) from bundle due to missing \(type) value – \(context.debugDescription)")
|
|
|
|
|
} catch DecodingError.dataCorrupted(_) {
|
|
|
|
|
fatalError("Failed to decode \(file) from bundle because it appears to be invalid JSON")
|
|
|
|
|
} catch {
|
|
|
|
|
fatalError("Failed to decode \(file) from bundle: \(error.localizedDescription)")
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-11-20 22:58:04 -05:00
|
|
|
|
}
|