1
1
mirror of https://github.com/danbee/persephone synced 2025-03-04 08:39:11 +00:00
persephone/Persephone/Controllers/NotificationsController.swift
Daniel Barber 8bab7c2bf5
Refactor more of the things!
1. Move all the mpdClient actions into a reducer.
2. Move global stuff into their own global struct
2019-04-30 09:11:50 -04:00

71 lines
1.8 KiB
Swift

//
// NotificationsController.swift
// Persephone
//
// Created by Daniel Barber on 2019/2/02.
// Copyright © 2019 Dan Barber. All rights reserved.
//
import Foundation
class NotificationsController: MPDClientDelegate {
let notificationQueue = DispatchQueue.main
func didConnect(mpdClient: MPDClient) {
sendNotification(name: Notification.didConnect)
}
func willDisconnect(mpdClient: MPDClient) {
DispatchQueue.main.async {
App.store.dispatch(UpdateAlbumListAction(albums: []))
}
sendNotification(name: Notification.willDisconnect)
}
func didUpdateStatus(mpdClient: MPDClient, status: MPDClient.MPDStatus) {
DispatchQueue.main.async {
App.store.dispatch(UpdateStatusAction(status: status))
}
}
func willStartDatabaseUpdate(mpdClient: MPDClient) {
DispatchQueue.main.async {
App.store.dispatch(StartedDatabaseUpdateAction())
}
}
func didFinishDatabaseUpdate(mpdClient: MPDClient) {
DispatchQueue.main.async {
App.store.dispatch(FinishedDatabaseUpdateAction())
}
}
func didUpdateQueue(mpdClient: MPDClient, queue: [MPDClient.MPDSong]) {
DispatchQueue.main.async {
App.store.dispatch(UpdateQueueAction(queue: queue))
}
}
func didUpdateQueuePos(mpdClient: MPDClient, song: Int) {
DispatchQueue.main.async {
App.store.dispatch(UpdateQueuePosAction(queuePos: song))
}
}
func didLoadAlbums(mpdClient: MPDClient, albums: [MPDClient.MPDAlbum]) {
DispatchQueue.main.async {
App.store.dispatch(UpdateAlbumListAction(albums: albums))
}
}
private func sendNotification(name: Notification.Name, userInfo: [AnyHashable : Any] = [:]) {
self.notificationQueue.async {
NotificationCenter.default.post(
name: name,
object: App.mpdClient,
userInfo: userInfo
)
}
}
}