mirror of
https://github.com/leonboe1/QuickDrop.git
synced 2026-11-22 14:26:01 +00:00
Formatting
This commit is contained in:
1 parent
3a2a8c70b9
commit
dbca8d6434
3 files changed
+58
-131
No files matched your search
+26
-58
@@ -5,19 +5,17 @@
|
|||||||
// Created by Grishka on 08.04.2023.
|
// Created by Grishka on 08.04.2023.
|
||||||
//
|
//
|
||||||
|
|
||||||
import Network
|
|
||||||
import Cocoa
|
|
||||||
import UserNotifications
|
|
||||||
import NearbyShare
|
|
||||||
import SwiftUI
|
|
||||||
import StoreKit
|
|
||||||
import AudioToolbox
|
import AudioToolbox
|
||||||
import BezelNotification
|
import BezelNotification
|
||||||
|
import Cocoa
|
||||||
|
import NearbyShare
|
||||||
import Network
|
import Network
|
||||||
|
import StoreKit
|
||||||
|
import SwiftUI
|
||||||
|
import UserNotifications
|
||||||
|
|
||||||
@main
|
@main
|
||||||
class AppDelegate: NSObject, NSApplicationDelegate, UNUserNotificationCenterDelegate, MainAppDelegate {
|
class AppDelegate: NSObject, NSApplicationDelegate, UNUserNotificationCenterDelegate, MainAppDelegate {
|
||||||
|
|
||||||
private var statusItem: NSStatusItem?
|
private var statusItem: NSStatusItem?
|
||||||
private var activeIncomingTransfers: [String: TransferInfo] = [:]
|
private var activeIncomingTransfers: [String: TransferInfo] = [:]
|
||||||
|
|
||||||
@@ -30,8 +28,7 @@ class AppDelegate: NSObject, NSApplicationDelegate, UNUserNotificationCenterDele
|
|||||||
|
|
||||||
var showsFirewallAlert = false
|
var showsFirewallAlert = false
|
||||||
|
|
||||||
func applicationDidFinishLaunching(_ aNotification: Notification) {
|
func applicationDidFinishLaunching(_: Notification) {
|
||||||
|
|
||||||
let menu = NSMenu()
|
let menu = NSMenu()
|
||||||
|
|
||||||
menu.addItem(withTitle: NSLocalizedString("VisibleToEveryone", value: "Visible to everyone", comment: ""), action: nil, keyEquivalent: "")
|
menu.addItem(withTitle: NSLocalizedString("VisibleToEveryone", value: "Visible to everyone", comment: ""), action: nil, keyEquivalent: "")
|
||||||
@@ -62,7 +59,6 @@ class AppDelegate: NSObject, NSApplicationDelegate, UNUserNotificationCenterDele
|
|||||||
|
|
||||||
// app did not lauch before
|
// app did not lauch before
|
||||||
if !UserDefaults.standard.bool(forKey: UserDefaultsKeys.appLaunchedBefore.rawValue) {
|
if !UserDefaults.standard.bool(forKey: UserDefaultsKeys.appLaunchedBefore.rawValue) {
|
||||||
|
|
||||||
log("Opening Welcome Screen")
|
log("Opening Welcome Screen")
|
||||||
// open welcome screen
|
// open welcome screen
|
||||||
openWelcomeScreen()
|
openWelcomeScreen()
|
||||||
@@ -70,16 +66,14 @@ class AppDelegate: NSObject, NSApplicationDelegate, UNUserNotificationCenterDele
|
|||||||
|
|
||||||
// user installed the app after the IAP was implemented, set the user as eligible for IAP
|
// user installed the app after the IAP was implemented, set the user as eligible for IAP
|
||||||
UserDefaults.standard.set(true, forKey: UserDefaultsKeys.isEligibleForIap.rawValue)
|
UserDefaults.standard.set(true, forKey: UserDefaultsKeys.isEligibleForIap.rawValue)
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
// app launched before
|
// app launched before
|
||||||
|
|
||||||
// user installed the app before the IAP was implemented, grant the plus version
|
// user installed the app before the IAP was implemented, grant the plus version
|
||||||
if !UserDefaults.standard.bool(forKey: UserDefaultsKeys.isEligibleForIap.rawValue) {
|
if !UserDefaults.standard.bool(forKey: UserDefaultsKeys.isEligibleForIap.rawValue) {
|
||||||
log("Granting QuickDrop+ for old user")
|
log("Granting QuickDrop+ for old user")
|
||||||
UserDefaults.standard.set(true, forKey: UserDefaultsKeys.plusVersion.rawValue)
|
UserDefaults.standard.set(true, forKey: UserDefaultsKeys.plusVersion.rawValue)
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
if !isPlusVersion() {
|
if !isPlusVersion() {
|
||||||
log("New user - QuickDrop+ not available")
|
log("New user - QuickDrop+ not available")
|
||||||
}
|
}
|
||||||
@@ -115,7 +109,6 @@ class AppDelegate: NSObject, NSApplicationDelegate, UNUserNotificationCenterDele
|
|||||||
// monitor.start(queue: queue)
|
// monitor.start(queue: queue)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
func performDeviceToDeviceCheck() {
|
func performDeviceToDeviceCheck() {
|
||||||
let scanner = DeviceToDeviceHeuristicScanner()
|
let scanner = DeviceToDeviceHeuristicScanner()
|
||||||
scanner.scan { allowed in
|
scanner.scan { allowed in
|
||||||
@@ -128,16 +121,13 @@ class AppDelegate: NSObject, NSApplicationDelegate, UNUserNotificationCenterDele
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@objc func sendClipboard() {
|
@objc func sendClipboard() {
|
||||||
if let fileURL = ClipboardManager.saveClipboardToTempFile() {
|
if let fileURL = ClipboardManager.saveClipboardToTempFile() {
|
||||||
|
|
||||||
let sharingService = NSSharingService(named: NSSharingService.Name("com.leonboettger.neardrop.ShareExtension"))
|
let sharingService = NSSharingService(named: NSSharingService.Name("com.leonboettger.neardrop.ShareExtension"))
|
||||||
sharingService?.perform(withItems: [fileURL])
|
sharingService?.perform(withItems: [fileURL])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@objc func openWelcomeScreen() {
|
@objc func openWelcomeScreen() {
|
||||||
// Create the welcome screen SwiftUI view
|
// Create the welcome screen SwiftUI view
|
||||||
let welcomeView = WelcomeScreen(openPlusScreen: openPlusScreen, checkForNetworkIssues: performDeviceToDeviceCheck)
|
let welcomeView = WelcomeScreen(openPlusScreen: openPlusScreen, checkForNetworkIssues: performDeviceToDeviceCheck)
|
||||||
@@ -165,9 +155,7 @@ class AppDelegate: NSObject, NSApplicationDelegate, UNUserNotificationCenterDele
|
|||||||
welcomeWindow?.level = .normal
|
welcomeWindow?.level = .normal
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@objc func openPlusScreen() {
|
@objc func openPlusScreen() {
|
||||||
|
|
||||||
// Create the welcome screen SwiftUI view
|
// Create the welcome screen SwiftUI view
|
||||||
let plusView = GetPlusView(closeView: {
|
let plusView = GetPlusView(closeView: {
|
||||||
log("Closing plus screen")
|
log("Closing plus screen")
|
||||||
@@ -203,13 +191,10 @@ class AppDelegate: NSObject, NSApplicationDelegate, UNUserNotificationCenterDele
|
|||||||
case Firewall
|
case Firewall
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
func openAlert(type: AlertType) {
|
func openAlert(type: AlertType) {
|
||||||
|
|
||||||
log("Opening Alert for \(type)")
|
log("Opening Alert for \(type)")
|
||||||
|
|
||||||
DispatchQueue.main.async { [self] in
|
DispatchQueue.main.async { [self] in
|
||||||
|
|
||||||
// Create an NSWindow to host the SwiftUI view
|
// Create an NSWindow to host the SwiftUI view
|
||||||
let alertWindow = NSWindow(
|
let alertWindow = NSWindow(
|
||||||
contentRect: NSRect(x: 0, y: 0, width: issueViewWidth, height: issueViewHeight),
|
contentRect: NSRect(x: 0, y: 0, width: issueViewWidth, height: issueViewHeight),
|
||||||
@@ -241,12 +226,10 @@ class AppDelegate: NSObject, NSApplicationDelegate, UNUserNotificationCenterDele
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
func transmissionCount() -> Int {
|
func transmissionCount() -> Int {
|
||||||
return UserDefaults.standard.integer(forKey: UserDefaultsKeys.transmissionCount.rawValue)
|
return UserDefaults.standard.integer(forKey: UserDefaultsKeys.transmissionCount.rawValue)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// Action for "Recommended Apps" menu item
|
// Action for "Recommended Apps" menu item
|
||||||
@objc func openRecommendedApps() {
|
@objc func openRecommendedApps() {
|
||||||
if let url = URL(string: "https://apps.apple.com/de/developer/leon-boettger/id1537384790") {
|
if let url = URL(string: "https://apps.apple.com/de/developer/leon-boettger/id1537384790") {
|
||||||
@@ -254,32 +237,28 @@ class AppDelegate: NSObject, NSApplicationDelegate, UNUserNotificationCenterDele
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func applicationShouldHandleReopen(_ sender: NSApplication, hasVisibleWindows flag: Bool) -> Bool {
|
func applicationShouldHandleReopen(_: NSApplication, hasVisibleWindows _: Bool) -> Bool {
|
||||||
|
|
||||||
openWelcomeScreen()
|
openWelcomeScreen()
|
||||||
statusItem?.isVisible = true
|
statusItem?.isVisible = true
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public func userNotificationCenter(_: UNUserNotificationCenter,
|
||||||
public func userNotificationCenter(_ center: UNUserNotificationCenter,
|
willPresent _: UNNotification,
|
||||||
willPresent notification: UNNotification,
|
withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void)
|
||||||
withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
|
{
|
||||||
completionHandler([.sound, .banner])
|
completionHandler([.sound, .banner])
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func applicationWillTerminate(_: Notification) {
|
||||||
func applicationWillTerminate(_ aNotification: Notification) {
|
|
||||||
UNUserNotificationCenter.current().removeAllDeliveredNotifications()
|
UNUserNotificationCenter.current().removeAllDeliveredNotifications()
|
||||||
iapManager?.stopObserving()
|
iapManager?.stopObserving()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func applicationSupportsSecureRestorableState(_: NSApplication) -> Bool {
|
||||||
func applicationSupportsSecureRestorableState(_ app: NSApplication) -> Bool {
|
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public func continueTransmission(accept: Bool, transferID: String, storeInTemp: Bool = false) {
|
public func continueTransmission(accept: Bool, transferID: String, storeInTemp: Bool = false) {
|
||||||
NearbyConnectionManager.shared.submitUserConsent(transferID: transferID, accept: accept, storeInTemp: storeInTemp)
|
NearbyConnectionManager.shared.submitUserConsent(transferID: transferID, accept: accept, storeInTemp: storeInTemp)
|
||||||
|
|
||||||
@@ -288,9 +267,8 @@ class AppDelegate: NSObject, NSApplicationDelegate, UNUserNotificationCenterDele
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
func obtainUserConsent(for transfer: TransferMetadata, from device: RemoteDeviceInfo) {
|
func obtainUserConsent(for transfer: TransferMetadata, from device: RemoteDeviceInfo) {
|
||||||
self.activeIncomingTransfers[transfer.id] = TransferInfo(device: device, transfer: transfer)
|
activeIncomingTransfers[transfer.id] = TransferInfo(device: device, transfer: transfer)
|
||||||
|
|
||||||
AudioManager.playSound()
|
AudioManager.playSound()
|
||||||
|
|
||||||
@@ -310,11 +288,10 @@ class AppDelegate: NSObject, NSApplicationDelegate, UNUserNotificationCenterDele
|
|||||||
let pinCodeMessage = String(format: NSLocalizedString("PinCode", value: "PIN: %@", comment: ""), arguments: [transfer.pinCode ?? "?"])
|
let pinCodeMessage = String(format: NSLocalizedString("PinCode", value: "PIN: %@", comment: ""), arguments: [transfer.pinCode ?? "?"])
|
||||||
let transferID = transfer.id
|
let transferID = transfer.id
|
||||||
|
|
||||||
|
|
||||||
if acceptAutomatically {
|
if acceptAutomatically {
|
||||||
pressAcceptButton(transferID: transfer.id)
|
pressAcceptButton(transferID: transfer.id)
|
||||||
|
|
||||||
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound]) { granted, err in
|
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound]) { granted, _ in
|
||||||
if granted {
|
if granted {
|
||||||
log("User granted notification permissions")
|
log("User granted notification permissions")
|
||||||
|
|
||||||
@@ -332,8 +309,7 @@ class AppDelegate: NSObject, NSApplicationDelegate, UNUserNotificationCenterDele
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
let alert = NSAlert()
|
let alert = NSAlert()
|
||||||
alert.alertStyle = .informational
|
alert.alertStyle = .informational
|
||||||
|
|
||||||
@@ -352,37 +328,31 @@ class AppDelegate: NSObject, NSApplicationDelegate, UNUserNotificationCenterDele
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private func pressAcceptButton(transferID: String) {
|
private func pressAcceptButton(transferID: String) {
|
||||||
if false || (!isPlusVersion() && transmissionCount() > 1) {
|
if false || (!isPlusVersion() && transmissionCount() > 1) {
|
||||||
|
continueTransmission(accept: true, transferID: transferID, storeInTemp: true)
|
||||||
self.continueTransmission(accept: true, transferID: transferID, storeInTemp: true)
|
|
||||||
log("Showing plus screen...")
|
log("Showing plus screen...")
|
||||||
|
|
||||||
self.openPlusScreen()
|
openPlusScreen()
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
continueTransmission(accept: true, transferID: transferID)
|
continueTransmission(accept: true, transferID: transferID)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
func showFirewallAlert() {
|
func showFirewallAlert() {
|
||||||
openAlert(type: .Firewall)
|
openAlert(type: .Firewall)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
func incomingTransfer(id: String, didFinishWith error: Error?) {
|
func incomingTransfer(id: String, didFinishWith error: Error?) {
|
||||||
guard let transfer = self.activeIncomingTransfers[id] else { return }
|
guard let transfer = activeIncomingTransfers[id] else { return }
|
||||||
if let error = error {
|
if let error = error {
|
||||||
|
|
||||||
var description = ""
|
var description = ""
|
||||||
|
|
||||||
if let ne = (error as? NearbyError) {
|
if let ne = (error as? NearbyError) {
|
||||||
switch ne {
|
switch ne {
|
||||||
case .inputOutput:
|
case .inputOutput:
|
||||||
description = "I/O Error";
|
description = "I/O Error"
|
||||||
case .protocolError(_):
|
case .protocolError:
|
||||||
description = NSLocalizedString("Error.Protocol", value: "Communication error", comment: "") + "(\(ne.localizedDescription))"
|
description = NSLocalizedString("Error.Protocol", value: "Communication error", comment: "") + "(\(ne.localizedDescription))"
|
||||||
case .packetFilterError:
|
case .packetFilterError:
|
||||||
openAlert(type: .NetworkFilter)
|
openAlert(type: .NetworkFilter)
|
||||||
@@ -420,8 +390,7 @@ class AppDelegate: NSObject, NSApplicationDelegate, UNUserNotificationCenterDele
|
|||||||
if result == .alertFirstButtonReturn {
|
if result == .alertFirstButtonReturn {
|
||||||
sendLoggingString()
|
sendLoggingString()
|
||||||
}
|
}
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
let currentCount = transmissionCount()
|
let currentCount = transmissionCount()
|
||||||
|
|
||||||
if currentCount % 20 == 0 {
|
if currentCount % 20 == 0 {
|
||||||
@@ -433,11 +402,10 @@ class AppDelegate: NSObject, NSApplicationDelegate, UNUserNotificationCenterDele
|
|||||||
UserDefaults.standard.set(currentCount + 1, forKey: UserDefaultsKeys.transmissionCount.rawValue)
|
UserDefaults.standard.set(currentCount + 1, forKey: UserDefaultsKeys.transmissionCount.rawValue)
|
||||||
}
|
}
|
||||||
|
|
||||||
self.activeIncomingTransfers.removeValue(forKey: id)
|
activeIncomingTransfers.removeValue(forKey: id)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
struct TransferInfo {
|
struct TransferInfo {
|
||||||
let device: RemoteDeviceInfo
|
let device: RemoteDeviceInfo
|
||||||
let transfer: TransferMetadata
|
let transfer: TransferMetadata
|
||||||
|
|||||||
@@ -19,7 +19,7 @@ class DeviceListCell:NSCollectionViewItem {
|
|||||||
btn.target = self
|
btn.target = self
|
||||||
}
|
}
|
||||||
|
|
||||||
@IBAction func onClick(_ sender:Any?){
|
@IBAction func onClick(_: Any?) {
|
||||||
guard let handler = clickHandler else { return }
|
guard let handler = clickHandler else { return }
|
||||||
handler()
|
handler()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,13 +5,12 @@
|
|||||||
// Created by Grishka on 12.09.2023.
|
// Created by Grishka on 12.09.2023.
|
||||||
//
|
//
|
||||||
|
|
||||||
import Foundation
|
|
||||||
import Cocoa
|
import Cocoa
|
||||||
import SwiftUI
|
import Foundation
|
||||||
import NearbyShare
|
import NearbyShare
|
||||||
|
import SwiftUI
|
||||||
|
|
||||||
class ShareViewController: NSViewController, ShareExtensionDelegate {
|
class ShareViewController: NSViewController, ShareExtensionDelegate {
|
||||||
|
|
||||||
private var urls: [URL] = []
|
private var urls: [URL] = []
|
||||||
private var foundDevices: [RemoteDeviceInfo] = []
|
private var foundDevices: [RemoteDeviceInfo] = []
|
||||||
private var chosenDevice: RemoteDeviceInfo?
|
private var chosenDevice: RemoteDeviceInfo?
|
||||||
@@ -46,22 +45,21 @@ class ShareViewController: NSViewController, ShareExtensionDelegate{
|
|||||||
override func loadView() {
|
override func loadView() {
|
||||||
super.loadView()
|
super.loadView()
|
||||||
|
|
||||||
let item = self.extensionContext!.inputItems[0] as! NSExtensionItem
|
let item = extensionContext!.inputItems[0] as! NSExtensionItem
|
||||||
if let attachments = item.attachments {
|
if let attachments = item.attachments {
|
||||||
|
|
||||||
if let text = item.attributedContentText?.string, attachments.isEmpty, let tempUrl = ClipboardManager.saveTextToTempFile(text: text) {
|
if let text = item.attributedContentText?.string, attachments.isEmpty, let tempUrl = ClipboardManager.saveTextToTempFile(text: text) {
|
||||||
self.urls.append(tempUrl)
|
urls.append(tempUrl)
|
||||||
|
|
||||||
DispatchQueue.main.async {
|
DispatchQueue.main.async {
|
||||||
self.urlsReady()
|
self.urlsReady()
|
||||||
}
|
}
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
for attachment in attachments {
|
for attachment in attachments {
|
||||||
if attachment.hasItemConformingToTypeIdentifier(kUTTypeURL as String) {
|
if attachment.hasItemConformingToTypeIdentifier(kUTTypeURL as String) {
|
||||||
attachment.loadItem(forTypeIdentifier: kUTTypeURL as String, options: nil) { data, err in
|
attachment.loadItem(forTypeIdentifier: kUTTypeURL as String, options: nil) { data, _ in
|
||||||
if let urlData = data as? Data,
|
if let urlData = data as? Data,
|
||||||
let url = URL(dataRepresentation: urlData, relativeTo: nil, isAbsolute: false) {
|
let url = URL(dataRepresentation: urlData, relativeTo: nil, isAbsolute: false)
|
||||||
|
{
|
||||||
self.urls.append(url)
|
self.urls.append(url)
|
||||||
} else if let url = data as? NSURL {
|
} else if let url = data as? NSURL {
|
||||||
self.urls.append(url as URL)
|
self.urls.append(url as URL)
|
||||||
@@ -76,10 +74,9 @@ class ShareViewController: NSViewController, ShareExtensionDelegate{
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
let cancelError = NSError(domain: NSCocoaErrorDomain, code: NSUserCancelledError, userInfo: nil)
|
let cancelError = NSError(domain: NSCocoaErrorDomain, code: NSUserCancelledError, userInfo: nil)
|
||||||
self.extensionContext!.cancelRequest(withError: cancelError)
|
extensionContext!.cancelRequest(withError: cancelError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -100,7 +97,7 @@ class ShareViewController: NSViewController, ShareExtensionDelegate{
|
|||||||
NSLayoutConstraint(item: loadingOverlay!, attribute: .centerY, relatedBy: .equal, toItem: contentWrap, attribute: .centerY, multiplier: 1, constant: 0),
|
NSLayoutConstraint(item: loadingOverlay!, attribute: .centerY, relatedBy: .equal, toItem: contentWrap, attribute: .centerY, multiplier: 1, constant: 0),
|
||||||
|
|
||||||
NSLayoutConstraint(item: progressView!, attribute: .width, relatedBy: .equal, toItem: contentWrap, attribute: .width, multiplier: 1, constant: 0),
|
NSLayoutConstraint(item: progressView!, attribute: .width, relatedBy: .equal, toItem: contentWrap, attribute: .width, multiplier: 1, constant: 0),
|
||||||
NSLayoutConstraint(item: progressView!, attribute: .centerY, relatedBy: .equal, toItem: contentWrap, attribute: .centerY, multiplier: 1, constant: 0)
|
NSLayoutConstraint(item: progressView!, attribute: .centerY, relatedBy: .equal, toItem: contentWrap, attribute: .centerY, multiplier: 1, constant: 0),
|
||||||
])
|
])
|
||||||
|
|
||||||
largeProgress!.startAnimation(nil)
|
largeProgress!.startAnimation(nil)
|
||||||
@@ -117,7 +114,6 @@ class ShareViewController: NSViewController, ShareExtensionDelegate{
|
|||||||
progressDeviceIconWrap!.layer!.masksToBounds = false
|
progressDeviceIconWrap!.layer!.masksToBounds = false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
override func viewDidLoad() {
|
override func viewDidLoad() {
|
||||||
super.viewDidLoad()
|
super.viewDidLoad()
|
||||||
|
|
||||||
@@ -125,9 +121,7 @@ class ShareViewController: NSViewController, ShareExtensionDelegate{
|
|||||||
NearbyConnectionManager.shared.addShareExtensionDelegate(self)
|
NearbyConnectionManager.shared.addShareExtensionDelegate(self)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
override func viewWillDisappear() {
|
override func viewWillDisappear() {
|
||||||
|
|
||||||
log("ShareViewController: viewWillDisappear")
|
log("ShareViewController: viewWillDisappear")
|
||||||
|
|
||||||
timeoutDispatchWorkItem?.cancel()
|
timeoutDispatchWorkItem?.cancel()
|
||||||
@@ -138,22 +132,18 @@ class ShareViewController: NSViewController, ShareExtensionDelegate{
|
|||||||
NearbyConnectionManager.shared.removeShareExtensionDelegate(self)
|
NearbyConnectionManager.shared.removeShareExtensionDelegate(self)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@IBAction func cancel(_: AnyObject?) {
|
||||||
@IBAction func cancel(_ sender: AnyObject?) {
|
|
||||||
|
|
||||||
if let device = chosenDevice {
|
if let device = chosenDevice {
|
||||||
NearbyConnectionManager.shared.cancelOutgoingTransfer(id: device.id!)
|
NearbyConnectionManager.shared.cancelOutgoingTransfer(id: device.id!)
|
||||||
}
|
}
|
||||||
let cancelError = NSError(domain: NSCocoaErrorDomain, code: NSUserCancelledError, userInfo: nil)
|
let cancelError = NSError(domain: NSCocoaErrorDomain, code: NSUserCancelledError, userInfo: nil)
|
||||||
self.extensionContext!.cancelRequest(withError: cancelError)
|
extensionContext!.cancelRequest(withError: cancelError)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@IBAction func dontSeeDeviceButton(_: AnyObject?) {
|
||||||
@IBAction func dontSeeDeviceButton(_ sender: AnyObject?) {
|
|
||||||
openQrCodeView()
|
openQrCodeView()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private func openQrCodeView() {
|
private func openQrCodeView() {
|
||||||
if qrCodeSheetView == nil {
|
if qrCodeSheetView == nil {
|
||||||
let contentView = QrCodeView {
|
let contentView = QrCodeView {
|
||||||
@@ -169,40 +159,35 @@ class ShareViewController: NSViewController, ShareExtensionDelegate{
|
|||||||
|
|
||||||
panel.contentView = hostingView
|
panel.contentView = hostingView
|
||||||
|
|
||||||
self.qrCodeSheetView = panel
|
qrCodeSheetView = panel
|
||||||
|
|
||||||
if let mainWindow = NSApp.mainWindow {
|
if let mainWindow = NSApp.mainWindow {
|
||||||
|
sheetAttachedWindow = mainWindow
|
||||||
self.sheetAttachedWindow = mainWindow
|
|
||||||
mainWindow.beginSheet(panel) { _ in }
|
mainWindow.beginSheet(panel) { _ in }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private func closeQrCodeView() {
|
private func closeQrCodeView() {
|
||||||
if let mainWindow = sheetAttachedWindow, let qrCodeView = self.qrCodeSheetView {
|
if let mainWindow = sheetAttachedWindow, let qrCodeView = qrCodeSheetView {
|
||||||
mainWindow.endSheet(qrCodeView)
|
mainWindow.endSheet(qrCodeView)
|
||||||
|
|
||||||
self.qrCodeSheetView = nil
|
qrCodeSheetView = nil
|
||||||
self.sheetAttachedWindow = nil
|
sheetAttachedWindow = nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private func urlsReady() {
|
private func urlsReady() {
|
||||||
for url in urls {
|
for url in urls {
|
||||||
if url.isFileURL {
|
if url.isFileURL {
|
||||||
let isDirectory = UnsafeMutablePointer<ObjCBool>.allocate(capacity: 1)
|
let isDirectory = UnsafeMutablePointer<ObjCBool>.allocate(capacity: 1)
|
||||||
if FileManager.default.fileExists(atPath: url.path, isDirectory: isDirectory) && isDirectory.pointee.boolValue {
|
if FileManager.default.fileExists(atPath: url.path, isDirectory: isDirectory) && isDirectory.pointee.boolValue {
|
||||||
|
|
||||||
do {
|
do {
|
||||||
let zipUrl = try createZipAtTmp(zipFilename: url.lastPathComponent, fromDirectory: url)
|
let zipUrl = try createZipAtTmp(zipFilename: url.lastPathComponent, fromDirectory: url)
|
||||||
|
|
||||||
let index = urls.firstIndex(of: url)
|
let index = urls.firstIndex(of: url)
|
||||||
urls[index!] = zipUrl
|
urls[index!] = zipUrl
|
||||||
}
|
} catch {
|
||||||
catch {
|
|
||||||
log("Error creating zip file: \(error)")
|
log("Error creating zip file: \(error)")
|
||||||
|
|
||||||
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
|
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
|
||||||
@@ -215,7 +200,7 @@ class ShareViewController: NSViewController, ShareExtensionDelegate{
|
|||||||
alert.informativeText = "TypeNotSupportedDescription".localized()
|
alert.informativeText = "TypeNotSupportedDescription".localized()
|
||||||
alert.addButton(withTitle: "TypeNotSupportedButton".localized())
|
alert.addButton(withTitle: "TypeNotSupportedButton".localized())
|
||||||
|
|
||||||
alert.beginSheetModal(for: self.view.window!) { resp in
|
alert.beginSheetModal(for: self.view.window!) { _ in
|
||||||
let cancelError = NSError(domain: NSCocoaErrorDomain, code: NSUserCancelledError, userInfo: nil)
|
let cancelError = NSError(domain: NSCocoaErrorDomain, code: NSUserCancelledError, userInfo: nil)
|
||||||
self.extensionContext!.cancelRequest(withError: cancelError)
|
self.extensionContext!.cancelRequest(withError: cancelError)
|
||||||
}
|
}
|
||||||
@@ -231,20 +216,16 @@ class ShareViewController: NSViewController, ShareExtensionDelegate{
|
|||||||
if urls[0].isFileURL {
|
if urls[0].isFileURL {
|
||||||
filesLabel!.stringValue = urls[0].lastPathComponent
|
filesLabel!.stringValue = urls[0].lastPathComponent
|
||||||
filesIcon!.image = NSWorkspace.shared.icon(forFile: urls[0].path)
|
filesIcon!.image = NSWorkspace.shared.icon(forFile: urls[0].path)
|
||||||
}
|
} else if urls[0].scheme == "http" || urls[0].scheme == "https" {
|
||||||
else if urls[0].scheme == "http" || urls[0].scheme == "https" {
|
|
||||||
|
|
||||||
filesLabel!.stringValue = urls[0].absoluteString
|
filesLabel!.stringValue = urls[0].absoluteString
|
||||||
filesIcon!.image = NSImage(named: NSImage.networkName)
|
filesIcon!.image = NSImage(named: NSImage.networkName)
|
||||||
}
|
}
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
filesLabel!.stringValue = String.localizedStringWithFormat(NSLocalizedString("NFiles", value: "%d files", comment: ""), urls.count)
|
filesLabel!.stringValue = String.localizedStringWithFormat(NSLocalizedString("NFiles", value: "%d files", comment: ""), urls.count)
|
||||||
filesIcon!.image = NSImage(named: NSImage.multipleDocumentsName)
|
filesIcon!.image = NSImage(named: NSImage.multipleDocumentsName)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
func addDevice(device: RemoteDeviceInfo) {
|
func addDevice(device: RemoteDeviceInfo) {
|
||||||
if foundDevices.isEmpty {
|
if foundDevices.isEmpty {
|
||||||
loadingOverlay?.animator().isHidden = true
|
loadingOverlay?.animator().isHidden = true
|
||||||
@@ -255,7 +236,6 @@ class ShareViewController: NSViewController, ShareExtensionDelegate{
|
|||||||
closeQrCodeView()
|
closeQrCodeView()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
func removeDevice(id: String) {
|
func removeDevice(id: String) {
|
||||||
if chosenDevice != nil {
|
if chosenDevice != nil {
|
||||||
return
|
return
|
||||||
@@ -272,9 +252,7 @@ class ShareViewController: NSViewController, ShareExtensionDelegate{
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
func connectionWasEstablished(pinCode: String) {
|
func connectionWasEstablished(pinCode: String) {
|
||||||
|
|
||||||
connectionEstablished = true
|
connectionEstablished = true
|
||||||
|
|
||||||
progressState?.stringValue = String(format: NSLocalizedString("PinCode", value: "PIN: %@", comment: ""), arguments: [pinCode])
|
progressState?.stringValue = String(format: NSLocalizedString("PinCode", value: "PIN: %@", comment: ""), arguments: [pinCode])
|
||||||
@@ -283,9 +261,7 @@ class ShareViewController: NSViewController, ShareExtensionDelegate{
|
|||||||
progressProgressBar?.doubleValue = 0
|
progressProgressBar?.doubleValue = 0
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
func connectionFailed(with error: Error) {
|
func connectionFailed(with error: Error) {
|
||||||
|
|
||||||
progressProgressBar?.isIndeterminate = false
|
progressProgressBar?.isIndeterminate = false
|
||||||
progressProgressBar?.maxValue = 1000
|
progressProgressBar?.maxValue = 1000
|
||||||
progressProgressBar?.doubleValue = 0
|
progressProgressBar?.doubleValue = 0
|
||||||
@@ -307,34 +283,28 @@ class ShareViewController: NSViewController, ShareExtensionDelegate{
|
|||||||
progressDeviceSecondaryIcon?.isHidden = false
|
progressDeviceSecondaryIcon?.isHidden = false
|
||||||
dismissDelayed()
|
dismissDelayed()
|
||||||
} else {
|
} else {
|
||||||
|
|
||||||
let alert = NSAlert(error: error)
|
let alert = NSAlert(error: error)
|
||||||
|
|
||||||
alert.beginSheetModal(for: view.window!) { resp in
|
alert.beginSheetModal(for: view.window!) { _ in
|
||||||
self.extensionContext!.cancelRequest(withError: error)
|
self.extensionContext!.cancelRequest(withError: error)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
func transferAccepted() {
|
func transferAccepted() {
|
||||||
progressState?.stringValue = NSLocalizedString("Sending", value: "Sending...", comment: "")
|
progressState?.stringValue = NSLocalizedString("Sending", value: "Sending...", comment: "")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
func transferProgress(progress: Double) {
|
func transferProgress(progress: Double) {
|
||||||
progressProgressBar!.doubleValue = progress * progressProgressBar!.maxValue
|
progressProgressBar!.doubleValue = progress * progressProgressBar!.maxValue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
func transferFinished() {
|
func transferFinished() {
|
||||||
progressState?.stringValue = NSLocalizedString("TransferFinished", value: "Transfer finished", comment: "")
|
progressState?.stringValue = NSLocalizedString("TransferFinished", value: "Transfer finished", comment: "")
|
||||||
dismissDelayed()
|
dismissDelayed()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
func selectDevice(device: RemoteDeviceInfo) {
|
func selectDevice(device: RemoteDeviceInfo) {
|
||||||
|
|
||||||
NearbyConnectionManager.shared.stopDeviceDiscovery()
|
NearbyConnectionManager.shared.stopDeviceDiscovery()
|
||||||
|
|
||||||
listViewWrapper?.animator().isHidden = true
|
listViewWrapper?.animator().isHidden = true
|
||||||
@@ -349,7 +319,6 @@ class ShareViewController: NSViewController, ShareExtensionDelegate{
|
|||||||
|
|
||||||
let timeoutAlert = DispatchWorkItem {
|
let timeoutAlert = DispatchWorkItem {
|
||||||
if !self.connectionEstablished {
|
if !self.connectionEstablished {
|
||||||
|
|
||||||
let alert = NSAlert()
|
let alert = NSAlert()
|
||||||
alert.alertStyle = .critical
|
alert.alertStyle = .critical
|
||||||
|
|
||||||
@@ -361,24 +330,20 @@ class ShareViewController: NSViewController, ShareExtensionDelegate{
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
self.timeoutDispatchWorkItem = timeoutAlert
|
timeoutDispatchWorkItem = timeoutAlert
|
||||||
DispatchQueue.main.asyncAfter(deadline: .now() + 10.0, execute: timeoutAlert)
|
DispatchQueue.main.asyncAfter(deadline: .now() + 10.0, execute: timeoutAlert)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private func dismissDelayed() {
|
private func dismissDelayed() {
|
||||||
DispatchQueue.main.asyncAfter(deadline: .now() + 2.0) {
|
DispatchQueue.main.asyncAfter(deadline: .now() + 2.0) {
|
||||||
|
|
||||||
if let error = self.lastError {
|
if let error = self.lastError {
|
||||||
self.extensionContext!.cancelRequest(withError: error)
|
self.extensionContext!.cancelRequest(withError: error)
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
self.extensionContext!.completeRequest(returningItems: nil, completionHandler: nil)
|
self.extensionContext!.completeRequest(returningItems: nil, completionHandler: nil)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private func dicoverDevices() {
|
private func dicoverDevices() {
|
||||||
let bundleIdentifier = "com.leonboettger.neardrop"
|
let bundleIdentifier = "com.leonboettger.neardrop"
|
||||||
let runningApps = NSWorkspace.shared.runningApplications
|
let runningApps = NSWorkspace.shared.runningApplications
|
||||||
@@ -397,15 +362,13 @@ class ShareViewController: NSViewController, ShareExtensionDelegate{
|
|||||||
NearbyConnectionManager.shared.startDeviceDiscovery()
|
NearbyConnectionManager.shared.startDeviceDiscovery()
|
||||||
self.scheduleAutomaticQrCodeView()
|
self.scheduleAutomaticQrCodeView()
|
||||||
}
|
}
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
log("Main app is already running")
|
log("Main app is already running")
|
||||||
NearbyConnectionManager.shared.startDeviceDiscovery()
|
NearbyConnectionManager.shared.startDeviceDiscovery()
|
||||||
self.scheduleAutomaticQrCodeView()
|
scheduleAutomaticQrCodeView()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private func scheduleAutomaticQrCodeView() {
|
private func scheduleAutomaticQrCodeView() {
|
||||||
DispatchQueue.main.asyncAfter(deadline: .now() + 2.5) {
|
DispatchQueue.main.asyncAfter(deadline: .now() + 2.5) {
|
||||||
if self.foundDevices.isEmpty {
|
if self.foundDevices.isEmpty {
|
||||||
@@ -415,8 +378,7 @@ class ShareViewController: NSViewController, ShareExtensionDelegate{
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private func imageForDeviceType(type: RemoteDeviceInfo.DeviceType) -> NSImage {
|
||||||
fileprivate func imageForDeviceType(type:RemoteDeviceInfo.DeviceType)->NSImage{
|
|
||||||
let imageName: String
|
let imageName: String
|
||||||
switch type {
|
switch type {
|
||||||
case .tablet:
|
case .tablet:
|
||||||
@@ -429,19 +391,16 @@ fileprivate func imageForDeviceType(type:RemoteDeviceInfo.DeviceType)->NSImage{
|
|||||||
return NSImage(contentsOfFile: "/System/Library/CoreServices/CoreTypes.bundle/Contents/Resources/\(imageName).icns")!
|
return NSImage(contentsOfFile: "/System/Library/CoreServices/CoreTypes.bundle/Contents/Resources/\(imageName).icns")!
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
extension ShareViewController: NSCollectionViewDataSource {
|
extension ShareViewController: NSCollectionViewDataSource {
|
||||||
|
func numberOfSections(in _: NSCollectionView) -> Int {
|
||||||
func numberOfSections(in collectionView: NSCollectionView) -> Int {
|
|
||||||
return 1
|
return 1
|
||||||
}
|
}
|
||||||
|
|
||||||
func collectionView(_ collectionView: NSCollectionView, numberOfItemsInSection section: Int) -> Int {
|
func collectionView(_: NSCollectionView, numberOfItemsInSection _: Int) -> Int {
|
||||||
return foundDevices.count
|
return foundDevices.count
|
||||||
}
|
}
|
||||||
|
|
||||||
func collectionView(_ collectionView: NSCollectionView, itemForRepresentedObjectAt indexPath: IndexPath) -> NSCollectionViewItem {
|
func collectionView(_ collectionView: NSCollectionView, itemForRepresentedObjectAt indexPath: IndexPath) -> NSCollectionViewItem {
|
||||||
|
|
||||||
let item = collectionView.makeItem(withIdentifier: NSUserInterfaceItemIdentifier(rawValue: "DeviceListCell"), for: indexPath)
|
let item = collectionView.makeItem(withIdentifier: NSUserInterfaceItemIdentifier(rawValue: "DeviceListCell"), for: indexPath)
|
||||||
|
|
||||||
guard let collectionViewItem = item as? DeviceListCell else { return item }
|
guard let collectionViewItem = item as? DeviceListCell else { return item }
|
||||||
|
|||||||
Reference in new issue
Block a user