From 4f614bcb37b8f8420f0a531ef5dac1ad69aca2b2 Mon Sep 17 00:00:00 2001 From: Laurent Date: Mon, 30 Jan 2023 17:52:39 +0100 Subject: [PATCH] Update live activity --- LaunchWidget/LaunchWidgetLiveActivity.swift | 17 +++++++---- LeCountdown.xcodeproj/project.pbxproj | 2 ++ LeCountdown/AppDelegate.swift | 2 +- LeCountdown/CountdownScheduler.swift | 32 ++++++++++++--------- LeCountdown/Info.plist | 2 -- LeCountdown/LeCountdownApp.swift | 6 ++++ 6 files changed, 39 insertions(+), 22 deletions(-) diff --git a/LaunchWidget/LaunchWidgetLiveActivity.swift b/LaunchWidget/LaunchWidgetLiveActivity.swift index 839bf22..ae6b3f2 100644 --- a/LaunchWidget/LaunchWidgetLiveActivity.swift +++ b/LaunchWidget/LaunchWidgetLiveActivity.swift @@ -41,19 +41,24 @@ struct LiveActivityView: View { } struct LaunchWidgetLiveActivity: Widget { + var body: some WidgetConfiguration { ActivityConfiguration(for: LaunchWidgetAttributes.self) { context in + let range = Date()...context.attributes.endDate + // Lock screen/banner UI goes here HStack { Text(context.attributes.name) Spacer() - if !context.state.ended { - Text(context.attributes.endDate, style: .timer) - .monospaced() - } else { - Text("It's time!") - } + Text(timerInterval: range, pauseTime: range.lowerBound) + +// if context.attributes.endDate > self.model.now { +// Text(context.attributes.endDate, style: .timer) +// .monospaced() +// } else { +// Text("It's time!") +// } }.padding() .font(.title) .activityBackgroundTint(Color.cyan) diff --git a/LeCountdown.xcodeproj/project.pbxproj b/LeCountdown.xcodeproj/project.pbxproj index 1bbfcc3..0033239 100644 --- a/LeCountdown.xcodeproj/project.pbxproj +++ b/LeCountdown.xcodeproj/project.pbxproj @@ -791,6 +791,7 @@ ENABLE_PREVIEWS = YES; GENERATE_INFOPLIST_FILE = YES; INFOPLIST_FILE = LeCountdown/Info.plist; + INFOPLIST_KEY_NSSupportsLiveActivities = YES; INFOPLIST_KEY_UIApplicationSupportsIndirectInputEvents = YES; INFOPLIST_KEY_UILaunchScreen_Generation = YES; INFOPLIST_KEY_UISupportedInterfaceOrientations = UIInterfaceOrientationPortrait; @@ -822,6 +823,7 @@ ENABLE_PREVIEWS = YES; GENERATE_INFOPLIST_FILE = YES; INFOPLIST_FILE = LeCountdown/Info.plist; + INFOPLIST_KEY_NSSupportsLiveActivities = YES; INFOPLIST_KEY_UIApplicationSupportsIndirectInputEvents = YES; INFOPLIST_KEY_UILaunchScreen_Generation = YES; INFOPLIST_KEY_UISupportedInterfaceOrientations = UIInterfaceOrientationPortrait; diff --git a/LeCountdown/AppDelegate.swift b/LeCountdown/AppDelegate.swift index b7b4531..0418b64 100644 --- a/LeCountdown/AppDelegate.swift +++ b/LeCountdown/AppDelegate.swift @@ -13,7 +13,7 @@ class AppDelegate : NSObject, UIApplicationDelegate { func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool { UNUserNotificationCenter.current().delegate = self - + AppEnvironment.sun.cleanup() return true } diff --git a/LeCountdown/CountdownScheduler.swift b/LeCountdown/CountdownScheduler.swift index c17092b..52c21c8 100644 --- a/LeCountdown/CountdownScheduler.swift +++ b/LeCountdown/CountdownScheduler.swift @@ -48,8 +48,8 @@ class CountdownScheduler { handler(.failure(error)) print("Scheduling error = \(error)") } else { - if let date = trigger.nextTriggerDate() { - AppEnvironment.sun.startCountdown(date, countdown: countdown) + if let triggerDate = trigger.nextTriggerDate() { + AppEnvironment.sun.startCountdown(triggerDate, countdown: countdown) handler(.success(trigger.nextTriggerDate())) } else { let backupDate = Date().addingTimeInterval(duration) @@ -89,7 +89,7 @@ class AppEnvironment : ObservableObject { DispatchQueue.main.async { let dateInterval = DateInterval(start: Date(), end: date) self.notificationDates[countdown.stringId] = dateInterval - + self._launchLiveActivity(countdown: countdown, endDate: date) } } @@ -179,16 +179,22 @@ class AppEnvironment : ObservableObject { return ActivityKit.Activity.activities.first(where: { $0.attributes.id == countdownId } ) } -// fileprivate func _updateLiveActivity(countdownId: String, endDate: Date) { -// if let activity = self._liveActivity(countdownId: countdownId) { -// Task { -// let state = LaunchWidgetAttributes.ContentState(ended: true) -// let content = ActivityContent(state: state, staleDate: endDate) -// await activity.update(content) -// print("Ending the Live Activity: \(activity.id)") -// } -// } -// } + func updateLiveActivities() { + + for (countdownId, interval) in self.notificationDates { + if let activity = self._liveActivity(countdownId: countdownId) { + + Task { + let ended = interval.end < Date() + let state = LaunchWidgetAttributes.ContentState(ended: ended) + let content = ActivityContent(state: state, staleDate: interval.end) + await activity.update(content) + print("Ending the Live Activity: \(activity.id)") + } + } + } + + } fileprivate func _endLiveActivity(countdownId: String) { diff --git a/LeCountdown/Info.plist b/LeCountdown/Info.plist index d5fa61c..3847eec 100644 --- a/LeCountdown/Info.plist +++ b/LeCountdown/Info.plist @@ -2,8 +2,6 @@ - NSSupportsLiveActivities - NSUserActivityTypes SelectCountdownIntent diff --git a/LeCountdown/LeCountdownApp.swift b/LeCountdown/LeCountdownApp.swift index e05da99..1506ee6 100644 --- a/LeCountdown/LeCountdownApp.swift +++ b/LeCountdown/LeCountdownApp.swift @@ -21,6 +21,8 @@ struct LeCountdownApp: App { .environment(\.managedObjectContext, persistenceController.container.viewContext) .onReceive(NotificationCenter.default.publisher(for: UIApplication.willEnterForegroundNotification)) { _ in self._willEnterForegroundNotification() + }.onAppear { + self._onAppear() } } } @@ -29,4 +31,8 @@ struct LeCountdownApp: App { AppEnvironment.sun.cleanup() } + fileprivate func _onAppear() { + + } + }