From a748099270dd59af8064c1f02c4301c35ccd5486 Mon Sep 17 00:00:00 2001 From: Max Goedjen Date: Sat, 30 Oct 2021 15:36:50 -0700 Subject: [PATCH] Async await and double conversion. --- Secretive/App.swift | 15 ++++++----- .../Controllers/LaunchAgentController.swift | 26 +++++++------------ Secretive/Views/SetupView.swift | 14 +++++----- 3 files changed, 26 insertions(+), 29 deletions(-) diff --git a/Secretive/App.swift b/Secretive/App.swift index e12712d..6f46903 100644 --- a/Secretive/App.swift +++ b/Secretive/App.swift @@ -68,13 +68,13 @@ extension Secretive { private func reinstallAgent() { justUpdatedChecker.check() - LaunchAgentController().install { + Task { + await LaunchAgentController().install() // Wait a second for launchd to kick in (next runloop isn't enough). - DispatchQueue.main.asyncAfter(deadline: .now() + 1) { - agentStatusChecker.check() - if !agentStatusChecker.running { - forceLaunchAgent() - } + await Task.sleep(UInt64(Measurement(value: 1, unit: UnitDuration.seconds).converted(to: .nanoseconds).value)) + agentStatusChecker.check() + if !agentStatusChecker.running { + forceLaunchAgent() } } } @@ -82,7 +82,8 @@ extension Secretive { private func forceLaunchAgent() { // We've run setup, we didn't just update, launchd is just not doing it's thing. // Force a launch directly. - LaunchAgentController().forceLaunch { _ in + Task { + try? await LaunchAgentController().forceLaunch() agentStatusChecker.check() } } diff --git a/Secretive/Controllers/LaunchAgentController.swift b/Secretive/Controllers/LaunchAgentController.swift index d50299d..dc558e9 100644 --- a/Secretive/Controllers/LaunchAgentController.swift +++ b/Secretive/Controllers/LaunchAgentController.swift @@ -6,33 +6,27 @@ import SecretKit struct LaunchAgentController { - func install(completion: (() -> Void)? = nil) { + func install() async { Logger().debug("Installing agent") _ = setEnabled(false) // This is definitely a bit of a "seems to work better" thing but: // Seems to more reliably hit if these are on separate runloops, otherwise it seems like it sometimes doesn't kill old // and start new? - DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) { - _ = setEnabled(true) - completion?() - } - + await Task.sleep(UInt64(Measurement(value: 0.1, unit: UnitDuration.seconds).converted(to: .nanoseconds).value)) + _ = setEnabled(true) } - func forceLaunch(completion: ((Bool) -> Void)?) { + func forceLaunch() async throws { Logger().debug("Agent is not running, attempting to force launch") let url = Bundle.main.bundleURL.appendingPathComponent("Contents/Library/LoginItems/SecretAgent.app") let config = NSWorkspace.OpenConfiguration() config.activates = false - NSWorkspace.shared.openApplication(at: url, configuration: config) { app, error in - DispatchQueue.main.async { - completion?(error == nil) - } - if let error = error { - Logger().error("Error force launching \(error.localizedDescription)") - } else { - Logger().debug("Agent force launched") - } + do { + try await NSWorkspace.shared.openApplication(at: url, configuration: config) + Logger().debug("Agent force launched") + } catch { + Logger().error("Error force launching \(error.localizedDescription)") + throw error } } diff --git a/Secretive/Views/SetupView.swift b/Secretive/Views/SetupView.swift index ad358f2..a79244e 100644 --- a/Secretive/Views/SetupView.swift +++ b/Secretive/Views/SetupView.swift @@ -22,7 +22,7 @@ struct SetupView: View { } .frame(width: proxy.size.width) } - .offset(x: -proxy.size.width * CGFloat(stepIndex), y: 0) + .offset(x: -proxy.size.width * Double(stepIndex), y: 0) } } } @@ -44,7 +44,7 @@ struct StepView: View { let currentStep: Int // Ideally we'd have a geometry reader inside this view doing this for us, but that crashes on 11.0b7 - let width: CGFloat + let width: Double var body: some View { ZStack(alignment: .leading) { @@ -53,7 +53,7 @@ struct StepView: View { .frame(height: 5) Rectangle() .foregroundColor(.green) - .frame(width: max(0, ((width - (Constants.padding * 2)) / CGFloat(numberOfSteps - 1)) * CGFloat(currentStep) - (Constants.circleWidth / 2)), height: 5) + .frame(width: max(0, ((width - (Constants.padding * 2)) / Double(numberOfSteps - 1)) * Double(currentStep) - (Constants.circleWidth / 2)), height: 5) .animation(.spring()) HStack { ForEach(0..