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 {
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)

@ -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;

@ -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
}

@ -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<LaunchWidgetAttributes>.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) {

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

@ -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() {
}
}

Loading…
Cancel
Save