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

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


At present have timer that counts down the remaining time till time adjustments for each minute. Obtained the the depend down working however questioning tips on how to get the depend down that is being exhibited to be hooked up an @State Variable. However actually since it is a widget I really feel prefer it won’t be potential. Code is under

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.part(.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: .heart) {
            Textual content(futureDate, type: .timer)
            Textual content(currentTime)

        }
    }
}

extension Date {
    func including(
        _ part: Calendar.Element,
        worth: Int,
        in calendar: Calendar = .present
    ) -> Self {
        calendar.date(byAdding: part, 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<DigitalClockEntry>) -> 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)
    }
}

author avatar
roosho Senior Engineer (Technical Services)
I am Rakib Raihan RooSho, Jack of all IT Trades. You got it right. Good for nothing. I try a lot of things and fail more than that. That's how I learn. Whenever I succeed, I note that in my cookbook. Eventually, that became my blog. 
rooshohttps://www.roosho.com
I am Rakib Raihan RooSho, Jack of all IT Trades. You got it right. Good for nothing. I try a lot of things and fail more than that. That's how I learn. Whenever I succeed, I note that in my cookbook. Eventually, that became my blog. 

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here


Latest Articles

author avatar
roosho Senior Engineer (Technical Services)
I am Rakib Raihan RooSho, Jack of all IT Trades. You got it right. Good for nothing. I try a lot of things and fail more than that. That's how I learn. Whenever I succeed, I note that in my cookbook. Eventually, that became my blog.