From e95a65730ffad2c431b84325657c993b188dea8b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Benedikt=20M=C3=BCller?= <91166910+bndkt@users.noreply.github.com> Date: Thu, 14 Sep 2023 10:19:08 +0800 Subject: [PATCH] Add update and end functions --- example/SampleWidgetExtension/Module.swift | 36 ++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/example/SampleWidgetExtension/Module.swift b/example/SampleWidgetExtension/Module.swift index ae14bd2..1279b98 100644 --- a/example/SampleWidgetExtension/Module.swift +++ b/example/SampleWidgetExtension/Module.swift @@ -41,5 +41,41 @@ public class ReactNativeWidgetExtensionModule: Module { } } } + + Function("updateActivity") { (quarter: Int, scoreLeft: Int, scoreRight: Int, bottomText: String) -> Void in + let logger = Logger() + logger.info("updateActivity()") + + if #available(iOS 16.2, *) { + let future = Calendar.current.date(byAdding: .minute, value: (Int(15) ), to: Date())! + let contentState = SportsLiveActivityAttributes.ContentState(quarter: quarter, scoreLeft: scoreLeft, scoreRight: scoreRight, bottomText: bottomText) + let alertConfiguration = AlertConfiguration(title: "Score update", body: bottomText, sound: .default) + let updatedContent = ActivityContent(state: contentState, staleDate: nil) + + Task { + for activity in Activity.activities { + await activity.update(updatedContent, alertConfiguration: alertConfiguration) + logger.info("Updated the Live Activity: \(activity.id)") + } + } + } + } + + Function("endActivity") { (quarter: Int, scoreLeft: Int, scoreRight: Int, bottomText: String) -> Void in + let logger = Logger() + logger.info("endActivity()") + + if #available(iOS 16.2, *) { + let contentState = SportsLiveActivityAttributes.ContentState(quarter: quarter, scoreLeft: scoreLeft, scoreRight: scoreRight, bottomText: bottomText) + let finalContent = ActivityContent(state: contentState, staleDate: nil) + + Task { + for activity in Activity.activities { + await activity.end(finalContent, dismissalPolicy: .default) + logger.info("Ending the Live Activity: \(activity.id)") + } + } + } + } } } \ No newline at end of file