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

View file

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

View file

@ -83,6 +83,7 @@ import SwiftUI
public struct MediaUIAttachmentVideoView: View { public struct MediaUIAttachmentVideoView: View {
@Environment(\.openWindow) private var openWindow @Environment(\.openWindow) private var openWindow
@Environment(\.scenePhase) private var scenePhase @Environment(\.scenePhase) private var scenePhase
@Environment(\.isCatalystWindow) private var isCatalystWindow
@Environment(\.isMediaCompact) private var isCompact @Environment(\.isMediaCompact) private var isCompact
@Environment(UserPreferences.self) private var preferences @Environment(UserPreferences.self) private var preferences
@Environment(Theme.self) private var theme @Environment(Theme.self) private var theme
@ -96,14 +97,13 @@ public struct MediaUIAttachmentVideoView: View {
public var body: some View { public var body: some View {
videoView videoView
.onAppear { .overlay(content: {
viewModel.preparePlayer(autoPlay: isFullScreen ? true : preferences.autoPlayVideo, if isCatalystWindow {
isCompact: isCompact) EmptyView()
viewModel.mute(preferences.muteVideo) } else {
} HStack { }
.onDisappear { .frame(maxWidth: .infinity, maxHeight: .infinity)
viewModel.stop() .contentShape(Rectangle())
}
.onTapGesture { .onTapGesture {
if !preferences.autoPlayVideo && !viewModel.isPlaying { if !preferences.autoPlayVideo && !viewModel.isPlaying {
viewModel.play() viewModel.play()
@ -117,7 +117,35 @@ public struct MediaUIAttachmentVideoView: View {
isFullScreen = true isFullScreen = true
#endif #endif
} }
}
})
.onAppear {
viewModel.preparePlayer(autoPlay: isFullScreen ? true : preferences.autoPlayVideo,
isCompact: isCompact)
viewModel.mute(preferences.muteVideo)
}
.onDisappear {
viewModel.stop()
}
.fullScreenCover(isPresented: $isFullScreen) { .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 { NavigationStack {
videoView videoView
.toolbar { .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 { private var videoView: some View {
VideoPlayer(player: viewModel.player, videoOverlay: { VideoPlayer(player: viewModel.player, videoOverlay: {
@ -194,5 +208,6 @@ public struct MediaUIAttachmentVideoView: View {
} }
}) })
.accessibilityAddTraits(.startsMediaSession) .accessibilityAddTraits(.startsMediaSession)
.ignoresSafeArea()
} }
} }