0

Is it possible to dynamically update a Widget / show a View based on a specific time?

For example, something like that the following code:

func getTimeline(for configuration: ConfigurationIntent, in context: Context, completion: @escaping (Timeline<Entry>) -> ()) {
        var entries: [SimpleEntry] = []
    

    // Generate a timeline consisting of five entries an hour apart, starting from the current date.
    let currentDate = Date()
   
    // Get specific date / time to update then compare it to system's current time. 
    var components = DateComponents()
    components.hour = 12
    components.month = 0
    let midnight = Calendar.current.date(from: components)
    

    if midnight == currentDate {
    
        let entry = SimpleEntry(date: Date(), text: "",  configuration: ConfigurationIntent())
        entries.append(entry)
        
        let timeline = Timeline(entries: entries, policy: .atEnd)
        completion(timeline)
        
    } else {
        
        for hourOffset in 0 ..< 6 {
            let entryDate = Calendar.current.date(byAdding: .hour, value: hourOffset, to: currentDate)!
        
            let entry = SimpleEntry(date: entryDate, text: update,  configuration: ConfigurationIntent())
            entries.append(entry)
        }
        let timeline = Timeline(entries: entries, policy: .atEnd)
        completion(timeline)
        
    }
    
}
pawello2222
  • 46,897
  • 22
  • 145
  • 209
P. Lemelle
  • 41
  • 1
  • 3
  • 1
    Please add more information. It's not clear what you're trying to do. – pawello2222 Nov 27 '20 at 22:30
  • I was wondering if it it's possible to have the Widget "reset" / present a new entry View at a certain time? For example, if 'text' field gets updated throughout the day, is it possible to have its value be automatically cleared at a certain time? – P. Lemelle Nov 28 '20 at 22:02
  • Does this answer your question? [How to change view on midnight in WidgetKit, SwiftUI?](https://stackoverflow.com/q/64209612/8697793) – pawello2222 Nov 28 '20 at 22:50
  • This might also help you: [SwiftUI iOS 14 Widget CountDown](https://stackoverflow.com/q/64155800/8697793) – pawello2222 Nov 28 '20 at 22:52
  • Yes, this solved my problem. Thank you. – P. Lemelle Dec 02 '20 at 21:50
  • I want to update widget 6am to 6pm. Any suggestions? @pawello2222 – Sukh Aug 17 '21 at 04:44

0 Answers0