@@ -76,19 +76,24 @@ final class ComplicationController: NSObject, CLKComplicationDataSource {
7676 func getCurrentTimelineEntry( for complication: CLKComplication , withHandler handler: ( @escaping ( CLKComplicationTimelineEntry ? ) -> Void ) ) {
7777 updateChartManagerIfNeeded ( completion: {
7878 let entry : CLKComplicationTimelineEntry ?
79-
80- if let context = ExtensionDelegate . shared ( ) . loopManager. activeContext,
81- let glucoseDate = context. glucoseDate,
82- glucoseDate. timeIntervalSinceNow. minutes >= - 15 ,
83- let template = CLKComplicationTemplate . templateForFamily ( complication. family, from: context, chartGenerator: self . makeChart)
79+
80+ let settings = ExtensionDelegate . shared ( ) . loopManager. settings
81+ let timelineDate = Date ( )
82+
83+ if let context = ExtensionDelegate . shared ( ) . loopManager. activeContext,
84+ let template = CLKComplicationTemplate . templateForFamily ( complication. family,
85+ from: context,
86+ at: timelineDate,
87+ recencyInterval: settings. recencyInterval,
88+ chartGenerator: self . makeChart)
8489 {
8590 switch complication. family {
8691 case . graphicRectangular:
8792 break
8893 default :
8994 template. tintColor = . tintColor
9095 }
91- entry = CLKComplicationTimelineEntry ( date: glucoseDate , complicationTemplate: template)
96+ entry = CLKComplicationTimelineEntry ( date: timelineDate , complicationTemplate: template)
9297 } else {
9398 entry = nil
9499 }
@@ -97,22 +102,31 @@ final class ComplicationController: NSObject, CLKComplicationDataSource {
97102 } )
98103 }
99104
100- func getTimelineEntries( for complication: CLKComplication , before date: Date , limit: Int , withHandler handler: ( @escaping ( [ CLKComplicationTimelineEntry ] ? ) -> Void ) ) {
101- // Call the handler with the timeline entries prior to the given date
102- handler ( nil )
103- }
104-
105105 func getTimelineEntries( for complication: CLKComplication , after date: Date , limit: Int , withHandler handler: ( @escaping ( [ CLKComplicationTimelineEntry ] ? ) -> Void ) ) {
106106 updateChartManagerIfNeeded {
107107 let entries : [ CLKComplicationTimelineEntry ] ?
108-
109- if let context = ExtensionDelegate . shared ( ) . loopManager. activeContext,
110- let glucoseDate = context. glucoseDate,
111- glucoseDate. timeIntervalSince ( date) > 0 ,
112- let template = CLKComplicationTemplate . templateForFamily ( complication. family, from: context, chartGenerator: self . makeChart)
108+
109+ let settings = ExtensionDelegate . shared ( ) . loopManager. settings
110+
111+ guard let context = ExtensionDelegate . shared ( ) . loopManager. activeContext,
112+ let glucoseDate = context. glucoseDate else
113+ {
114+ handler ( nil )
115+ return
116+ }
117+
118+ // Stale date is just a second after glucose expires
119+ let staleDate = glucoseDate + settings. recencyInterval + 1
120+
121+ if staleDate > date,
122+ let template = CLKComplicationTemplate . templateForFamily ( complication. family,
123+ from: context,
124+ at: staleDate,
125+ recencyInterval: settings. recencyInterval,
126+ chartGenerator: self . makeChart)
113127 {
114128 template. tintColor = UIColor . tintColor
115- entries = [ CLKComplicationTimelineEntry ( date: glucoseDate , complicationTemplate: template) ]
129+ entries = [ CLKComplicationTimelineEntry ( date: staleDate , complicationTemplate: template) ]
116130 } else {
117131 entries = nil
118132 }
0 commit comments