From e86c6774630006c903ad528eb0f9fe3b9ab4dbce Mon Sep 17 00:00:00 2001 From: Serhiy Mytrovtsiy Date: Fri, 17 Mar 2023 18:31:44 +0100 Subject: [PATCH] feat: added an error window when the update failed --- Kit/helpers.swift | 11 +++++++++++ Kit/plugins/Updater.swift | 6 +++--- Stats/AppDelegate.swift | 6 +++++- Stats/Views/Update.swift | 6 +++++- Stats/helpers.swift | 6 +++++- 5 files changed, 29 insertions(+), 6 deletions(-) diff --git a/Kit/helpers.swift b/Kit/helpers.swift index b0967533..9563d711 100644 --- a/Kit/helpers.swift +++ b/Kit/helpers.swift @@ -1372,3 +1372,14 @@ public func iconFromSymbol(name: String, scale: NSImage.SymbolScale) -> NSImage? } return nil } + +public func showAlert(_ message: String, _ information: String? = nil, _ style: NSAlert.Style = .informational) { + let alert = NSAlert() + alert.messageText = message + if let information = information { + alert.informativeText = information + } + alert.addButton(withTitle: "OK") + alert.alertStyle = style + alert.runModal() +} diff --git a/Kit/plugins/Updater.swift b/Kit/plugins/Updater.swift index c3503a14..a033196b 100644 --- a/Kit/plugins/Updater.swift +++ b/Kit/plugins/Updater.swift @@ -165,7 +165,7 @@ public class Updater { downloadTask.resume() } - public func install(path: String) { + public func install(path: String, completion: @escaping (_ error: String?) -> Void) { let pwd = Bundle.main.bundleURL.absoluteString .replacingOccurrences(of: "file://", with: "") .replacingOccurrences(of: "Stats.app", with: "") @@ -173,13 +173,13 @@ public class Updater { let dmg = path.replacingOccurrences(of: "file://", with: "") if !FileManager.default.isWritableFile(atPath: pwd) { - print("has no write permission on \(pwd)") + completion("has no write permission on \(pwd)") return } let diff = (Int(Date().timeIntervalSince1970) - self.lastInstallTS) / 60 if diff <= 3 { - print("last install was \(diff) minutes ago, stopping...") + completion("last install was \(diff) minutes ago, stopping...") return } diff --git a/Stats/AppDelegate.swift b/Stats/AppDelegate.swift index 93d212f8..84e43504 100755 --- a/Stats/AppDelegate.swift +++ b/Stats/AppDelegate.swift @@ -103,7 +103,11 @@ class AppDelegate: NSObject, NSApplicationDelegate, UNUserNotificationCenterDele debug("Downloading new version of app...") if let url = URL(string: uri) { updater.download(url, completion: { path in - updater.install(path: path) + updater.install(path: path) { error in + if let error { + showAlert("Error update Stats", error, .critical) + } + } }) } } diff --git a/Stats/Views/Update.swift b/Stats/Views/Update.swift index adbeb190..4d9bab70 100644 --- a/Stats/Views/Update.swift +++ b/Stats/Views/Update.swift @@ -284,6 +284,10 @@ private class UpdateView: NSView { } @objc private func install(_ sender: Any) { - updater.install(path: self.path) + updater.install(path: self.path) { error in + if let error { + showAlert("Error update Stats", error, .critical) + } + } } } diff --git a/Stats/helpers.swift b/Stats/helpers.swift index fb39ed00..93047619 100644 --- a/Stats/helpers.swift +++ b/Stats/helpers.swift @@ -159,7 +159,11 @@ extension AppDelegate { if silent { if let url = URL(string: version.url) { updater.download(url, completion: { path in - updater.install(path: path) + updater.install(path: path) { error in + if let error { + showAlert("Error update Stats", error, .critical) + } + } }) } return