Skip to content

Commit

Permalink
Implement send notification
Browse files Browse the repository at this point in the history
  • Loading branch information
nekohasekai committed Nov 7, 2024
1 parent c72167b commit 348f280
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 25 deletions.
17 changes: 11 additions & 6 deletions ApplicationLibrary/Views/Dashboard/DashboardView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -138,16 +138,21 @@ public struct DashboardView: View {
private func loopShowDeprecateNotes(_ reports: any LibboxDeprecatedNoteIteratorProtocol) {
if reports.hasNext() {
let report = reports.next()!
NSLog("show next")
alert = Alert(
title: Text("Deprecated Warning"),
message: Text(report.message()),
primaryButton: .cancel(Text("Ok")) {
loopShowDeprecateNotes(reports)
},
secondaryButton: .default(Text("Documentation")) {
primaryButton: .default(Text("Documentation")) {
openURL(URL(string: report.migrationLink)!)
loopShowDeprecateNotes(reports)
Task.detached {
try await Task.sleep(nanoseconds: 300 * MSEC_PER_SEC)
await loopShowDeprecateNotes(reports)
}
},
secondaryButton: .cancel(Text("Ok")) {
Task.detached {
try await Task.sleep(nanoseconds: 300 * MSEC_PER_SEC)
await loopShowDeprecateNotes(reports)
}
}
)
}
Expand Down
5 changes: 1 addition & 4 deletions ApplicationLibrary/Views/Dashboard/ExtensionStatusView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import SwiftUI

public struct ExtensionStatusView: View {
@Environment(\.scenePhase) private var scenePhase
@Environment(\.openURL) private var openURL
@StateObject private var commandClient = CommandClient(.status)

@State private var columnCount: Int = 4
Expand Down Expand Up @@ -91,9 +90,7 @@ public struct ExtensionStatusView: View {
.padding([.top, .leading, .trailing])
}
.onAppear {
commandClient.connect { urlString in
openURL(URL(string: urlString)!)
}
commandClient.connect()
}
.onDisappear {
commandClient.disconnect()
Expand Down
14 changes: 1 addition & 13 deletions Library/Network/CommandClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ public class CommandClient: ObservableObject {
private let logMaxLines: Int
private var commandClient: LibboxCommandClient?
private var connectTask: Task<Void, Error>?
private var openURLFunc: ((String) -> Void)?

@Published public var isConnected: Bool
@Published public var status: LibboxStatusMessage?
@Published public var groups: [LibboxOutboundGroup]?
Expand All @@ -31,15 +29,13 @@ public class CommandClient: ObservableObject {
public init(_ connectionType: ConnectionType, logMaxLines: Int = 300) {
self.connectionType = connectionType
self.logMaxLines = logMaxLines
openURLFunc = nil
logList = []
clashModeList = []
clashMode = ""
isConnected = false
}

public func connect(_ openURLFunc: ((String) -> Void)? = nil) {
self.openURLFunc = openURLFunc
public func connect() {
if isConnected {
return
}
Expand All @@ -52,7 +48,6 @@ public class CommandClient: ObservableObject {
}

public func disconnect() {
openURLFunc = nil
if let connectTask {
connectTask.cancel()
self.connectTask = nil
Expand Down Expand Up @@ -227,13 +222,6 @@ public class CommandClient: ObservableObject {
commandClient.connections = connections
}
}

func openURL(_ url: String?) {
guard let url else {
return
}
commandClient.openURLFunc?(url)
}
}
}

Expand Down
25 changes: 25 additions & 0 deletions Library/Network/ExtensionPlatformInterface.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import Foundation
import Libbox
import NetworkExtension
import UserNotifications
#if canImport(CoreWLAN)
import CoreWLAN
#endif
Expand Down Expand Up @@ -338,4 +339,28 @@ public class ExtensionPlatformInterface: NSObject, LibboxPlatformInterfaceProtoc
func reset() {
networkSettings = nil
}

public func send(_ notification: LibboxNotification?) throws {
#if !os(tvOS)
guard let notification else {
return
}
let center = UNUserNotificationCenter.current()
let content = UNMutableNotificationContent()

content.title = notification.title
content.subtitle = notification.subtitle
content.body = notification.body
if !notification.openURL.isEmpty {
content.userInfo["OPEN_URL"] = notification.openURL
content.categoryIdentifier = "OPEN_URL"
}
content.interruptionLevel = .active
let request = UNNotificationRequest(identifier: notification.identifier, content: content, trigger: nil)
try runBlocking {
try await center.requestAuthorization(options: [.alert])
try await center.add(request)
}
#endif
}
}
33 changes: 32 additions & 1 deletion MacLibrary/ApplicationDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,25 @@ import ApplicationLibrary
import Foundation
import Libbox
import Library
import UserNotifications

open class ApplicationDelegate: NSObject, NSApplicationDelegate {
open class ApplicationDelegate: NSObject, NSApplicationDelegate, UNUserNotificationCenterDelegate {
public func applicationDidFinishLaunching(_: Notification) {
NSLog("Here I stand")
LibboxSetup(FilePath.sharedDirectory.relativePath, FilePath.workingDirectory.relativePath, FilePath.cacheDirectory.relativePath, false)
let notificationCenter = UNUserNotificationCenter.current()
notificationCenter.setNotificationCategories([
UNNotificationCategory(
identifier: "OPEN_URL",
actions: [
UNNotificationAction(identifier: "COPY_URL", title: "Copy URL", options: .foreground, icon: UNNotificationActionIcon(systemImageName: "clipboard.fill")),
UNNotificationAction(identifier: "OPEN_URL", title: "Open", options: .foreground, icon: UNNotificationActionIcon(systemImageName: "safari.fill")),
],
intentIdentifiers: []
),
]
)
notificationCenter.delegate = self
let event = NSAppleEventManager.shared().currentAppleEvent
let launchedAsLogInItem =
event?.eventID == kAEOpenApplication &&
Expand All @@ -34,6 +48,23 @@ open class ApplicationDelegate: NSObject, NSApplicationDelegate {
}
}

public func userNotificationCenter(_: UNUserNotificationCenter, willPresent _: UNNotification) async -> UNNotificationPresentationOptions {
.banner
}

public func userNotificationCenter(_: UNUserNotificationCenter, didReceive response: UNNotificationResponse) async {
if let url = response.notification.request.content.userInfo["OPEN_URL"] as? String {
switch response.actionIdentifier {
case "COPY_URL":
NSPasteboard.general.setString(url, forType: .URL)
case "OPEN_URL":
fallthrough
default:
NSWorkspace.shared.open(URL(string: url)!)
}
}
}

public func applicationShouldTerminateAfterLastWindowClosed(_: NSApplication) -> Bool {
SharedPreferences.inDebug || !SharedPreferences.menuBarExtraInBackground.getBlocking()
}
Expand Down
33 changes: 32 additions & 1 deletion SFI/ApplicationDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,48 @@ import Libbox
import Library
import Network
import UIKit
import UserNotifications

class ApplicationDelegate: NSObject, UIApplicationDelegate {
class ApplicationDelegate: NSObject, UIApplicationDelegate, UNUserNotificationCenterDelegate {
private var profileServer: ProfileServer?

func application(_: UIApplication, didFinishLaunchingWithOptions _: [UIApplication.LaunchOptionsKey: Any]? = nil) -> Bool {
NSLog("Here I stand")
LibboxSetup(FilePath.sharedDirectory.relativePath, FilePath.workingDirectory.relativePath, FilePath.cacheDirectory.relativePath, false)
let notificationCenter = UNUserNotificationCenter.current()
notificationCenter.setNotificationCategories([
UNNotificationCategory(
identifier: "OPEN_URL",
actions: [
UNNotificationAction(identifier: "COPY_URL", title: "Copy URL", options: .foreground, icon: UNNotificationActionIcon(systemImageName: "clipboard.fill")),
UNNotificationAction(identifier: "OPEN_URL", title: "Open", options: .foreground, icon: UNNotificationActionIcon(systemImageName: "safari.fill")),
],
intentIdentifiers: []
),
]
)
notificationCenter.delegate = self
setup()
return true
}

func userNotificationCenter(_: UNUserNotificationCenter, willPresent _: UNNotification) async -> UNNotificationPresentationOptions {
.banner
}

func userNotificationCenter(_: UNUserNotificationCenter, didReceive response: UNNotificationResponse) async {
if let url = response.notification.request.content.userInfo["OPEN_URL"] as? String {
switch response.actionIdentifier {
case "COPY_URL":
UIPasteboard.general.string = url
case "OPEN_URL":
fallthrough
default:
await UIApplication.shared.open(URL(string: url)!)
}
}
}

private func setup() {
do {
try UIProfileUpdateTask.configure()
Expand Down

0 comments on commit 348f280

Please sign in to comment.