1
1
mirror of https://github.com/danbee/persephone synced 2025-03-04 08:39:11 +00:00

Add handler delegate for MPD client notifications

This commit is contained in:
Daniel Barber 2019-02-02 12:36:11 -05:00
parent 5d1230f2cc
commit f77141faa0
Signed by: danbarber
GPG Key ID: 931D8112E0103DD8
6 changed files with 56 additions and 24 deletions

View File

@ -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 = "<group>"; };
E4E8CC8F2204EC7F0024217A /* MPDClientDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MPDClientDelegate.swift; sourceTree = "<group>"; };
E4E8CC912204F4B80024217A /* QueueController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = QueueController.swift; sourceTree = "<group>"; };
E4E8CC932206097F0024217A /* MPDClientNotificationHandler.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MPDClientNotificationHandler.swift; sourceTree = "<group>"; };
/* 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 */,

View File

@ -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()
}
}

View File

@ -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()
}
}

View File

@ -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)
}

View File

@ -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]
)
}
}
}

View File

@ -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()
}
}