Reputation: 4411
I have an array of 10 events, suppose current time is 10 am and the event will start at 11am. Each event is of 30 mins and gap between two events is of 30 mins.
Now, I am able to show that all the required info on Watch Face correctly, i.e, when watch face is launched, then it says, next event is at 11AM, and when first event is finished at 11:30 AM, at 11:31 AM watch face shows next event will start at 12PM.
I am able to do this all successfully.
I am facing issue when I want to show the reminder of first event just 15 mins before its starting time.
Eg, If first event of day will start at 11 AM, then watch face will show the reminder of that first event only at 10:45 AM not before 10:45AM.
Also, How can I remove Complication when last event of the day is over?
I have gone through the video of Complications WWDC - 2015.
I am trying to show Complication on CLKComplicationTemplateModularLargeStandardBody.
private func getTimeLineEntry() -> Date {
if shouldShowCountDown {
return startDate.addingTimeInterval(-15 * 60)
} else {
return startDate
}
}
func getCurrentTimelineEntry(for complication: CLKComplication, withHandler handler: @escaping (CLKComplicationTimelineEntry?) -> Void) {
// Call the handler with the current timeline entry
let headerTextProvider = CLKSimpleTextProvider(text: "Complication Updates")
let body1 = "My Title"
let body1TextProvider = CLKSimpleTextProvider(text: body1)
let body2TextProvider = CLKSimpleTextProvider(text: "My Subtitle")
let template = CLKComplicationTemplateModularLargeStandardBody(headerTextProvider: headerTextProvider, body1TextProvider: body1TextProvider, body2TextProvider: body2TextProvider)
let entry = CLKComplicationTimelineEntry(date: getTimeLineEntry(), complicationTemplate: template)
handler(entry)
}
Here, startDate is 11 AM and I want to show the reminder on Complication 15 mins before the event i.e, 10:45 AM. I have used a bool variable to decide whether current time is startDate > Current Date. If yes, then show the Countdown 15 mins before.
Upvotes: 0
Views: 269
Reputation: 4411
Fix for this issue is - We need to make first template an empty one.
func getCurrentTimelineEntry(for complication: CLKComplication, withHandler handler: @escaping (CLKComplicationTimelineEntry?) -> Void) {
// Call the handler with the current timeline entry
let headerTextProvider = CLKSimpleTextProvider(text: "")
let body1TextProvider = CLKSimpleTextProvider(text: "")
let body2TextProvider = CLKSimpleTextProvider(text: "")
let template = CLKComplicationTemplateModularLargeStandardBody(headerTextProvider: headerTextProvider, body1TextProvider: body1TextProvider, body2TextProvider: body2TextProvider)
let entry = CLKComplicationTimelineEntry(date: Date(), complicationTemplate: template)
handler(entry)
}
So, even if complication is launched at any time, it will show blank template to user and when time is of 10:45 AM, then it will start displaying first complication on WatchFace.
func getTimelineEntries(for complication: CLKComplication, after date: Date, limit: Int, withHandler handler: @escaping ([CLKComplicationTimelineEntry]?) -> Void) {
// Call the handler with the timeline entries after the given date
guard !array.isEmpty else {
handler(nil)
return
}
var entries: [CLKComplicationTimelineEntry] = []
for (index, arrayObj) in array.enumerated() {
var nextDate = Date()
nextDate = (index == 0) ? (arrayObj.startDate).addingTimeInterval(TimeInterval(-15 * 60)) : arrayObj.startDate
let headerTextProvider = CLKSimpleTextProvider(text: "Event Update")
let body1TextProvider = CLKSimpleTextProvider(text: "Event will start at")
let body2TextProvider = Date(timeInterval: arrayObj.interval, since: arrayObj.startDate)
let template = CLKComplicationTemplateModularLargeStandardBody(headerTextProvider: headerTextProvider, body1TextProvider: body1TextProvider, body2TextProvider: body2TextProvider)
let entry = CLKComplicationTimelineEntry(date: nextDate, complicationTemplate: template)
entries.append(entry)
if entries.count == limit { break }
}
handler(entries)
}
Upvotes: 0