Here is how you can create a countdown and when the time is over display some other text.
- Create an Entry where
endDate
is optional - if it's nil
it means that the countdown is over:
struct SimpleEntry: TimelineEntry {
let date: Date
var endDate: Date?
}
- In your Provider create two Entries - one for the countdown time and one for when the countdown is over:
struct SimpleProvider: TimelineProvider {
...
func getTimeline(in context: Context, completion: @escaping (Timeline<SimpleEntry>) -> Void) {
let currentDate = Date()
let endDate = Calendar.current.date(byAdding: .second, value: 15, to: currentDate)!
let entries = [
SimpleEntry(date: currentDate, endDate: endDate),
SimpleEntry(date: endDate),
]
let timeline = Timeline(entries: entries, policy: .never)
completion(timeline)
}
}
- Use it in your view:
struct WidgetEntryView: View {
var entry: Provider.Entry
var body: some View {
if let endDate = entry.endDate {
Text(endDate, style: .relative)
} else {
Text("Timer finished")
}
}
}