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

Move MPD connection to AppDelegate

This commit is contained in:
Daniel Barber 2019-02-01 19:53:34 -05:00
parent 607fa1770d
commit 5d1230f2cc
Signed by: danbarber
GPG Key ID: 931D8112E0103DD8
6 changed files with 88 additions and 23 deletions

View File

@ -17,6 +17,8 @@
E41B22C121FB6C3300D544F6 /* libmpdclient.2.dylib in Embed Libraries */ = {isa = PBXBuildFile; fileRef = E41B22BF21FB6BBA00D544F6 /* libmpdclient.2.dylib */; settings = {ATTRIBUTES = (CodeSignOnCopy, ); }; };
E41B22C621FB932700D544F6 /* MPDClient.swift in Sources */ = {isa = PBXBuildFile; fileRef = E41B22C521FB932700D544F6 /* MPDClient.swift */; };
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 */; };
/* End PBXBuildFile section */
/* Begin PBXContainerItemProxy section */
@ -103,6 +105,8 @@
E41B22EA21FB966C00D544F6 /* queue.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = queue.h; sourceTree = "<group>"; };
E41B22EB21FB966C00D544F6 /* playlist.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = playlist.h; 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>"; };
E4E8CC912204F4B80024217A /* QueueController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = QueueController.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */
/* Begin PBXFrameworksBuildPhase section */
@ -160,9 +164,11 @@
E407861D2110CE6E006887B1 /* ViewController.swift */,
E407861F2110CE70006887B1 /* Assets.xcassets */,
E40786212110CE70006887B1 /* Main.storyboard */,
E4E8CC912204F4B80024217A /* QueueController.swift */,
E465049921E94DF500A70F4C /* WindowController.swift */,
E40786242110CE70006887B1 /* Info.plist */,
E40786252110CE70006887B1 /* Persephone.entitlements */,
E4E8CC8F2204EC7F0024217A /* MPDClientDelegate.swift */,
E41B22C521FB932700D544F6 /* MPDClient.swift */,
);
path = Persephone;
@ -385,6 +391,8 @@
E465049A21E94DF500A70F4C /* WindowController.swift in Sources */,
E41B22C621FB932700D544F6 /* MPDClient.swift in Sources */,
E407861C2110CE6E006887B1 /* AppDelegate.swift in Sources */,
E4E8CC902204EC7F0024217A /* MPDClientDelegate.swift in Sources */,
E4E8CC922204F4B80024217A /* QueueController.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};

View File

@ -8,19 +8,17 @@
import Cocoa
extension MPDClient {
static let shared = MPDClient(notificationQueue: .main)
}
@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {
func applicationDidFinishLaunching(_ aNotification: Notification) {
// Insert code here to initialize your application
MPDClient.shared.connect()
}
func applicationWillTerminate(_ aNotification: Notification) {
// Insert code here to tear down your application
MPDClient.shared.disconnect()
}
}

View File

@ -713,9 +713,25 @@
</segmentedControl>
</toolbarItem>
<toolbarItem implicitItemIdentifier="NSToolbarFlexibleSpaceItem" id="9ol-aR-mzv"/>
<toolbarItem implicitItemIdentifier="6D294101-04FF-4E2A-98A7-77A48E2296CD" label="" paletteLabel="" sizingBehavior="auto" id="FoD-o2-4f2">
<nil key="toolTip"/>
<textField key="view" horizontalHuggingPriority="251" verticalHuggingPriority="750" id="KVt-e7-7I5">
<rect key="frame" x="0.0" y="14" width="37" height="17"/>
<autoresizingMask key="autoresizingMask"/>
<textFieldCell key="cell" lineBreakMode="clipping" title="Label" id="bCv-8Z-qfx">
<font key="font" usesAppearanceFont="YES"/>
<color key="textColor" name="labelColor" catalog="System" colorSpace="catalog"/>
<color key="backgroundColor" name="textBackgroundColor" catalog="System" colorSpace="catalog"/>
</textFieldCell>
<connections>
<action selector="stateLabel:" target="B8D-0N-5wS" id="YXt-Ni-yie"/>
</connections>
</textField>
</toolbarItem>
</allowedToolbarItems>
<defaultToolbarItems>
<toolbarItem reference="p3r-ty-Pxf"/>
<toolbarItem reference="FoD-o2-4f2"/>
</defaultToolbarItems>
</toolbar>
<connections>
@ -723,6 +739,7 @@
</connections>
</window>
<connections>
<outlet property="stateLabel" destination="KVt-e7-7I5" id="xMD-Iq-CSh"/>
<segue destination="fnD-7K-pHK" kind="relationship" relationship="window.shadowedContentViewController" id="fQQ-kB-KVc"/>
</connections>
</windowController>
@ -755,10 +772,10 @@
</objects>
<point key="canvasLocation" x="74" y="677"/>
</scene>
<!--View Controller-->
<!--Queue Controller-->
<scene sceneID="QcX-dC-cTZ">
<objects>
<viewController id="KIP-rq-4dM" sceneMemberID="viewController">
<viewController id="KIP-rq-4dM" customClass="QueueController" customModule="Persephone" customModuleProvider="target" sceneMemberID="viewController">
<view key="view" id="2su-YT-hba">
<rect key="frame" x="0.0" y="0.0" width="450" height="300"/>
<autoresizingMask key="autoresizingMask"/>

View File

@ -10,8 +10,14 @@ import Foundation
import mpdclient
class MPDClient {
static let stateChanged = Notification.Name("MPDClientStateChanged")
static let queueChanged = Notification.Name("MPDClientQueueChanged")
static let stateKey = "state"
let HOST = "localhost"
let PORT: UInt32 = 6600
let notificationQueue: DispatchQueue
private var connection: OpaquePointer?
private var status: OpaquePointer?
@ -30,6 +36,10 @@ class MPDClient {
case paused = 3
}
init(notificationQueue: DispatchQueue) {
self.notificationQueue = notificationQueue
}
func connect() {
guard let connection = mpd_connection_new(HOST, PORT, 0)
else { return }
@ -99,8 +109,6 @@ class MPDClient {
guard let status = mpd_run_status(connection) else { break }
self.status = status
}
print(getLastErrorMessage()!)
}
func sendNextTrack() {
@ -137,9 +145,15 @@ class MPDClient {
mpd_recv_idle(self.connection, true)
if !self.commandQueued {
print("Fetching status")
self.fetchStatus()
print(self.getState())
let state = self.getState()
self.notificationQueue.async {
NotificationCenter.default.post(
name: MPDClient.stateChanged,
object: self,
userInfo: [MPDClient.stateKey: state]
)
}
self.idle()
}
}

View File

@ -0,0 +1,18 @@
//
// QueueController.swift
// Persephone
//
// Created by Daniel Barber on 2019/2/01.
// Copyright © 2019 Dan Barber. All rights reserved.
//
import Cocoa
class QueueController: NSViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do view setup here.
}
}

View File

@ -9,24 +9,32 @@
import Cocoa
class WindowController: NSWindowController {
let mpdClient = MPDClient.shared
enum TransportAction: Int {
case prevTrack = 0
case playPause = 1
case stop = 2
case nextTrack = 3
}
var mpdClient: MPDClient?
override func windowDidLoad() {
super.windowDidLoad()
window?.titleVisibility = .hidden
mpdInit()
NotificationCenter.default.addObserver(
self,
selector: #selector(stateChanged(_:)),
name: MPDClient.stateChanged,
object: mpdClient
)
}
func mpdInit() {
mpdClient = MPDClient()
mpdClient?.connect()
@objc func stateChanged(_ notification: Notification) {
guard let state = notification.userInfo?[MPDClient.stateKey] as? MPDClient.State
else { return }
stateLabel.stringValue = "\(state)".localizedCapitalized
}
@IBAction func handleTransportControl(_ sender: NSSegmentedControl) {
@ -35,13 +43,15 @@ class WindowController: NSWindowController {
switch transportAction {
case .prevTrack:
mpdClient?.prevTrack()
mpdClient.prevTrack()
case .playPause:
mpdClient?.playPause()
mpdClient.playPause()
case .stop:
mpdClient?.stop()
mpdClient.stop()
case .nextTrack:
mpdClient?.nextTrack()
mpdClient.nextTrack()
}
}
@IBOutlet var stateLabel: NSTextField!
}