Skip to content
Permalink
Browse files

added complication template handlers for each family

  • Loading branch information...
mackuba committed Dec 27, 2018
1 parent b050c92 commit 0d3c073c0fc78ea68448b7cea27c7adedf1e5bb9
@@ -9,10 +9,6 @@
import ClockKit

private let MeasurementValidityTime = 3600 * 3
private let PMTitleText = "PM10"
private let PMShortTitleText = "PM"
private let PlaceholderText = ""
private let SampleValueText = "50"


class ComplicationController: NSObject, CLKComplicationDataSource {
@@ -38,79 +34,34 @@ class ComplicationController: NSObject, CLKComplicationDataSource {
func getCurrentTimelineEntry(
for complication: CLKComplication,
withHandler handler: @escaping (CLKComplicationTimelineEntry?) -> Void)
withHandler callback: @escaping (CLKComplicationTimelineEntry?) -> Void)
{
let entry: CLKComplicationTimelineEntry
let handler = ComplicationHandlers.handler(for: complication)

if let date = dataStore.lastMeasurementDate, let level = dataStore.currentLevel {
let valueText = String(Int(level.rounded()))

switch complication.family {
case .circularSmall:
let template = CLKComplicationTemplateCircularSmallStackText()
template.line1TextProvider = CLKSimpleTextProvider(text: PMShortTitleText)
template.line2TextProvider = CLKSimpleTextProvider(text: valueText)

entry = CLKComplicationTimelineEntry(date: date, complicationTemplate: template)

case .modularSmall:
let template = CLKComplicationTemplateModularSmallStackText()
template.line1TextProvider = CLKSimpleTextProvider(text: PMTitleText, shortText: PMShortTitleText)
template.line2TextProvider = CLKSimpleTextProvider(text: valueText)

entry = CLKComplicationTimelineEntry(date: date, complicationTemplate: template)

default:
preconditionFailure("Complication family not supported")
}
let template = handler.template(for: level)
entry = CLKComplicationTimelineEntry(date: date, complicationTemplate: template)
} else {
switch complication.family {
case .circularSmall:
let template = CLKComplicationTemplateCircularSmallStackText()
template.line1TextProvider = CLKSimpleTextProvider(text: PMShortTitleText)
template.line2TextProvider = CLKSimpleTextProvider(text: PlaceholderText)

entry = CLKComplicationTimelineEntry(date: Date(), complicationTemplate: template)

case .modularSmall:
let template = CLKComplicationTemplateModularSmallStackText()
template.line1TextProvider = CLKSimpleTextProvider(text: PMTitleText, shortText: PMShortTitleText)
template.line2TextProvider = CLKSimpleTextProvider(text: PlaceholderText)

entry = CLKComplicationTimelineEntry(date: Date(), complicationTemplate: template)

default:
preconditionFailure("Complication family not supported")
}
let template = handler.templateForNoValue()
entry = CLKComplicationTimelineEntry(date: Date(), complicationTemplate: template)
}

handler(entry)
callback(entry)
}


// MARK: - Placeholder Templates
func getLocalizableSampleTemplate(
for complication: CLKComplication,
withHandler handler: @escaping (CLKComplicationTemplate?) -> Void)
withHandler callback: @escaping (CLKComplicationTemplate?) -> Void)
{
// This method will be called once per supported complication, and the results will be cached
switch complication.family {
case .circularSmall:
let template = CLKComplicationTemplateCircularSmallStackText()
template.line1TextProvider = CLKSimpleTextProvider(text: PMShortTitleText)
template.line2TextProvider = CLKSimpleTextProvider(text: SampleValueText)
handler(template)

case .modularSmall:
let template = CLKComplicationTemplateModularSmallStackText()
template.line1TextProvider = CLKSimpleTextProvider(text: PMTitleText, shortText: PMShortTitleText)
template.line2TextProvider = CLKSimpleTextProvider(text: SampleValueText)
handler(template)

default:
preconditionFailure("Complication family not supported")
}
let handler = ComplicationHandlers.handler(for: complication)
let template = handler.templateForSampleValue()

callback(template)
}
}
@@ -0,0 +1,89 @@
//
// ComplicationHandler.swift
// SmogWatch WatchKit Extension
//
// Created by Kuba Suder on 27.12.2018.
// Copyright © 2018 Kuba Suder. Licensed under WTFPL license.
//
import ClockKit

private let PMTitleText = "PM10"
private let PMShortTitleText = "PM"
private let PlaceholderText = ""
private let SampleValueText = "50"

protocol ComplicationHandler {
func template(for value: Double) -> CLKComplicationTemplate
func templateForNoValue() -> CLKComplicationTemplate
func templateForSampleValue() -> CLKComplicationTemplate
}

extension ComplicationHandler {
func integerValue(_ value: Double) -> Int {
return Int(value.rounded())
}

func integerText(_ value: Double) -> String {
return String(integerValue(value))
}
}

