mirror of
https://github.com/maxgoedjen/secretive.git
synced 2026-03-05 09:24:49 +01:00
Relaunch agent if updated (#86)
This commit is contained in:
@@ -17,6 +17,7 @@ class AppDelegate: NSObject, NSApplicationDelegate {
|
||||
}()
|
||||
let updater = Updater()
|
||||
let agentStatusChecker = AgentStatusChecker()
|
||||
let justUpdatedChecker = JustUpdatedChecker()
|
||||
|
||||
func applicationDidFinishLaunching(_ aNotification: Notification) {
|
||||
let contentView = ContentView(storeList: storeList, updater: updater, agentStatusChecker: agentStatusChecker, runSetupBlock: { self.runSetup(sender: nil) })
|
||||
@@ -40,6 +41,7 @@ class AppDelegate: NSObject, NSApplicationDelegate {
|
||||
newMenuItem.isEnabled = true
|
||||
}
|
||||
runSetupIfNeeded()
|
||||
relaunchAgentIfNeeded()
|
||||
}
|
||||
|
||||
func applicationDidBecomeActive(_ notification: Notification) {
|
||||
@@ -89,6 +91,12 @@ extension AppDelegate {
|
||||
}
|
||||
}
|
||||
|
||||
func relaunchAgentIfNeeded() {
|
||||
if agentStatusChecker.running && justUpdatedChecker.justUpdated {
|
||||
LaunchAgentController().relaunch()
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
extension AppDelegate {
|
||||
|
||||
36
Secretive/Controllers/JustUpdatedChecker.swift
Normal file
36
Secretive/Controllers/JustUpdatedChecker.swift
Normal file
@@ -0,0 +1,36 @@
|
||||
import Foundation
|
||||
import Combine
|
||||
import AppKit
|
||||
|
||||
protocol JustUpdatedCheckerProtocol: ObservableObject {
|
||||
var justUpdated: Bool { get }
|
||||
}
|
||||
|
||||
class JustUpdatedChecker: ObservableObject, JustUpdatedCheckerProtocol {
|
||||
|
||||
@Published var justUpdated: Bool = false
|
||||
|
||||
init() {
|
||||
check()
|
||||
}
|
||||
|
||||
func check() {
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
extension JustUpdatedChecker {
|
||||
|
||||
enum Constants {
|
||||
static let previousVersionUserDefaultsKey = "com.maxgoedjen.Secretive.lastBuild"
|
||||
}
|
||||
|
||||
}
|
||||
19
Secretive/Controllers/LaunchAgentController.swift
Normal file
19
Secretive/Controllers/LaunchAgentController.swift
Normal file
@@ -0,0 +1,19 @@
|
||||
import Foundation
|
||||
import ServiceManagement
|
||||
|
||||
struct LaunchAgentController {
|
||||
|
||||
func install() -> Bool {
|
||||
setEnabled(true)
|
||||
}
|
||||
|
||||
func relaunch() {
|
||||
_ = setEnabled(false)
|
||||
_ = setEnabled(true)
|
||||
}
|
||||
|
||||
private func setEnabled(_ enabled: Bool) -> Bool {
|
||||
SMLoginItemSetEnabled("com.maxgoedjen.Secretive.SecretAgent" as CFString, enabled)
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,6 +1,5 @@
|
||||
import Foundation
|
||||
import SwiftUI
|
||||
import ServiceManagement
|
||||
|
||||
struct SetupView: View {
|
||||
|
||||
@@ -117,7 +116,7 @@ struct SetupStepCommandView: View {
|
||||
extension SetupView {
|
||||
|
||||
func installLaunchAgent() -> Bool {
|
||||
SMLoginItemSetEnabled("com.maxgoedjen.Secretive.SecretAgent" as CFString, true)
|
||||
LaunchAgentController().install()
|
||||
}
|
||||
|
||||
func markAsDone() -> Bool {
|
||||
|
||||
Reference in New Issue
Block a user