Kurdtvs-Live-Kurdish-TV-Kur.../Mastodon/Scene/Share/ViewModel/AudioContainerViewModel.swift

118 lines
4.7 KiB
Swift
Raw Normal View History

2021-03-08 04:42:10 +01:00
//
// AudioContainerViewModel.swift
// Mastodon
//
// Created by sxiaojian on 2021/3/9.
//
import CoreDataStack
import Foundation
2021-03-08 04:42:10 +01:00
import UIKit
class AudioContainerViewModel {
2021-03-08 04:42:10 +01:00
static func configure(
2021-04-19 12:06:02 +02:00
cell: StatusCell,
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-08 04:42:10 +01:00
audioView.playButton.publisher(for: .touchUpInside)
.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 {
audioService.resume()
2021-03-09 10:13:17 +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 {
audioService.playAudio(audioAttachment: audioAttachment)
2021-03-08 04:42:10 +01:00
}
}
.store(in: &cell.disposeBag)
audioView.slider.maximumValue = Float(duration)
2021-03-08 04:42:10 +01:00
audioView.slider.publisher(for: .valueChanged)
.sink { [weak audioService] slider in
guard let audioService = audioService else { return }
2021-03-08 04:42:10 +01:00
let slider = slider as! UISlider
let time = TimeInterval(slider.value)
audioService.seekToTime(time: time)
2021-03-08 04:42:10 +01:00
}
.store(in: &cell.disposeBag)
observePlayer(cell: cell, audioAttachment: audioAttachment, audioService: audioService)
if audioAttachment != audioService.attachment {
configureAudioView(audioView: audioView, audioAttachment: audioAttachment, playbackState: .stopped)
2021-03-08 04:42:10 +01:00
}
}
2021-03-08 04:42:10 +01:00
static func observePlayer(
2021-04-19 12:06:02 +02:00
cell: StatusCell,
audioAttachment: Attachment,
audioService: AudioPlaybackService
2021-03-08 04:42:10 +01:00
) {
let audioView = cell.statusView.audioView
var lastCurrentTimeSubject: TimeInterval?
audioService.currentTimeSubject
.throttle(for: 0.008, scheduler: DispatchQueue.main, latest: true)
.compactMap { [weak audioService] time -> TimeInterval? in
defer {
lastCurrentTimeSubject = time
}
guard audioAttachment === audioService?.attachment else { return nil }
// guard let duration = audioAttachment.meta?.original?.duration else { return nil }
if let lastCurrentTimeSubject = lastCurrentTimeSubject, time != 0.0 {
guard abs(time - lastCurrentTimeSubject) < 0.5 else { return nil } // debounce
}
guard !audioView.slider.isTracking else { return nil }
return TimeInterval(time)
2021-03-08 04:42:10 +01:00
}
.sink(receiveValue: { time in
2021-03-08 04:42:10 +01:00
audioView.timeLabel.text = time.asString(style: .positional)
audioView.slider.setValue(Float(time), animated: true)
2021-03-08 04:42:10 +01:00
})
.store(in: &cell.disposeBag)
audioService.playbackState
.receive(on: DispatchQueue.main)
.sink(receiveValue: { playbackState in
if audioAttachment === audioService.attachment {
configureAudioView(audioView: audioView, audioAttachment: audioAttachment, playbackState: playbackState)
2021-03-08 04:42:10 +01:00
} else {
configureAudioView(audioView: audioView, audioAttachment: audioAttachment, playbackState: .stopped)
2021-03-08 04:42:10 +01:00
}
})
.store(in: &cell.disposeBag)
}
static func configureAudioView(
2021-03-09 10:13:17 +01:00
audioView: AudioContainerView,
audioAttachment: Attachment,
playbackState: PlaybackState
2021-03-09 10:13:17 +01:00
) {
switch playbackState {
case .stopped:
audioView.playButton.isSelected = false
audioView.slider.isUserInteractionEnabled = false
audioView.slider.setValue(0, animated: false)
case .paused:
audioView.playButton.isSelected = false
audioView.slider.isUserInteractionEnabled = true
case .playing, .readyToPlay:
audioView.playButton.isSelected = true
audioView.slider.isUserInteractionEnabled = true
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
}
}