Controls fixes

This commit is contained in:
Arkadiusz Fal 2022-03-27 21:24:32 +02:00
parent fa91367b3d
commit cf3fa3871b
4 changed files with 59 additions and 8 deletions

View File

@ -148,6 +148,8 @@ final class MPVBackend: PlayerBackend {
} else { } else {
replaceItem(nil) replaceItem(nil)
} }
startClientUpdates()
} }
func play() { func play() {

View File

@ -11,6 +11,8 @@ final class PlayerControlsModel: ObservableObject {
@Published var timer: Timer? @Published var timer: Timer?
@Published var playingFullscreen = false @Published var playingFullscreen = false
private var throttle = Throttle(interval: 1)
var player: PlayerModel! var player: PlayerModel!
var playbackTime: String { var playbackTime: String {
@ -52,7 +54,13 @@ final class PlayerControlsModel: ObservableObject {
} }
func show() { func show() {
player.backend.updateControls() guard !(player?.currentItem.isNil ?? true) else {
return
}
guard !presentingControls else {
return
}
withAnimation(PlayerControls.animation) { withAnimation(PlayerControls.animation) {
presentingControls = true presentingControls = true
@ -60,16 +68,21 @@ final class PlayerControlsModel: ObservableObject {
} }
func hide() { func hide() {
player?.backend.stopControlsUpdates()
guard !(player?.currentItem.isNil ?? true) else {
return
}
guard presentingControls else {
return
}
withAnimation(PlayerControls.animation) { withAnimation(PlayerControls.animation) {
presentingControls = false presentingControls = false
} }
} }
func toggle() { func toggle() {
if !presentingControls {
player.backend.updateControls()
}
withAnimation(PlayerControls.animation) { withAnimation(PlayerControls.animation) {
presentingControls.toggle() presentingControls.toggle()
} }
@ -118,4 +131,10 @@ final class PlayerControlsModel: ObservableObject {
timer?.invalidate() timer?.invalidate()
timer = nil timer = nil
} }
func update() {
throttle.execute { [weak self] in
self?.player?.backend.updateControls()
}
}
} }

View File

@ -14,6 +14,9 @@ struct PlayerGestures: View {
}, },
doubleTapAction: { doubleTapAction: {
player.backend.seek(relative: .secondsInDefaultTimescale(-10)) player.backend.seek(relative: .secondsInDefaultTimescale(-10))
},
anyTapAction: {
model.update()
} }
) )
@ -25,6 +28,9 @@ struct PlayerGestures: View {
}, },
doubleTapAction: { doubleTapAction: {
player.backend.togglePlay() player.backend.togglePlay()
},
anyTapAction: {
model.update()
} }
) )
@ -36,6 +42,9 @@ struct PlayerGestures: View {
}, },
doubleTapAction: { doubleTapAction: {
player.backend.seek(relative: .secondsInDefaultTimescale(10)) player.backend.seek(relative: .secondsInDefaultTimescale(10))
},
anyTapAction: {
model.update()
} }
) )
} }

View File

@ -6,11 +6,18 @@ struct TapRecognizerViewModifier: ViewModifier {
var tapSensitivity: Double var tapSensitivity: Double
var singleTapAction: () -> Void var singleTapAction: () -> Void
var doubleTapAction: () -> Void var doubleTapAction: () -> Void
var anyTapAction: () -> Void
init(tapSensitivity: Double, singleTapAction: @escaping () -> Void, doubleTapAction: @escaping () -> Void) { init(
tapSensitivity: Double,
singleTapAction: @escaping () -> Void,
doubleTapAction: @escaping () -> Void,
anyTapAction: @escaping () -> Void
) {
self.tapSensitivity = tapSensitivity self.tapSensitivity = tapSensitivity
self.singleTapAction = singleTapAction self.singleTapAction = singleTapAction
self.doubleTapAction = doubleTapAction self.doubleTapAction = doubleTapAction
self.anyTapAction = anyTapAction
} }
func body(content: Content) -> some View { func body(content: Content) -> some View {
@ -19,6 +26,8 @@ struct TapRecognizerViewModifier: ViewModifier {
private var singleTapGesture: some Gesture { private var singleTapGesture: some Gesture {
TapGesture(count: 1).onEnded { TapGesture(count: 1).onEnded {
anyTapAction()
singleTapIsTaped = true singleTapIsTaped = true
DispatchQueue.main.asyncAfter(deadline: .now() + tapSensitivity) { DispatchQueue.main.asyncAfter(deadline: .now() + tapSensitivity) {
@ -42,7 +51,19 @@ struct TapRecognizerViewModifier: ViewModifier {
} }
extension View { extension View {
func tapRecognizer(tapSensitivity: Double, singleTapAction: @escaping () -> Void, doubleTapAction: @escaping () -> Void) -> some View { func tapRecognizer(
modifier(TapRecognizerViewModifier(tapSensitivity: tapSensitivity, singleTapAction: singleTapAction, doubleTapAction: doubleTapAction)) tapSensitivity: Double,
singleTapAction: @escaping () -> Void,
doubleTapAction: @escaping () -> Void,
anyTapAction: @escaping () -> Void = {}
) -> some View {
modifier(
TapRecognizerViewModifier(
tapSensitivity: tapSensitivity,
singleTapAction: singleTapAction,
doubleTapAction: doubleTapAction,
anyTapAction: anyTapAction
)
)
} }
} }