fix 通知中心

This commit is contained in:
anlicheng 2026-04-15 10:34:03 +08:00
parent 5bb39c8564
commit ac01e68311
2 changed files with 72 additions and 0 deletions

View File

@ -0,0 +1,71 @@
//
// SDLTunnelAppNotifier.swift
// Tun
//
// Created by on 2026/4/15.
//
import Foundation
final class SDLTunnelAppNotifier {
struct Event: Codable, Sendable {
enum Level: String, Codable, Sendable {
case info
case warning
case error
case fatal
}
let id: String
let timestamp: TimeInterval
let level: Level
let code: Int?
let message: String
}
static let shared = SDLTunnelAppNotifier()
static let appGroupSuiteName = "group.com.jihe.punchnetmac"
static let latestEventKey = "tunnel.latestEvent"
private let suiteName: String
private let eventKey: String
init(suiteName: String = SDLTunnelAppNotifier.appGroupSuiteName,
eventKey: String = SDLTunnelAppNotifier.latestEventKey) {
self.suiteName = suiteName
self.eventKey = eventKey
}
func publish(level: Event.Level = .error, code: Int? = nil, message: String) {
let event = Event(
id: UUID().uuidString,
timestamp: Date().timeIntervalSince1970,
level: level,
code: code,
message: message
)
self.publish(event)
}
func publish(_ event: Event) {
guard let shared = UserDefaults(suiteName: self.suiteName),
let data = try? JSONEncoder().encode(event) else {
return
}
shared.set(data, forKey: self.eventKey)
shared.synchronize()
DarwinNotificationCenter.shared.post(.tunnelEventChanged)
}
func clear() {
guard let shared = UserDefaults(suiteName: self.suiteName) else {
return
}
shared.removeObject(forKey: self.eventKey)
shared.synchronize()
DarwinNotificationCenter.shared.post(.tunnelEventChanged)
}
}

View File

@ -18,6 +18,7 @@ public struct DarwinNotificationName: RawRepresentable, Hashable {
//
extension DarwinNotificationName {
static let vpnStatusChanged = DarwinNotificationName(rawValue: "com.jihe.punchnetmac.vpnStatusChanged")
static let tunnelEventChanged = DarwinNotificationName(rawValue: "com.jihe.punchnetmac.tunnelEventChanged")
}