2022-12-24 07:29:45 +00:00
|
|
|
import AVKit
|
2023-02-19 10:44:18 +00:00
|
|
|
import DesignSystem
|
2023-01-29 09:19:59 +00:00
|
|
|
import Env
|
2024-02-14 11:48:14 +00:00
|
|
|
import Models
|
2023-09-18 05:01:23 +00:00
|
|
|
import Observation
|
2023-01-30 06:27:06 +00:00
|
|
|
import SwiftUI
|
2022-12-24 07:29:45 +00:00
|
|
|
|
2023-09-15 10:46:15 +00:00
|
|
|
@MainActor
|
2023-10-16 17:08:59 +00:00
|
|
|
@Observable public class MediaUIAttachmentVideoViewModel {
|
2023-09-18 05:01:23 +00:00
|
|
|
var player: AVPlayer?
|
2024-01-22 20:54:28 +00:00
|
|
|
let url: URL
|
2023-10-17 06:24:11 +00:00
|
|
|
let forceAutoPlay: Bool
|
2024-01-07 14:29:59 +00:00
|
|
|
var isPlaying: Bool = false
|
2023-01-17 10:36:01 +00:00
|
|
|
|
2023-10-17 06:24:11 +00:00
|
|
|
public init(url: URL, forceAutoPlay: Bool = false) {
|
2022-12-24 07:29:45 +00:00
|
|
|
self.url = url
|
2023-10-17 06:24:11 +00:00
|
|
|
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)
|
2023-01-29 10:17:43 +00:00
|
|
|
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 {
|
2023-01-29 09:19:59 +00:00
|
|
|
player?.play()
|
2024-01-07 14:29:59 +00:00
|
|
|
isPlaying = true
|
2023-02-04 06:53:03 +00:00
|
|
|
} else {
|
|
|
|
player?.pause()
|
2024-01-07 14:29:59 +00:00
|
|
|
isPlaying = false
|
2023-01-29 09:19:59 +00:00
|
|
|
}
|
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)
|
2023-09-15 10:46:15 +00:00
|
|
|
{ _ in
|
|
|
|
Task { @MainActor [weak self] in
|
2023-10-17 06:24:11 +00:00
|
|
|
if autoPlay || self?.forceAutoPlay == true {
|
2023-09-15 10:46:15 +00:00
|
|
|
self?.play()
|
|
|
|
}
|
2023-02-04 06:53:03 +00:00
|
|
|
}
|
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
|
|
|
|
2023-01-17 18:41:46 +00:00
|
|
|
func pause() {
|
2024-01-07 14:29:59 +00:00
|
|
|
isPlaying = false
|
2023-01-17 18:41:46 +00:00
|
|
|
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
|
|
|
|
2023-01-17 18:41:46 +00:00
|
|
|
func play() {
|
2024-01-07 14:29:59 +00:00
|
|
|
isPlaying = true
|
2023-09-15 10:46:15 +00:00
|
|
|
player?.seek(to: CMTime.zero)
|
2023-01-17 18:41:46 +00:00
|
|
|
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 {
|
2023-09-15 10:46:15 +00:00
|
|
|
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 {
|
2024-01-22 21:04:28 +00:00
|
|
|
@Environment(\.openWindow) private var openWindow
|
2023-01-17 18:41:46 +00:00
|
|
|
@Environment(\.scenePhase) private var scenePhase
|
2024-01-21 18:46:29 +00:00
|
|
|
@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
|
|
|
}
|
2023-01-17 18:41:46 +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
|
|
|
}
|