enum ComplicationHandlers {
static func handler(for complication: CLKComplication) -> ComplicationHandler {
switch complication.family {
case .circularSmall:
return CircularSmall()
case .modularSmall:
return ModularSmall()
default:
preconditionFailure("Complication family not supported")
}
}

class CircularSmall: ComplicationHandler {
func template(for value: Double) -> CLKComplicationTemplate {
let template = CLKComplicationTemplateCircularSmallStackText()
template.line1TextProvider = CLKSimpleTextProvider(text: PMShortTitleText)
template.line2TextProvider = CLKSimpleTextProvider(text: integerText(value))
return template
}

func templateForNoValue() -> CLKComplicationTemplate {
let template = CLKComplicationTemplateCircularSmallStackText()
template.line1TextProvider = CLKSimpleTextProvider(text: PMShortTitleText)
template.line2TextProvider = CLKSimpleTextProvider(text: PlaceholderText)
return template
}

func templateForSampleValue() -> CLKComplicationTemplate {
let template = CLKComplicationTemplateCircularSmallStackText()
template.line1TextProvider = CLKSimpleTextProvider(text: PMShortTitleText)
template.line2TextProvider = CLKSimpleTextProvider(text: SampleValueText)
return template
}
}

class ModularSmall: ComplicationHandler {
func template(for value: Double) -> CLKComplicationTemplate {
let template = CLKComplicationTemplateModularSmallStackText()
template.line1TextProvider = CLKSimpleTextProvider(text: PMTitleText, shortText: PMShortTitleText)
template.line2TextProvider = CLKSimpleTextProvider(text: integerText(value))
return template
}

func templateForNoValue() -> CLKComplicationTemplate {
let template = CLKComplicationTemplateModularSmallStackText()
template.line1TextProvider = CLKSimpleTextProvider(text: PMTitleText, shortText: PMShortTitleText)
template.line2TextProvider = CLKSimpleTextProvider(text: PlaceholderText)
return template
}

func templateForSampleValue() -> CLKComplicationTemplate {
let template = CLKComplicationTemplateModularSmallStackText()
template.line1TextProvider = CLKSimpleTextProvider(text: PMTitleText, shortText: PMShortTitleText)
template.line2TextProvider = CLKSimpleTextProvider(text: SampleValueText)
return template
}
}
}
@@ -23,6 +23,7 @@
8C2A470921C5523C0029E4BC /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 8C2A470821C5523C0029E4BC /* Assets.xcassets */; };
8C2A472421CEEAAA0029E4BC /* DataStore.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8C2A472321CEEAAA0029E4BC /* DataStore.swift */; };
8C2A472621CEF41B0029E4BC /* KrakowPiosDataLoader.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8C2A472521CEF41B0029E4BC /* KrakowPiosDataLoader.swift */; };
8CDC741D21D5681100D170D0 /* ComplicationHandler.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8CDC741C21D5681100D170D0 /* ComplicationHandler.swift */; };
/* End PBXBuildFile section */

/* Begin PBXContainerItemProxy section */
@@ -89,6 +90,7 @@
8C2A470B21C5523C0029E4BC /* PushNotificationPayload.apns */ = {isa = PBXFileReference; lastKnownFileType = text; path = PushNotificationPayload.apns; sourceTree = "<group>"; };
8C2A472321CEEAAA0029E4BC /* DataStore.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DataStore.swift; sourceTree = "<group>"; };
8C2A472521CEF41B0029E4BC /* KrakowPiosDataLoader.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = KrakowPiosDataLoader.swift; sourceTree = "<group>"; };
8CDC741C21D5681100D170D0 /* ComplicationHandler.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ComplicationHandler.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
@@ -157,6 +159,7 @@
children = (
8C2A470821C5523C0029E4BC /* Assets.xcassets */,
8C2A470621C5523C0029E4BC /* ComplicationController.swift */,
8CDC741C21D5681100D170D0 /* ComplicationHandler.swift */,
8C2A472321CEEAAA0029E4BC /* DataStore.swift */,
8C2A470221C5523C0029E4BC /* ExtensionDelegate.swift */,
8C2A470A21C5523C0029E4BC /* Info.plist */,
@@ -313,6 +316,7 @@
8C2A470721C5523C0029E4BC /* ComplicationController.swift in Sources */,
8C2A470321C5523C0029E4BC /* ExtensionDelegate.swift in Sources */,
8C2A472421CEEAAA0029E4BC /* DataStore.swift in Sources */,
8CDC741D21D5681100D170D0 /* ComplicationHandler.swift in Sources */,
8C2A472621CEF41B0029E4BC /* KrakowPiosDataLoader.swift in Sources */,
8C2A470121C5523C0029E4BC /* InterfaceController.swift in Sources */,
);

0 comments on commit 0d3c073

Please sign in to comment.
You can’t perform that action at this time.