Fix video player

This commit is contained in:
Thomas Ricouard 2024-09-16 17:32:31 +02:00
parent 93421c56d9
commit e4d1196301
3 changed files with 70 additions and 52 deletions

View file

@ -100,6 +100,7 @@ extension IceCubesApp {
}
}
.withEnvironments()
.environment(\.isCatalystWindow, true)
.environment(RouterPath())
.withModelContainer()
.applyTheme(theme)
@ -121,6 +122,7 @@ extension IceCubesApp {
.withEnvironments()
.withModelContainer()
.applyTheme(theme)
.environment(\.isCatalystWindow, true)
.frame(minWidth: 300, minHeight: 400)
}
.defaultSize(width: 1200, height: 1000)

View file

@ -6,6 +6,7 @@ extension EnvironmentValues {
@Entry public var extraLeadingInset: CGFloat = 0
@Entry public var isCompact: Bool = false
@Entry public var isMediaCompact: Bool = false
@Entry public var isCatalystWindow: Bool = false
@Entry public var isModal: Bool = false
@Entry public var isInCaptureMode: Bool = false
@Entry public var isSupporter: Bool = false

View file

@ -83,6 +83,7 @@ import SwiftUI
public struct MediaUIAttachmentVideoView: View {
@Environment(\.openWindow) private var openWindow
@Environment(\.scenePhase) private var scenePhase
@Environment(\.isCatalystWindow) private var isCatalystWindow
@Environment(\.isMediaCompact) private var isCompact
@Environment(UserPreferences.self) private var preferences
@Environment(Theme.self) private var theme
@ -96,14 +97,13 @@ public struct MediaUIAttachmentVideoView: View {
public var body: some View {
videoView
.onAppear {
viewModel.preparePlayer(autoPlay: isFullScreen ? true : preferences.autoPlayVideo,
isCompact: isCompact)
viewModel.mute(preferences.muteVideo)
}
.onDisappear {
viewModel.stop()
}
.overlay(content: {
if isCatalystWindow {
EmptyView()
} else {
HStack { }
.frame(maxWidth: .infinity, maxHeight: .infinity)
.contentShape(Rectangle())
.onTapGesture {
if !preferences.autoPlayVideo && !viewModel.isPlaying {
viewModel.play()
@ -117,7 +117,35 @@ public struct MediaUIAttachmentVideoView: View {
isFullScreen = true
#endif
}
}
})
.onAppear {
viewModel.preparePlayer(autoPlay: isFullScreen ? true : preferences.autoPlayVideo,
isCompact: isCompact)
viewModel.mute(preferences.muteVideo)
}
.onDisappear {
viewModel.stop()
}
.fullScreenCover(isPresented: $isFullScreen) {
modalPreview
}
.cornerRadius(4)
.onChange(of: scenePhase) { _, newValue in
switch newValue {
case .background, .inactive:
viewModel.pause()
case .active:
if (preferences.autoPlayVideo || viewModel.forceAutoPlay || isFullScreen) && !isCompact {
viewModel.play()
}
default:
break
}
}
}
private var modalPreview: some View {
NavigationStack {
videoView
.toolbar {
@ -158,20 +186,6 @@ public struct MediaUIAttachmentVideoView: View {
}
}
}
.cornerRadius(4)
.onChange(of: scenePhase) { _, newValue in
switch newValue {
case .background, .inactive:
viewModel.pause()
case .active:
if (preferences.autoPlayVideo || viewModel.forceAutoPlay || isFullScreen) && !isCompact {
viewModel.play()
}
default:
break
}
}
}
private var videoView: some View {
VideoPlayer(player: viewModel.player, videoOverlay: {
@ -194,5 +208,6 @@ public struct MediaUIAttachmentVideoView: View {
}
})
.accessibilityAddTraits(.startsMediaSession)
.ignoresSafeArea()
}
}