Posts

Post not yet marked as solved
5 Replies
2.1k Views
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;} }
Posted
by ameir.
Last updated
.
Post not yet marked as solved
0 Replies
509 Views
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?
Posted
by ameir.
Last updated
.
Post not yet marked as solved
6 Replies
1.4k Views
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)
Posted
by ameir.
Last updated
.