feat: added an error window when the update failed

This commit is contained in:
Serhiy Mytrovtsiy
2023-03-17 18:31:44 +01:00
parent 7dd4e0ecf3
commit e86c677463
5 changed files with 29 additions and 6 deletions

View File

@@ -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()
}

View File

@@ -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
}

View File

@@ -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)
}
}
})
}
}

View File

@@ -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)
}
}
}
}

View File

@@ -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