secretive/Sources/Secretive/Controllers/JustUpdatedChecker.swift

42 lines
1.1 KiB
Swift
Raw Permalink Normal View History

2020-04-04 22:16:31 +00:00
import Foundation
import Combine
import AppKit
protocol JustUpdatedCheckerProtocol: ObservableObject {
var justUpdated: Bool { get }
}
class JustUpdatedChecker: ObservableObject, JustUpdatedCheckerProtocol {
@Published var justUpdated: Bool = false
2022-01-03 06:15:18 +00:00
var alreadyRelaunchedForDebug = false
2020-04-04 22:16:31 +00:00
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)
2022-01-03 06:15:18 +00:00
if currentBuild != Constants.debugVersionKey {
justUpdated = lastBuild != currentBuild
} else {
justUpdated = !alreadyRelaunchedForDebug
alreadyRelaunchedForDebug = true
}
2020-04-04 22:16:31 +00:00
}
}
extension JustUpdatedChecker {
enum Constants {
static let previousVersionUserDefaultsKey = "com.maxgoedjen.Secretive.lastBuild"
2022-01-03 06:15:18 +00:00
static let debugVersionKey = "GITHUB_CI_VERSION"
2020-04-04 22:16:31 +00:00
}
}