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:
parent
5d1230f2cc
commit
f77141faa0
@ -19,6 +19,7 @@
|
|||||||
E465049A21E94DF500A70F4C /* WindowController.swift in Sources */ = {isa = PBXBuildFile; fileRef = E465049921E94DF500A70F4C /* WindowController.swift */; };
|
E465049A21E94DF500A70F4C /* WindowController.swift in Sources */ = {isa = PBXBuildFile; fileRef = E465049921E94DF500A70F4C /* WindowController.swift */; };
|
||||||
E4E8CC902204EC7F0024217A /* MPDClientDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = E4E8CC8F2204EC7F0024217A /* MPDClientDelegate.swift */; };
|
E4E8CC902204EC7F0024217A /* MPDClientDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = E4E8CC8F2204EC7F0024217A /* MPDClientDelegate.swift */; };
|
||||||
E4E8CC922204F4B80024217A /* QueueController.swift in Sources */ = {isa = PBXBuildFile; fileRef = E4E8CC912204F4B80024217A /* QueueController.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 */
|
/* End PBXBuildFile section */
|
||||||
|
|
||||||
/* Begin PBXContainerItemProxy section */
|
/* Begin PBXContainerItemProxy section */
|
||||||
@ -107,6 +108,7 @@
|
|||||||
E465049921E94DF500A70F4C /* WindowController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WindowController.swift; sourceTree = "<group>"; };
|
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>"; };
|
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>"; };
|
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 */
|
/* End PBXFileReference section */
|
||||||
|
|
||||||
/* Begin PBXFrameworksBuildPhase section */
|
/* Begin PBXFrameworksBuildPhase section */
|
||||||
@ -168,6 +170,7 @@
|
|||||||
E465049921E94DF500A70F4C /* WindowController.swift */,
|
E465049921E94DF500A70F4C /* WindowController.swift */,
|
||||||
E40786242110CE70006887B1 /* Info.plist */,
|
E40786242110CE70006887B1 /* Info.plist */,
|
||||||
E40786252110CE70006887B1 /* Persephone.entitlements */,
|
E40786252110CE70006887B1 /* Persephone.entitlements */,
|
||||||
|
E4E8CC932206097F0024217A /* MPDClientNotificationHandler.swift */,
|
||||||
E4E8CC8F2204EC7F0024217A /* MPDClientDelegate.swift */,
|
E4E8CC8F2204EC7F0024217A /* MPDClientDelegate.swift */,
|
||||||
E41B22C521FB932700D544F6 /* MPDClient.swift */,
|
E41B22C521FB932700D544F6 /* MPDClient.swift */,
|
||||||
);
|
);
|
||||||
@ -388,6 +391,7 @@
|
|||||||
buildActionMask = 2147483647;
|
buildActionMask = 2147483647;
|
||||||
files = (
|
files = (
|
||||||
E407861E2110CE6E006887B1 /* ViewController.swift in Sources */,
|
E407861E2110CE6E006887B1 /* ViewController.swift in Sources */,
|
||||||
|
E4E8CC942206097F0024217A /* MPDClientNotificationHandler.swift in Sources */,
|
||||||
E465049A21E94DF500A70F4C /* WindowController.swift in Sources */,
|
E465049A21E94DF500A70F4C /* WindowController.swift in Sources */,
|
||||||
E41B22C621FB932700D544F6 /* MPDClient.swift in Sources */,
|
E41B22C621FB932700D544F6 /* MPDClient.swift in Sources */,
|
||||||
E407861C2110CE6E006887B1 /* AppDelegate.swift in Sources */,
|
E407861C2110CE6E006887B1 /* AppDelegate.swift in Sources */,
|
||||||
|
|||||||
@ -8,17 +8,17 @@
|
|||||||
|
|
||||||
import Cocoa
|
import Cocoa
|
||||||
|
|
||||||
extension MPDClient {
|
|
||||||
static let shared = MPDClient(notificationQueue: .main)
|
|
||||||
}
|
|
||||||
|
|
||||||
@NSApplicationMain
|
@NSApplicationMain
|
||||||
class AppDelegate: NSObject, NSApplicationDelegate {
|
class AppDelegate: NSObject, NSApplicationDelegate {
|
||||||
|
static let mpdClient = MPDClient(
|
||||||
|
withDelegate: MPDClientNotificationHandler() as MPDClientDelegate
|
||||||
|
)
|
||||||
|
|
||||||
func applicationDidFinishLaunching(_ aNotification: Notification) {
|
func applicationDidFinishLaunching(_ aNotification: Notification) {
|
||||||
MPDClient.shared.connect()
|
AppDelegate.mpdClient.connect()
|
||||||
}
|
}
|
||||||
|
|
||||||
func applicationWillTerminate(_ aNotification: Notification) {
|
func applicationWillTerminate(_ aNotification: Notification) {
|
||||||
MPDClient.shared.disconnect()
|
AppDelegate.mpdClient.disconnect()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -10,6 +10,7 @@ import Foundation
|
|||||||
import mpdclient
|
import mpdclient
|
||||||
|
|
||||||
class MPDClient {
|
class MPDClient {
|
||||||
|
var delegate: MPDClientDelegate?
|
||||||
static let stateChanged = Notification.Name("MPDClientStateChanged")
|
static let stateChanged = Notification.Name("MPDClientStateChanged")
|
||||||
static let queueChanged = Notification.Name("MPDClientQueueChanged")
|
static let queueChanged = Notification.Name("MPDClientQueueChanged")
|
||||||
|
|
||||||
@ -17,7 +18,6 @@ class MPDClient {
|
|||||||
|
|
||||||
let HOST = "localhost"
|
let HOST = "localhost"
|
||||||
let PORT: UInt32 = 6600
|
let PORT: UInt32 = 6600
|
||||||
let notificationQueue: DispatchQueue
|
|
||||||
|
|
||||||
private var connection: OpaquePointer?
|
private var connection: OpaquePointer?
|
||||||
private var status: OpaquePointer?
|
private var status: OpaquePointer?
|
||||||
@ -36,8 +36,9 @@ class MPDClient {
|
|||||||
case paused = 3
|
case paused = 3
|
||||||
}
|
}
|
||||||
|
|
||||||
init(notificationQueue: DispatchQueue) {
|
init(withDelegate delegate: MPDClientDelegate?) {
|
||||||
self.notificationQueue = notificationQueue
|
print(delegate)
|
||||||
|
self.delegate = delegate
|
||||||
}
|
}
|
||||||
|
|
||||||
func connect() {
|
func connect() {
|
||||||
@ -146,14 +147,7 @@ class MPDClient {
|
|||||||
|
|
||||||
if !self.commandQueued {
|
if !self.commandQueued {
|
||||||
self.fetchStatus()
|
self.fetchStatus()
|
||||||
let state = self.getState()
|
self.delegate?.didUpdateState(mpdClient: self, state: self.getState())
|
||||||
self.notificationQueue.async {
|
|
||||||
NotificationCenter.default.post(
|
|
||||||
name: MPDClient.stateChanged,
|
|
||||||
object: self,
|
|
||||||
userInfo: [MPDClient.stateKey: state]
|
|
||||||
)
|
|
||||||
}
|
|
||||||
self.idle()
|
self.idle()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
13
Persephone/MPDClientDelegate.swift
Normal file
13
Persephone/MPDClientDelegate.swift
Normal 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)
|
||||||
|
}
|
||||||
23
Persephone/MPDClientNotificationHandler.swift
Normal file
23
Persephone/MPDClientNotificationHandler.swift
Normal 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]
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -9,8 +9,6 @@
|
|||||||
import Cocoa
|
import Cocoa
|
||||||
|
|
||||||
class WindowController: NSWindowController {
|
class WindowController: NSWindowController {
|
||||||
let mpdClient = MPDClient.shared
|
|
||||||
|
|
||||||
enum TransportAction: Int {
|
enum TransportAction: Int {
|
||||||
case prevTrack = 0
|
case prevTrack = 0
|
||||||
case playPause = 1
|
case playPause = 1
|
||||||
@ -26,7 +24,7 @@ class WindowController: NSWindowController {
|
|||||||
self,
|
self,
|
||||||
selector: #selector(stateChanged(_:)),
|
selector: #selector(stateChanged(_:)),
|
||||||
name: MPDClient.stateChanged,
|
name: MPDClient.stateChanged,
|
||||||
object: mpdClient
|
object: AppDelegate.mpdClient
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -43,13 +41,13 @@ class WindowController: NSWindowController {
|
|||||||
|
|
||||||
switch transportAction {
|
switch transportAction {
|
||||||
case .prevTrack:
|
case .prevTrack:
|
||||||
mpdClient.prevTrack()
|
AppDelegate.mpdClient.prevTrack()
|
||||||
case .playPause:
|
case .playPause:
|
||||||
mpdClient.playPause()
|
AppDelegate.mpdClient.playPause()
|
||||||
case .stop:
|
case .stop:
|
||||||
mpdClient.stop()
|
AppDelegate.mpdClient.stop()
|
||||||
case .nextTrack:
|
case .nextTrack:
|
||||||
mpdClient.nextTrack()
|
AppDelegate.mpdClient.nextTrack()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user