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)
}
}