1
1
mirror of https://github.com/danbee/persephone synced 2025-03-04 08:39:11 +00:00
persephone/Persephone/MPDClient/MPDClient.swift
Dan Barber fe748e2c61
WIP: Refactor MPDClient
This should make handling the queuing side work more reliably.
2019-03-20 20:06:23 -04:00

53 lines
1.2 KiB
Swift

//
// MPDClient.swift
// Persephone
//
// Created by Daniel Barber on 2019/1/25.
// Copyright © 2019 Dan Barber. All rights reserved.
//
import Foundation
import mpdclient
class MPDClient {
var delegate: MPDClientDelegate?
var connection: OpaquePointer?
var isConnected: Bool = false
var isIdle: Bool = false
var status: Status?
var queue: [Song] = []
let commandQueue = OperationQueue()
var commandsQueued: UInt = 0
enum Command {
case prevTrack, nextTrack, playPause, stop,
fetchStatus, fetchQueue, fetchAllAlbums,
playAlbum, getAlbumURI
}
init(withDelegate delegate: MPDClientDelegate?) {
commandQueue.maxConcurrentOperationCount = 1
self.delegate = delegate
}
func queueCommand(
command: Command,
priority: BlockOperation.QueuePriority = .normal,
userData: Dictionary<String, Any> = [:]
) {
guard isConnected else { return }
noIdle()
let commandOperation = BlockOperation() { [unowned self] in
self.commandsQueued -= 1
self.sendCommand(command: command, userData: userData)
}
commandOperation.queuePriority = priority
commandsQueued += 1
commandQueue.addOperation(commandOperation)
idle()
}
}