chore: the play interrupts event could be sent with the notification

This commit is contained in:
sunxiaojian 2021-03-10 21:19:56 +08:00
parent 7556e57de9
commit 2657dde184
7 changed files with 50 additions and 13 deletions

View File

@ -37,7 +37,11 @@ extension StatusSection {
StatusSection.configure( StatusSection.configure(
cell: cell, cell: cell,
dependency: dependency, dependency: dependency,
readableLayoutFrame: tableView.readableContentGuide.layoutFrame, timestampUpdatePublisher: timestampUpdatePublisher, toot: timelineIndex.toot, requestUserID: timelineIndex.userID, statusItemAttribute: attribute readableLayoutFrame: tableView.readableContentGuide.layoutFrame,
timestampUpdatePublisher: timestampUpdatePublisher,
toot: timelineIndex.toot,
requestUserID: timelineIndex.userID,
statusItemAttribute: attribute
) )
} }
cell.delegate = statusTableViewCellDelegate cell.delegate = statusTableViewCellDelegate
@ -52,7 +56,11 @@ extension StatusSection {
StatusSection.configure( StatusSection.configure(
cell: cell, cell: cell,
dependency: dependency, dependency: dependency,
readableLayoutFrame: tableView.readableContentGuide.layoutFrame, timestampUpdatePublisher: timestampUpdatePublisher, toot: toot, requestUserID: requestUserID, statusItemAttribute: attribute readableLayoutFrame: tableView.readableContentGuide.layoutFrame,
timestampUpdatePublisher: timestampUpdatePublisher,
toot: toot,
requestUserID: requestUserID,
statusItemAttribute: attribute
) )
} }
cell.delegate = statusTableViewCellDelegate cell.delegate = statusTableViewCellDelegate
@ -168,7 +176,7 @@ extension StatusSection {
// set audio // set audio
if let audioAttachment = mediaAttachments.filter({ $0.type == .audio }).first { if let audioAttachment = mediaAttachments.filter({ $0.type == .audio }).first {
cell.statusView.audioView.isHidden = false cell.statusView.audioView.isHidden = false
AudioContainerViewModel.configure(cell: cell, audioAttachment: audioAttachment, videoPlaybackService: dependency.context.videoPlaybackService) AudioContainerViewModel.configure(cell: cell, audioAttachment: audioAttachment )
} else { } else {
cell.statusView.audioView.isHidden = true cell.statusView.audioView.isHidden = true
} }

View File

@ -141,7 +141,7 @@ extension HomeTimelineViewController {
override func viewDidDisappear(_ animated: Bool) { override func viewDidDisappear(_ animated: Bool) {
super.viewDidDisappear(animated) super.viewDidDisappear(animated)
context.videoPlaybackService.viewDidDisappear(from: self)
} }
override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) { override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
@ -237,6 +237,9 @@ extension HomeTimelineViewController: UITableViewDelegate {
handleTableView(tableView, willDisplay: cell, forRowAt: indexPath) handleTableView(tableView, willDisplay: cell, forRowAt: indexPath)
} }
func tableView(_ tableView: UITableView, didEndDisplaying cell: UITableViewCell, forRowAt indexPath: IndexPath) {
handleTableView(tableView, didEndDisplaying: cell, forRowAt: indexPath)
}
} }
// MARK: - ContentOffsetAdjustableTimelineViewControllerDelegate // MARK: - ContentOffsetAdjustableTimelineViewControllerDelegate

View File

