In the Complication for my WatchOS app I show 2 variables:
- Date
- Counter (start value = 1)
The Complication has to be updated when a user manually updates the counter variable by clicking on a button. Currently I am using WidgetCenter.shared.reloadTimelines(ofKind: "Complications")
from my App to update the Timeline.
The date variable is updating correctly, but my counter value stays at 1. While it actually is 4 for example.
Here is the code for the class holding the counter:
class ExampleSettings: ObservableObject{
@Published var testCounter = 1
}
This is the Widget:
@main
struct Complications: Widget {
let kind: String = "Complications"
var body: some WidgetConfiguration {
IntentConfiguration(kind: kind, intent: ConfigurationIntent.self, provider: Provider()) { entry in
ComplicationsEntryView(entry: entry)
}
.configurationDisplayName("Example Widget")
.description("Example widget.")
}
}
The Widget view:
struct ComplicationsEntryView : View {
var entry: Provider.Entry
var body: some View {
VStack{
Text(entry.date, style: .time)
Text("\(entry.counterData)")
}
}
}
The TimelineEntry:
struct SimpleEntry: TimelineEntry {
let date: Date
let configuration: ConfigurationIntent
let counterData: Int
}
And finally the TimelineProvider:
struct Provider: IntentTimelineProvider {
func getTimeline(for configuration: ConfigurationIntent, in context: Context, completion: @escaping (Timeline<SimpleEntry>) -> ()) {
let currentCount = ExampleSettings().testCounter
let entry = SimpleEntry(date: Date(), configuration: configuration, counterData: currentCount)
let timeline = Timeline(entries: [entry], policy: .atEnd)
completion(timeline)
}
}
I suspect the problem is with the way I am currently calling let currentCount = ExampleSettings().testCounter
(part of TimelineProvider), tried a lot of options but just don't get it to work unfortunately. So I am looking for a way to retrieve the current value of the @Published var testCounter = 1
within the Provider.