From f77141faa0e0b621c2bc0236d32185fb605359b5 Mon Sep 17 00:00:00 2001 From: Dan Barber Date: Sat, 2 Feb 2019 12:36:11 -0500 Subject: [PATCH] Add handler delegate for MPD client notifications --- Persephone.xcodeproj/project.pbxproj | 4 ++++ Persephone/AppDelegate.swift | 12 +++++----- Persephone/MPDClient.swift | 16 ++++--------- Persephone/MPDClientDelegate.swift | 13 +++++++++++ Persephone/MPDClientNotificationHandler.swift | 23 +++++++++++++++++++ Persephone/WindowController.swift | 12 ++++------ 6 files changed, 56 insertions(+), 24 deletions(-) create mode 100644 Persephone/MPDClientDelegate.swift create mode 100644 Persephone/MPDClientNotificationHandler.swift diff --git a/Persephone.xcodeproj/project.pbxproj b/Persephone.xcodeproj/project.pbxproj index 4dc3b4c..67f4d30 100644 --- a/Persephone.xcodeproj/project.pbxproj +++ b/Persephone.xcodeproj/project.pbxproj @@ -19,6 +19,7 @@ E465049A21E94DF500A70F4C /* WindowController.swift in Sources */ = {isa = PBXBuildFile; fileRef = E465049921E94DF500A70F4C /* WindowController.swift */; }; E4E8CC902204EC7F0024217A /* MPDClientDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = E4E8CC8F2204EC7F0024217A /* MPDClientDelegate.swift */; }; E4E8CC922204F4B80024217A /* QueueController.swift in Sources */ = {isa = PBXBuildFile; fileRef = E4E8CC912204F4B80024217A /* QueueController.swift */; }; + E4E8CC942206097F0024217A /* MPDClientNotificationHandler.swift in Sources */ = {isa = PBXBuildFile; fileRef = E4E8CC932206097F0024217A /* MPDClientNotificationHandler.swift */; }; /* End PBXBuildFile section */ /* Begin PBXContainerItemProxy section */ @@ -107,6 +108,7 @@ E465049921E94DF500A70F4C /* WindowController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WindowController.swift; sourceTree = ""; }; E4E8CC8F2204EC7F0024217A /* MPDClientDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MPDClientDelegate.swift; sourceTree = ""; }; E4E8CC912204F4B80024217A /* QueueController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = QueueController.swift; sourceTree = ""; }; + E4E8CC932206097F0024217A /* MPDClientNotificationHandler.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MPDClientNotificationHandler.swift; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -168,6 +170,7 @@ E465049921E94DF500A70F4C /* WindowController.swift */, E40786242110CE70006887B1 /* Info.plist */, E40786252110CE70006887B1 /* Persephone.entitlements */, + E4E8CC932206097F0024217A /* MPDClientNotificationHandler.swift */, E4E8CC8F2204EC7F0024217A /* MPDClientDelegate.swift */, E41B22C521FB932700D544F6 /* MPDClient.swift */, ); @@ -388,6 +391,7 @@ buildActionMask = 2147483647; files = ( E407861E2110CE6E006887B1 /* ViewController.swift in Sources */, + E4E8CC942206097F0024217A /* MPDClientNotificationHandler.swift in Sources */, E465049A21E94DF500A70F4C /* WindowController.swift in Sources */, E41B22C621FB932700D544F6 /* MPDClient.swift in Sources */, E407861C2110CE6E006887B1 /* AppDelegate.swift in Sources */, diff --git a/Persephone/AppDelegate.swift b/Persephone/AppDelegate.swift index 511829d..2657c6b 100644 --- a/Persephone/AppDelegate.swift +++ b/Persephone/AppDelegate.swift @@ -8,17 +8,17 @@ import Cocoa -extension MPDClient { - static let shared = MPDClient(notificationQueue: .main) -} - @NSApplicationMain class AppDelegate: NSObject, NSApplicationDelegate { + static let mpdClient = MPDClient( + withDelegate: MPDClientNotificationHandler() as MPDClientDelegate + ) + func applicationDidFinishLaunching(_ aNotification: Notification) { - MPDClient.shared.connect() + AppDelegate.mpdClient.connect() } func applicationWillTerminate(_ aNotification: Notification) { - MPDClient.shared.disconnect() + AppDelegate.mpdClient.disconnect() } } diff --git a/Persephone/MPDClient.swift b/Persephone/MPDClient.swift index 7f69871..da63f3d 100644 --- a/Persephone/MPDClient.swift +++ b/Persephone/MPDClient.swift @@ -10,6 +10,7 @@ import Foundation import mpdclient class MPDClient { + var delegate: MPDClientDelegate? static let stateChanged = Notification.Name("MPDClientStateChanged") static let queueChanged = Notification.Name("MPDClientQueueChanged") @@ -17,7 +18,6 @@ class MPDClient { let HOST = "localhost" let PORT: UInt32 = 6600 - let notificationQueue: DispatchQueue private var connection: OpaquePointer? private var status: OpaquePointer? @@ -36,8 +36,9 @@ class MPDClient { case paused = 3 } - init(notificationQueue: DispatchQueue) { - self.notificationQueue = notificationQueue + init(withDelegate delegate: MPDClientDelegate?) { + print(delegate) + self.delegate = delegate } func connect() { @@ -146,14 +147,7 @@ class MPDClient { if !self.commandQueued { self.fetchStatus() - let state = self.getState() - self.notificationQueue.async { - NotificationCenter.default.post( - name: MPDClient.stateChanged, - object: self, - userInfo: [MPDClient.stateKey: state] - ) - } + self.delegate?.didUpdateState(mpdClient: self, state: self.getState()) self.idle() } } diff --git a/Persephone/MPDClientDelegate.swift b/Persephone/MPDClientDelegate.swift new file mode 100644 index 0000000..9b9e23a --- /dev/null +++ b/Persephone/MPDClientDelegate.swift @@ -0,0 +1,13 @@ +// +// MPDClientDelegate.swift +// Persephone +// +// Created by Daniel Barber on 2019/2/01. +// Copyright © 2019 Dan Barber. All rights reserved. +// + +import Foundation + +protocol MPDClientDelegate { + func didUpdateState(mpdClient: MPDClient, state: MPDClient.State) +} diff --git a/Persephone/MPDClientNotificationHandler.swift b/Persephone/MPDClientNotificationHandler.swift new file mode 100644 index 0000000..eb0aa60 --- /dev/null +++ b/Persephone/MPDClientNotificationHandler.swift @@ -0,0 +1,23 @@ +// +// MPDClientNotificationHandler.swift +// Persephone +// +// Created by Daniel Barber on 2019/2/02. +// Copyright © 2019 Dan Barber. All rights reserved. +// + +import Foundation + +class MPDClientNotificationHandler: MPDClientDelegate { + let notificationQueue = DispatchQueue.main + + func didUpdateState(mpdClient: MPDClient, state: MPDClient.State) { + self.notificationQueue.async { + NotificationCenter.default.post( + name: MPDClient.stateChanged, + object: AppDelegate.mpdClient, + userInfo: [MPDClient.stateKey: state] + ) + } + } +} diff --git a/Persephone/WindowController.swift b/Persephone/WindowController.swift index 6f19807..2ac2d51 100644 --- a/Persephone/WindowController.swift +++ b/Persephone/WindowController.swift @@ -9,8 +9,6 @@ import Cocoa class WindowController: NSWindowController { - let mpdClient = MPDClient.shared - enum TransportAction: Int { case prevTrack = 0 case playPause = 1 @@ -26,7 +24,7 @@ class WindowController: NSWindowController { self, selector: #selector(stateChanged(_:)), name: MPDClient.stateChanged, - object: mpdClient + object: AppDelegate.mpdClient ) } @@ -43,13 +41,13 @@ class WindowController: NSWindowController { switch transportAction { case .prevTrack: - mpdClient.prevTrack() + AppDelegate.mpdClient.prevTrack() case .playPause: - mpdClient.playPause() + AppDelegate.mpdClient.playPause() case .stop: - mpdClient.stop() + AppDelegate.mpdClient.stop() case .nextTrack: - mpdClient.nextTrack() + AppDelegate.mpdClient.nextTrack() } }