From 6ea07825651551ab75c989b8c5693b353fd7680d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Leon=20B=C3=B6ttger?= Date: Thu, 15 May 2025 17:55:52 +0200 Subject: [PATCH] Fix router address button --- NearDrop/AppDelegate.swift | 40 ++++++++++--------- .../IssueViews/ApIsolationIssueView.swift | 22 ++++++---- NearDrop/IssueViews/IssueView.swift | 23 ++++++++++- 3 files changed, 56 insertions(+), 29 deletions(-) diff --git a/NearDrop/AppDelegate.swift b/NearDrop/AppDelegate.swift index 424b4de..d6aca2f 100644 --- a/NearDrop/AppDelegate.swift +++ b/NearDrop/AppDelegate.swift @@ -205,16 +205,17 @@ class AppDelegate: NSObject, NSApplicationDelegate, UNUserNotificationCenterDele func openAlert(type: AlertType) { - - // Create an NSWindow to host the SwiftUI view - let alertWindow = NSWindow( - contentRect: NSRect(x: 0, y: 0, width: issueViewWidth, height: issueViewHeight), - styleMask: [.titled, .closable], - backing: .buffered, - defer: false - ) - - switch type { + DispatchQueue.main.async { [self] in + + // Create an NSWindow to host the SwiftUI view + let alertWindow = NSWindow( + contentRect: NSRect(x: 0, y: 0, width: issueViewWidth, height: issueViewHeight), + styleMask: [.titled, .closable], + backing: .buffered, + defer: false + ) + + switch type { case .ApIsolation: apIsolationAlertWindow = alertWindow alertWindow.contentView = NSHostingView(rootView: ApIsolationIssueView()) @@ -224,16 +225,17 @@ class AppDelegate: NSObject, NSApplicationDelegate, UNUserNotificationCenterDele case .Firewall: firewallAlertWindow = alertWindow alertWindow.contentView = NSHostingView(rootView: FirewallIssueView()) + } + + alertWindow.title = "QuickDrop" + alertWindow.center() + alertWindow.isReleasedWhenClosed = false + alertWindow.setFrameAutosaveName(type.rawValue) + + NSApp.activate(ignoringOtherApps: true) + alertWindow.makeKeyAndOrderFront(nil) + alertWindow.level = .normal } - - alertWindow.title = "QuickDrop" - alertWindow.center() - alertWindow.isReleasedWhenClosed = false - alertWindow.setFrameAutosaveName(type.rawValue) - - NSApp.activate(ignoringOtherApps: true) - alertWindow.makeKeyAndOrderFront(nil) - alertWindow.level = .normal } diff --git a/NearDrop/IssueViews/ApIsolationIssueView.swift b/NearDrop/IssueViews/ApIsolationIssueView.swift index 10104dc..c5905dc 100644 --- a/NearDrop/IssueViews/ApIsolationIssueView.swift +++ b/NearDrop/IssueViews/ApIsolationIssueView.swift @@ -17,27 +17,33 @@ struct ApIsolationIssueView: View { var body: some View { IssueView(image: .router, header: "ApIsolationHeader".localized(), description: "ApIsolationDescription".localized(), actionLabel: "ApIsolationAction".localized()) { - openRouterSettingsPage() + await openRouterSettingsPage() } } } +fileprivate let routerCheckQueue = DispatchQueue(label: "routerCheckQueue") -func openRouterSettingsPage() { - guard let routerIP = getDefaultGatewayIP() else { - + +func openRouterSettingsPage() async { + let routerIP: String? = await withCheckedContinuation { continuation in + DispatchQueue.global(qos: .userInitiated).async { + let ip = getDefaultGatewayIP() + continuation.resume(returning: ip) + } + } + + guard let routerIP else { DispatchQueue.main.async { let alert = NSAlert() alert.alertStyle = .critical - + alert.messageText = "ApIsolationFindRouterFailedHeader".localized() alert.informativeText = "ApIsolationFindRouterFailedDescription".localized() - alert.addButton(withTitle: "CloseAlert".localized()) - let _ = alert.runModal() + alert.runModal() } - return } diff --git a/NearDrop/IssueViews/IssueView.swift b/NearDrop/IssueViews/IssueView.swift index d4ee9e4..bcf53c2 100644 --- a/NearDrop/IssueViews/IssueView.swift +++ b/NearDrop/IssueViews/IssueView.swift @@ -20,7 +20,9 @@ struct IssueView: View { let description: String var actionLabel: String? = nil - var action: (() -> Void)? = nil + var action: (() async -> Void)? = nil + + @State var isLoading = false var body: some View { @@ -67,8 +69,25 @@ struct IssueView: View { if let actionLabel = actionLabel, let action = action { Button(actionLabel) { - action() + + withAnimation { + isLoading = true + } + + Task { + await action() + + withAnimation { + isLoading = false + } + } } + .opacity(isLoading ? 0 : 1) + .overlay( + ProgressView() + .opacity(isLoading ? 1 : 0) + .scaleEffect(0.7) + ) .padding(.bottom, 30) } }