BlueWallet/ios/WalletInformationWidget/Widgets/Shared/WidgetDataStore.swift

90 lines
2.8 KiB
Swift
Raw Normal View History

2020-10-30 10:19:38 -04:00
//
// TodayDataStore.swift
// TodayExtension
//
// Created by Marcos Rodriguez on 11/3/19.
// Copyright © 2019 Facebook. All rights reserved.
//
import Foundation
2021-05-22 00:44:08 -04:00
extension Numeric {
var abbreviated: String {
let bytecountFormatter = ByteCountFormatter()
bytecountFormatter.zeroPadsFractionDigits = true
bytecountFormatter.countStyle = .decimal
bytecountFormatter.allowsNonnumericFormatting = true
let bytesString = bytecountFormatter.string(fromByteCount: (self as! NSNumber).int64Value)
let numericString = bytesString
.replacingOccurrences(of: "bytes", with: "")
.replacingOccurrences(of: "B", with: "") // removes B (bytes) in 'KB'/'MB'/'GB'
.replacingOccurrences(of: "G", with: "B") // replace G (Giga) to just B (billions)
return numericString.replacingOccurrences(of: " ", with: "")
}
}
2021-02-08 21:41:08 -05:00
struct WidgetDataStore: Codable {
2020-10-30 10:19:38 -04:00
let rate: String
let lastUpdate: String
2020-11-06 16:30:35 -05:00
let rateDouble: Double
2020-10-30 10:19:38 -04:00
var formattedRate: String? {
let numberFormatter = NumberFormatter()
2020-11-02 08:11:28 -05:00
numberFormatter.locale = Locale(identifier: WidgetAPI.getUserPreferredCurrencyLocale())
2020-11-20 23:11:51 -05:00
numberFormatter.numberStyle = .currency
2020-11-02 08:11:28 -05:00
numberFormatter.maximumFractionDigits = 0
numberFormatter.minimumFractionDigits = 0
2020-11-20 23:11:51 -05:00
if let rateString = numberFormatter.string(from: NSNumber(value: rateDouble)) {
return rateString
2020-10-30 10:19:38 -04:00
}
return rate
}
2021-05-22 00:44:08 -04:00
var formattedRateForSmallComplication: String? {
return rateDouble.abbreviated
}
2021-02-08 21:41:08 -05:00
var formattedRateForComplication: String? {
let numberFormatter = NumberFormatter()
numberFormatter.locale = Locale(identifier: WidgetAPI.getUserPreferredCurrencyLocale())
numberFormatter.numberStyle = .currency
numberFormatter.currencySymbol = ""
if let rateString = numberFormatter.string(from: NSNumber(value: rateDouble)) {
return rateString
}
return rate
}
2021-05-22 00:44:08 -04:00
2021-02-08 21:41:08 -05:00
var date: Date? {
let isoDateFormatter = ISO8601DateFormatter()
let dateFormatter = DateFormatter()
dateFormatter.locale = Locale.current
dateFormatter.timeStyle = .short
return isoDateFormatter.date(from: lastUpdate)
}
2021-02-16 21:58:01 -05:00
var formattedDate: String? {
let isoDateFormatter = ISO8601DateFormatter()
let dateFormatter = DateFormatter()
dateFormatter.locale = Locale.current
dateFormatter.timeStyle = .short
if let date = isoDateFormatter.date(from: lastUpdate) {
return dateFormatter.string(from: date)
}
return nil
}
2020-10-30 10:19:38 -04:00
}
2020-11-02 08:11:28 -05:00
class WidgetData {
2020-10-30 10:19:38 -04:00
2020-11-02 08:11:28 -05:00
static let WidgetDataStoreKey = "WidgetDataStoreKey"
static let WidgetCachedDataStoreKey = "WidgetCachedDataStoreKey"
2020-10-30 10:19:38 -04:00
static func savePriceRateAndLastUpdate(rate: String, lastUpdate: String) {
2020-11-02 08:11:28 -05:00
UserDefaults.standard.setValue(["rate": rate, "lastUpdate": lastUpdate], forKey: WidgetDataStoreKey)
2020-10-30 10:19:38 -04:00
UserDefaults.standard.synchronize()
}
}