2021-03-08 04:42:10 +01:00
|
|
|
//
|
|
|
|
// AudioContainerViewModel.swift
|
|
|
|
// Mastodon
|
|
|
|
//
|
|
|
|
// Created by sxiaojian on 2021/3/9.
|
|
|
|
//
|
|
|
|
|
|
|
|
import CoreDataStack
|
2021-03-09 09:25:47 +01:00
|
|
|
import Foundation
|
2021-03-08 04:42:10 +01:00
|
|
|
import UIKit
|
|
|
|
|
|
|
|
class AudioContainerViewModel {
|
|
|
|
static func configure(
|
|
|
|
cell: StatusTableViewCell,
|
|
|
|
audioAttachment: Attachment
|
|
|
|
) {
|
|
|
|
guard let duration = audioAttachment.meta?.original?.duration else { return }
|
|
|
|
let audioView = cell.statusView.audioView
|
|
|
|
audioView.timeLabel.text = duration.asString(style: .positional)
|
2021-03-09 09:25:47 +01:00
|
|
|
|
2021-03-08 04:42:10 +01:00
|
|
|
audioView.playButton.publisher(for: .touchUpInside)
|
2021-03-09 09:25:47 +01:00
|
|
|
.sink { _ in
|
2021-03-09 09:54:05 +01:00
|
|
|
if AudioPlayer.shared.isPlaying() {
|
2021-03-08 04:42:10 +01:00
|
|
|
AudioPlayer.shared.pause()
|
|
|
|
} else {
|
|
|
|
if audioAttachment === AudioPlayer.shared.attachment {
|
|
|
|
if AudioPlayer.shared.currentTimeSubject.value == 0 {
|
|
|
|
AudioPlayer.shared.playAudio(audioAttachment: audioAttachment)
|
|
|
|
} else {
|
|
|
|
AudioPlayer.shared.resume()
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
AudioPlayer.shared.playAudio(audioAttachment: audioAttachment)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.store(in: &cell.disposeBag)
|
|
|
|
audioView.slider.publisher(for: .valueChanged)
|
|
|
|
.sink { slider in
|
|
|
|
let slider = slider as! UISlider
|
|
|
|
let time = Double(slider.value) * duration
|
|
|
|
AudioPlayer.shared.seekToTime(time: time)
|
|
|
|
}
|
|
|
|
.store(in: &cell.disposeBag)
|
2021-03-09 09:25:47 +01:00
|
|
|
self.observePlayer(cell: cell, audioAttachment: audioAttachment)
|
2021-03-08 04:42:10 +01:00
|
|
|
if audioAttachment != AudioPlayer.shared.attachment {
|
|
|
|
self.resetAudioView(audioView: audioView)
|
|
|
|
}
|
|
|
|
}
|
2021-03-09 09:25:47 +01:00
|
|
|
|
2021-03-08 04:42:10 +01:00
|
|
|
static func observePlayer(
|
|
|
|
cell: StatusTableViewCell,
|
|
|
|
audioAttachment: Attachment
|
|
|
|
) {
|
|
|
|
let audioView = cell.statusView.audioView
|
|
|
|
AudioPlayer.shared.currentTimeSubject
|
|
|
|
.receive(on: DispatchQueue.main)
|
|
|
|
.filter { _ in
|
|
|
|
audioAttachment === AudioPlayer.shared.attachment
|
|
|
|
}
|
|
|
|
.sink(receiveValue: { time in
|
|
|
|
audioView.timeLabel.text = time.asString(style: .positional)
|
|
|
|
if let duration = audioAttachment.meta?.original?.duration, !audioView.slider.isTracking {
|
2021-03-09 09:25:47 +01:00
|
|
|
audioView.slider.setValue(Float(time / duration), animated: true)
|
2021-03-08 04:42:10 +01:00
|
|
|
}
|
|
|
|
})
|
|
|
|
.store(in: &cell.disposeBag)
|
|
|
|
AudioPlayer.shared.playbackState
|
2021-03-09 08:18:36 +01:00
|
|
|
.receive(on: DispatchQueue.main)
|
|
|
|
.sink(receiveValue: { playbackState in
|
2021-03-09 09:25:47 +01:00
|
|
|
if audioAttachment === AudioPlayer.shared.attachment {
|
2021-03-09 09:54:05 +01:00
|
|
|
let isPlaying = AudioPlayer.shared.isPlaying()
|
2021-03-08 04:42:10 +01:00
|
|
|
audioView.playButton.isSelected = isPlaying
|
2021-03-09 09:25:47 +01:00
|
|
|
audioView.slider.isEnabled = isPlaying
|
2021-03-09 08:18:36 +01:00
|
|
|
if playbackState == .stopped {
|
|
|
|
self.resetAudioView(audioView: audioView)
|
|
|
|
}
|
2021-03-08 04:42:10 +01:00
|
|
|
} else {
|
|
|
|
self.resetAudioView(audioView: audioView)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.store(in: &cell.disposeBag)
|
|
|
|
}
|
2021-03-09 09:25:47 +01:00
|
|
|
|
|
|
|
static func resetAudioView(audioView: AudioContainerView) {
|
2021-03-08 04:42:10 +01:00
|
|
|
audioView.playButton.isSelected = false
|
|
|
|
audioView.slider.setValue(0, animated: false)
|
2021-03-09 09:25:47 +01:00
|
|
|
audioView.slider.isEnabled = false
|
2021-03-08 04:42:10 +01:00
|
|
|
}
|
|
|
|
}
|