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 {