From defb0ae6e06753f82ff087253756fe919a9589aa Mon Sep 17 00:00:00 2001 From: CMK Date: Tue, 9 Mar 2021 19:07:30 +0800 Subject: [PATCH] feat: make play button reflect with state change --- .../ViewModel/AudioContainerViewModel.swift | 33 +++++++++++-------- 1 file changed, 20 insertions(+), 13 deletions(-) diff --git a/Mastodon/Scene/Share/ViewModel/AudioContainerViewModel.swift b/Mastodon/Scene/Share/ViewModel/AudioContainerViewModel.swift index cb1603404..70509d40e 100644 --- a/Mastodon/Scene/Share/ViewModel/AudioContainerViewModel.swift +++ b/Mastodon/Scene/Share/ViewModel/AudioContainerViewModel.swift @@ -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 ) { - audioView.playButton.isSelected = false - audioView.slider.setValue(0, animated: false) - audioView.slider.isEnabled = false + switch playbackState { + case .stopped: + audioView.playButton.isSelected = 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) }