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 {
|
2021-06-17 12:21:49 +02:00
|
|
|
|
2021-03-08 04:42:10 +01:00
|
|
|
static func configure(
|
2021-04-19 12:06:02 +02:00
|
|
|
cell: StatusCell,
|
2021-03-11 06:11:13 +01:00
|
|
|
audioAttachment: Attachment,
|
|
|
|
audioService: AudioPlaybackService
|
2021-03-08 04:42:10 +01:00
|
|
|
) {
|
|
|
|
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-11 06:11:13 +01:00
|
|
|
.sink { [weak audioService] _ in
|
|
|
|
guard let audioService = audioService else { return }
|
|
|
|
if audioAttachment === audioService.attachment {
|
|
|
|
if audioService.isPlaying() {
|
|
|
|
audioService.pause()
|
2021-03-08 04:42:10 +01:00
|
|
|
} else {
|
2021-03-11 06:11:13 +01:00
|
|
|
audioService.resume()
|
2021-03-09 10:13:17 +01:00
|
|
|
}
|
2021-03-11 06:11:13 +01:00
|
|
|
if audioService.currentTimeSubject.value == 0 {
|
|
|
|
audioService.playAudio(audioAttachment: audioAttachment)
|
2021-03-08 04:42:10 +01:00
|
|
|
}
|
2021-03-09 10:13:17 +01:00
|
|
|
} else {
|
2021-03-11 06:11:13 +01:00
|
|
|
audioService.playAudio(audioAttachment: audioAttachment)
|
2021-03-08 04:42:10 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
.store(in: &cell.disposeBag)
|
2021-06-17 12:21:49 +02:00
|
|
|
audioView.slider.maximumValue = Float(duration)
|
2021-03-08 04:42:10 +01:00
|
|
|
audioView.slider.publisher(for: .valueChanged)
|
2021-03-11 06:11:13 +01:00
|
|
|
.sink { [weak audioService] slider in
|
|
|
|
guard let audioService = audioService else { return }
|
2021-03-08 04:42:10 +01:00
|
|
|
let slider = slider as! UISlider
|
2021-06-17 12:21:49 +02:00
|
|
|
let time = TimeInterval(slider.value)
|
2021-03-11 06:11:13 +01:00
|
|
|
audioService.seekToTime(time: time)
|
2021-03-08 04:42:10 +01:00
|
|
|
}
|
|
|
|
.store(in: &cell.disposeBag)
|
2021-03-11 06:11:13 +01:00
|
|
|
observePlayer(cell: cell, audioAttachment: audioAttachment, audioService: audioService)
|
|
|
|
if audioAttachment != audioService.attachment {
|
2021-03-09 12:07:30 +01:00
|
|
|
configureAudioView(audioView: audioView, audioAttachment: audioAttachment, playbackState: .stopped)
|
2021-03-08 04:42:10 +01:00
|
|
|
}
|
|
|
|
}
|
2021-03-09 09:25:47 +01:00
|
|
|
|
2021-03-08 04:42:10 +01:00
|
|
|
static func observePlayer(
|
2021-04-19 12:06:02 +02:00
|
|
|
cell: StatusCell,
|
2021-03-11 06:11:13 +01:00
|
|
|
audioAttachment: Attachment,
|
|
|
|
audioService: AudioPlaybackService
|
2021-03-08 04:42:10 +01:00
|
|
|
) {
|
|
|
|
let audioView = cell.statusView.audioView
|
2021-03-09 11:54:21 +01:00
|
|
|
var lastCurrentTimeSubject: TimeInterval?
|
2021-03-11 06:11:13 +01:00
|
|
|
audioService.currentTimeSubject
|
2021-06-17 12:21:49 +02:00
|
|
|
.throttle(for: 0.008, scheduler: DispatchQueue.main, latest: true)
|
|
|
|
.compactMap { [weak audioService] time -> TimeInterval? in
|
2021-03-09 11:54:21 +01:00
|
|
|
defer {
|
|
|
|
lastCurrentTimeSubject = time
|
|
|
|
}
|
2021-03-11 06:11:13 +01:00
|
|
|
guard audioAttachment === audioService?.attachment else { return nil }
|
2021-06-17 12:21:49 +02:00
|
|
|
// guard let duration = audioAttachment.meta?.original?.duration else { return nil }
|
2021-03-10 10:14:12 +01:00
|
|
|
|
2021-03-09 11:54:21 +01:00
|
|
|
if let lastCurrentTimeSubject = lastCurrentTimeSubject, time != 0.0 {
|
2021-03-10 10:14:12 +01:00
|
|
|
guard abs(time - lastCurrentTimeSubject) < 0.5 else { return nil } // debounce
|
2021-03-09 11:54:21 +01:00
|
|
|
}
|
2021-03-10 10:14:12 +01:00
|
|
|
|
2021-03-09 11:54:21 +01:00
|
|
|
guard !audioView.slider.isTracking else { return nil }
|
2021-06-17 12:21:49 +02:00
|
|
|
return TimeInterval(time)
|
2021-03-08 04:42:10 +01:00
|
|
|
}
|
2021-06-17 12:21:49 +02:00
|
|
|
.sink(receiveValue: { time in
|
2021-03-08 04:42:10 +01:00
|
|
|
audioView.timeLabel.text = time.asString(style: .positional)
|
2021-06-17 12:21:49 +02:00
|
|
|
audioView.slider.setValue(Float(time), animated: true)
|
2021-03-08 04:42:10 +01:00
|
|
|
})
|
|
|
|
.store(in: &cell.disposeBag)
|
2021-03-11 06:11:13 +01:00
|
|
|
audioService.playbackState
|
2021-03-09 08:18:36 +01:00
|
|
|
.receive(on: DispatchQueue.main)
|
|
|
|
.sink(receiveValue: { playbackState in
|
2021-03-11 06:11:13 +01:00
|
|
|
if audioAttachment === audioService.attachment {
|
2021-03-09 12:07:30 +01:00
|
|
|
configureAudioView(audioView: audioView, audioAttachment: audioAttachment, playbackState: playbackState)
|
2021-03-08 04:42:10 +01:00
|
|
|
} else {
|
2021-03-09 12:07:30 +01:00
|
|
|
configureAudioView(audioView: audioView, audioAttachment: audioAttachment, playbackState: .stopped)
|
2021-03-08 04:42:10 +01:00
|
|
|
}
|
|
|
|
})
|
|
|
|
.store(in: &cell.disposeBag)
|
|
|
|
}
|
2021-03-09 09:25:47 +01:00
|
|
|
|
2021-03-09 12:07:30 +01:00
|
|
|
static func configureAudioView(
|
2021-03-09 10:13:17 +01:00
|
|
|
audioView: AudioContainerView,
|
2021-03-09 12:07:30 +01:00
|
|
|
audioAttachment: Attachment,
|
|
|
|
playbackState: PlaybackState
|
2021-03-09 10:13:17 +01:00
|
|
|
) {
|
2021-03-09 12:07:30 +01:00
|
|
|
switch playbackState {
|
|
|
|
case .stopped:
|
|
|
|
audioView.playButton.isSelected = false
|
2021-06-17 12:31:14 +02:00
|
|
|
audioView.slider.isUserInteractionEnabled = false
|
2021-03-09 12:07:30 +01:00
|
|
|
audioView.slider.setValue(0, animated: false)
|
|
|
|
case .paused:
|
|
|
|
audioView.playButton.isSelected = false
|
2021-06-17 12:31:14 +02:00
|
|
|
audioView.slider.isUserInteractionEnabled = true
|
2021-03-09 12:07:30 +01:00
|
|
|
case .playing, .readyToPlay:
|
|
|
|
audioView.playButton.isSelected = true
|
2021-06-17 12:31:14 +02:00
|
|
|
audioView.slider.isUserInteractionEnabled = true
|
2021-03-09 12:07:30 +01:00
|
|
|
default:
|
|
|
|
assertionFailure()
|
|
|
|
}
|
2021-03-09 10:13:17 +01:00
|
|
|
guard let duration = audioAttachment.meta?.original?.duration else { return }
|
|
|
|
audioView.timeLabel.text = duration.asString(style: .positional)
|
2021-03-08 04:42:10 +01:00
|
|
|
}
|
|
|
|
}
|