// // Swiftfin is subject to the terms of the Mozilla Public // License, v2.0. If a copy of the MPL was not distributed with this // file, you can obtain one at https://mozilla.org/MPL/2.0/. // // Copyright (c) 2025 Jellyfin & Jellyfin Contributors // import Combine import Factory import Foundation import JellyfinAPI import UIKit extension Container { var notificationCenter: Factory { self { NotificationCenter.default }.singleton } } enum Notifications { typealias Keys = _AnyKey class _AnyKey { typealias Key = Notifications.Key } final class Key: _AnyKey { @Injected(\.notificationCenter) private var notificationCenter let name: Notification.Name var rawValue: String { name.rawValue } init(_ string: String) { self.name = Notification.Name(string) } init(_ name: Notification.Name) { self.name = name } func post(_ payload: Payload) { notificationCenter .post( name: name, object: nil, userInfo: ["payload": payload] ) } func post() where Payload == Void { notificationCenter .post( name: name, object: nil, userInfo: nil ) } var publisher: AnyPublisher { notificationCenter .publisher(for: name) .compactMap { notification in notification.userInfo?["payload"] as? Payload } .eraseToAnyPublisher() } func subscribe(_ object: Any, selector: Selector) { notificationCenter.addObserver(object, selector: selector, name: name, object: nil) } } static subscript(key: Key) -> Key { key } } // MARK: - Keys extension Notifications.Key { // MARK: - Authentication static var didSignIn: Key { Key("didSignIn") } static var didSignOut: Key { Key("didSignOut") } // MARK: - App Flow static var processDeepLink: Key { Key("processDeepLink") } static var didPurge: Key { Key("didPurge") } static var didChangeCurrentServerURL: Key { Key("didChangeCurrentServerURL") } static var didSendStopReport: Key { Key("didSendStopReport") } static var didRequestGlobalRefresh: Key { Key("didRequestGlobalRefresh") } static var didFailMigration: Key { Key("didFailMigration") } // MARK: - Media Items // TODO: come up with a cleaner, more defined way for item update notifications /// - Payload: The new item with updated metadata. static var itemMetadataDidChange: Key { Key("itemMetadataDidChange") } /// - Payload: The ID of the item that should refresh static var itemShouldRefreshMetadata: Key { Key("itemShouldRefresh") } /// - Payload: The ID of the deleted item. static var didDeleteItem: Key { Key("didDeleteItem") } // MARK: - Server static var didConnectToServer: Key { Key("didConnectToServer") } static var didDeleteServer: Key { Key("didDeleteServer") } // MARK: - User /// - Payload: The ID of the user whose Profile Image changed. static var didChangeUserProfile: Key { Key("didChangeUserProfile") } static var didAddServerUser: Key { Key("didAddServerUser") } // MARK: - Playback static var didStartPlayback: Key { Key("didStartPlayback") } // MARK: - UIApplication static var applicationDidEnterBackground: Key { Key(UIApplication.didEnterBackgroundNotification) } static var applicationWillEnterForeground: Key { Key(UIApplication.willEnterForegroundNotification) } static var applicationWillResignActive: Key { Key(UIApplication.willResignActiveNotification) } static var applicationWillTerminate: Key { Key(UIApplication.willTerminateNotification) } }