IceCubesApp/Packages/MediaUI/Sources/MediaUI/MediaUIAttachmentVideoView.swift

199 lines
5.6 KiB
Swift
Raw Normal View History

2022-12-24 07:29:45 +00:00
import AVKit
import DesignSystem
import Env
2024-02-14 11:48:14 +00:00
import Models
import Observation
2023-01-30 06:27:06 +00:00
import SwiftUI
2022-12-24 07:29:45 +00:00
@MainActor
2023-10-16 17:08:59 +00:00
@Observable public class MediaUIAttachmentVideoViewModel {
var player: AVPlayer?
2024-01-22 20:54:28 +00:00
let url: URL
let forceAutoPlay: Bool
2024-01-07 14:29:59 +00:00
var isPlaying: Bool = false
2023-01-17 10:36:01 +00:00
public init(url: URL, forceAutoPlay: Bool = false) {
2022-12-24 07:29:45 +00:00
self.url = url
self.forceAutoPlay = forceAutoPlay
2022-12-24 07:29:45 +00:00
}
2023-01-17 10:36:01 +00:00
2024-01-07 15:32:36 +00:00
func preparePlayer(autoPlay: Bool, isCompact: Bool) {
2022-12-24 07:29:45 +00:00
player = .init(url: url)
player?.audiovisualBackgroundPlaybackPolicy = .pauses
2024-01-08 20:21:14 +00:00
#if !os(visionOS)
2024-02-14 11:48:14 +00:00
player?.preventsDisplaySleepDuringVideoPlayback = false
2024-01-08 20:21:14 +00:00
#endif
2024-02-14 11:48:14 +00:00
if autoPlay || forceAutoPlay, !isCompact {
player?.play()
2024-01-07 14:29:59 +00:00
isPlaying = true
} else {
player?.pause()
2024-01-07 14:29:59 +00:00
isPlaying = false
}
2022-12-24 07:29:45 +00:00
guard let player else { return }
NotificationCenter.default.addObserver(forName: .AVPlayerItemDidPlayToEndTime,
2023-03-13 12:38:28 +00:00
object: player.currentItem, queue: .main)
{ _ in
Task { @MainActor [weak self] in
if autoPlay || self?.forceAutoPlay == true {
self?.play()
}
}
2022-12-24 07:29:45 +00:00
}
}
2024-02-14 11:48:14 +00:00
2024-01-07 16:33:37 +00:00
func mute(_ mute: Bool) {
player?.isMuted = mute
}
2023-01-22 05:38:30 +00:00
func pause() {
2024-01-07 14:29:59 +00:00
isPlaying = false
player?.pause()
}
2024-02-14 11:48:14 +00:00
2024-01-27 09:20:12 +00:00
func stop() {
isPlaying = false
player?.pause()
player = nil
}
2023-01-22 05:38:30 +00:00
func play() {
2024-01-07 14:29:59 +00:00
isPlaying = true
player?.seek(to: CMTime.zero)
player?.play()
}
2024-02-14 11:48:14 +00:00
2024-01-07 17:33:13 +00:00
func resume() {
isPlaying = true
player?.play()
}
2024-02-14 11:48:14 +00:00
2024-01-09 09:32:50 +00:00
func preventSleep(_ preventSleep: Bool) {
#if !os(visionOS)
2024-02-14 11:48:14 +00:00
player?.preventsDisplaySleepDuringVideoPlayback = preventSleep
2024-01-09 09:32:50 +00:00
#endif
}
2023-01-17 10:36:01 +00:00
2022-12-24 07:29:45 +00:00
deinit {
NotificationCenter.default.removeObserver(self, name: .AVPlayerItemDidPlayToEndTime, object: nil)
2022-12-24 07:29:45 +00:00
}
}
2024-01-07 14:29:59 +00:00
@MainActor
2023-10-16 17:08:59 +00:00
public struct MediaUIAttachmentVideoView: View {
@Environment(\.openWindow) private var openWindow
@Environment(\.scenePhase) private var scenePhase
@Environment(\.isMediaCompact) private var isCompact
2023-09-19 07:18:20 +00:00
@Environment(UserPreferences.self) private var preferences
2023-09-18 19:03:52 +00:00
@Environment(Theme.self) private var theme
2023-01-30 06:27:06 +00:00
2023-10-16 17:08:59 +00:00
@State var viewModel: MediaUIAttachmentVideoViewModel
2024-01-07 14:29:59 +00:00
@State var isFullScreen: Bool = false
2023-11-01 17:58:44 +00:00
2023-10-16 17:08:59 +00:00
public init(viewModel: MediaUIAttachmentVideoViewModel) {
_viewModel = .init(wrappedValue: viewModel)
}
2023-01-22 05:38:30 +00:00
2023-10-16 17:08:59 +00:00
public var body: some View {
2024-01-07 14:29:59 +00:00
videoView
2024-02-14 11:48:14 +00:00
.onAppear {
viewModel.preparePlayer(autoPlay: isFullScreen ? true : preferences.autoPlayVideo,
isCompact: isCompact)
viewModel.mute(preferences.muteVideo)
2024-01-07 15:49:49 +00:00
}
2024-02-14 11:48:14 +00:00
.onDisappear {
viewModel.stop()
}
.onTapGesture {
if !preferences.autoPlayVideo && !viewModel.isPlaying {
viewModel.play()
return
}
#if targetEnvironment(macCatalyst)
viewModel.pause()
let attachement = MediaAttachment.videoWith(url: viewModel.url)
openWindow(value: WindowDestinationMedia.mediaViewer(attachments: [attachement], selectedAttachment: attachement))
#else
isFullScreen = true
#endif
}
.fullScreenCover(isPresented: $isFullScreen) {
NavigationStack {
videoView
.toolbar {
ToolbarItem(placement: .topBarLeading) {
Button { isFullScreen.toggle() } label: {
Image(systemName: "xmark.circle")
}
2024-01-07 14:29:59 +00:00
}
2024-02-14 11:48:14 +00:00
QuickLookToolbarItem(itemUrl: viewModel.url)
}
}
.onAppear {
DispatchQueue.global().async {
try? AVAudioSession.sharedInstance().setActive(false, options: .notifyOthersOnDeactivation)
try? AVAudioSession.sharedInstance().setCategory(.playback, options: .duckOthers)
try? AVAudioSession.sharedInstance().setActive(true)
}
viewModel.preventSleep(true)
viewModel.mute(false)
DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) {
if isCompact || !preferences.autoPlayVideo {
viewModel.play()
} else {
viewModel.resume()
2024-01-07 14:29:59 +00:00
}
}
2024-01-07 16:52:28 +00:00
}
2024-02-14 11:48:14 +00:00
.onDisappear {
2024-01-07 17:33:13 +00:00
if isCompact || !preferences.autoPlayVideo {
2024-02-14 11:48:14 +00:00
viewModel.pause()
}
viewModel.preventSleep(false)
viewModel.mute(preferences.muteVideo)
DispatchQueue.global().async {
try? AVAudioSession.sharedInstance().setActive(false, options: .notifyOthersOnDeactivation)
try? AVAudioSession.sharedInstance().setCategory(.ambient, options: .mixWithOthers)
try? AVAudioSession.sharedInstance().setActive(true)
2024-01-07 17:33:13 +00:00
}
}
2024-01-07 15:49:49 +00:00
}
2024-02-14 11:48:14 +00:00
.cornerRadius(4)
.onChange(of: scenePhase) { _, newValue in
switch newValue {
case .background, .inactive:
2024-01-07 15:49:49 +00:00
viewModel.pause()
2024-02-14 11:48:14 +00:00
case .active:
if (preferences.autoPlayVideo || viewModel.forceAutoPlay || isFullScreen) && !isCompact {
viewModel.play()
}
default:
break
2024-01-07 15:49:49 +00:00
}
}
2022-12-24 07:29:45 +00:00
}
2024-02-14 11:48:14 +00:00
2024-01-07 14:29:59 +00:00
private var videoView: some View {
VideoPlayer(player: viewModel.player, videoOverlay: {
2024-02-14 11:48:14 +00:00
if !preferences.autoPlayVideo,
!viewModel.forceAutoPlay,
2024-01-07 14:29:59 +00:00
!isFullScreen,
2024-02-14 11:48:14 +00:00
!viewModel.isPlaying,
!isCompact
{
2024-01-07 14:29:59 +00:00
Button(action: {
viewModel.play()
}, label: {
Image(systemName: "play.fill")
.font(isCompact ? .body : .largeTitle)
.foregroundColor(theme.tintColor)
.padding(.all, isCompact ? 6 : nil)
.background(Circle().fill(.thinMaterial))
.padding(theme.statusDisplayStyle == .compact ? 0 : 10)
})
}
})
.accessibilityAddTraits(.startsMediaSession)
}
2022-12-24 07:29:45 +00:00
}