My app has two static widgets. In the widget extension, I have the following code:
import WidgetKit
import SwiftUI
import ItemWidgetsKit
@main
struct ItemWidgetsBundle: WidgetBundle {
var body: some Widget {
RecentSavesWidget()
RecommendationsWidget()
}
}
the rest of the code, including the two Widget
implementations, the TimelineProvider
, etc, are in the imported ItemsWidgetKit
, which is a library inside a Swift Package containing several libraries.
These two widgets work as expected.
Now I am adding a configurable widget, and the code above changes like so:
import WidgetKit
import SwiftUI
import ItemWidgetsKit
@main
struct ItemWidgetsBundle: WidgetBundle {
@WidgetBundleBuilder var body: some Widget {
makeWidgets()
}
func makeWidgets() -> some Widget {
if #available(iOS 17.0, *) {
return WidgetBundleBuilder.buildBlock(RecentSavesWidget(), TopicRecommendationsWidget())
} else {
return WidgetBundleBuilder.buildBlock(RecentSavesWidget(), RecommendationsWidget())
}
}
}
TopicRecommendationsWidget
is similar to RecommendationsWidget
except that it lets you choose a topic to display recommendations for, by long pressing and selecting an intent.
I followed the same approach of the other two widgets, and put the code inside ItemWidgetsKit
This time, though, the widget does not work, and it only loads the placeholder; in fact, in the AppIntentTimelineProvider
implementation (see below) only placeholder(in:)
gets ever called
@available(iOS 17.0, *)
public struct TopicTimelineProvider: AppIntentTimelineProvider {
public func placeholder(in context: Context) -> TopicEntry {
return TopicEntry(date: Date(), content: TopicContent.sampleContent)
}
public func snapshot(for configuration: TopicIntent, in context: Context) async -> TopicEntry {
return TopicEntry(date: Date(), content: configuration.topicEntity.topic)
}
public func timeline(for configuration: TopicIntent, in context: Context) async -> Timeline<TopicEntry> {
let entry = TopicEntry(date: Date(), content: configuration.topicEntity.topic)
return Timeline(entries: [entry], policy: .never)
}
}
for more context, here's the code for the configurable Widget
implementation (that resides inside ItemWidgetsKit
@available(iOS 17.0, *)
public struct TopicRecommendationsWidget: Widget {
let kind: String
public init() {
self.kind = WidgetKind.topicRecommendations // a string stored elsewhere
}
public var body: some WidgetConfiguration {
AppIntentConfiguration(kind: kind, intent: TopicIntent.self, provider: TopicTimelineProvider()) { entry in
TopicWidgetContainerView(entry: entry)
}
.configurationDisplayName("New Recommendations")
.description("Select a Topic")
.supportedFamilies([.systemMedium, .systemLarge])
}
}
Any Idea why this happens and what would be the solution to this problem? Thank you