2019-08-24 23:11:47 +02:00
|
|
|
//
|
|
|
|
// ComplicationController.swift
|
|
|
|
// T WatchKit Extension
|
|
|
|
//
|
|
|
|
// Created by Marcos Rodriguez on 8/24/19.
|
|
|
|
// Copyright © 2019 Marcos Rodriguez. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import ClockKit
|
|
|
|
|
|
|
|
|
|
|
|
class ComplicationController: NSObject, CLKComplicationDataSource {
|
2021-02-09 03:41:08 +01:00
|
|
|
|
|
|
|
// MARK: - Timeline Configuration
|
|
|
|
|
|
|
|
func getSupportedTimeTravelDirections(for complication: CLKComplication, withHandler handler: @escaping (CLKComplicationTimeTravelDirections) -> Void) {
|
|
|
|
handler([])
|
|
|
|
}
|
|
|
|
|
|
|
|
func getTimelineStartDate(for complication: CLKComplication, withHandler handler: @escaping (Date?) -> Void) {
|
|
|
|
handler(nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
func getTimelineEndDate(for complication: CLKComplication, withHandler handler: @escaping (Date?) -> Void) {
|
|
|
|
handler(nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
func getPrivacyBehavior(for complication: CLKComplication, withHandler handler: @escaping (CLKComplicationPrivacyBehavior) -> Void) {
|
|
|
|
handler(.showOnLockScreen)
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: - Timeline Population
|
|
|
|
|
|
|
|
func getCurrentTimelineEntry(
|
|
|
|
for complication: CLKComplication,
|
|
|
|
withHandler handler: @escaping (CLKComplicationTimelineEntry?) -> Void)
|
|
|
|
{
|
|
|
|
let marketData: WidgetDataStore? = UserDefaults.standard.codable(forKey: MarketData.string)
|
|
|
|
let entry: CLKComplicationTimelineEntry
|
|
|
|
let date: Date
|
|
|
|
let valueLabel: String
|
|
|
|
let currencySymbol: String
|
2019-08-24 23:11:47 +02:00
|
|
|
|
2021-02-09 03:41:08 +01:00
|
|
|
if let price = marketData?.formattedRateForComplication, let marketDatadata = marketData?.date, let userCurrencySymbol = fiatUnit(currency: WidgetAPI.getUserPreferredCurrency())?.symbol {
|
|
|
|
date = marketDatadata
|
|
|
|
valueLabel = price
|
|
|
|
currencySymbol = userCurrencySymbol
|
|
|
|
} else {
|
|
|
|
valueLabel = "--"
|
|
|
|
currencySymbol = "USD"
|
|
|
|
date = Date()
|
2019-08-24 23:11:47 +02:00
|
|
|
}
|
|
|
|
|
2021-02-09 03:41:08 +01:00
|
|
|
let line1Text = CLKSimpleTextProvider(text:valueLabel)
|
|
|
|
let line2Text = CLKSimpleTextProvider(text:currencySymbol)
|
|
|
|
|
|
|
|
switch complication.family {
|
|
|
|
case .circularSmall:
|
|
|
|
let template = CLKComplicationTemplateCircularSmallStackText()
|
|
|
|
template.line1TextProvider = line1Text
|
|
|
|
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)
|
|
|
|
} else {
|
2019-08-24 23:11:47 +02:00
|
|
|
handler(nil)
|
2021-02-09 03:41:08 +01:00
|
|
|
}
|
|
|
|
entry = CLKComplicationTimelineEntry(date: date, complicationTemplate: template)
|
|
|
|
handler(entry)
|
|
|
|
case .utilitarianSmall:
|
|
|
|
let template = CLKComplicationTemplateUtilitarianSmallRingImage()
|
|
|
|
template.imageProvider = CLKImageProvider(onePieceImage: UIImage(named: "Complication/Utilitarian")!)
|
|
|
|
entry = CLKComplicationTimelineEntry(date: date, complicationTemplate: template)
|
|
|
|
handler(entry)
|
|
|
|
case .graphicCircular:
|
|
|
|
if #available(watchOSApplicationExtension 6.0, *) {
|
|
|
|
let template = CLKComplicationTemplateGraphicCircularStackText()
|
|
|
|
template.line1TextProvider = line1Text
|
|
|
|
template.line2TextProvider = line2Text
|
|
|
|
entry = CLKComplicationTimelineEntry(date: date, complicationTemplate: template)
|
|
|
|
handler(entry)
|
|
|
|
} else {
|
2019-08-24 23:11:47 +02:00
|
|
|
handler(nil)
|
2021-02-09 03:41:08 +01:00
|
|
|
}
|
|
|
|
case .modularSmall:
|
|
|
|
let template = CLKComplicationTemplateModularSmallStackText()
|
|
|
|
template.line1TextProvider = line1Text
|
|
|
|
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.innerTextProvider = CLKTextProvider(format: "%@", currencySymbol)
|
|
|
|
} else {
|
2019-08-24 23:11:47 +02:00
|
|
|
handler(nil)
|
2021-02-09 03:41:08 +01:00
|
|
|
}
|
|
|
|
entry = CLKComplicationTimelineEntry(date: date, complicationTemplate: template)
|
|
|
|
handler(entry)
|
|
|
|
default:
|
|
|
|
preconditionFailure("Complication family not supported")
|
2019-08-24 23:11:47 +02:00
|
|
|
}
|
2021-02-09 03:41:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func getTimelineEntries(for complication: CLKComplication, before date: Date, limit: Int, withHandler handler: @escaping ([CLKComplicationTimelineEntry]?) -> Void) {
|
|
|
|
// Call the handler with the timeline entries prior to the given date
|
|
|
|
handler(nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
func getTimelineEntries(for complication: CLKComplication, after date: Date, limit: Int, withHandler handler: @escaping ([CLKComplicationTimelineEntry]?) -> Void) {
|
|
|
|
// Call the handler with the timeline entries after to the given date
|
|
|
|
handler(nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: - Placeholder Templates
|
|
|
|
|
|
|
|
func getLocalizableSampleTemplate(for complication: CLKComplication, withHandler handler: @escaping (CLKComplicationTemplate?) -> Void) {
|
|
|
|
// This method will be called once per supported complication, and the results will be cached
|
|
|
|
let line1Text = CLKSimpleTextProvider(text:"46 K")
|
|
|
|
let line2Text = CLKSimpleTextProvider(text:"$")
|
2019-08-24 23:11:47 +02:00
|
|
|
|
2021-02-09 03:41:08 +01:00
|
|
|
switch complication.family {
|
|
|
|
case .circularSmall:
|
|
|
|
let template = CLKComplicationTemplateCircularSmallStackText()
|
|
|
|
template.line1TextProvider = line1Text
|
|
|
|
template.line2TextProvider = line2Text
|
|
|
|
handler(template)
|
|
|
|
case .utilitarianSmallFlat:
|
|
|
|
let template = CLKComplicationTemplateUtilitarianSmallFlat()
|
|
|
|
if #available(watchOSApplicationExtension 6.0, *) {
|
|
|
|
template.textProvider = CLKTextProvider(format: "%@", "$46,134")
|
|
|
|
} else {
|
2019-08-24 23:11:47 +02:00
|
|
|
handler(nil)
|
2021-02-09 03:41:08 +01:00
|
|
|
}
|
|
|
|
handler(template)
|
|
|
|
case .utilitarianSmall:
|
|
|
|
let template = CLKComplicationTemplateUtilitarianSmallRingImage()
|
|
|
|
template.imageProvider = CLKImageProvider(onePieceImage: UIImage(named: "Complication/Utilitarian")!)
|
|
|
|
handler(template)
|
|
|
|
case .graphicCircular:
|
|
|
|
if #available(watchOSApplicationExtension 6.0, *) {
|
|
|
|
let template = CLKComplicationTemplateGraphicCircularStackText()
|
|
|
|
template.line1TextProvider = line1Text
|
|
|
|
template.line2TextProvider = line2Text
|
|
|
|
handler(template)
|
|
|
|
} else {
|
2019-08-24 23:11:47 +02:00
|
|
|
handler(nil)
|
2021-02-09 03:41:08 +01:00
|
|
|
}
|
|
|
|
case .graphicCorner:
|
|
|
|
let template = CLKComplicationTemplateGraphicCornerStackText()
|
|
|
|
if #available(watchOSApplicationExtension 6.0, *) {
|
|
|
|
template.outerTextProvider = CLKTextProvider(format: "46,134")
|
|
|
|
template.innerTextProvider = CLKTextProvider(format: "$")
|
|
|
|
} else {
|
2019-08-24 23:11:47 +02:00
|
|
|
handler(nil)
|
2021-02-09 03:41:08 +01:00
|
|
|
}
|
|
|
|
handler(template)
|
|
|
|
case .modularSmall:
|
|
|
|
let template = CLKComplicationTemplateModularSmallStackText()
|
|
|
|
template.line1TextProvider = line1Text
|
|
|
|
template.line2TextProvider = line2Text
|
|
|
|
handler(template)
|
|
|
|
default:
|
|
|
|
handler(nil)
|
2019-08-24 23:11:47 +02:00
|
|
|
}
|
2021-02-09 03:41:08 +01:00
|
|
|
}
|
|
|
|
|
2019-08-24 23:11:47 +02:00
|
|
|
}
|