From 502f9894171d992c62851ebf3f1db754556f0b64 Mon Sep 17 00:00:00 2001 From: Simon Rogers <63044346+siroger@users.noreply.github.com> Date: Thu, 9 Apr 2020 18:49:39 +0100 Subject: [PATCH] Currency bar update (#293) * Updated CurrenyBarItem to display second currency (TO FROM>RATE) and also correct number of decimal places. Added INR to list of currencies. * Update CurrencyBarItem.swift * Updated else statements Co-authored-by: medden --- MTMR/Widgets/CurrencyBarItem.swift | 35 ++++++++++++++++++++++++++++-- 1 file changed, 33 insertions(+), 2 deletions(-) diff --git a/MTMR/Widgets/CurrencyBarItem.swift b/MTMR/Widgets/CurrencyBarItem.swift index dfe3479..71e5599 100644 --- a/MTMR/Widgets/CurrencyBarItem.swift +++ b/MTMR/Widgets/CurrencyBarItem.swift @@ -15,6 +15,9 @@ class CurrencyBarItem: CustomButtonTouchBarItem { private var postfix: String private var from: String private var to: String + private var decimal: Int + private var decimalValue: Float32! + private var decimalString: String! private var oldValue: Float32! private var full: Bool = false @@ -32,11 +35,29 @@ class CurrencyBarItem: CustomButtonTouchBarItem { "IDR": "Rp", "MXN": "$", "SGD": "$", - "CHF": "Fr.", "BTC": "฿", "LTC": "Ł", "ETH": "Ξ", ] + private let decimals = [ + "USD": 4, + "EUR": 4, + "RUB": 2, + "JPY": 2, + "GBP": 4, + "CAD": 4, + "KRW": 4, + "CNY": 4, + "AUD": 4, + "BRL": 4, + "IDR": 1, + "MXN": 2, + "SGD": 4, + "CHF": 4, + "BTC": 2, + "LTC": 2, + "ETH": 2, + ] init(identifier: NSTouchBarItem.Identifier, interval: TimeInterval, from: String, to: String, full: Bool) { activity = NSBackgroundActivityScheduler(identifier: "\(identifier.rawValue).updatecheck") @@ -57,6 +78,14 @@ class CurrencyBarItem: CustomButtonTouchBarItem { postfix = to } + + if let decimal = decimals[to] { + self.decimal = decimal + } else { + decimal = 2 + } + + super.init(identifier: identifier, title: "⏳") activity.repeats = true @@ -114,10 +143,12 @@ class CurrencyBarItem: CustomButtonTouchBarItem { } oldValue = value + decimalValue = (value * pow(10,Float(decimal))).rounded() / pow(10,Float(decimal)) + decimalString = String(decimalValue) var title = "" if full { - title = String(format: "%@‣%.2f%@", prefix, value, postfix) + title = String(format: "%@%@‣%@", prefix, postfix, decimalString) } else { title = String(format: "%@%.2f", prefix, value) }