Async await and double conversion.

This commit is contained in:
Max Goedjen 2021-10-30 15:36:50 -07:00
parent 88b7ef82da
commit a748099270
3 changed files with 26 additions and 29 deletions

View File

@ -68,13 +68,13 @@ extension Secretive {
private func reinstallAgent() { private func reinstallAgent() {
justUpdatedChecker.check() justUpdatedChecker.check()
LaunchAgentController().install { Task {
await LaunchAgentController().install()
// Wait a second for launchd to kick in (next runloop isn't enough). // Wait a second for launchd to kick in (next runloop isn't enough).
DispatchQueue.main.asyncAfter(deadline: .now() + 1) { await Task.sleep(UInt64(Measurement(value: 1, unit: UnitDuration.seconds).converted(to: .nanoseconds).value))
agentStatusChecker.check() agentStatusChecker.check()
if !agentStatusChecker.running { if !agentStatusChecker.running {
forceLaunchAgent() forceLaunchAgent()
}
} }
} }
} }
@ -82,7 +82,8 @@ extension Secretive {
private func forceLaunchAgent() { private func forceLaunchAgent() {
// We've run setup, we didn't just update, launchd is just not doing it's thing. // We've run setup, we didn't just update, launchd is just not doing it's thing.
// Force a launch directly. // Force a launch directly.
LaunchAgentController().forceLaunch { _ in Task {
try? await LaunchAgentController().forceLaunch()
agentStatusChecker.check() agentStatusChecker.check()
} }
} }

View File

@ -6,33 +6,27 @@ import SecretKit
struct LaunchAgentController { struct LaunchAgentController {
func install(completion: (() -> Void)? = nil) { func install() async {
Logger().debug("Installing agent") Logger().debug("Installing agent")
_ = setEnabled(false) _ = setEnabled(false)
// This is definitely a bit of a "seems to work better" thing but: // 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 // Seems to more reliably hit if these are on separate runloops, otherwise it seems like it sometimes doesn't kill old
// and start new? // and start new?
DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) { await Task.sleep(UInt64(Measurement(value: 0.1, unit: UnitDuration.seconds).converted(to: .nanoseconds).value))
_ = setEnabled(true) _ = setEnabled(true)
completion?()
}
} }
func forceLaunch(completion: ((Bool) -> Void)?) { func forceLaunch() async throws {
Logger().debug("Agent is not running, attempting to force launch") Logger().debug("Agent is not running, attempting to force launch")
let url = Bundle.main.bundleURL.appendingPathComponent("Contents/Library/LoginItems/SecretAgent.app") let url = Bundle.main.bundleURL.appendingPathComponent("Contents/Library/LoginItems/SecretAgent.app")
let config = NSWorkspace.OpenConfiguration() let config = NSWorkspace.OpenConfiguration()
config.activates = false config.activates = false
NSWorkspace.shared.openApplication(at: url, configuration: config) { app, error in do {
DispatchQueue.main.async { try await NSWorkspace.shared.openApplication(at: url, configuration: config)
completion?(error == nil) Logger().debug("Agent force launched")
} } catch {
if let error = error { Logger().error("Error force launching \(error.localizedDescription)")
Logger().error("Error force launching \(error.localizedDescription)") throw error
} else {
Logger().debug("Agent force launched")
}
} }
} }

View File

@ -22,7 +22,7 @@ struct SetupView: View {
} }
.frame(width: proxy.size.width) .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 let currentStep: Int
// Ideally we'd have a geometry reader inside this view doing this for us, but that crashes on 11.0b7 // 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 { var body: some View {
ZStack(alignment: .leading) { ZStack(alignment: .leading) {
@ -53,7 +53,7 @@ struct StepView: View {
.frame(height: 5) .frame(height: 5)
Rectangle() Rectangle()
.foregroundColor(.green) .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()) .animation(.spring())
HStack { HStack {
ForEach(0..<numberOfSteps) { index in ForEach(0..<numberOfSteps) { index in
@ -93,8 +93,8 @@ extension StepView {
enum Constants { enum Constants {
static let padding: CGFloat = 15 static let padding: Double = 15
static let circleWidth: CGFloat = 30 static let circleWidth: Double = 30
} }
@ -157,7 +157,9 @@ struct SecretAgentSetupView: View {
} }
func install() { func install() {
LaunchAgentController().install() Task {
await LaunchAgentController().install()
}
buttonAction() buttonAction()
} }