feat: make play button reflect with state change
This commit is contained in:
parent
fd4e99907b
commit
defb0ae6e0
|
@ -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)
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue