forked from LoopKit/Loop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathComplicationController.swift
130 lines (107 loc) · 5.45 KB
/
ComplicationController.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
//
// ComplicationController.swift
// WatchApp Extension
//
// Created by Nathan Racklyeft on 8/29/15.
// Copyright © 2015 Nathan Racklyeft. All rights reserved.
//
import ClockKit
import WatchKit
final class ComplicationController: NSObject, CLKComplicationDataSource {
// MARK: - Timeline Configuration
func getSupportedTimeTravelDirections(for complication: CLKComplication, withHandler handler: @escaping (CLKComplicationTimeTravelDirections) -> Void) {
handler([.backward])
}
func getTimelineStartDate(for complication: CLKComplication, withHandler handler: @escaping (Date?) -> Void) {
if let date = ExtensionDelegate.shared().lastContext?.glucoseDate {
handler(date)
} else {
handler(nil)
}
}
func getTimelineEndDate(for complication: CLKComplication, withHandler handler: @escaping (Date?) -> Void) {
if let date = ExtensionDelegate.shared().lastContext?.glucoseDate {
handler(date)
} else {
handler(nil)
}
}
func getPrivacyBehavior(for complication: CLKComplication, withHandler handler: @escaping (CLKComplicationPrivacyBehavior) -> Void) {
handler(.hideOnLockScreen)
}
// MARK: - Timeline Population
private lazy var formatter = NumberFormatter()
func getCurrentTimelineEntry(for complication: CLKComplication, withHandler handler: (@escaping (CLKComplicationTimelineEntry?) -> Void)) {
let entry: CLKComplicationTimelineEntry?
if let context = ExtensionDelegate.shared().lastContext,
let glucoseDate = context.glucoseDate,
glucoseDate.timeIntervalSinceNow.minutes >= -15,
let template = CLKComplicationTemplate.templateForFamily(complication.family, from: context)
{
template.tintColor = UIColor.tintColor
entry = CLKComplicationTimelineEntry(date: glucoseDate, complicationTemplate: template)
} else {
entry = nil
}
handler(entry)
}
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
let entries: [CLKComplicationTimelineEntry]?
if let context = ExtensionDelegate.shared().lastContext,
let glucoseDate = context.glucoseDate,
glucoseDate.timeIntervalSince(date) > 0,
let template = CLKComplicationTemplate.templateForFamily(complication.family, from: context)
{
template.tintColor = UIColor.tintColor
entries = [CLKComplicationTimelineEntry(date: glucoseDate, complicationTemplate: template)]
} else {
entries = nil
}
handler(entries)
}
// MARK: - Placeholder Templates
func getLocalizableSampleTemplate(for complication: CLKComplication, withHandler handler: @escaping (CLKComplicationTemplate?) -> Void) {
let template: CLKComplicationTemplate?
let glucoseText = CLKSimpleTextProvider.localizableTextProvider(withStringsFileTextKey: "120↘︎", shortTextKey: "120")
let timeText = CLKRelativeDateTextProvider(date: Date(), style: .natural, units: .minute)
switch complication.family {
case .graphicCorner, .graphicCircular, .graphicRectangular, .graphicBezel:
template = nil
case .modularSmall:
let modularSmall = CLKComplicationTemplateModularSmallStackText()
modularSmall.line1TextProvider = glucoseText
modularSmall.line2TextProvider = timeText
template = modularSmall
case .modularLarge:
let modularSmall = CLKComplicationTemplateModularLargeTallBody()
modularSmall.bodyTextProvider = glucoseText
modularSmall.headerTextProvider = timeText
template = modularSmall
case .circularSmall:
let circularSmall = CLKComplicationTemplateCircularSmallSimpleText()
circularSmall.textProvider = glucoseText
template = circularSmall
case .extraLarge:
let extraLarge = CLKComplicationTemplateExtraLargeStackText()
extraLarge.line1TextProvider = glucoseText
extraLarge.line2TextProvider = timeText
template = extraLarge
case .utilitarianSmall, .utilitarianSmallFlat:
let utilitarianSmallFlat = CLKComplicationTemplateUtilitarianSmallFlat()
utilitarianSmallFlat.textProvider = glucoseText
template = utilitarianSmallFlat
case .utilitarianLarge:
let utilitarianLarge = CLKComplicationTemplateUtilitarianLargeFlat()
let eventualGlucoseText = CLKSimpleTextProvider.localizableTextProvider(withStringsFileTextKey: "75")
utilitarianLarge.textProvider = CLKSimpleTextProvider.localizableTextProvider(withStringsFileFormatKey: "UtilitarianLargeFlat", textProviders: [glucoseText, eventualGlucoseText, CLKTimeTextProvider(date: Date())])
template = utilitarianLarge
}
template?.tintColor = UIColor.tintColor
handler(template)
}
}