From dec8307adbc723123d0f41d9d9c56bb3eb6ba97b Mon Sep 17 00:00:00 2001 From: David Alarcon Date: Mon, 30 Nov 2020 15:30:28 +0100 Subject: [PATCH] MOS-10 Refactor Product from typedef to struct and related changes. --- .../pEpNotifications/AppDelegate.swift | 9 +++------ .../DownloadNotificationManager.swift | 19 +++++++++---------- .../DownloadStateNotifier.swift | 8 +++++++- 3 files changed, 19 insertions(+), 17 deletions(-) diff --git a/Submodules/pEpNotifications/pEpNotifications/AppDelegate.swift b/Submodules/pEpNotifications/pEpNotifications/AppDelegate.swift index 5ca3eb0..c4a417f 100644 --- a/Submodules/pEpNotifications/pEpNotifications/AppDelegate.swift +++ b/Submodules/pEpNotifications/pEpNotifications/AppDelegate.swift @@ -196,7 +196,7 @@ extension AppDelegate: PEPNotificationProtocol { } case .downloadArrived: - let product = ["name": withName, "filename": withFilename] + let product = Product(name: withName as String, filename: withFilename as String); if #available(OSX 10.14, *) { downloadNotificationManager.scheduleDownloadNotification(with: product) @@ -304,12 +304,9 @@ extension AppDelegate: DownloadNotificationManagerDelegate { defer { uninstallMenuExtra() } - guard let filename = product["filename"] as? String else { - return - } - NSLog("pEpNotifications: actionButtonClicked for %@", filename) - NSWorkspace.shared.openFile(filename) + NSLog("pEpNotifications: actionButtonClicked for %@", product.filename) + NSWorkspace.shared.openFile(product.filename) downloadStateNotifier.notify(.Connected) } } diff --git a/Submodules/pEpNotifications/pEpNotifications/DownloadNotificationManager.swift b/Submodules/pEpNotifications/pEpNotifications/DownloadNotificationManager.swift index f50e2f5..887552b 100644 --- a/Submodules/pEpNotifications/pEpNotifications/DownloadNotificationManager.swift +++ b/Submodules/pEpNotifications/pEpNotifications/DownloadNotificationManager.swift @@ -122,17 +122,12 @@ extension DownloadNotificationManager { } private func sendNotification(with notification: DownloadNotification) { - guard let name = notification.product["name"] as? String else { - return - } - let notificationContent = UNMutableNotificationContent() notificationContent.title = NSLocalizedString("Update available", comment: "") - notificationContent.body = String(format: NSLocalizedString("A new update for %@ is ready to be installed.", comment: ""), name) + notificationContent.body = String(format: NSLocalizedString("A new update for %@ is ready to be installed.", comment: ""), notification.product.name) notificationContent.sound = UNNotificationSound.default notificationContent.categoryIdentifier = "p≡p" - notificationContent.userInfo = notification.product - + notificationContent.userInfo = notification.product.productInfo let trigger = UNTimeIntervalNotificationTrigger(timeInterval: Double.leastNonzeroMagnitude, repeats: false) @@ -158,14 +153,18 @@ extension DownloadNotificationManager: UNUserNotificationCenterDelegate { switch response.actionIdentifier { case "installAction": - if let product = response.notification.request.content.userInfo as? Product { - delegate?.installSelected(with: product) + guard let userInfo = response.notification.request.content.userInfo as? Dictionary, + let name = userInfo["name"], + let filename = userInfo["filename"] + else { + break } + let product = Product(name: name, filename: filename) + delegate?.installSelected(with: product) case "notNowAction": delegate?.notNowSelected() default: break } - } } diff --git a/Submodules/pEpNotifications/pEpNotifications/DownloadStateNotifier.swift b/Submodules/pEpNotifications/pEpNotifications/DownloadStateNotifier.swift index c91d34a..86773fe 100644 --- a/Submodules/pEpNotifications/pEpNotifications/DownloadStateNotifier.swift +++ b/Submodules/pEpNotifications/pEpNotifications/DownloadStateNotifier.swift @@ -8,7 +8,13 @@ import SwiftUI -public typealias Product = Dictionary +public struct Product { + public var name: String + public var filename: String + public var productInfo: Dictionary { + return ["name": name, "filename": filename] + } +} struct DownloadStateNotifier { var menuItem: NSMenuItem