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