@ -81,6 +81,10 @@ extension PublicTimelineViewController {
) )
} }
override func viewDidDisappear(_ animated: Bool) {
super.viewDidDisappear(animated)
context.videoPlaybackService.viewDidDisappear(from: self)
}
} }
// MARK: - UIScrollViewDelegate // MARK: - UIScrollViewDelegate
@ -103,6 +107,7 @@ extension PublicTimelineViewController {
// MARK: - UITableViewDelegate // MARK: - UITableViewDelegate
extension PublicTimelineViewController: UITableViewDelegate { extension PublicTimelineViewController: UITableViewDelegate {
func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat { func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
guard let diffableDataSource = viewModel.diffableDataSource else { return 100 } guard let diffableDataSource = viewModel.diffableDataSource else { return 100 }
guard let item = diffableDataSource.itemIdentifier(for: indexPath) else { return 100 } guard let item = diffableDataSource.itemIdentifier(for: indexPath) else { return 100 }
@ -114,8 +119,11 @@ extension PublicTimelineViewController: UITableViewDelegate {
} }
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {} func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {}
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
handleTableView(tableView, willDisplay: cell, forRowAt: indexPath)
}
func tableView(_ tableView: UITableView, didEndDisplaying cell: UITableViewCell, forRowAt indexPath: IndexPath) { func tableView(_ tableView: UITableView, didEndDisplaying cell: UITableViewCell, forRowAt indexPath: IndexPath) {
handleTableView(tableView, didEndDisplaying: cell, forRowAt: indexPath)
guard let diffableDataSource = viewModel.diffableDataSource else { return } guard let diffableDataSource = viewModel.diffableDataSource else { return }
guard let item = diffableDataSource.itemIdentifier(for: indexPath) else { return } guard let item = diffableDataSource.itemIdentifier(for: indexPath) else { return }

View File

@ -12,8 +12,7 @@ import UIKit
class AudioContainerViewModel { class AudioContainerViewModel {
static func configure( static func configure(
cell: StatusTableViewCell, cell: StatusTableViewCell,
audioAttachment: Attachment, audioAttachment: Attachment
videoPlaybackService: VideoPlaybackService
) { ) {
guard let duration = audioAttachment.meta?.original?.duration else { return } guard let duration = audioAttachment.meta?.original?.duration else { return }
let audioView = cell.statusView.audioView let audioView = cell.statusView.audioView
@ -26,15 +25,12 @@ class AudioContainerViewModel {
AudioPlayer.shared.pause() AudioPlayer.shared.pause()
} else { } else {
AudioPlayer.shared.resume() AudioPlayer.shared.resume()
videoPlaybackService.pauseWhenPlayAudio()
} }
if AudioPlayer.shared.currentTimeSubject.value == 0 { if AudioPlayer.shared.currentTimeSubject.value == 0 {
AudioPlayer.shared.playAudio(audioAttachment: audioAttachment) AudioPlayer.shared.playAudio(audioAttachment: audioAttachment)
videoPlaybackService.pauseWhenPlayAudio()
} }
} else { } else {
AudioPlayer.shared.playAudio(audioAttachment: audioAttachment) AudioPlayer.shared.playAudio(audioAttachment: audioAttachment)
videoPlaybackService.pauseWhenPlayAudio()
} }
} }
.store(in: &cell.disposeBag) .store(in: &cell.disposeBag)

View File

@ -14,6 +14,7 @@ import UIKit
final class VideoPlayerViewModel { final class VideoPlayerViewModel {
var disposeBag = Set<AnyCancellable>() var disposeBag = Set<AnyCancellable>()
static let appWillPlayVideoNotification = NSNotification.Name(rawValue: "appWillPlayVideoNotification")
// input // input
let previewImageURL: URL? let previewImageURL: URL?
let videoURL: URL let videoURL: URL
@ -63,7 +64,7 @@ final class VideoPlayerViewModel {
.sink { [weak self] timeControlStatus in .sink { [weak self] timeControlStatus in
guard let _ = self else { return } guard let _ = self else { return }
guard timeControlStatus == .playing else { return } guard timeControlStatus == .playing else { return }
AudioPlayer.shared.pauseIfNeed() NotificationCenter.default.post(name: VideoPlayerViewModel.appWillPlayVideoNotification, object: nil)
switch videoKind { switch videoKind {
case .gif: case .gif:
break break

View File

@ -12,6 +12,9 @@ import Foundation
import UIKit import UIKit
final class AudioPlayer: NSObject { final class AudioPlayer: NSObject {
static let appWillPlayAudioNotification = NSNotification.Name(rawValue: "appWillPlayAudioNotification")
var disposeBag = Set<AnyCancellable>() var disposeBag = Set<AnyCancellable>()
var player = AVPlayer() var player = AVPlayer()
@ -45,6 +48,7 @@ extension AudioPlayer {
return return
} }
pushWillPlayAudioNotification()
if audioAttachment == attachment { if audioAttachment == attachment {
if self.playbackState.value == .stopped { if self.playbackState.value == .stopped {
self.seekToTime(time: .zero) self.seekToTime(time: .zero)
@ -83,6 +87,12 @@ extension AudioPlayer {
} }
} }
.store(in: &disposeBag) .store(in: &disposeBag)
NotificationCenter.default.publisher(for: VideoPlayerViewModel.appWillPlayVideoNotification)
.sink { [weak self] _ in
guard let self = self else { return }
self.pauseIfNeed()
}
.store(in: &disposeBag)
timeObserver = player.addPeriodicTimeObserver(forInterval: CMTimeMake(value: 1, timescale: CMTimeScale(NSEC_PER_SEC)), queue: DispatchQueue.main, using: { [weak self] time in timeObserver = player.addPeriodicTimeObserver(forInterval: CMTimeMake(value: 1, timescale: CMTimeScale(NSEC_PER_SEC)), queue: DispatchQueue.main, using: { [weak self] time in
guard let self = self else { return } guard let self = self else { return }
@ -119,10 +129,14 @@ extension AudioPlayer {
.store(in: &disposeBag) .store(in: &disposeBag)
} }
func pushWillPlayAudioNotification() {
NotificationCenter.default.post(name: AudioPlayer.appWillPlayAudioNotification, object: nil)
}
func isPlaying() -> Bool { func isPlaying() -> Bool {
return self.playbackState.value == .readyToPlay || self.playbackState.value == .playing return playbackState.value == .readyToPlay || playbackState.value == .playing
} }
func resume() { func resume() {
pushWillPlayAudioNotification()
player.play() player.play()
playbackState.value = .playing playbackState.value = .playing
} }

View File

@ -90,6 +90,13 @@ extension VideoPlaybackService {
self.playerViewModel(viewModel, didUpdateTimeControlStatus: timeControlStatus) self.playerViewModel(viewModel, didUpdateTimeControlStatus: timeControlStatus)
} }
.store(in: &disposeBag) .store(in: &disposeBag)
NotificationCenter.default.publisher(for: AudioPlayer.appWillPlayAudioNotification)
.sink { [weak self] _ in
guard let self = self else { return }
self.pauseWhenPlayAudio()
}
.store(in: &disposeBag)
} }
} }