From 98231b91b93381d8b5bb039ac10fc88cbe7c3124 Mon Sep 17 00:00:00 2001 From: marcosrdz Date: Sat, 22 May 2021 00:44:08 -0400 Subject: [PATCH] ADD: Abbreviated Price display --- .../ComplicationController.swift | 16 +++++++----- .../Widgets/Shared/WidgetDataStore.swift | 25 ++++++++++++++++--- 2 files changed, 32 insertions(+), 9 deletions(-) diff --git a/ios/BlueWalletWatch Extension/ComplicationController.swift b/ios/BlueWalletWatch Extension/ComplicationController.swift index 4b485b77b..704b388fe 100644 --- a/ios/BlueWalletWatch Extension/ComplicationController.swift +++ b/ios/BlueWalletWatch Extension/ComplicationController.swift @@ -39,12 +39,14 @@ class ComplicationController: NSObject, CLKComplicationDataSource { let entry: CLKComplicationTimelineEntry let date: Date let valueLabel: String + let valueSmallLabel: String let currencySymbol: String let timeLabel: String - if let price = marketData?.formattedRateForComplication, let marketDatadata = marketData?.date, let lastUpdated = marketData?.formattedDate { + if let price = marketData?.formattedRateForComplication, let priceAbbreviated = marketData?.formattedRateForSmallComplication, let marketDatadata = marketData?.date, let lastUpdated = marketData?.formattedDate { date = marketDatadata valueLabel = price timeLabel = lastUpdated + valueSmallLabel = priceAbbreviated if let preferredFiatCurrency = UserDefaults.standard.string(forKey: "preferredFiatCurrency"), let preferredFiatUnit = fiatUnit(currency: preferredFiatCurrency) { currencySymbol = preferredFiatUnit.symbol } else { @@ -53,24 +55,26 @@ class ComplicationController: NSObject, CLKComplicationDataSource { } else { valueLabel = "--" timeLabel = "--" + valueSmallLabel = "--" currencySymbol = fiatUnit(currency: "USD")!.symbol date = Date() } let line1Text = CLKSimpleTextProvider(text:valueLabel) let line2Text = CLKSimpleTextProvider(text:currencySymbol) + let line1SmallText = CLKSimpleTextProvider(text: valueSmallLabel) switch complication.family { case .circularSmall: let template = CLKComplicationTemplateCircularSmallStackText() - template.line1TextProvider = line1Text + template.line1TextProvider = line1SmallText template.line2TextProvider = line2Text entry = CLKComplicationTimelineEntry(date: date, complicationTemplate: template) handler(entry) case .utilitarianSmallFlat: let template = CLKComplicationTemplateUtilitarianSmallFlat() if #available(watchOSApplicationExtension 6.0, *) { - template.textProvider = CLKTextProvider(format: "%@%@", currencySymbol, valueLabel) + template.textProvider = CLKTextProvider(format: "%@%@", currencySymbol, valueSmallLabel) } else { handler(nil) } @@ -94,14 +98,14 @@ class ComplicationController: NSObject, CLKComplicationDataSource { } case .modularSmall: let template = CLKComplicationTemplateModularSmallStackText() - template.line1TextProvider = line1Text + template.line1TextProvider = line1SmallText template.line2TextProvider = line2Text entry = CLKComplicationTimelineEntry(date: date, complicationTemplate: template) handler(entry) case .graphicCorner: let template = CLKComplicationTemplateGraphicCornerStackText() if #available(watchOSApplicationExtension 6.0, *) { - template.outerTextProvider = CLKTextProvider(format: "%@", valueLabel) + template.outerTextProvider = CLKTextProvider(format: "%@", valueSmallLabel) template.innerTextProvider = CLKTextProvider(format: "%@", currencySymbol) } else { handler(nil) @@ -111,7 +115,7 @@ class ComplicationController: NSObject, CLKComplicationDataSource { case .graphicBezel: let template = CLKComplicationTemplateGraphicBezelCircularText() if #available(watchOSApplicationExtension 6.0, *) { - template.textProvider = CLKTextProvider(format: "%@%@", currencySymbol, valueLabel) + template.textProvider = CLKTextProvider(format: "%@%@", currencySymbol, valueSmallLabel) let imageProvider = CLKFullColorImageProvider(fullColorImage: UIImage(named: "Complication/Graphic Bezel")!) let circularTemplate = CLKComplicationTemplateGraphicCircularImage() circularTemplate.imageProvider = imageProvider diff --git a/ios/WalletInformationWidget/Widgets/Shared/WidgetDataStore.swift b/ios/WalletInformationWidget/Widgets/Shared/WidgetDataStore.swift index c332ea98b..1305f87d0 100644 --- a/ios/WalletInformationWidget/Widgets/Shared/WidgetDataStore.swift +++ b/ios/WalletInformationWidget/Widgets/Shared/WidgetDataStore.swift @@ -8,6 +8,23 @@ import Foundation +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: "") + } +} + struct WidgetDataStore: Codable { let rate: String let lastUpdate: String @@ -23,19 +40,21 @@ struct WidgetDataStore: Codable { } return rate } + var formattedRateForSmallComplication: String? { + return rateDouble.abbreviated + } + var formattedRateForComplication: String? { let numberFormatter = NumberFormatter() numberFormatter.locale = Locale(identifier: WidgetAPI.getUserPreferredCurrencyLocale()) numberFormatter.numberStyle = .currency - numberFormatter.usesSignificantDigits = true - numberFormatter.maximumFractionDigits = 0 - numberFormatter.minimumFractionDigits = 0 numberFormatter.currencySymbol = "" if let rateString = numberFormatter.string(from: NSNumber(value: rateDouble)) { return rateString } return rate } + var date: Date? { let isoDateFormatter = ISO8601DateFormatter() let dateFormatter = DateFormatter()