1
1
mirror of https://github.com/danbee/persephone synced 2025-03-04 08:39:11 +00:00
persephone/Mac/Components/Shared/PlayerStateInfoController.swift

82 lines
1.9 KiB
Swift

//
// MediaInfoController.swift
// Persephone
//
// Created by Alan Harper on 18/11/20.
// Copyright © 2020 Dan Barber. All rights reserved.
//
import Foundation
import ReSwift
import MediaPlayer
import Kingfisher
class PlayerStateInfoController {
let commandCenter = MPRemoteCommandCenter.shared()
init() {
App.store.subscribe(self) {
$0.select { $0.playerState.state }
}
commandCenter.playCommand.addTarget { _ in
App.mpdClient.playPause()
return .success
}
commandCenter.togglePlayPauseCommand.addTarget { _ in
App.mpdClient.playPause()
return .success
}
commandCenter.pauseCommand.addTarget { _ in
App.mpdClient.playPause()
return .success
}
commandCenter.nextTrackCommand.addTarget { _ in
App.mpdClient.nextTrack()
return .success
}
commandCenter.previousTrackCommand.addTarget { _ in
App.mpdClient.prevTrack()
return .success
}
commandCenter.changePlaybackPositionCommand.addTarget { event in
let changeEvent = event as! MPChangePlaybackPositionCommandEvent
App.mpdClient.seekCurrentSong(timeInSeconds: Float(changeEvent.positionTime))
return .success
}
}
func notifyState(_ state: MPDClient.MPDStatus.State?) {
let infoCenter = MPNowPlayingInfoCenter.default()
switch(state) {
case .unknown:
infoCenter.playbackState = .unknown
break;
case .paused:
infoCenter.playbackState = .paused
break;
case .stopped:
infoCenter.playbackState = .stopped
break;
case .playing:
infoCenter.playbackState = .playing
break;
case .none:
return
}
}
}
extension PlayerStateInfoController: StoreSubscriber {
typealias StoreSubscriberStateType = MPDClient.MPDStatus.State?
func newState(state: StoreSubscriberStateType) {
notifyState(state)
}
}