I'm working on a timer-based app, and trying to build a widget that displays several timers at once.
It should look something like this
So, in order to accommodate multiple time items, my TimelineEntry contains an array of [SingleTimeItem]
struct MultipleTimeItemsEntry: TimelineEntry {
let date: Date
let timeItems: [SingleTimeItem]
let configuration: MultipleConfigurationIntent
}
Next is my method for constructing the timeline. It's a bit incomprehensible, for which I'm sorry and tried to negate that with comments.
var timeItems = configure(results: results, configuration: configuration)
var entries = [MultipleTimeItemsEntry]()
// Append initial entry
entries.append(MultipleTimeItemsEntry(date: date, timeItems: tempTimeItems, configuration: configuration))
// Sort by the time they should finish to construct a proper timeline of entries
timeItems.sort {
$0.timeFinished < $1.timeFinished
}
// Start mapping entries to their respective dates
for i in 0...timeItems.count-1 {
var tempTimeItems = timeItems
// Mark all timers that should have finished before or simultaniously with timeItem[i] as finished
for j in 0...i {
if tempTimeItems[j].isRunning {
tempTimeItems[j].isFinished = true
}
}
// Return the array to its initial sorting
tempTimeItems.sort {
$0.timeStarted > $1.timeStarted
}
// Append entry for each timer finish (each entry containing finishes for all preceding timers)
entries.append(MultipleTimeItemsEntry(date: timeItems[i].timeFinished, timeItems: tempTimeItems, configuration: configuration))
}
return entries
In addition to that, here's the entirety of my getTimeline method:
func getTimeline(for configuration: MultipleConfigurationIntent, in context: Context, completion: @escaping (Timeline<Entry>) -> ()) {
var entries: [MultipleTimeItemsEntry] = []
let currentDate = Date()
entries.append(contentsOf: fetchTimeItemWithConfig(date: currentDate, configuration: configuration))
print("Debug output")
print(Date())
print(entries[0].date)
print(entries[0].timeItems[0].isFinished)
print(entries[1].date)
print(entries[1].timeItems[0].isFinished)
let timeline = Timeline(entries: entries, policy: .atEnd)
completion(timeline)
}
For simplicity, I've added only one timer to present this.
When a timer is started, the Debug output is this:
It consists of 2 entries: one when the timer is started, and the other when it should finish and the UI should update, and they are 30 seconds apart.
Now to the punchline: for some reason, and I don't understand this AT ALL, the update doesn't happen.
Any ideas fellas?