Update live activity

release
Laurent 3 years ago
parent e56e89aa34
commit 4f614bcb37
  1. 17
      LaunchWidget/LaunchWidgetLiveActivity.swift
  2. 2
      LeCountdown.xcodeproj/project.pbxproj
  3. 2
      LeCountdown/AppDelegate.swift
  4. 32
      LeCountdown/CountdownScheduler.swift
  5. 2
      LeCountdown/Info.plist
  6. 6
      LeCountdown/LeCountdownApp.swift

@ -41,19 +41,24 @@ struct LiveActivityView: View {
} }
struct LaunchWidgetLiveActivity: Widget { struct LaunchWidgetLiveActivity: Widget {
var body: some WidgetConfiguration { var body: some WidgetConfiguration {
ActivityConfiguration(for: LaunchWidgetAttributes.self) { context in ActivityConfiguration(for: LaunchWidgetAttributes.self) { context in
let range = Date()...context.attributes.endDate
// Lock screen/banner UI goes here // Lock screen/banner UI goes here
HStack { HStack {
Text(context.attributes.name) Text(context.attributes.name)
Spacer() Spacer()
if !context.state.ended { Text(timerInterval: range, pauseTime: range.lowerBound)
Text(context.attributes.endDate, style: .timer)
.monospaced() // if context.attributes.endDate > self.model.now {
} else { // Text(context.attributes.endDate, style: .timer)
Text("It's time!") // .monospaced()
} // } else {
// Text("It's time!")
// }
}.padding() }.padding()
.font(.title) .font(.title)
.activityBackgroundTint(Color.cyan) .activityBackgroundTint(Color.cyan)

@ -791,6 +791,7 @@
ENABLE_PREVIEWS = YES; ENABLE_PREVIEWS = YES;
GENERATE_INFOPLIST_FILE = YES; GENERATE_INFOPLIST_FILE = YES;
INFOPLIST_FILE = LeCountdown/Info.plist; INFOPLIST_FILE = LeCountdown/Info.plist;
INFOPLIST_KEY_NSSupportsLiveActivities = YES;
INFOPLIST_KEY_UIApplicationSupportsIndirectInputEvents = YES; INFOPLIST_KEY_UIApplicationSupportsIndirectInputEvents = YES;
INFOPLIST_KEY_UILaunchScreen_Generation = YES; INFOPLIST_KEY_UILaunchScreen_Generation = YES;
INFOPLIST_KEY_UISupportedInterfaceOrientations = UIInterfaceOrientationPortrait; INFOPLIST_KEY_UISupportedInterfaceOrientations = UIInterfaceOrientationPortrait;
@ -822,6 +823,7 @@
ENABLE_PREVIEWS = YES; ENABLE_PREVIEWS = YES;
GENERATE_INFOPLIST_FILE = YES; GENERATE_INFOPLIST_FILE = YES;
INFOPLIST_FILE = LeCountdown/Info.plist; INFOPLIST_FILE = LeCountdown/Info.plist;
INFOPLIST_KEY_NSSupportsLiveActivities = YES;
INFOPLIST_KEY_UIApplicationSupportsIndirectInputEvents = YES; INFOPLIST_KEY_UIApplicationSupportsIndirectInputEvents = YES;
INFOPLIST_KEY_UILaunchScreen_Generation = YES; INFOPLIST_KEY_UILaunchScreen_Generation = YES;
INFOPLIST_KEY_UISupportedInterfaceOrientations = UIInterfaceOrientationPortrait; INFOPLIST_KEY_UISupportedInterfaceOrientations = UIInterfaceOrientationPortrait;

@ -13,7 +13,7 @@ class AppDelegate : NSObject, UIApplicationDelegate {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool { func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
UNUserNotificationCenter.current().delegate = self UNUserNotificationCenter.current().delegate = self
AppEnvironment.sun.cleanup()
return true return true
} }

@ -48,8 +48,8 @@ class CountdownScheduler {
handler(.failure(error)) handler(.failure(error))
print("Scheduling error = \(error)") print("Scheduling error = \(error)")
} else { } else {
if let date = trigger.nextTriggerDate() { if let triggerDate = trigger.nextTriggerDate() {
AppEnvironment.sun.startCountdown(date, countdown: countdown) AppEnvironment.sun.startCountdown(triggerDate, countdown: countdown)
handler(.success(trigger.nextTriggerDate())) handler(.success(trigger.nextTriggerDate()))
} else { } else {
let backupDate = Date().addingTimeInterval(duration) let backupDate = Date().addingTimeInterval(duration)
@ -89,7 +89,7 @@ class AppEnvironment : ObservableObject {
DispatchQueue.main.async { DispatchQueue.main.async {
let dateInterval = DateInterval(start: Date(), end: date) let dateInterval = DateInterval(start: Date(), end: date)
self.notificationDates[countdown.stringId] = dateInterval self.notificationDates[countdown.stringId] = dateInterval
self._launchLiveActivity(countdown: countdown, endDate: date) self._launchLiveActivity(countdown: countdown, endDate: date)
} }
} }
@ -179,16 +179,22 @@ class AppEnvironment : ObservableObject {
return ActivityKit.Activity<LaunchWidgetAttributes>.activities.first(where: { $0.attributes.id == countdownId } ) return ActivityKit.Activity<LaunchWidgetAttributes>.activities.first(where: { $0.attributes.id == countdownId } )
} }
// fileprivate func _updateLiveActivity(countdownId: String, endDate: Date) { func updateLiveActivities() {
// if let activity = self._liveActivity(countdownId: countdownId) {
// Task { for (countdownId, interval) in self.notificationDates {
// let state = LaunchWidgetAttributes.ContentState(ended: true) if let activity = self._liveActivity(countdownId: countdownId) {
// let content = ActivityContent(state: state, staleDate: endDate)
// await activity.update(content) Task {
// print("Ending the Live Activity: \(activity.id)") 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) { fileprivate func _endLiveActivity(countdownId: String) {

@ -2,8 +2,6 @@
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0"> <plist version="1.0">
<dict> <dict>
<key>NSSupportsLiveActivities</key>
<true/>
<key>NSUserActivityTypes</key> <key>NSUserActivityTypes</key>
<array> <array>
<string>SelectCountdownIntent</string> <string>SelectCountdownIntent</string>

@ -21,6 +21,8 @@ struct LeCountdownApp: App {
.environment(\.managedObjectContext, persistenceController.container.viewContext) .environment(\.managedObjectContext, persistenceController.container.viewContext)
.onReceive(NotificationCenter.default.publisher(for: UIApplication.willEnterForegroundNotification)) { _ in .onReceive(NotificationCenter.default.publisher(for: UIApplication.willEnterForegroundNotification)) { _ in
self._willEnterForegroundNotification() self._willEnterForegroundNotification()
}.onAppear {
self._onAppear()
} }
} }
} }
@ -29,4 +31,8 @@ struct LeCountdownApp: App {
AppEnvironment.sun.cleanup() AppEnvironment.sun.cleanup()
} }
fileprivate func _onAppear() {
}
} }

Loading…
Cancel
Save