mirror of
https://github.com/leonboe1/QuickDrop.git
synced 2026-11-21 22:05:59 +00:00
Fix compilation for iOS
This commit is contained in:
1 parent
d2fc94df75
commit
273b7459d8
14 files changed
+367
-27
No files matched your search
@@ -5,7 +5,6 @@
|
||||
// Created by Grishka on 08.04.2023.
|
||||
//
|
||||
|
||||
import AppKit
|
||||
import CommonCrypto
|
||||
import CryptoKit
|
||||
import Foundation
|
||||
@@ -14,6 +13,12 @@ import System
|
||||
import BigInt
|
||||
import SwiftECC
|
||||
|
||||
#if os(macOS)
|
||||
import AppKit
|
||||
#else
|
||||
import UIKit
|
||||
#endif
|
||||
|
||||
class InboundNearbyConnection: NearbyConnection {
|
||||
|
||||
private var filesToBeReceived: [Int64: InternalFileInfo] = [:]
|
||||
@@ -148,7 +153,9 @@ class InboundNearbyConnection: NearbyConnection {
|
||||
else if (frame.payloadChunk.flags & 1) == 1 {
|
||||
try fileInfo.fileHandle?.close()
|
||||
filesToBeReceived[id]!.fileHandle = nil
|
||||
#if os(macOS)
|
||||
fileInfo.progress?.unpublish()
|
||||
#endif
|
||||
SaveFilesManager.shared.registerFileFinishedDownloading(fileInfo.destinationURL)
|
||||
|
||||
filesToBeReceived.removeValue(forKey: id)
|
||||
@@ -166,15 +173,27 @@ class InboundNearbyConnection: NearbyConnection {
|
||||
if let urlStr = String(data: payload, encoding: .utf8) {
|
||||
|
||||
if isPlainTextTransfer {
|
||||
// paste to clipboard
|
||||
#if os(macOS)
|
||||
// macOS clipboard
|
||||
let pasteboard = NSPasteboard.general
|
||||
pasteboard.clearContents()
|
||||
pasteboard.setString(urlStr, forType: .string)
|
||||
|
||||
NearbyConnectionManager.shared.mainAppDelegate?.showCopiedToClipboardAlert()
|
||||
}
|
||||
else if let url = URL(string: urlStr) {
|
||||
|
||||
#elseif os(iOS)
|
||||
// iOS clipboard
|
||||
UIPasteboard.general.string = urlStr
|
||||
|
||||
// Optionally show an alert (requires a way to present it)
|
||||
// For example, post a notification or use a delegate to show a toast or alert
|
||||
#endif
|
||||
} else if let url = URL(string: urlStr) {
|
||||
#if os(macOS)
|
||||
NSWorkspace.shared.open(url)
|
||||
#elseif os(iOS)
|
||||
UIApplication.shared.open(url, options: [:], completionHandler: nil)
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
@@ -188,7 +207,9 @@ class InboundNearbyConnection: NearbyConnection {
|
||||
fileInfo.progress?.completedUnitCount = filesToBeReceived[id]!.bytesTransferred
|
||||
try fileInfo.fileHandle?.close()
|
||||
filesToBeReceived[id]!.fileHandle = nil
|
||||
#if os(macOS)
|
||||
fileInfo.progress?.unpublish()
|
||||
#endif
|
||||
filesToBeReceived.removeValue(forKey: id)
|
||||
SaveFilesManager.shared.registerFileFinishedDownloading(fileInfo.destinationURL)
|
||||
|
||||
@@ -477,7 +498,9 @@ class InboundNearbyConnection: NearbyConnection {
|
||||
progress.totalUnitCount = file.meta.size
|
||||
progress.kind = .file
|
||||
progress.isPausable = false
|
||||
#if os(macOS)
|
||||
progress.publish()
|
||||
#endif
|
||||
filesToBeReceived[id]!.progress = progress
|
||||
filesToBeReceived[id]!.created = true
|
||||
}
|
||||
@@ -518,9 +541,11 @@ class InboundNearbyConnection: NearbyConnection {
|
||||
private func deletePartiallyReceivedFiles() {
|
||||
for (_, file) in filesToBeReceived {
|
||||
|
||||
#if os(macOS)
|
||||
if let progress = file.progress {
|
||||
progress.unpublish()
|
||||
}
|
||||
#endif
|
||||
|
||||
guard file.created else { continue }
|
||||
do {
|
||||
|
||||
@@ -9,6 +9,9 @@ import Foundation
|
||||
import Network
|
||||
import System
|
||||
|
||||
#if os(iOS)
|
||||
import UIKit
|
||||
#endif
|
||||
|
||||
public class NearbyConnectionManager: NSObject, NetServiceDelegate, InboundNearbyConnectionDelegate, OutboundNearbyConnectionDelegate {
|
||||
|
||||
@@ -90,20 +93,29 @@ public class NearbyConnectionManager: NSObject, NetServiceDelegate, InboundNearb
|
||||
]
|
||||
|
||||
let name = Data(nameBytes).urlSafeBase64EncodedString()
|
||||
let endpointInfo = EndpointInfo(name: Host.current().localizedName!, deviceType: .computer)
|
||||
|
||||
let port = Int32(tcpListener.port!.rawValue)
|
||||
|
||||
mdnsServices = serviceTypes.map { serviceType in
|
||||
let service = NetService(domain: "", type: serviceType, name: name, port: port)
|
||||
service.delegate = self
|
||||
service.setTXTRecord(NetService.data(fromTXTRecord: [
|
||||
"n": endpointInfo.serialize().urlSafeBase64EncodedString().data(using: .utf8)!,
|
||||
"n": getEndpointInfo().serialize().urlSafeBase64EncodedString().data(using: .utf8)!,
|
||||
]))
|
||||
service.publish()
|
||||
return service
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
func getEndpointInfo() -> EndpointInfo {
|
||||
#if os(macOS)
|
||||
let endpointInfo = EndpointInfo(name: Host.current().localizedName!, deviceType: .computer)
|
||||
#else
|
||||
let endpointInfo = EndpointInfo(name: UIDevice.current.name, deviceType: .phone)
|
||||
#endif
|
||||
|
||||
return endpointInfo
|
||||
}
|
||||
|
||||
|
||||
func obtainUserConsent(for transfer: TransferMetadata, from device: RemoteDeviceInfo, connection _: InboundNearbyConnection) {
|
||||
|
||||
@@ -165,14 +165,16 @@ class OutboundNearbyConnection: NearbyConnection {
|
||||
|
||||
|
||||
private func sendConnectionRequest() throws {
|
||||
|
||||
let endpointInfo = NearbyConnectionManager.shared.getEndpointInfo()
|
||||
|
||||
var frame = Location_Nearby_Connections_OfflineFrame()
|
||||
frame.version = .v1
|
||||
frame.v1 = Location_Nearby_Connections_V1Frame()
|
||||
frame.v1.type = .connectionRequest
|
||||
frame.v1.connectionRequest = Location_Nearby_Connections_ConnectionRequestFrame()
|
||||
frame.v1.connectionRequest.endpointID = String(bytes: NearbyConnectionManager.shared.endpointID, encoding: .ascii)!
|
||||
frame.v1.connectionRequest.endpointName = Host.current().localizedName!
|
||||
let endpointInfo = EndpointInfo(name: Host.current().localizedName!, deviceType: .computer)
|
||||
frame.v1.connectionRequest.endpointName = endpointInfo.name
|
||||
frame.v1.connectionRequest.endpointInfo = endpointInfo.serialize()
|
||||
frame.v1.connectionRequest.mediums = [.wifiLan]
|
||||
try sendFrameAsync(frame.serializedData())
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
//
|
||||
// Localized.swift
|
||||
// QuickDrop
|
||||
//
|
||||
// Created by Leon Böttger on 10.03.25.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
|
||||
public extension String {
|
||||
func localized() -> String {
|
||||
let localizedString = NSLocalizedString(self, comment: "")
|
||||
return localizedString
|
||||
}
|
||||
}
|
||||
@@ -6,8 +6,10 @@
|
||||
//
|
||||
|
||||
import Foundation
|
||||
import AppKit
|
||||
import ImageIO
|
||||
#if os(macOS)
|
||||
import AppKit
|
||||
#endif
|
||||
|
||||
public class SaveFilesManager {
|
||||
|
||||
@@ -88,11 +90,15 @@ public class SaveFilesManager {
|
||||
progress.totalUnitCount = 10
|
||||
progress.kind = .file
|
||||
progress.isPausable = false
|
||||
#if os(macOS)
|
||||
progress.publish()
|
||||
#endif
|
||||
|
||||
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
|
||||
progress.completedUnitCount = 10
|
||||
#if os(macOS)
|
||||
progress.unpublish()
|
||||
#endif
|
||||
}
|
||||
|
||||
try fileManager.removeItem(at: file)
|
||||
@@ -110,10 +116,12 @@ public class SaveFilesManager {
|
||||
|
||||
if !filesFinishedDownloadingSinceLastRun.isEmpty && !isFileTransferRestricted() {
|
||||
|
||||
#if os(macOS)
|
||||
if UserDefaults.standard.bool(forKey: UserDefaultsKeys.openFinderAfterReceiving.rawValue) {
|
||||
log("Opening \(filesFinishedDownloadingSinceLastRun.count) file(s) in Finder.")
|
||||
NSWorkspace.shared.activateFileViewerSelecting(filesFinishedDownloadingSinceLastRun)
|
||||
}
|
||||
#endif
|
||||
|
||||
// Clear the list of finished files
|
||||
filesFinishedDownloadingSinceLastRun.removeAll()
|
||||
@@ -143,6 +151,8 @@ public class SaveFilesManager {
|
||||
if let bookmarkData = UserDefaults.standard.data(forKey: UserDefaultsKeys.saveFolderBookmark.rawValue) {
|
||||
var isStale = false
|
||||
|
||||
// Not supported on iOS
|
||||
#if os(macOS)
|
||||
do {
|
||||
let url = try URL(resolvingBookmarkData: bookmarkData, options: .withSecurityScope, relativeTo: nil, bookmarkDataIsStale: &isStale)
|
||||
|
||||
@@ -160,6 +170,7 @@ public class SaveFilesManager {
|
||||
} catch {
|
||||
log("Failed to resolve bookmark: \(error), using default downloads folder.")
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
do {
|
||||
|
||||
@@ -6,12 +6,21 @@
|
||||
//
|
||||
|
||||
import Foundation
|
||||
|
||||
#if os(macOS)
|
||||
import IOKit
|
||||
import IOKit.pwr_mgt
|
||||
#elseif os(iOS)
|
||||
import UIKit
|
||||
#endif
|
||||
|
||||
class SleepManager {
|
||||
public static let shared = SleepManager()
|
||||
|
||||
#if os(macOS)
|
||||
private var assertionID: IOPMAssertionID = 0
|
||||
#endif
|
||||
|
||||
private var timer: Timer?
|
||||
|
||||
private init() {
|
||||
@@ -24,28 +33,35 @@ class SleepManager {
|
||||
}
|
||||
|
||||
private func disableSleep() {
|
||||
if assertionID != 0 {
|
||||
return
|
||||
}
|
||||
#if os(macOS)
|
||||
if assertionID != 0 { return }
|
||||
|
||||
log("[SleepManager] Enabling Wakelock")
|
||||
|
||||
|
||||
IOPMAssertionCreateWithName(
|
||||
kIOPMAssertionTypePreventUserIdleDisplaySleep as CFString,
|
||||
IOPMAssertionLevel(kIOPMAssertionLevelOn),
|
||||
"QuickDrop Data Transfer" as CFString,
|
||||
&assertionID
|
||||
)
|
||||
#elseif os(iOS)
|
||||
log("[SleepManager] Keeping screen awake (iOS)")
|
||||
UIApplication.shared.isIdleTimerDisabled = true
|
||||
#endif
|
||||
}
|
||||
|
||||
private func enableSleep() {
|
||||
if assertionID == 0 {
|
||||
return
|
||||
}
|
||||
#if os(macOS)
|
||||
if assertionID == 0 { return }
|
||||
|
||||
log("[SleepManager] Disabling Wakelock")
|
||||
|
||||
IOPMAssertionRelease(assertionID)
|
||||
assertionID = 0
|
||||
#elseif os(iOS)
|
||||
log("[SleepManager] Allowing screen to sleep (iOS)")
|
||||
UIApplication.shared.isIdleTimerDisabled = false
|
||||
#endif
|
||||
}
|
||||
|
||||
private func updateSleepState() {
|
||||
|
||||
Reference in new issue
Block a user