//
|
|
// DownloadStateNotifier.swift
|
|
// pEpNotifications
|
|
//
|
|
// Created by David Alarcon on 26/11/2020.
|
|
// Copyright © 2020 p≡p foundation. All rights reserved.
|
|
//
|
|
|
|
import SwiftUI
|
|
|
|
public struct Product {
|
|
public var name: String
|
|
public var filename: String
|
|
public var productInfo: Dictionary<String, String> {
|
|
return ["name": name, "filename": filename]
|
|
}
|
|
@available(macOS, obsoleted: 10.14)
|
|
public var notification: NSUserNotification?
|
|
}
|
|
|
|
struct DownloadStateNotifier {
|
|
var menuItem: NSMenuItem
|
|
|
|
init(at menuItem: NSMenuItem) {
|
|
self.menuItem = menuItem
|
|
}
|
|
|
|
func notify(_ state: DownloadState, with product: Product? = nil) {
|
|
DispatchQueue.main.async {
|
|
menuItem.title = state.localizedString()
|
|
menuItem.isEnabled = (product != nil)
|
|
menuItem.representedObject = product
|
|
}
|
|
}
|
|
}
|
|
|
|
public enum DownloadState {
|
|
|
|
case Connecting
|
|
case Connected
|
|
case Downloading(String)
|
|
case NewVersionAvailable(String)
|
|
case UpToDate
|
|
case ConnectionFailed
|
|
|
|
func localizedString() -> String {
|
|
switch self {
|
|
case .Connecting:
|
|
return NSLocalizedString("Connecting...", comment: "")
|
|
case .Connected:
|
|
return NSLocalizedString("Connected.", comment: "")
|
|
case .Downloading(let product):
|
|
return String.localizedStringWithFormat(NSLocalizedString("Downloading update for %@…", comment: ""), product)
|
|
case .NewVersionAvailable(let product):
|
|
return String.localizedStringWithFormat(NSLocalizedString("New version of %@ available", comment: ""), product)
|
|
case .UpToDate:
|
|
return NSLocalizedString("The software is up to date.", comment: "")
|
|
case .ConnectionFailed:
|
|
return NSLocalizedString("Connection failed", comment: "")
|
|
}
|
|
}
|
|
}
|
|
|