feat: make play button reflect with state change

This commit is contained in:
CMK 2021-03-09 19:07:30 +08:00
parent fd4e99907b
commit defb0ae6e0
1 changed files with 20 additions and 13 deletions

View File

@ -43,7 +43,7 @@ class AudioContainerViewModel {
.store(in: &cell.disposeBag)
self.observePlayer(cell: cell, audioAttachment: audioAttachment)
if audioAttachment != AudioPlayer.shared.attachment {
self.resetAudioView(audioView: audioView, audioAttachment: audioAttachment)
configureAudioView(audioView: audioView, audioAttachment: audioAttachment, playbackState: .stopped)
}
}
@ -78,26 +78,33 @@ class AudioContainerViewModel {
.receive(on: DispatchQueue.main)
.sink(receiveValue: { playbackState in
if audioAttachment === AudioPlayer.shared.attachment {
let isPlaying = AudioPlayer.shared.isPlaying()
audioView.playButton.isSelected = isPlaying
audioView.slider.isEnabled = isPlaying
if playbackState == .stopped {
self.resetAudioView(audioView: audioView, audioAttachment: audioAttachment)
}
configureAudioView(audioView: audioView, audioAttachment: audioAttachment, playbackState: playbackState)
} else {
self.resetAudioView(audioView: audioView, audioAttachment: audioAttachment)
configureAudioView(audioView: audioView, audioAttachment: audioAttachment, playbackState: .stopped)
}
})
.store(in: &cell.disposeBag)
}
static func resetAudioView(
static func configureAudioView(
audioView: AudioContainerView,
audioAttachment: Attachment
audioAttachment: Attachment,
playbackState: PlaybackState
) {
switch playbackState {
case .stopped:
audioView.playButton.isSelected = false
audioView.slider.setValue(0, animated: false)
audioView.slider.isEnabled = false
audioView.slider.setValue(0, animated: false)
case .paused:
audioView.playButton.isSelected = false
audioView.slider.isEnabled = true
case .playing, .readyToPlay:
audioView.playButton.isSelected = true
audioView.slider.isEnabled = true
default:
assertionFailure()
}
guard let duration = audioAttachment.meta?.original?.duration else { return }
audioView.timeLabel.text = duration.asString(style: .positional)
}