From 8071a068963da4557589c1c0a6f6189530e9b25f Mon Sep 17 00:00:00 2001 From: Serhiy Mytrovtsiy Date: Fri, 10 Jun 2022 20:27:24 +0200 Subject: [PATCH] feat: reverted f8c9eb987d7e1ca678ae13466262265ac1d96f9b --- Kit/plugins/Updater.swift | 39 ++++++++++++++++++++++++++++++++++- Stats/Views/AppSettings.swift | 2 +- 2 files changed, 39 insertions(+), 2 deletions(-) diff --git a/Kit/plugins/Updater.swift b/Kit/plugins/Updater.swift index e604133d..2769924f 100644 --- a/Kit/plugins/Updater.swift +++ b/Kit/plugins/Updater.swift @@ -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 diff --git a/Stats/Views/AppSettings.swift b/Stats/Views/AppSettings.swift index 466e0132..1abb32d2 100644 --- a/Stats/Views/AppSettings.swift +++ b/Stats/Views/AppSettings.swift @@ -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