Post

Replies

Boosts

Views

Activity

WidgetKit TimelineProvider calling timeline() continuously
A newly created Widget extension, with the default code created by Xcode will continuously call the timeline() function while the widget is being debugged. Code provided below: import WidgetKit import SwiftUI struct Provider: TimelineProvider { &#9;&#9;public typealias Entry = SimpleEntry &#9;&#9;public func snapshot(with context: Context, completion: @escaping (SimpleEntry) -> ()) { &#9;&#9;&#9;&#9;NSLog("getting snapshot") &#9;&#9;&#9;&#9;let entry = SimpleEntry(date: Date()) &#9;&#9;&#9;&#9;completion(entry) &#9;&#9;} &#9;&#9;public func timeline(with context: Context, completion: @escaping (Timeline&lt;Entry&gt;) -> ()) { &#9;&#9;&#9;&#9;NSLog("getting timeline") &#9;&#9;&#9;&#9;var entries: [SimpleEntry] = [] &#9;&#9;&#9;&#9;let currentDate = Date() &#9;&#9;&#9;&#9;for hourOffset in 0 ..< 5 { &#9;&#9;&#9;&#9;&#9;&#9;let entryDate = Calendar.current.date(byAdding: .hour, value: hourOffset, to: currentDate)! &#9;&#9;&#9;&#9;&#9;&#9;let entry = SimpleEntry(date: entryDate) &#9;&#9;&#9;&#9;&#9;&#9;entries.append(entry) &#9;&#9;&#9;&#9;} &#9;&#9;&#9;&#9;NSLog("\(entries)") &#9;&#9;&#9;&#9;let timeline = Timeline(entries: entries, policy: .atEnd) &#9;&#9;&#9;&#9;completion(timeline) &#9;&#9;} } struct SimpleEntry: TimelineEntry { &#9;&#9;public let date: Date } struct PlaceholderView : View { &#9;&#9;var body: some View { &#9;&#9;&#9;&#9;Text("Placeholder View") &#9;&#9;} } struct SampleWidgetEntryView : View { &#9;&#9;var entry: Provider.Entry &#9;&#9;var body: some View { &#9;&#9;&#9;&#9;VStack { &#9;&#9;&#9;&#9;&#9;&#9;Text(entry.date, style: .time) &#9;&#9;&#9;&#9;} &#9;&#9;} } @main struct SampleWidget: Widget { &#9;&#9;private let kind: String = "SampleWidget" &#9;&#9;public var body: some WidgetConfiguration { &#9;&#9;&#9;&#9;StaticConfiguration(kind: kind, provider: Provider(), placeholder: PlaceholderView()) { entry in &#9;&#9;&#9;&#9;&#9;&#9;SampleWidgetEntryView(entry: entry) &#9;&#9;&#9;&#9;} &#9;&#9;&#9;&#9;.configurationDisplayName("My Widget") &#9;&#9;&#9;&#9;.description("This is an example widget.") &#9;&#9;} }
5
0
2.6k
Jun ’20
Relative date in widget gets cut off
I'm trying to display a countdown in my widget, similar to the one shown in the WWDC video on WidgetKit. The problem is that as the time changes the font size is not readjusting and the text is getting cut off. Is there a workaround for this behavior? Here is my code: VStack(alignment: .leading) { &#9;&#9;Text("\(entry.name) is in") &#9;&#9;&#9;&#9;.font(.subheadline)      Text(entry.next.time, style: .relative)          .font(.title2)  &#9;      .lineLimit(1)  &#9;      .frame(maxWidth: .infinity, alignment: .leading) } .minimumScaleFactor(0.5) .allowsTightening(true)
6
0
1.6k
Aug ’20
Widget with relative date text not updating when device is locked
I have a widget that uses a "countdown" or "timer" by using a Text view of a date with a style of relative. The first time this widget is viewed on the Today screen in a locked state it gets the correct data, however all subsequent viewing of the widget from a locked device does not change the "countdown" at all. Even locking, viewing the now correct today screen and then locking again, will go back to the data it seems to have cached on the first viewing of that widget from the locked state. Is there anyway for this relative date to update while the device is locked?
0
0
580
Nov ’21