I'm creating a watchOS 3 complication that shows departure times from a public transit service. I've created a data model with an array that contains Train
objects with a stationName
(String) and departureTime
(NSDate).
I've implemented the getCurrentTimelineEntry() method and the entries are showing on the watch. The problem is that the watch shows only the previous entry. For example, I've the following departure times:
Train(startStation: "Station name", endStation: "Station name", departureTime: stringToDate(dateString: "2016-06-20 14:00")),
Train(startStation: "Station name", endStation: "Station name", departureTime: stringToDate(dateString: "2016-06-20 14:30")),
Train(startStation: "Station name", endStation: "Station name", departureTime: stringToDate(dateString: "2016-06-20 14:45")),
If the current time is 14:10, the first entry (with time 14:00) is still showing on the watch. Until the current time is 14:30, then that entry is showing up. If the current time is 14:10, I would want to see the 14:30 departure time on my watch.
Can anybody help me with this or point me in the right direction?
func getCurrentTimelineEntry(for complication: CLKComplication, withHandler handler: ((CLKComplicationTimelineEntry?) -> Void)) {
if let train = dataProvider.getTrains().nextTrain() {
handler(timelineEntryForTrain(train: train))
} else {
handler(nil)
}
}
extension Array where Element : OnRailable {
func nextTrain() -> Element?{
let now = NSDate()
for d in self {
if now.compare(d.departureTime) == .orderedAscending{
return d
}
}
return nil
}
}