1
0
mirror of https://github.com/yattee/yattee.git synced 2024-12-13 22:00:31 +05:30
yattee/Shared/Player/PlayerDragGesture.swift

106 lines
3.6 KiB
Swift
Raw Normal View History

import Defaults
import SwiftUI
extension VideoPlayerView {
var playerDragGesture: some Gesture {
DragGesture(minimumDistance: 0, coordinateSpace: .global)
#if os(iOS)
.updating($dragGestureOffset) { value, state, _ in
guard isVerticalDrag else { return }
var translation = value.translation
translation.height = max(0, translation.height)
state = translation
}
#endif
.updating($dragGestureState) { _, state, _ in
state = true
}
.onChanged { value in
guard player.presentingPlayer,
!playerControls.presentingControlsOverlay else { return }
if playerControls.presentingControls, !player.musicMode {
playerControls.presentingControls = false
}
if player.musicMode {
player.backend.stopControlsUpdates()
}
let verticalDrag = value.translation.height
let horizontalDrag = value.translation.width
#if os(iOS)
if viewDragOffset > 0, !isVerticalDrag {
isVerticalDrag = true
}
#endif
2022-08-29 17:37:27 +05:30
if !isVerticalDrag, horizontalPlayerGestureEnabled, abs(horizontalDrag) > seekGestureSensitivity, !isHorizontalDrag {
isHorizontalDrag = true
2022-08-29 17:25:23 +05:30
player.seek.onSeekGestureStart()
viewDragOffset = 0
}
if horizontalPlayerGestureEnabled, isHorizontalDrag {
2022-08-29 17:25:23 +05:30
player.seek.updateCurrentTime {
let time = player.backend.playerItemDuration?.seconds ?? 0
if player.seek.gestureStart.isNil {
player.seek.gestureStart = time
}
let timeSeek = (time / player.playerSize.width) * horizontalDrag * seekGestureSpeed
player.seek.gestureSeek = timeSeek
}
return
}
guard verticalDrag > 0 else { return }
viewDragOffset = verticalDrag
if verticalDrag > 60,
player.playingFullScreen
{
player.exitFullScreen(showControls: false)
#if os(iOS)
if Defaults[.rotateToPortraitOnExitFullScreen] {
Orientation.lockOrientation(.allButUpsideDown, andRotateTo: .portrait)
}
#endif
}
}
.onEnded { _ in
onPlayerDragGestureEnded()
}
}
2022-08-29 21:28:40 +05:30
func onPlayerDragGestureEnded() {
if horizontalPlayerGestureEnabled, isHorizontalDrag {
isHorizontalDrag = false
2022-08-29 17:25:23 +05:30
player.seek.onSeekGestureEnd()
}
isVerticalDrag = false
guard player.presentingPlayer,
!playerControls.presentingControlsOverlay else { return }
if viewDragOffset > 100 {
withAnimation(Constants.overlayAnimation) {
viewDragOffset = Self.hiddenOffset
}
2022-08-29 21:26:49 +05:30
player.backend.setNeedsDrawing(false)
} else {
withAnimation(Constants.overlayAnimation) {
viewDragOffset = 0
}
player.backend.setNeedsDrawing(true)
player.show()
if player.musicMode {
player.backend.startControlsUpdates()
}
}
}
}