1
1
mirror of https://github.com/danbee/persephone synced 2025-03-04 08:39:11 +00:00
persephone/Persephone/MPDClient/Extensions/MPDClient+Album.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

95 lines
2.5 KiB
Swift

//
// MPDAlbum.swift
// Persephone
//
// Created by Daniel Barber on 2019/3/15.
// Copyright © 2019 Dan Barber. All rights reserved.
//
import Foundation
import mpdclient
extension MPDClient {
func fetchAllAlbums() {
queueCommand(command: .fetchAllAlbums)
}
func playAlbum(_ album: Album) {
queueCommand(command: .playAlbum, userData: ["album": album])
}
func getAlbumURI(for album: Album) {
queueCommand(command: .getAlbumURI, userData: ["album": album])
}
func sendPlayAlbum(_ album: Album) {
var songs: [Song] = []
mpd_run_clear(self.connection)
mpd_search_db_songs(self.connection, true)
mpd_search_add_tag_constraint(self.connection, MPD_OPERATOR_DEFAULT, MPD_TAG_ALBUM, album.title)
mpd_search_add_tag_constraint(self.connection, MPD_OPERATOR_DEFAULT, MPD_TAG_ALBUM_ARTIST, album.artist)
mpd_search_commit(self.connection)
while let mpdSong = mpd_recv_song(self.connection) {
songs.append(Song(mpdSong))
}
for song in songs {
mpd_run_add(self.connection, song.uri)
}
mpd_run_play_pos(self.connection, 0)
}
func allAlbums() {
var albums: [Album] = []
var artist: String = ""
mpd_search_db_tags(self.connection, MPD_TAG_ALBUM)
mpd_search_add_group_tag(self.connection, MPD_TAG_ALBUM_ARTIST)
mpd_search_commit(self.connection)
while let mpdPair = mpd_recv_pair(self.connection) {
let pair = Pair(mpdPair)
switch pair.name {
case "AlbumArtist":
artist = pair.value
case "Album":
albums.append(Album(title: pair.value, artist: artist))
default:
break
}
mpd_return_pair(self.connection, pair.mpdPair)
}
self.delegate?.didLoadAlbums(mpdClient: self, albums: albums)
}
func albumURI(for album: Album) -> String? {
var songURI: String?
guard isConnected else { return nil }
print("Getting URI")
mpd_search_db_songs(self.connection, true)
mpd_search_add_tag_constraint(self.connection, MPD_OPERATOR_DEFAULT, MPD_TAG_ALBUM, album.title)
mpd_search_add_tag_constraint(self.connection, MPD_OPERATOR_DEFAULT, MPD_TAG_ALBUM_ARTIST, album.artist)
mpd_search_add_tag_constraint(self.connection, MPD_OPERATOR_DEFAULT, MPD_TAG_TRACK, "1")
mpd_search_commit(self.connection)
print("Performed search")
while let mpdSong = mpd_recv_song(self.connection) {
let song = Song(mpdSong)
print(song)
if songURI == nil {
songURI = song.uriString
}
}
print("Got URI")
return songURI
}
}