CustomNotification.swift 1.2 KB

1234567891011121314151617181920212223242526272829
  1. import Combine
  2. import Foundation
  3. extension Notification.Name {
  4. static let willUpdateOverrideConfiguration = Notification.Name("willUpdateOverrideConfiguration")
  5. static let didUpdateOverrideConfiguration = Notification.Name("didUpdateOverrideConfiguration")
  6. static let willUpdateTempTargetConfiguration = Notification.Name("willUpdateTempTargetConfiguration")
  7. static let didUpdateTempTargetConfiguration = Notification.Name("didUpdateTempTargetConfiguration")
  8. static let liveActivityOrderDidChange = Notification.Name("liveActivityOrderDidChange")
  9. static let openFromGarminConnect = Notification.Name("Notification.Name.openFromGarminConnect")
  10. }
  11. func awaitNotification(_ name: Notification.Name) async {
  12. await withCheckedContinuation { continuation in
  13. var cancellable: AnyCancellable?
  14. // Create a Combine publisher that listens for notifications
  15. cancellable = Foundation.NotificationCenter.default
  16. .publisher(for: name)
  17. .sink { _ in
  18. // When the notification is received, resume the awaiting task
  19. continuation.resume()
  20. // Cancel the subscription after the continuation has resumed
  21. cancellable?.cancel()
  22. }
  23. }
  24. }