Skip to content

Commit

Permalink
Fix undesired animation (#881)
Browse files Browse the repository at this point in the history
  • Loading branch information
waliid authored May 15, 2024
1 parent e02de41 commit 12d49c8
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 16 deletions.
36 changes: 21 additions & 15 deletions Demo/Sources/Players/PlaybackView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,22 +25,26 @@ private struct MainView: View {
@State private var selectedGravity: AVLayerVideoGravity = .resizeAspect
@State private var isInteracting = false

private var areControlsAlwaysVisible: Bool {
player.isExternalPlaybackActive || player.mediaType == .audio
}

private var prioritizesVideoDevices: Bool {
player.mediaType == .video
}

private var shouldHideInterface: Bool {
isUserInterfaceHidden || (isInteracting && !shouldKeepControlsAlwaysVisible)
}

private var shouldKeepControlsAlwaysVisible: Bool {
player.isExternalPlaybackActive || player.mediaType != .video
}

var body: some View {
ZStack {
main()
bottomBar()
topBar()
}
.statusBarHidden(isFullScreen ? isUserInterfaceHidden : false)
.animation(.defaultLinear, value: isUserInterfaceHidden)
.animation(.defaultLinear, value: shouldHideInterface)
.bind(visibilityTracker, to: player)
}

Expand All @@ -61,7 +65,7 @@ private struct MainView: View {
}

private var isUserInterfaceHidden: Bool {
visibilityTracker.isUserInterfaceHidden && !areControlsAlwaysVisible && !player.canReplay()
visibilityTracker.isUserInterfaceHidden && !shouldKeepControlsAlwaysVisible && !player.canReplay()
}

private var title: String? {
Expand Down Expand Up @@ -126,7 +130,7 @@ private struct MainView: View {
}
.frame(maxWidth: .infinity, alignment: .leading)
.foregroundStyle(.white)
.opacity(isInteracting ? 0 : 1)
.opacity(shouldHideInterface ? 0 : 1)
}

@ViewBuilder
Expand All @@ -135,7 +139,7 @@ private struct MainView: View {
skipButton()
bottomControls()
}
.animation(.linear(duration: 0.2), values: isUserInterfaceHidden, isInteracting)
.animation(.defaultLinear, values: isUserInterfaceHidden, isInteracting)
.padding(.horizontal)
.padding(.vertical, 10)
.frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .bottom)
Expand Down Expand Up @@ -164,10 +168,12 @@ private struct MainView: View {

@ViewBuilder
private func bottomButtons() -> some View {
HStack(spacing: 20) {
LiveButton(player: player, progressTracker: progressTracker)
settingsMenu()
FullScreenButton(layout: $layout)
if !shouldHideInterface {
HStack(spacing: 20) {
LiveButton(player: player, progressTracker: progressTracker)
settingsMenu()
FullScreenButton(layout: $layout)
}
}
}

Expand All @@ -179,13 +185,14 @@ private struct MainView: View {
PiPButton()
routePickerView()
}
.opacity(shouldHideInterface ? 0 : 1)
Spacer()
HStack(spacing: 20) {
LoadingIndicator(player: player)
VolumeButton(player: player)
.opacity(shouldHideInterface ? 0 : 1)
}
}
.opacity(isUserInterfaceHidden ? 0 : 1)
.topBarStyle()
.preventsTouchPropagation()
.frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .top)
Expand Down Expand Up @@ -247,11 +254,10 @@ private struct MainView: View {
private func controls() -> some View {
ZStack {
Color(white: 0, opacity: 0.5)
.opacity(isUserInterfaceHidden || (isInteracting && !areControlsAlwaysVisible) ? 0 : 1)
.ignoresSafeArea()
ControlsView(player: player, progressTracker: progressTracker)
.opacity(isUserInterfaceHidden || isInteracting ? 0 : 1)
}
.opacity(shouldHideInterface ? 0 : 1)
}

@ViewBuilder
Expand Down
2 changes: 1 addition & 1 deletion Demo/Sources/Tools/Constant.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import SwiftUI
let kPageSize: UInt = 50

extension Animation {
static let defaultLinear = Self.linear(duration: 0.2)
static let defaultLinear = linear(duration: 0.2)
}

func constant<T>(iOS: T, tvOS: T) -> T {
Expand Down

0 comments on commit 12d49c8

Please sign in to comment.