59 lines
1.9 KiB
Swift
59 lines
1.9 KiB
Swift
//
|
|
// PushManager.swift
|
|
// PushNotifications
|
|
//
|
|
// Created by Shadowfacts on 4/7/24.
|
|
//
|
|
|
|
import Foundation
|
|
import OSLog
|
|
import Pachyderm
|
|
import UserAccounts
|
|
|
|
public struct PushManager {
|
|
@MainActor
|
|
public static let shared = createPushManager()
|
|
|
|
public static let logger = Logger(subsystem: Bundle.main.bundleIdentifier!, category: "PushManager")
|
|
|
|
@MainActor
|
|
public static var captureError: ((any Error) -> Void)?
|
|
|
|
private init() {}
|
|
|
|
@MainActor
|
|
private static func createPushManager() -> any _PushManager {
|
|
guard let info = Bundle.main.object(forInfoDictionaryKey: "TuskerInfo") as? [String: Any],
|
|
let scheme = info["PushProxyScheme"] as? String,
|
|
let host = info["PushProxyHost"] as? String,
|
|
!scheme.isEmpty,
|
|
!host.isEmpty else {
|
|
logger.debug("Missing proxy info, push disabled")
|
|
return DisabledPushManager()
|
|
}
|
|
var endpoint = URLComponents()
|
|
endpoint.scheme = scheme
|
|
endpoint.host = host
|
|
let url = endpoint.url!
|
|
logger.debug("Push notifications enabled with proxy \(url.absoluteString, privacy: .public)")
|
|
return PushManagerImpl(endpoint: url)
|
|
}
|
|
}
|
|
|
|
@MainActor
|
|
public protocol _PushManager {
|
|
var enabled: Bool { get }
|
|
var proxyRegistration: PushProxyRegistration? { get }
|
|
|
|
func createSubscription(account: UserAccountInfo) throws -> PushSubscription
|
|
func removeSubscription(account: UserAccountInfo)
|
|
func pushSubscription(account: UserAccountInfo) -> PushSubscription?
|
|
|
|
func register(transactionID: UInt64) async throws -> PushProxyRegistration
|
|
func unregister() async throws
|
|
func updateIfNecessary(updateSubscription: @escaping (PushSubscription) async -> Bool) async
|
|
|
|
func didRegisterForRemoteNotifications(deviceToken: Data)
|
|
func didFailToRegisterForRemoteNotifications(error: any Error)
|
|
}
|