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

133 lines
3.5 KiB
Swift
Raw Normal View History

2022-12-24 07:29:45 +00:00
import AVKit
import DesignSystem
import Env
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?
2022-12-24 07:29:45 +00:00
private 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-07 15:32:36 +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
}
}
2023-01-22 05:38:30 +00:00
func pause() {
2024-01-07 14:29:59 +00:00
isPlaying = false
player?.pause()
}
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()
}
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(\.scenePhase) private var scenePhase
@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
}
.onDisappear {
2024-01-07 14:29:59 +00:00
try? AVAudioSession.sharedInstance().setCategory(.ambient)
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")
}
}
}
}
}
.cornerRadius(4)
.onChange(of: scenePhase) { _, newValue in
switch newValue {
case .background, .inactive:
viewModel.pause()
case .active:
2024-01-07 15:32:36 +00:00
if (preferences.autoPlayVideo || viewModel.forceAutoPlay || isFullScreen) && !isCompact {
viewModel.play()
}
default:
break
}
}
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
}