From 5d1230f2ccbaee7d3ad85d6c845a5530803bce6b Mon Sep 17 00:00:00 2001 From: Dan Barber Date: Fri, 1 Feb 2019 19:53:34 -0500 Subject: [PATCH] Move MPD connection to AppDelegate --- Persephone.xcodeproj/project.pbxproj | 8 ++++++++ Persephone/AppDelegate.swift | 14 ++++++-------- Persephone/Base.lproj/Main.storyboard | 21 ++++++++++++++++++-- Persephone/MPDClient.swift | 22 +++++++++++++++++---- Persephone/QueueController.swift | 18 +++++++++++++++++ Persephone/WindowController.swift | 28 ++++++++++++++++++--------- 6 files changed, 88 insertions(+), 23 deletions(-) create mode 100644 Persephone/QueueController.swift diff --git a/Persephone.xcodeproj/project.pbxproj b/Persephone.xcodeproj/project.pbxproj index f37a739..4dc3b4c 100644 --- a/Persephone.xcodeproj/project.pbxproj +++ b/Persephone.xcodeproj/project.pbxproj @@ -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 = ""; }; E41B22EB21FB966C00D544F6 /* playlist.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = playlist.h; sourceTree = ""; }; 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 = ""; }; /* 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; }; diff --git a/Persephone/AppDelegate.swift b/Persephone/AppDelegate.swift index 6a5d7b2..511829d 100644 --- a/Persephone/AppDelegate.swift +++ b/Persephone/AppDelegate.swift @@ -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() } - - } - diff --git a/Persephone/Base.lproj/Main.storyboard b/Persephone/Base.lproj/Main.storyboard index 16867e0..8de9e69 100644 --- a/Persephone/Base.lproj/Main.storyboard +++ b/Persephone/Base.lproj/Main.storyboard @@ -713,9 +713,25 @@ + + + + + + + + + + + + + + + + @@ -723,6 +739,7 @@ + @@ -755,10 +772,10 @@ - + - + diff --git a/Persephone/MPDClient.swift b/Persephone/MPDClient.swift index 50b1f12..7f69871 100644 --- a/Persephone/MPDClient.swift +++ b/Persephone/MPDClient.swift @@ -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() } } diff --git a/Persephone/QueueController.swift b/Persephone/QueueController.swift new file mode 100644 index 0000000..0e83b7e --- /dev/null +++ b/Persephone/QueueController.swift @@ -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. + } + +} diff --git a/Persephone/WindowController.swift b/Persephone/WindowController.swift index ac282c5..6f19807 100644 --- a/Persephone/WindowController.swift +++ b/Persephone/WindowController.swift @@ -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! }