Browse Source

MOS-8 Create DownloadStateNotifier, which is responsible to show state messages in statusText menu item.

MOS-8
David Alarcon 2 years ago
parent
commit
834b007d9c
3 changed files with 67 additions and 21 deletions
  1. +4
    -0
      Submodules/pEpNotifications/pEpNotifications.xcodeproj/project.pbxproj
  2. +10
    -21
      Submodules/pEpNotifications/pEpNotifications/AppDelegate.swift
  3. +53
    -0
      Submodules/pEpNotifications/pEpNotifications/DownloadStateNotifier.swift

+ 4
- 0
Submodules/pEpNotifications/pEpNotifications.xcodeproj/project.pbxproj View File

@ -12,6 +12,7 @@
3527B2B924802F87007A6276 /* Preview Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 3527B2B824802F87007A6276 /* Preview Assets.xcassets */; };
3594303F2483011000DCBD65 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 3594303D2483011000DCBD65 /* Main.storyboard */; };
359430482483264F00DCBD65 /* Localizable.strings in Resources */ = {isa = PBXBuildFile; fileRef = 3594304A2483264F00DCBD65 /* Localizable.strings */; };
4E1107AF256FE89900EB1711 /* DownloadStateNotifier.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4E1107AD256FCF2900EB1711 /* DownloadStateNotifier.swift */; };
/* End PBXBuildFile section */
/* Begin PBXFileReference section */
@ -28,6 +29,7 @@
35943044248309BE00DCBD65 /* de */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = de; path = de.lproj/Main.strings; sourceTree = "<group>"; };
359430492483264F00DCBD65 /* de */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = de; path = de.lproj/Localizable.strings; sourceTree = "<group>"; };
3594304B2483266200DCBD65 /* en */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = en; path = en.lproj/Localizable.strings; sourceTree = "<group>"; };
4E1107AD256FCF2900EB1711 /* DownloadStateNotifier.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DownloadStateNotifier.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */
/* Begin PBXFrameworksBuildPhase section */
@ -64,6 +66,7 @@
3594304A2483264F00DCBD65 /* Localizable.strings */,
3594303D2483011000DCBD65 /* Main.storyboard */,
3527B2B124802F84007A6276 /* AppDelegate.swift */,
4E1107AD256FCF2900EB1711 /* DownloadStateNotifier.swift */,
3527B2B524802F87007A6276 /* Assets.xcassets */,
3527B2BD24802F87007A6276 /* Info.plist */,
3527B2BE24802F87007A6276 /* pEpNotifications.entitlements */,
@ -184,6 +187,7 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
4E1107AF256FE89900EB1711 /* DownloadStateNotifier.swift in Sources */,
3527B2B224802F84007A6276 /* AppDelegate.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;


+ 10
- 21
Submodules/pEpNotifications/pEpNotifications/AppDelegate.swift View File

@ -47,6 +47,7 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSUserNotificationCenterDele
var nc = NSUserNotificationCenter.default
lazy var clientListener: NSXPCListener = NSXPCListener.anonymous()
var receiver: pEpNotification!
lazy var downloadStateNotifier = DownloadStateNotifier(at: statusText)
@objc func installMenuExtra() {
guard statusBarItem == nil else {
@ -70,19 +71,6 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSUserNotificationCenterDele
statusBarItem = nil
}
func setDownloadState(_ text: String, _ product: Dictionary<String, Any>? = nil) {
if product == nil {
statusText.title = NSLocalizedString(text, comment: "")
statusText.isEnabled = false
} else {
let name = product?["name"] as! String
statusText.title = String.localizedStringWithFormat(NSLocalizedString(text, comment: ""), name)
statusText.isEnabled = true
}
statusText.representedObject = product
}
@IBAction func scheduleUpdates(_ sender: NSMenuItem) {
if sender.state == NSControl.StateValue.on {
sender.state = NSControl.StateValue.off
@ -116,7 +104,7 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSUserNotificationCenterDele
NSLog("pEpNotifications: installNow clicked for %@", product["name"] as! String)
NSWorkspace.shared.openFile(product["filename"] as! String)
sender.representedObject = nil
setDownloadState("Connected.")
downloadStateNotifier.notify(.Connected)
}
func notifyDownload(_ type: Int, withName: NSString, withFilename: NSString) {
@ -124,7 +112,7 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSUserNotificationCenterDele
switch _type {
case .downloading:
NSLog("pEpNotifications: downloading")
setDownloadState(String(format: NSLocalizedString("Downloading update for %@…", comment: ""), withName))
downloadStateNotifier.notify(.Downloading(String(withName)))
self.performSelector(onMainThread: #selector(installMenuExtra), with:nil, waitUntilDone: false)
case .downloadArrived:
@ -142,15 +130,16 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSUserNotificationCenterDele
un.setValue(true, forKey: "_showsButtons")
un.userInfo = ["name": withName, "filename": withFilename]
nc.deliver(un)
setDownloadState("New version of %@ available", ["name": withName, "filename": withFilename, "notification": un])
let product = ["name": withName, "filename": withFilename, "notification": un]
downloadStateNotifier.notify(.NewVersionAvailable(String(withName)), with: product)
case .noDownloadAvailable:
NSLog("pEpNotifications: no download available")
setDownloadState("The software is up to date.")
downloadStateNotifier.notify(.UpToDate)
case .ready:
NSLog("pEpNotifications: ready.")
setDownloadState("Connected.")
downloadStateNotifier.notify(.Connected)
case .none:
break
@ -189,12 +178,12 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSUserNotificationCenterDele
let filename : String = didActivate.userInfo?["filename"] as! String
NSLog("pEpNotifications: actionButtonClicked for %@", filename)
NSWorkspace.shared.openFile(filename)
setDownloadState("Connected.")
downloadStateNotifier.notify(.Connected)
}
func proxyErrorHandler(err: Error) -> Void {
NSLog("%@", err.localizedDescription)
setDownloadState("Connection failed")
downloadStateNotifier.notify(.ConnectionFailed)
}
@objc func listener(_ listener: NSXPCListener, shouldAcceptNewConnection newConnection: NSXPCConnection) -> Bool {
@ -211,7 +200,7 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSUserNotificationCenterDele
// preference defaults
setupAppDefaults()
setDownloadState("Connecting…")
downloadStateNotifier.notify(.Connecting)
// connect to XPC service
initXPCConnection()


+ 53
- 0
Submodules/pEpNotifications/pEpNotifications/DownloadStateNotifier.swift View File

@ -0,0 +1,53 @@
//
// DownloadStateNotifier.swift
// pEpNotifications
//
// Created by David Alarcon on 26/11/2020.
// Copyright © 2020 pp foundation. All rights reserved.
//
import SwiftUI
typealias Product = Dictionary<String, Any>
struct DownloadStateNotifier {
var menuItem: NSMenuItem
init(at menuItem: NSMenuItem) {
self.menuItem = menuItem
}
func notify(_ state: DownloadState, with product: Product? = nil) {
menuItem.title = state.localizedString()
menuItem.isEnabled = (product != nil)
menuItem.representedObject = product
}
}
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: "")
}
}
}

Loading…
Cancel
Save