调整代码

This commit is contained in:
Fin 2025-01-21 12:12:00 +08:00
parent 3cf72b107e
commit 294f8224f0

View File

@ -104,29 +104,67 @@ class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterD
} }
private func notificatonHandler(userInfo: [AnyHashable: Any]) { private func notificatonHandler(userInfo: [AnyHashable: Any]) {
let viewController = Client.shared.currentSnackbarController
func presentController() {
let alert = (userInfo["aps"] as? [String: Any])?["alert"] as? [String: Any]
let title = alert?["title"] as? String
let subtitle = alert?["subtitle"] as? String
let body = alert?["body"] as? String
let url: URL? = {
if let url = userInfo["url"] as? String {
return URL(string: url)
}
return nil
}()
if let action = userInfo["action"] as? String, action == "none" { if let action = userInfo["action"] as? String, action == "none" {
return return
} }
// URL // URL
if let url = url { if let url = try? (userInfo["url"] as? String)?.asURL() {
Client.shared.openUrl(url: url) Client.shared.openUrl(url: url)
return return
} }
alertNotification(userInfo: userInfo)
}
func applicationWillResignActive(_ application: UIApplication) {
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
}
func applicationDidEnterBackground(_ application: UIApplication) {
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}
func applicationWillEnterForeground(_ application: UIApplication) {
// -1
// 0
UIApplication.shared.applicationIconBadgeNumber = -1
}
func applicationWillTerminate(_ application: UIApplication) {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey: Any] = [:]) -> Bool {
if url.scheme?.lowercased() == "bark" && url.host?.lowercased() == "addserver" {
//
let queryItems = URLComponents(url: url, resolvingAgainstBaseURL: false)?.queryItems
let address = queryItems?.first(where: { $0.name == "address" })?.value
//
if let serverAddress = try? address?.asURL() {
let server = Server(address: serverAddress.absoluteString, key: "")
ServerManager.shared.addServer(server: server)
ServerManager.shared.setCurrentServer(serverId: server.id)
ServerManager.shared.syncAllServers()
HUDSuccess(NSLocalizedString("AddedSuccessfully"))
}
return true
}
return false
}
}
extension AppDelegate {
func alertNotification(userInfo: [AnyHashable: Any]) {
let alert = (userInfo["aps"] as? [String: Any])?["alert"] as? [String: Any]
let title = alert?["title"] as? String
let subtitle = alert?["subtitle"] as? String
let body = alert?["body"] as? String
let url = try? (userInfo["url"] as? String)?.asURL()
let alertController = UIAlertController(title: title, message: body, preferredStyle: .alert) let alertController = UIAlertController(title: title, message: body, preferredStyle: .alert)
alertController.addAction(UIAlertAction(title: NSLocalizedString("CopyContent"), style: .default, handler: { _ in alertController.addAction(UIAlertAction(title: NSLocalizedString("CopyContent"), style: .default, handler: { _ in
if let copy = userInfo["copy"] as? String { if let copy = userInfo["copy"] as? String {
@ -168,54 +206,14 @@ class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterD
})) }))
alertController.addAction(UIAlertAction(title: NSLocalizedString("Cancel"), style: .cancel, handler: nil)) alertController.addAction(UIAlertAction(title: NSLocalizedString("Cancel"), style: .cancel, handler: nil))
viewController?.present(alertController, animated: true, completion: nil) let viewController = Client.shared.currentSnackbarController
}
if let presentedController = viewController?.presentedViewController { if let presentedController = viewController?.presentedViewController {
presentedController.dismiss(animated: false) { presentedController.dismiss(animated: false) {
presentController() viewController?.present(alertController, animated: true, completion: nil)
} }
} else { } else {
presentController() viewController?.present(alertController, animated: true, completion: nil)
} }
} }
func applicationWillResignActive(_ application: UIApplication) {
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
}
func applicationDidEnterBackground(_ application: UIApplication) {
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}
func applicationWillEnterForeground(_ application: UIApplication) {
// -1
// 0
UIApplication.shared.applicationIconBadgeNumber = -1
}
func applicationWillTerminate(_ application: UIApplication) {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey: Any] = [:]) -> Bool {
if url.scheme?.lowercased() == "bark" && url.host?.lowercased() == "addserver" {
//
let queryItems = URLComponents(url: url, resolvingAgainstBaseURL: false)?.queryItems
let address = queryItems?.first(where: { $0.name == "address" })?.value
//
if let serverAddress = try? address?.asURL() {
let server = Server(address: serverAddress.absoluteString, key: "")
ServerManager.shared.addServer(server: server)
ServerManager.shared.setCurrentServer(serverId: server.id)
ServerManager.shared.syncAllServers()
HUDSuccess(NSLocalizedString("AddedSuccessfully"))
}
return true
}
return false
}
} }