ADD: Abbreviated Price display

This commit is contained in:
marcosrdz 2021-05-22 00:44:08 -04:00
parent d13c9832e1
commit 98231b91b9
2 changed files with 32 additions and 9 deletions

View File

@ -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

View File

@ -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()