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:
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)
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user