0

So I am new to WidgetKit and SwiftUI, but is there an event or a way to detect when the countdown reaches 00:00 for Text()?

let components = DateComponents(minute: 15)
let futureDate = Calendar.current.date(byAdding: components, to: Date())!

Text(futureDate, style: .timer)

It appears that it is either poorly documented -to me at least- or there is still nothing like that..?

Sayed
  • 1,022
  • 3
  • 12
  • 27

1 Answers1

0

It's not possible, when it reaches 0 it will start counting up.

You have to use the timeline to refresh that widget at the end date of your countdown, using a normal Text that just shows 0:00:00

an example:

var entries: [SingleEntry] = []
                // First entry at Date() which is now... with the countdown endDate at 60 seconds in the future
                // which you'll use in the Text(date, style)
                entries.append(
                    SingleEntry(
                        date: Date(),
                        configuration: configuration,
                        endDate: Date().addingTimeInterval(60)
                    )
                )
                
                // Second entry which will be scheduled at the Date when you want to stop the timer
                // in the TimelineEntry now you can check if endDate is nil and use a normale text
                // to say that the countdown is over
                entries.append(
                    SingleEntry(
                        date: Date().addingTimeInterval(60),
                        configuration: configuration,
                        endDate: nil
                    )
                )
                
                let timeline = Timeline(entries: entries, policy: .atEnd)
                completion(timeline)

Ludyem
  • 1,709
  • 1
  • 18
  • 33
  • I already have a timeline 5 minutes apart for a full day.. how do I add that check in between those entries. Also widgets dont seem to actually update exactly at the same given date – Sayed Oct 09 '20 at 12:10
  • @Sayed check the edited answer, that is all you need to start and stop the countdown, if you have to do other things in between every 5 minutes just create more entries before the endDate and setup the updated widget accordingly – Ludyem Oct 09 '20 at 12:51