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
|
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?
|
2022-12-24 07:29:45 +00:00
|
|
|
private 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-07 15:32:36 +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
|
|
|
}
|
|
|
|
}
|
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()
|
|
|
|
}
|
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()
|
|
|
|
}
|
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 {
|
2023-01-17 18:41:46 +00:00
|
|
|
@Environment(\.scenePhase) private var scenePhase
|
2023-02-22 06:06:01 +00:00
|
|
|
@Environment(\.isCompact) 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
|
|
|
|
.onAppear {
|
|
|
|
try? AVAudioSession.sharedInstance().setCategory(.playback)
|
2024-01-07 15:32:36 +00:00
|
|
|
viewModel.preparePlayer(autoPlay: isFullScreen ? true : preferences.autoPlayVideo,
|
|
|
|
isCompact: isCompact)
|
2022-12-24 07:29:45 +00:00
|
|
|
}
|
2023-08-09 11:53:41 +00:00
|
|
|
.onDisappear {
|
2024-01-07 14:29:59 +00:00
|
|
|
try? AVAudioSession.sharedInstance().setCategory(.ambient)
|
2023-08-09 11:53:41 +00:00
|
|
|
viewModel.pause()
|
|
|
|
}
|
2024-01-07 14:29:59 +00:00
|
|
|
.onTapGesture {
|
|
|
|
isFullScreen = true
|
|
|
|
}
|
|
|
|
.fullScreenCover(isPresented: $isFullScreen) {
|
|
|
|
NavigationStack {
|
|
|
|
videoView
|
|
|
|
.toolbar {
|
|
|
|
ToolbarItem(placement: .topBarLeading) {
|
|
|
|
Button { isFullScreen.toggle() } label: {
|
|
|
|
Image(systemName: "xmark.circle")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-02-17 17:17:51 +00:00
|
|
|
.cornerRadius(4)
|
2023-09-18 05:01:23 +00:00
|
|
|
.onChange(of: scenePhase) { _, newValue in
|
|
|
|
switch newValue {
|
2023-01-17 18:41:46 +00:00
|
|
|
case .background, .inactive:
|
|
|
|
viewModel.pause()
|
|
|
|
case .active:
|
2024-01-07 15:32:36 +00:00
|
|
|
if (preferences.autoPlayVideo || viewModel.forceAutoPlay || isFullScreen) && !isCompact {
|
2023-02-04 06:53:03 +00:00
|
|
|
viewModel.play()
|
|
|
|
}
|
2023-01-17 18:41:46 +00:00
|
|
|
default:
|
|
|
|
break
|
|
|
|
}
|
2023-09-18 05:01:23 +00:00
|
|
|
}
|
2022-12-24 07:29:45 +00:00
|
|
|
}
|
2024-01-07 14:29:59 +00:00
|
|
|
|
|
|
|
private var videoView: some View {
|
|
|
|
VideoPlayer(player: viewModel.player, videoOverlay: {
|
|
|
|
if !preferences.autoPlayVideo,
|
|
|
|
!viewModel.forceAutoPlay,
|
|
|
|
!isFullScreen,
|
|
|
|
!viewModel.isPlaying {
|
|
|
|
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
|
|
|
}
|