mirror of
https://github.com/leonboe1/QuickDrop.git
synced 2026-11-23 06:46:03 +00:00
Only poll clipboard if clipboard sync was enabled
This commit is contained in:
1 parent
72bfd88161
commit
488f1a14b2
2 files changed
+65
-7
No files matched your search
@@ -11,6 +11,7 @@ import CryptoKit
|
|||||||
class TrustStore: ObservableObject {
|
class TrustStore: ObservableObject {
|
||||||
|
|
||||||
static let shared = TrustStore()
|
static let shared = TrustStore()
|
||||||
|
static let trustedCertificatesDidChange = Notification.Name("TrustStore.trustedCertificatesDidChange")
|
||||||
|
|
||||||
@Published private(set) var trustedCertificates: [String: TrustedCertificate] = [:]
|
@Published private(set) var trustedCertificates: [String: TrustedCertificate] = [:]
|
||||||
|
|
||||||
@@ -58,12 +59,18 @@ class TrustStore: ObservableObject {
|
|||||||
do {
|
do {
|
||||||
try persistTrustedCertificates()
|
try persistTrustedCertificates()
|
||||||
AppGroup.appGroupUD.removeObject(forKey: trustedKeysKey)
|
AppGroup.appGroupUD.removeObject(forKey: trustedKeysKey)
|
||||||
|
notifyTrustedCertificatesDidChange()
|
||||||
} catch {
|
} catch {
|
||||||
print("[TrustStore] Failed to save trusted certificates to keychain: \(error.localizedDescription)")
|
print("[TrustStore] Failed to save trusted certificates to keychain: \(error.localizedDescription)")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private func notifyTrustedCertificatesDidChange() {
|
||||||
|
NotificationCenter.default.post(name: Self.trustedCertificatesDidChange, object: self)
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
// MARK: - Public API
|
// MARK: - Public API
|
||||||
|
|
||||||
func findTrustedKey(for secretIdHash: Data) -> Data? {
|
func findTrustedKey(for secretIdHash: Data) -> Data? {
|
||||||
|
|||||||
@@ -15,7 +15,9 @@ final class ClipboardSyncCoordinator {
|
|||||||
private let pollInterval: TimeInterval = 1
|
private let pollInterval: TimeInterval = 1
|
||||||
private let transferTimeout: TimeInterval = 75
|
private let transferTimeout: TimeInterval = 75
|
||||||
|
|
||||||
|
private var isStarted = false
|
||||||
private var pollTimer: Timer?
|
private var pollTimer: Timer?
|
||||||
|
private var trustStoreObserver: NSObjectProtocol?
|
||||||
private var lastObservedChangeCount: Int
|
private var lastObservedChangeCount: Int
|
||||||
private var suppressedClipboardText: String?
|
private var suppressedClipboardText: String?
|
||||||
private var startedOwnDiscovery = false
|
private var startedOwnDiscovery = false
|
||||||
@@ -27,6 +29,56 @@ final class ClipboardSyncCoordinator {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func start() {
|
func start() {
|
||||||
|
guard !isStarted else {
|
||||||
|
refreshPollingState()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
isStarted = true
|
||||||
|
trustStoreObserver = NotificationCenter.default.addObserver(
|
||||||
|
forName: TrustStore.trustedCertificatesDidChange,
|
||||||
|
object: TrustStore.shared,
|
||||||
|
queue: .main
|
||||||
|
) { [weak self] _ in
|
||||||
|
self?.refreshPollingState()
|
||||||
|
}
|
||||||
|
|
||||||
|
refreshPollingState()
|
||||||
|
}
|
||||||
|
|
||||||
|
func stop() {
|
||||||
|
guard isStarted || pollTimer != nil else { return }
|
||||||
|
|
||||||
|
log("[ClipboardSyncCoordinator] Stopping clipboard watcher")
|
||||||
|
isStarted = false
|
||||||
|
if let trustStoreObserver {
|
||||||
|
NotificationCenter.default.removeObserver(trustStoreObserver)
|
||||||
|
self.trustStoreObserver = nil
|
||||||
|
}
|
||||||
|
stopPolling(clearTransfers: true)
|
||||||
|
}
|
||||||
|
|
||||||
|
private func refreshPollingState() {
|
||||||
|
guard isStarted else { return }
|
||||||
|
|
||||||
|
guard hasTrustedClipboardSyncDevice else {
|
||||||
|
if pollTimer != nil {
|
||||||
|
log("[ClipboardSyncCoordinator] Pausing clipboard watcher because no trusted clipboard sync device is saved")
|
||||||
|
}
|
||||||
|
stopPolling(clearTransfers: true)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
startPollingIfNeeded()
|
||||||
|
}
|
||||||
|
|
||||||
|
private var hasTrustedClipboardSyncDevice: Bool {
|
||||||
|
TrustStore.shared.trustedCertificates.values.contains {
|
||||||
|
$0.grantedUseCases.contains(.clipboardSync)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private func startPollingIfNeeded() {
|
||||||
guard pollTimer == nil else { return }
|
guard pollTimer == nil else { return }
|
||||||
|
|
||||||
let manager = NearbyConnectionManager.shared
|
let manager = NearbyConnectionManager.shared
|
||||||
@@ -42,16 +94,15 @@ final class ClipboardSyncCoordinator {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func stop() {
|
private func stopPolling(clearTransfers: Bool) {
|
||||||
guard pollTimer != nil else { return }
|
|
||||||
|
|
||||||
log("[ClipboardSyncCoordinator] Stopping clipboard watcher")
|
|
||||||
pollTimer?.invalidate()
|
pollTimer?.invalidate()
|
||||||
pollTimer = nil
|
pollTimer = nil
|
||||||
suppressedClipboardText = nil
|
suppressedClipboardText = nil
|
||||||
activeTransfersByFingerprint.values.forEach { $0.cancelTimeout() }
|
if clearTransfers {
|
||||||
activeTransfersByFingerprint.removeAll()
|
activeTransfersByFingerprint.values.forEach { $0.cancelTimeout() }
|
||||||
queuedTextByFingerprint.removeAll()
|
activeTransfersByFingerprint.removeAll()
|
||||||
|
queuedTextByFingerprint.removeAll()
|
||||||
|
}
|
||||||
|
|
||||||
guard startedOwnDiscovery else { return }
|
guard startedOwnDiscovery else { return }
|
||||||
startedOwnDiscovery = false
|
startedOwnDiscovery = false
|
||||||
|
|||||||
Reference in new issue
Block a user