Browse Source

MOS-13 Add notification property in product before 10.14 and all related refactor.

MOS-13
David Alarcon 1 year ago
parent
commit
9b4e040795
2 changed files with 17 additions and 44 deletions
  1. +14
    -18
      Submodules/pEpNotifications/pEpNotifications/AppDelegate.swift
  2. +3
    -26
      Submodules/pEpNotifications/pEpNotifications/DownloadStateNotifier.swift

+ 14
- 18
Submodules/pEpNotifications/pEpNotifications/AppDelegate.swift View File

@ -71,28 +71,23 @@ class AppDelegate: NSObject {
}
@IBAction func installNow(_ sender: NSMenuItem) {
guard let product = sender.representedObject as? Product else {
return
}
uninstallMenuExtra()
if #available(OSX 10.14, *) {
guard let product = sender.representedObject as? Product else {
return
}
uninstallMenuExtra()
downloadNotificationManager.removeDeliveredNotifications(of: product)
NSLog("pEpNotifications: installNow clicked for %@", product.name)
NSWorkspace.shared.openFile(product.filename)
} else {
guard let product = sender.representedObject as? Dictionary<String, Any>,
let un = product["notification"] as? NSUserNotification,
let name = product["name"] as? String,
let filename = product["filename"] as? String
else {
guard let notification = product.notification else {
return
}
uninstallMenuExtra()
nc.removeDeliveredNotification(un)
NSLog("pEpNotifications: installNow clicked for %@", name)
NSWorkspace.shared.openFile(filename)
nc.removeDeliveredNotification(notification)
}
NSLog("pEpNotifications: installNow clicked for %@", product.name)
NSWorkspace.shared.openFile(product.filename)
sender.representedObject = nil
downloadStateNotifier.notify(.Connected)
}
@ -212,11 +207,10 @@ extension AppDelegate: PEPNotificationProtocol {
}
case .downloadArrived:
let product = Product(name: withName as String, filename: withFilename as String);
var product = Product(name: withName as String, filename: withFilename as String);
if #available(OSX 10.14, *) {
downloadNotificationManager.scheduleDownloadNotification(with: product)
downloadStateNotifier.notify(.NewVersionAvailable(String(withName)), with: product)
} else {
/*
UNUserNotificationCenter availability is 10.14+, so for older os
@ -235,8 +229,10 @@ extension AppDelegate: PEPNotificationProtocol {
un.setValue(true, forKey: "_showsButtons")
un.userInfo = ["name": withName, "filename": withFilename]
nc.deliver(un)
downloadStateNotifier.notify(.NewVersionAvailable(String(withName)), with: product, in: un)
product.notification = un
}
downloadStateNotifier.notify(.NewVersionAvailable(String(withName)), with: product)
case .noDownloadAvailable:
NSLog("pEpNotifications: no download available")


+ 3
- 26
Submodules/pEpNotifications/pEpNotifications/DownloadStateNotifier.swift View File

@ -11,9 +11,11 @@ import SwiftUI
public struct Product {
public var name: String
public var filename: String
public var productInfo: Dictionary<String, Any> {
public var productInfo: Dictionary<String, String> {
return ["name": name, "filename": filename]
}
@available(macOS, obsoleted: 10.14)
public var notification: NSUserNotification?
}
struct DownloadStateNotifier {
@ -23,36 +25,11 @@ struct DownloadStateNotifier {
self.menuItem = menuItem
}
@available(OSX 10.14, *)
func notify(_ state: DownloadState, with product: Product? = nil) {
menuItem.title = state.localizedString()
menuItem.isEnabled = (product != nil)
menuItem.representedObject = product
}
@available(macOS, obsoleted: 10.14)
func notify(_ state: DownloadState, with product: Product? = nil, in notification: NSUserNotification? = nil) {
menuItem.title = state.localizedString()
guard let product = product else {
menuItem.isEnabled = false
return
}
menuItem.isEnabled = true
guard let notification = notification else {
menuItem.representedObject = product.productInfo
return
}
var info = product.productInfo
info["notification"] = notification
menuItem.representedObject = info
}
}
public enum DownloadState {


Loading…
Cancel
Save