Skip to content

Commit

Permalink
Fix issues around upgrading (#173)
Browse files Browse the repository at this point in the history
* Re-check after

* Completion

* Relaunch
  • Loading branch information
maxgoedjen committed Nov 13, 2020
1 parent 08b5c7b commit 76514d4
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 17 deletions.
42 changes: 32 additions & 10 deletions Secretive/App.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,16 @@ struct Secretive: App {
.onAppear {
if !hasRunSetup {
showingSetup = true
} else {
if agentStatusChecker.running && justUpdatedChecker.justUpdated {
// Relaunch the agent, since it'll be running from earlier update still
_ = LaunchAgentController().install()
}
}
}
.onReceive(NotificationCenter.default.publisher(for: NSApplication.willBecomeActiveNotification)) { _ in
.onReceive(NotificationCenter.default.publisher(for: NSApplication.didBecomeActiveNotification)) { _ in
guard hasRunSetup else { return }
agentStatusChecker.check()
if hasRunSetup && !agentStatusChecker.running {
// We've run setup, we didn't just update, launchd is just not doing it's thing.
// Force a launch directly.
LaunchAgentController().forceLaunch()
if agentStatusChecker.running && justUpdatedChecker.justUpdated {
// Relaunch the agent, since it'll be running from earlier update still
reinstallAgent()
} else if !agentStatusChecker.running {
forceLaunchAgent()
}
}
}
Expand All @@ -67,6 +64,31 @@ struct Secretive: App {

}

extension Secretive {

private func reinstallAgent() {
justUpdatedChecker.check()
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()
}
}
}
}

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
agentStatusChecker.check()
}
}

}


private enum Constants {
static let helpURL = URL(string: "https://github.com/maxgoedjen/secretive/blob/main/FAQ.md")!
Expand Down
4 changes: 1 addition & 3 deletions Secretive/Controllers/JustUpdatedChecker.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@ class JustUpdatedChecker: ObservableObject, JustUpdatedCheckerProtocol {
let lastBuild = UserDefaults.standard.object(forKey: Constants.previousVersionUserDefaultsKey) as? String ?? "None"
let currentBuild = Bundle.main.infoDictionary!["CFBundleShortVersionString"] as! String
UserDefaults.standard.set(currentBuild, forKey: Constants.previousVersionUserDefaultsKey)
if lastBuild != currentBuild {
justUpdated = true
}
justUpdated = lastBuild != currentBuild
}


Expand Down
14 changes: 11 additions & 3 deletions Secretive/Controllers/LaunchAgentController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,24 @@ import OSLog

struct LaunchAgentController {

func install() -> Bool {
func install(completion: (() -> Void)? = nil) {
Logger().debug("Installing agent")
_ = setEnabled(false)
return setEnabled(true)
// 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?()
}

}

func forceLaunch() {
func forceLaunch(completion: ((Bool) -> Void)?) {
Logger().debug("Agent is not running, attempting to force launch")
let url = Bundle.main.bundleURL.appendingPathComponent("Contents/Library/LoginItems/SecretAgent.app")
NSWorkspace.shared.openApplication(at: url, configuration: NSWorkspace.OpenConfiguration()) { app, error in
completion?(error == nil)
if let error = error {
Logger().error("Error force launching \(error.localizedDescription)")
} else {
Expand Down
2 changes: 1 addition & 1 deletion Secretive/Views/SetupView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ struct SecretAgentSetupView: View {
}

func install() {
_ = LaunchAgentController().install()
LaunchAgentController().install()
buttonAction()
}

Expand Down

0 comments on commit 76514d4

Please sign in to comment.