mirror of
https://github.com/leonboe1/QuickDrop.git
synced 2026-11-19 12:56:06 +00:00
Fix clipboard notification occasionally not appearing
This commit is contained in:
1 parent
4394b78242
commit
db70d40e43
3 files changed
+23
-9
No files matched your search
@@ -1798,7 +1798,7 @@
|
|||||||
MTL_FAST_MATH = YES;
|
MTL_FAST_MATH = YES;
|
||||||
ONLY_ACTIVE_ARCH = YES;
|
ONLY_ACTIVE_ARCH = YES;
|
||||||
SDKROOT = macosx;
|
SDKROOT = macosx;
|
||||||
SHARED_BUILD = 144;
|
SHARED_BUILD = 147;
|
||||||
SHARED_VERSION = 4.0;
|
SHARED_VERSION = 4.0;
|
||||||
STRING_CATALOG_GENERATE_SYMBOLS = YES;
|
STRING_CATALOG_GENERATE_SYMBOLS = YES;
|
||||||
SWIFT_ACTIVE_COMPILATION_CONDITIONS = DEBUG;
|
SWIFT_ACTIVE_COMPILATION_CONDITIONS = DEBUG;
|
||||||
@@ -1861,7 +1861,7 @@
|
|||||||
MTL_ENABLE_DEBUG_INFO = NO;
|
MTL_ENABLE_DEBUG_INFO = NO;
|
||||||
MTL_FAST_MATH = YES;
|
MTL_FAST_MATH = YES;
|
||||||
SDKROOT = macosx;
|
SDKROOT = macosx;
|
||||||
SHARED_BUILD = 144;
|
SHARED_BUILD = 147;
|
||||||
SHARED_VERSION = 4.0;
|
SHARED_VERSION = 4.0;
|
||||||
STRING_CATALOG_GENERATE_SYMBOLS = YES;
|
STRING_CATALOG_GENERATE_SYMBOLS = YES;
|
||||||
SWIFT_COMPILATION_MODE = wholemodule;
|
SWIFT_COMPILATION_MODE = wholemodule;
|
||||||
|
|||||||
@@ -35,6 +35,8 @@ class AppDelegate: NSObject, NSApplicationDelegate, UNUserNotificationCenterDele
|
|||||||
// MARK: NSApplicationDelegate functions
|
// MARK: NSApplicationDelegate functions
|
||||||
|
|
||||||
func applicationDidFinishLaunching(_: Notification) {
|
func applicationDidFinishLaunching(_: Notification) {
|
||||||
|
UNUserNotificationCenter.current().delegate = self
|
||||||
|
|
||||||
// app did not lauch before
|
// app did not lauch before
|
||||||
if !Settings.sharedInstance.appLaunchedBefore {
|
if !Settings.sharedInstance.appLaunchedBefore {
|
||||||
runAfter(seconds: 0.1) { log("[AppDelegate] Opening Welcome Screen") }
|
runAfter(seconds: 0.1) { log("[AppDelegate] Opening Welcome Screen") }
|
||||||
@@ -66,9 +68,6 @@ class AppDelegate: NSObject, NSApplicationDelegate, UNUserNotificationCenterDele
|
|||||||
startReceiving()
|
startReceiving()
|
||||||
}
|
}
|
||||||
|
|
||||||
UNUserNotificationCenter.current().delegate = self
|
|
||||||
|
|
||||||
|
|
||||||
// MARK: Menu Bar
|
// MARK: Menu Bar
|
||||||
|
|
||||||
let menu = NSMenu()
|
let menu = NSMenu()
|
||||||
@@ -171,7 +170,7 @@ class AppDelegate: NSObject, NSApplicationDelegate, UNUserNotificationCenterDele
|
|||||||
public func userNotificationCenter(_: UNUserNotificationCenter,
|
public func userNotificationCenter(_: UNUserNotificationCenter,
|
||||||
willPresent notification: UNNotification,
|
willPresent notification: UNNotification,
|
||||||
withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
|
withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
|
||||||
if notification.request.identifier == ClipboardSyncNotificationPresenter.notificationIdentifier {
|
if ClipboardSyncNotificationPresenter.isClipboardSyncNotification(notification) {
|
||||||
completionHandler([.banner])
|
completionHandler([.banner])
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -13,14 +13,22 @@ final class ClipboardSyncNotificationPresenter {
|
|||||||
|
|
||||||
static let shared = ClipboardSyncNotificationPresenter()
|
static let shared = ClipboardSyncNotificationPresenter()
|
||||||
static let notificationIdentifier = "quickdrop-clipboard-sync"
|
static let notificationIdentifier = "quickdrop-clipboard-sync"
|
||||||
|
private static let notificationIdentifierPrefix = "\(notificationIdentifier)-"
|
||||||
|
|
||||||
private let center = UNUserNotificationCenter.current()
|
private let center = UNUserNotificationCenter.current()
|
||||||
private let queue = DispatchQueue(label: "ClipboardSyncNotificationPresenter")
|
private let queue = DispatchQueue(label: "ClipboardSyncNotificationPresenter")
|
||||||
private var pendingNotifications: [(String, String?)] = []
|
private var pendingNotifications: [(String, String?)] = []
|
||||||
private var authorizationRequestInFlight = false
|
private var authorizationRequestInFlight = false
|
||||||
|
private var postedNotificationIdentifiers: Set<String> = []
|
||||||
|
|
||||||
private init() {}
|
private init() {}
|
||||||
|
|
||||||
|
static func isClipboardSyncNotification(_ notification: UNNotification) -> Bool {
|
||||||
|
notification.request.identifier == notificationIdentifier
|
||||||
|
|| notification.request.identifier.hasPrefix(notificationIdentifierPrefix)
|
||||||
|
|| notification.request.content.threadIdentifier == notificationIdentifier
|
||||||
|
}
|
||||||
|
|
||||||
func present(clipboardText: String, senderDeviceName: String?) {
|
func present(clipboardText: String, senderDeviceName: String?) {
|
||||||
queue.async {
|
queue.async {
|
||||||
self.postNotificationIfAuthorized(
|
self.postNotificationIfAuthorized(
|
||||||
@@ -101,9 +109,12 @@ final class ClipboardSyncNotificationPresenter {
|
|||||||
content.threadIdentifier = Self.notificationIdentifier
|
content.threadIdentifier = Self.notificationIdentifier
|
||||||
content.interruptionLevel = .active
|
content.interruptionLevel = .active
|
||||||
|
|
||||||
let identifier = Self.notificationIdentifier
|
let identifier = Self.makeNotificationIdentifier()
|
||||||
center.removePendingNotificationRequests(withIdentifiers: [identifier])
|
var identifiersToRemove = postedNotificationIdentifiers
|
||||||
center.removeDeliveredNotifications(withIdentifiers: [identifier])
|
identifiersToRemove.insert(Self.notificationIdentifier)
|
||||||
|
postedNotificationIdentifiers = [identifier]
|
||||||
|
center.removePendingNotificationRequests(withIdentifiers: Array(identifiersToRemove))
|
||||||
|
center.removeDeliveredNotifications(withIdentifiers: Array(identifiersToRemove))
|
||||||
|
|
||||||
let request = UNNotificationRequest(identifier: identifier, content: content, trigger: nil)
|
let request = UNNotificationRequest(identifier: identifier, content: content, trigger: nil)
|
||||||
center.add(request) { error in
|
center.add(request) { error in
|
||||||
@@ -120,6 +131,10 @@ final class ClipboardSyncNotificationPresenter {
|
|||||||
.replacingOccurrences(of: "\\s+", with: " ", options: .regularExpression)
|
.replacingOccurrences(of: "\\s+", with: " ", options: .regularExpression)
|
||||||
.trimmingCharacters(in: .whitespacesAndNewlines)
|
.trimmingCharacters(in: .whitespacesAndNewlines)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static func makeNotificationIdentifier() -> String {
|
||||||
|
"\(notificationIdentifierPrefix)\(UUID().uuidString)"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
Reference in new issue
Block a user