mirror of
https://github.com/danbee/persephone
synced 2025-03-04 08:39:11 +00:00
Compare commits
No commits in common. "2186cd69e3309b279b95c9456f80bf3823111d17" and "88ff35929c298a5637cfd191cb6999932008c713" have entirely different histories.
2186cd69e3
...
88ff35929c
@ -23,6 +23,9 @@ class AppDelegate: NSObject,
|
||||
@IBOutlet weak var addSelectedSongToQueueMenuItem: NSMenuItem!
|
||||
|
||||
func applicationDidFinishLaunching(_ aNotification: Notification) {
|
||||
connectToMPDServer()
|
||||
instantiateControllers()
|
||||
|
||||
mediaKeyTap = MediaKeyTap(delegate: self)
|
||||
mediaKeyTap?.start()
|
||||
|
||||
@ -31,16 +34,22 @@ class AppDelegate: NSObject,
|
||||
$0.uiState
|
||||
}
|
||||
}
|
||||
|
||||
instantiateControllers()
|
||||
connectToMPDServer()
|
||||
}
|
||||
|
||||
func connectToMPDServer() {
|
||||
App.mpdServerController.connect()
|
||||
let mpdServer = App.store.state.preferencesState.mpdServer
|
||||
|
||||
App.mpdClient = MPDClient(
|
||||
host: mpdServer.hostOrDefault,
|
||||
port: mpdServer.portOrDefault,
|
||||
withDelegate: App.mpdServerDelegate
|
||||
)
|
||||
|
||||
App.mpdClient.connect()
|
||||
}
|
||||
|
||||
func instantiateControllers() {
|
||||
_ = App.mpdServerController
|
||||
_ = App.userNotificationsController
|
||||
}
|
||||
|
||||
|
||||
@ -11,24 +11,9 @@ import ReSwift
|
||||
|
||||
class MPDServerController {
|
||||
init() {
|
||||
App.mpdClient = MPDClient(withDelegate: App.mpdServerDelegate)
|
||||
|
||||
App.store.subscribe(self) {
|
||||
$0.select { $0.preferencesState.mpdServer }
|
||||
}
|
||||
}
|
||||
|
||||
func connect() {
|
||||
let mpdServer = App.store.state.preferencesState.mpdServer
|
||||
|
||||
App.mpdClient.connect(
|
||||
host: mpdServer.hostOrDefault,
|
||||
port: mpdServer.portOrDefault
|
||||
)
|
||||
}
|
||||
|
||||
func disconnect() {
|
||||
App.mpdClient.disconnect()
|
||||
// App.store.subscribe(self) {
|
||||
// $0.select { $0.preferencesState.mpdServer }
|
||||
// }
|
||||
}
|
||||
}
|
||||
|
||||
@ -36,9 +21,7 @@ extension MPDServerController: StoreSubscriber {
|
||||
typealias StoreSubscriberStateType = MPDServer
|
||||
|
||||
func newState(state: MPDServer) {
|
||||
guard App.mpdClient != nil else { return }
|
||||
|
||||
disconnect()
|
||||
connect()
|
||||
App.mpdClient.disconnect()
|
||||
App.mpdClient.connect()
|
||||
}
|
||||
}
|
||||
|
||||
@ -163,7 +163,8 @@ class WindowController: NSWindowController {
|
||||
}
|
||||
|
||||
@IBAction func handleSearchQuery(_ sender: NSSearchField) {
|
||||
App.store.dispatch(SetSearchQuery(searchQuery: sender.stringValue))
|
||||
//App.store.dispatch(SetSearchQuery(searchQuery: sender.stringValue))
|
||||
App.mpdClient.fetchAlbums(filter: sender.stringValue)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -14,14 +14,6 @@ extension MPDClient {
|
||||
userData: Dictionary<String, Any> = [:]
|
||||
) {
|
||||
switch command {
|
||||
|
||||
case .connect:
|
||||
guard let host = userData["host"] as? String,
|
||||
let port = userData["port"] as? Int
|
||||
else { return }
|
||||
createConnection(host: host, port: port)
|
||||
case .disconnect:
|
||||
freeConnection()
|
||||
|
||||
// Transport commands
|
||||
case .prevTrack:
|
||||
|
||||
@ -10,42 +10,40 @@ import Foundation
|
||||
import mpdclient
|
||||
|
||||
extension MPDClient {
|
||||
func createConnection(host: String, port: Int) {
|
||||
guard let connection = mpd_connection_new(host, UInt32(port), 10000),
|
||||
mpd_connection_get_error(connection) == MPD_ERROR_SUCCESS
|
||||
else { return }
|
||||
func makeConnectionOperation(host: String, port: Int) -> BlockOperation {
|
||||
BlockOperation { [unowned self] in
|
||||
guard let connection = mpd_connection_new(host, UInt32(port), 10000),
|
||||
mpd_connection_get_error(connection) == MPD_ERROR_SUCCESS
|
||||
else { return }
|
||||
|
||||
self.isConnected = true
|
||||
self.isConnected = true
|
||||
|
||||
guard let status = mpd_run_status(connection)
|
||||
else { return }
|
||||
guard let status = mpd_run_status(connection)
|
||||
else { return }
|
||||
|
||||
self.connection = connection
|
||||
self.status = MPDStatus(status)
|
||||
self.connection = connection
|
||||
self.status = MPDStatus(status)
|
||||
|
||||
self.delegate?.didConnect(mpdClient: self)
|
||||
self.delegate?.didUpdateStatus(mpdClient: self, status: self.status!)
|
||||
}
|
||||
|
||||
func freeConnection() {
|
||||
guard isConnected else { return }
|
||||
|
||||
self.delegate?.willDisconnect(mpdClient: self)
|
||||
|
||||
mpd_connection_free(self.connection)
|
||||
self.isConnected = false
|
||||
}
|
||||
|
||||
func connect(host: String, port: Int) {
|
||||
let commandOperation = BlockOperation() { [unowned self] in
|
||||
self.sendCommand(command: .connect, userData: ["host": host, "port": port])
|
||||
self.delegate?.didConnect(mpdClient: self)
|
||||
self.delegate?.didUpdateStatus(mpdClient: self, status: self.status!)
|
||||
|
||||
self.idle()
|
||||
}
|
||||
commandQueue.addOperation(commandOperation)
|
||||
}
|
||||
|
||||
|
||||
func connect() {
|
||||
commandQueue.addOperation(connectionOperation)
|
||||
}
|
||||
|
||||
func disconnect() {
|
||||
enqueueCommand(command: .disconnect)
|
||||
guard isConnected else { return }
|
||||
|
||||
noIdle()
|
||||
commandQueue.addOperation { [unowned self] in
|
||||
self.delegate?.willDisconnect(mpdClient: self)
|
||||
|
||||
mpd_connection_free(self.connection)
|
||||
self.isConnected = false
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -22,8 +22,9 @@ class MPDClient {
|
||||
|
||||
let commandQueue = OperationQueue()
|
||||
|
||||
init(withDelegate delegate: MPDClientDelegate?) {
|
||||
init(host: String, port: Int, withDelegate delegate: MPDClientDelegate?) {
|
||||
commandQueue.maxConcurrentOperationCount = 1
|
||||
self.delegate = delegate
|
||||
self.connectionOperation = makeConnectionOperation(host: host, port: port)
|
||||
}
|
||||
}
|
||||
|
||||
@ -10,9 +10,6 @@ import Foundation
|
||||
|
||||
extension MPDClient {
|
||||
enum MPDCommand {
|
||||
case connect
|
||||
case disconnect
|
||||
|
||||
// Transport commands
|
||||
case prevTrack
|
||||
case nextTrack
|
||||
|
||||
@ -36,7 +36,7 @@ func queueReducer(action: Action, state: QueueState?) -> QueueState {
|
||||
if oldSongRowPos >= 0 {
|
||||
state.queue[oldSongRowPos].isPlaying = false
|
||||
}
|
||||
if newSongRowPos >= 0 && state.queue.count > newSongRowPos {
|
||||
if newSongRowPos >= 0 {
|
||||
state.queue[newSongRowPos].isPlaying = true
|
||||
|
||||
DispatchQueue.main.async {
|
||||
|
||||
@ -6,7 +6,6 @@
|
||||
// Copyright © 2019 Dan Barber. All rights reserved.
|
||||
//
|
||||
|
||||
import AppKit
|
||||
import ReSwift
|
||||
|
||||
func uiReducer(action: Action, state: UIState?) -> UIState {
|
||||
@ -36,9 +35,6 @@ func uiReducer(action: Action, state: UIState?) -> UIState {
|
||||
|
||||
case let action as SetSearchQuery:
|
||||
state.searchQuery = action.searchQuery
|
||||
DispatchQueue.main.async {
|
||||
App.mpdClient.fetchAlbums(filter: state.searchQuery)
|
||||
}
|
||||
|
||||
default:
|
||||
break
|
||||
|
||||
Loading…
Reference in New Issue
Block a user