This commit is contained in:
Serhiy Mytrovtsiy
2022-06-10 20:27:24 +02:00
parent 2824f2df7c
commit 8071a06896
2 changed files with 39 additions and 2 deletions

View File

@@ -43,6 +43,23 @@ public class Updater {
private var observation: NSKeyValueObservation?
private var lastCheckTS: Int {
get {
return Store.shared.int(key: "updater_check_ts", defaultValue: -1)
}
set {
Store.shared.set(key: "updater_check_ts", value: newValue)
}
}
private var lastInstallTS: Int {
get {
return Store.shared.int(key: "updater_install_ts", defaultValue: -1)
}
set {
Store.shared.set(key: "updater_install_ts", value: newValue)
}
}
public init(github: String, url: String) {
self.github = URL(string: "https://api.github.com/repos/\(github)/releases/latest")!
self.server = URL(string: url)!
@@ -52,12 +69,22 @@ public class Updater {
observation?.invalidate()
}
public func check(completion: @escaping (_ result: version_s?, _ error: Error?) -> Void) {
public func check(force: Bool = false, completion: @escaping (_ result: version_s?, _ error: Error?) -> Void) {
if !isConnectedToNetwork() {
completion(nil, "No internet connection")
return
}
let diff = (Int(Date().timeIntervalSince1970) - self.lastCheckTS) / 60
if !force && diff <= 10 {
completion(nil, "last check was \(diff) minutes ago, stopping...")
return
}
defer {
self.lastCheckTS = Int(Date().timeIntervalSince1970)
}
self.fetchRelease(uri: self.server) { (result, err) in
guard let result = result, err == nil else {
self.fetchRelease(uri: self.github) { (result, err) in
@@ -139,6 +166,16 @@ public class Updater {
}
public func install(path: String) {
let diff = (Int(Date().timeIntervalSince1970) - self.lastInstallTS) / 60
if diff <= 3 {
print("last install was \(diff) minutes ago, stopping...")
return
}
defer {
self.lastInstallTS = Int(Date().timeIntervalSince1970)
}
print("Started new version installation...")
_ = syncShell("mkdir /tmp/Stats") // make sure that directory exist

View File

@@ -203,7 +203,7 @@ class ApplicationSettings: NSStackView {
// MARK: - actions
@objc func updateAction(_ sender: NSObject) {
updater.check(completion: { result, error in
updater.check(force: true, completion: { result, error in
if error != nil {
debug("error updater.check(): \(error!.localizedDescription)")
return