Is it possible to run LiveActivity from within SwiftUI Widget?

48 views Asked by At

I have two separated widgets: WidgetTimer and LiveActivityTimer.

This is WidgetTimer:

struct WidgetTimer: Widget {
    var families: [WidgetFamily] {
        [.systemSmall, .systemMedium, .systemLarge, .systemExtraLarge]
    }
    
    var body: some WidgetConfiguration {
        StaticConfiguration(
            kind: "WidgetTimer",
            provider: TimerTimeline(), content: { entry in
                WidgetTimerView(entry: entry)
            })
            .configurationDisplayName("Timer")
            .description("")
            .supportedFamilies(families)
            .contentMarginsDisabled()
    }
}

And LiveActivity WidgetCurrentTime :

@available(iOS 16.1, *)
struct WidgetCurrentTime: Widget {
    let kind: String = "WidgetCurrentTime"
    var body: some WidgetConfiguration {
        ActivityConfiguration(
            for: WidgetCurrentTimeAttributes.self,
            content: { context in
                fullView(with: context)
            },
            dynamicIsland: { context in
                DynamicIsland {

                    DynamicIslandExpandedRegion(.leading) {
                        serviceIcon(with: context)
                    }
                    
                    DynamicIslandExpandedRegion(.trailing) {
                        VStack(alignment: .trailing, spacing: 0) {
                            Spacer()
                                .frame(height: 10)
                            Text(timerInterval: context.state.timer, countsDown: false)
                                .foregroundColor(.white)
                                .font(.liberationMonoRegular(withSize: 21))
                                .multilineTextAlignment(.trailing)
                        }
                    }
                    
                    DynamicIslandExpandedRegion(.center) {
                        descriptionView(with: context, textColor: .white)
                    }
                    
                    DynamicIslandExpandedRegion(.bottom) {
                        statView(with: context, textColor: .white, alignment: .center)
                    }
                    
                } compactLeading: {
                    icon(with: context)
                } compactTrailing: {
                    Text(timerInterval: context.state.timer, countsDown: false)
                        .foregroundColor(.white)
                        .monospacedDigit()
                        .font(.liberationMonoRegular(withSize: 16))
                        .frame(width: 70)
                        .multilineTextAlignment(.trailing)
                } minimal: {
                    icon(with: context)
                }
                .keylineTint(.cyan)
            }
        )
    }
}

It is possible to run/update/cancel LiveActivity from Main App, but not from within WidgetTimer even I use the same code shared between all my targets. what is wrong?

0

There are 0 answers