mirror of
https://github.com/danbee/persephone
synced 2025-03-04 08:39:11 +00:00
128 lines
2.8 KiB
Swift
128 lines
2.8 KiB
Swift
//
|
|
// Queue.swift
|
|
// Persephone
|
|
//
|
|
// Created by Daniel Barber on 2019/3/15.
|
|
// Copyright © 2019 Dan Barber. All rights reserved.
|
|
//
|
|
|
|
import Foundation
|
|
import mpdclient
|
|
|
|
extension MPDClient {
|
|
func fetchQueue() {
|
|
enqueueCommand(command: .fetchQueue)
|
|
}
|
|
|
|
func clearQueue() {
|
|
enqueueCommand(command: .clearQueue)
|
|
}
|
|
|
|
func playTrack(at queuePos: Int) {
|
|
enqueueCommand(
|
|
command: .playTrack,
|
|
forceIdle: true,
|
|
userData: ["queuePos": queuePos]
|
|
)
|
|
}
|
|
|
|
func appendSong(_ song: MPDSong) {
|
|
enqueueCommand(
|
|
command: .appendSong,
|
|
forceIdle: true,
|
|
userData: ["song": song]
|
|
)
|
|
}
|
|
|
|
func removeSong(at queuePos: Int) {
|
|
enqueueCommand(
|
|
command: .removeSong,
|
|
forceIdle: true,
|
|
userData: ["queuePos": queuePos]
|
|
)
|
|
}
|
|
|
|
func moveSongInQueue(at queuePos: Int, to newQueuePos: Int) {
|
|
enqueueCommand(
|
|
command: .moveSongInQueue,
|
|
forceIdle: true,
|
|
userData: ["oldQueuePos": queuePos, "newQueuePos": newQueuePos]
|
|
)
|
|
}
|
|
|
|
func addSongToQueue(songUri: String, at queuePos: Int) {
|
|
enqueueCommand(
|
|
command: .addSongToQueue,
|
|
forceIdle: true,
|
|
userData: ["uri": songUri, "queuePos": queuePos]
|
|
)
|
|
}
|
|
|
|
func addAlbumToQueue(album: MPDAlbum, at queuePos: Int) {
|
|
enqueueCommand(
|
|
command: .addAlbumToQueue,
|
|
forceIdle: true,
|
|
userData: ["album": album, "queuePos": queuePos]
|
|
)
|
|
}
|
|
|
|
func sendPlayTrack(at queuePos: Int) {
|
|
mpd_run_play_pos(self.connection, UInt32(queuePos))
|
|
}
|
|
|
|
func sendFetchQueue() {
|
|
self.queue = []
|
|
mpd_send_list_queue_meta(connection)
|
|
|
|
while let mpdSong = mpd_recv_song(connection) {
|
|
let song = MPDSong(mpdSong)
|
|
self.queue.append(song)
|
|
}
|
|
|
|
self.delegate?.didUpdateQueue(mpdClient: self, queue: self.queue)
|
|
}
|
|
|
|
func sendClearQueue() {
|
|
mpd_run_clear(self.connection)
|
|
}
|
|
|
|
func sendReplaceQueue(_ songs: [MPDSong]) {
|
|
mpd_run_clear(self.connection)
|
|
|
|
for song in songs {
|
|
mpd_run_add(self.connection, song.uri)
|
|
}
|
|
mpd_run_play_pos(self.connection, 0)
|
|
}
|
|
|
|
func sendAppendSong(_ song: MPDSong) {
|
|
mpd_run_add(self.connection, song.uri)
|
|
}
|
|
|
|
func sendRemoveSong(at queuePos: Int) {
|
|
mpd_run_delete(self.connection, UInt32(queuePos))
|
|
}
|
|
|
|
func sendMoveSongInQueue(at oldQueuePos: Int, to newQueuePos: Int) {
|
|
mpd_run_move(self.connection, UInt32(oldQueuePos), UInt32(newQueuePos))
|
|
}
|
|
|
|
func sendAddSongToQueue(uri: String, at queuePos: Int) {
|
|
mpd_run_add_id_to(self.connection, uri, UInt32(queuePos))
|
|
}
|
|
|
|
func sendAddAlbumToQueue(album: MPDAlbum, at queuePos: Int) {
|
|
let songs = searchSongs(
|
|
[MPDTag.album: album.title, MPDTag.albumArtist: album.artist]
|
|
)
|
|
|
|
var insertPos = UInt32(queuePos)
|
|
|
|
for song in songs {
|
|
mpd_run_add_id_to(self.connection, song.uri, insertPos)
|
|
|
|
insertPos += 1
|
|
}
|
|
}
|
|
}
|