Hi there,
i have just made a widget, whoohoo! As you can see in the code below, it draws an icon and a random number.
This works fine, however, i have noticed when running, that it is drawn twice every time. That is, first a number is shown, then a second later, it changes again. This seems unneccessary, and i don't understand why it happens.
Hoping that someone can show me the light. Code:
import SwiftUI
import Intents
struct Provider: IntentTimelineProvider {
func placeholder(in context: Context) -> WidgetEntry {
print(">>>> MyWidget;getTimeline")
return WidgetEntry(status: 0, date: Date(), configuration: ConfigurationIntent())
}
func getSnapshot(for configuration: ConfigurationIntent, in context: Context, completion: @escaping (WidgetEntry) -> ()) {
print(">>>> MyWidget;getSnapshot")
let entry = WidgetEntry(status: 0, date: Date(), configuration: configuration)
completion(entry)
}
func getTimeline(for configuration: ConfigurationIntent, in context: Context, completion: @escaping (Timeline<Entry>) -> ()) {
print(">>>> MyWidget;getTimeline")
let entry = WidgetEntry(status: 0, date: Date(), configuration: configuration)
let timeline = Timeline(entries: [entry], policy: .never)
completion(timeline)
}
}
struct WidgetEntry: TimelineEntry {
let status: Int
let date: Date
let configuration: ConfigurationIntent
}
struct WidgetView : View {
var entry: Provider.Entry
init(entry: Provider.Entry){
print(">>>> WidgetView;init()")
self.entry = entry;
}
var body: some View {
//Text(entry.date, style: .time)
VStack{
Image("check")
Text("# \(Int.random(in: 1..<6))")
}
}
}
@main
struct MyWidget: Widget {
let kind: String = "MyWidget"
var body: some WidgetConfiguration {
IntentConfiguration(kind: kind, intent: ConfigurationIntent.self, provider: Provider()) { entry in
WidgetView(entry: entry)
}
.configurationDisplayName("My Widget")
.description("Leeloo Dallas Multipass.")
.supportedFamilies([.accessoryInline, .accessoryCircular, .systemSmall])
}
}
struct MyWidget_Previews: PreviewProvider {
static var previews: some View {
WidgetView(entry: WidgetEntry(status: 0, date: Date(), configuration: ConfigurationIntent()))
.previewContext(WidgetPreviewContext(family: .accessoryCircular))
}
}