From a90a63bf1b2c539d4b04d4ef3a65957001fe1370 Mon Sep 17 00:00:00 2001 From: Thomas Ricouard Date: Sat, 24 Dec 2022 08:29:45 +0100 Subject: [PATCH] Cleanup --- .../Status/Media/VideoPlayerView.swift | 37 +++++++++++++++ .../Status/Row/StatusMediaPreviewView.swift | 45 +------------------ 2 files changed, 38 insertions(+), 44 deletions(-) create mode 100644 Packages/Status/Sources/Status/Media/VideoPlayerView.swift diff --git a/Packages/Status/Sources/Status/Media/VideoPlayerView.swift b/Packages/Status/Sources/Status/Media/VideoPlayerView.swift new file mode 100644 index 00000000..e16c499d --- /dev/null +++ b/Packages/Status/Sources/Status/Media/VideoPlayerView.swift @@ -0,0 +1,37 @@ +import SwiftUI +import AVKit + +class VideoPlayerViewModel: ObservableObject { + @Published var player: AVPlayer? + private let url: URL + + init(url: URL) { + self.url = url + } + + func preparePlayer() { + player = .init(url: url) + player?.play() + guard let player else { return } + NotificationCenter.default.addObserver(forName: .AVPlayerItemDidPlayToEndTime, + object: player.currentItem, queue: .main) { [weak self] _ in + self?.player?.seek(to: CMTime.zero) + self?.player?.play() + } + } + + deinit { + NotificationCenter.default.removeObserver(self, name: .AVPlayerItemDidPlayToEndTime, object: self.player) + } +} + +struct VideoPlayerView: View { + @StateObject var viewModel: VideoPlayerViewModel + var body: some View { + VStack { + VideoPlayer(player: viewModel.player) + }.onAppear { + viewModel.preparePlayer() + } + } +} diff --git a/Packages/Status/Sources/Status/Row/StatusMediaPreviewView.swift b/Packages/Status/Sources/Status/Row/StatusMediaPreviewView.swift index 1de961cb..d2ca076e 100644 --- a/Packages/Status/Sources/Status/Row/StatusMediaPreviewView.swift +++ b/Packages/Status/Sources/Status/Row/StatusMediaPreviewView.swift @@ -1,56 +1,13 @@ import SwiftUI import Models -import AVKit import Env import Shimmer -private class VideoPlayerViewModel: ObservableObject { - @Published var player: AVPlayer? - private let url: URL - - init(url: URL) { - self.url = url - } - - func preparePlayer() { - player = .init(url: url) - player?.play() - guard let player else { return } - NotificationCenter.default.addObserver(forName: .AVPlayerItemDidPlayToEndTime, - object: player.currentItem, queue: .main) { [weak self] _ in - self?.player?.seek(to: CMTime.zero) - self?.player?.play() - } - } - - deinit { - NotificationCenter.default.removeObserver(self, name: .AVPlayerItemDidPlayToEndTime, object: self.player) - } -} - -private struct VideoPlayerView: View { - @StateObject var viewModel: VideoPlayerViewModel - var body: some View { - VStack { - VideoPlayer(player: viewModel.player) - }.onAppear { - viewModel.preparePlayer() - } - } -} - -// Could have just been a state, but SwiftUI .sheet is buggy ATM without @StateObject -private class SelectedMediaSheetManager: ObservableObject { - @Published var selectedAttachement: MediaAttachement? -} - public struct StatusMediaPreviewView: View { @EnvironmentObject private var quickLook: QuickLook public let attachements: [MediaAttachement] - - @StateObject private var selectedMediaSheetManager = SelectedMediaSheetManager() - + @State private var isQuickLookLoading: Bool = false private var imageMaxHeight: CGFloat {