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