ios – Swift: Getting Widget timer to point out up on a State variable

0
18
ios – Swift: Getting Widget timer to point out up on a State variable


Presently have timer that counts down the remaining time till time modifications for each minute. Bought the the rely down working however questioning methods to get the rely down that is being exhibited to be hooked up an @State Variable. However truthfully since it is a widget I really feel prefer it won’t be doable. Code is beneath

struct DigitalClockView: View {
    let entry: DigitalClockEntry
    let elements: DateComponents
    let futureDate: Date
    
    
    var currentTime: String {
        let time = entry.date
        let timeFormatter = DateFormatter()
        timeFormatter.dateFormat = "H:mm"
        return timeFormatter.string(from: time)
    }
    
    @State var countDown: String = ""
    
    init(entry: DigitalClockEntry) {
        self.entry = entry
        let distinction =  60 - Calendar.present.element(.second, from: entry.date)
        self.elements = DateComponents(second: distinction)
        self.futureDate = Calendar.present.date(byAdding: elements, to: entry.date)!
    }
    
    
    var physique: some View {
        VStack(alignment: .middle) {
            Textual content(futureDate, type: .timer)
            Textual content(currentTime)

        }
    }
}

extension Date {
    func including(
        _ element: Calendar.Element,
        worth: Int,
        in calendar: Calendar = .present
    ) -> Self {
        calendar.date(byAdding: element, worth: worth, to: self)!
    }
}

/// Supplier

struct DigitalClockProvider: TimelineProvider {
    non-public let placeholderEntry = DigitalClockEntry(
        date: Date(),
        futureDate: Date()
    )
    
    func placeholder(in context: Context) -> DigitalClockEntry{
        placeholderEntry
    }
    
    
    func getSnapshot(in context: Context, completion: @escaping (DigitalClockEntry) -> ()) {
        completion(DigitalClockEntry(date: Date(), futureDate: Date()))
    }
    
    func getTimeline(in context: Context, completion: @escaping (Timeline) -> Void) {
        let currentDate = Date()
        let entryDate = Calendar.present.date(byAdding: .second, worth: 60, to: currentDate)!

        let entries = [ DigitalClockEntry(date: currentDate, futureDate: entryDate) ]

        let timeline = Timeline(entries: entries, coverage: .after(currentDate))
                completion(timeline)
    }
}

LEAVE A REPLY

Please enter your comment!
Please enter your name here