Skip to content

Commit

Permalink
调整代码
Browse files Browse the repository at this point in the history
  • Loading branch information
Finb committed Jan 21, 2025
1 parent 3cf72b1 commit 294f822
Showing 1 changed file with 70 additions and 72 deletions.
142 changes: 70 additions & 72 deletions Bark/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -104,80 +104,17 @@ class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterD
}

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" {
return
}

// URL 直接打开
if let url = url {
Client.shared.openUrl(url: url)
return
}

let alertController = UIAlertController(title: title, message: body, preferredStyle: .alert)
alertController.addAction(UIAlertAction(title: NSLocalizedString("CopyContent"), style: .default, handler: { _ in
if let copy = userInfo["copy"] as? String {
UIPasteboard.general.string = copy
} else {
UIPasteboard.general.string = body
}
}))
alertController.addAction(UIAlertAction(title: NSLocalizedString("MoreActions"), style: .default, handler: { _ in
var shareContent = ""
if let title = title {
shareContent += "\(title)\n"
}
if let subtitle = subtitle {
shareContent += "\(subtitle)\n"
}
if let body = body {
shareContent += "\(body)\n"
}
for (key, value) in userInfo {
if ["aps", "title", "subtitle", "body", "url"].contains((key as? String) ?? "") {
continue
}
shareContent += "\(key): \(value) \n"
}
var items: [Any] = []
items.append(shareContent)
if let url = url {
items.append(url)
}
let controller = Client.shared.window?.rootViewController
let activityController = UIActivityViewController(activityItems: items,
applicationActivities: nil)
if let popover = activityController.popoverPresentationController {
popover.sourceView = controller?.view
popover.sourceRect = CGRect(x: controller?.view.bounds.midX ?? 0, y: controller?.view.bounds.midY ?? 0, width: 0, height: 0)
}
controller?.present(activityController, animated: true, completion: nil)
}))
alertController.addAction(UIAlertAction(title: NSLocalizedString("Cancel"), style: .cancel, handler: nil))

viewController?.present(alertController, animated: true, completion: nil)
if let action = userInfo["action"] as? String, action == "none" {
return
}

if let presentedController = viewController?.presentedViewController {
presentedController.dismiss(animated: false) {
presentController()
}
} else {
presentController()

// URL 直接打开
if let url = try? (userInfo["url"] as? String)?.asURL() {
Client.shared.openUrl(url: url)
return
}

alertNotification(userInfo: userInfo)
}

func applicationWillResignActive(_ application: UIApplication) {
Expand Down Expand Up @@ -219,3 +156,64 @@ class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterD
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)
alertController.addAction(UIAlertAction(title: NSLocalizedString("CopyContent"), style: .default, handler: { _ in
if let copy = userInfo["copy"] as? String {
UIPasteboard.general.string = copy
} else {
UIPasteboard.general.string = body
}
}))
alertController.addAction(UIAlertAction(title: NSLocalizedString("MoreActions"), style: .default, handler: { _ in
var shareContent = ""
if let title = title {
shareContent += "\(title)\n"
}
if let subtitle = subtitle {
shareContent += "\(subtitle)\n"
}
if let body = body {
shareContent += "\(body)\n"
}
for (key, value) in userInfo {
if ["aps", "title", "subtitle", "body", "url"].contains((key as? String) ?? "") {
continue
}
shareContent += "\(key): \(value) \n"
}
var items: [Any] = []
items.append(shareContent)
if let url = url {
items.append(url)
}
let controller = Client.shared.window?.rootViewController
let activityController = UIActivityViewController(activityItems: items,
applicationActivities: nil)
if let popover = activityController.popoverPresentationController {
popover.sourceView = controller?.view
popover.sourceRect = CGRect(x: controller?.view.bounds.midX ?? 0, y: controller?.view.bounds.midY ?? 0, width: 0, height: 0)
}
controller?.present(activityController, animated: true, completion: nil)
}))
alertController.addAction(UIAlertAction(title: NSLocalizedString("Cancel"), style: .cancel, handler: nil))

let viewController = Client.shared.currentSnackbarController

if let presentedController = viewController?.presentedViewController {
presentedController.dismiss(animated: false) {
viewController?.present(alertController, animated: true, completion: nil)
}
} else {
viewController?.present(alertController, animated: true, completion: nil)
}
}
}

0 comments on commit 294f822

Please sign in to comment.