From db98124de5dc4d526cde9f6bf903045e00fcf664 Mon Sep 17 00:00:00 2001 From: Arkadiusz Fal Date: Thu, 1 Sep 2022 20:01:01 +0200 Subject: [PATCH] Model improvements --- Model/Player/PlayerControlsModel.swift | 8 ++--- Model/SeekModel.swift | 2 +- Shared/Navigation/ContentView.swift | 4 +-- Shared/Player/PlayerDragGesture.swift | 8 ++--- Shared/Player/PlayerGestures.swift | 9 ------ Shared/Player/PlayerOrientation.swift | 4 +-- Shared/Player/VideoPlayerView.swift | 41 +++++++++++++------------- Shared/YatteeApp.swift | 3 +- 8 files changed, 34 insertions(+), 45 deletions(-) diff --git a/Model/Player/PlayerControlsModel.swift b/Model/Player/PlayerControlsModel.swift index 2f1f5975..f23a4267 100644 --- a/Model/Player/PlayerControlsModel.swift +++ b/Model/Player/PlayerControlsModel.swift @@ -5,6 +5,8 @@ import Foundation import SwiftUI final class PlayerControlsModel: ObservableObject { + static var shared = PlayerControlsModel() + @Published var isLoadingVideo = false @Published var isPlaying = true @Published var presentingControls = false { didSet { handlePresentationChange() } } @@ -33,7 +35,7 @@ final class PlayerControlsModel: ObservableObject { self.presentingControlsOverlay = presentingControlsOverlay self.presentingDetailsOverlay = presentingDetailsOverlay self.timer = timer - self.player = player + self.player = player ?? .shared } func handlePresentationChange() { @@ -130,8 +132,4 @@ final class PlayerControlsModel: ObservableObject { timer?.invalidate() timer = nil } - - func update() { - player?.backend.updateControls() - } } diff --git a/Model/SeekModel.swift b/Model/SeekModel.swift index 7561f4cf..52b225b6 100644 --- a/Model/SeekModel.swift +++ b/Model/SeekModel.swift @@ -4,7 +4,7 @@ import SwiftUI final class SeekModel: ObservableObject { static let shared = SeekModel() - + @Published var currentTime = CMTime.zero @Published var duration = CMTime.zero diff --git a/Shared/Navigation/ContentView.swift b/Shared/Navigation/ContentView.swift index 37785054..d164e471 100644 --- a/Shared/Navigation/ContentView.swift +++ b/Shared/Navigation/ContentView.swift @@ -12,7 +12,7 @@ struct ContentView: View { @EnvironmentObject private var comments @EnvironmentObject private var instances @EnvironmentObject private var navigation - @EnvironmentObject private var playerControls + @EnvironmentObject private var player @EnvironmentObject private var playlists @EnvironmentObject private var recents @EnvironmentObject private var search @@ -28,7 +28,7 @@ struct ContentView: View { @State private var playerInitialized = false - private var player = PlayerModel.shared + var playerControls: PlayerControlsModel { .shared } let persistenceController = PersistenceController.shared diff --git a/Shared/Player/PlayerDragGesture.swift b/Shared/Player/PlayerDragGesture.swift index 73d34c5f..a613807c 100644 --- a/Shared/Player/PlayerDragGesture.swift +++ b/Shared/Player/PlayerDragGesture.swift @@ -17,10 +17,10 @@ extension VideoPlayerView { } .onChanged { value in guard player.presentingPlayer, - !playerControls.presentingControlsOverlay else { return } + !player.controls.presentingControlsOverlay else { return } - if playerControls.presentingControls, !player.musicMode { - playerControls.presentingControls = false + if player.controls.presentingControls, !player.musicMode { + player.controls.presentingControls = false } if player.musicMode { @@ -83,7 +83,7 @@ extension VideoPlayerView { isVerticalDrag = false guard player.presentingPlayer, - !playerControls.presentingControlsOverlay else { return } + !player.controls.presentingControlsOverlay else { return } if viewDragOffset > 100 { withAnimation(Constants.overlayAnimation) { diff --git a/Shared/Player/PlayerGestures.swift b/Shared/Player/PlayerGestures.swift index b5f7e6b6..24e09135 100644 --- a/Shared/Player/PlayerGestures.swift +++ b/Shared/Player/PlayerGestures.swift @@ -12,9 +12,6 @@ struct PlayerGestures: View { singleTapAction: { singleTapAction() }, doubleTapAction: { player.backend.seek(relative: .secondsInDefaultTimescale(-10), seekType: .userInteracted) - }, - anyTapAction: { - model.update() } ) @@ -24,9 +21,6 @@ struct PlayerGestures: View { singleTapAction: { singleTapAction() }, doubleTapAction: { player.backend.togglePlay() - }, - anyTapAction: { - model.update() } ) @@ -36,9 +30,6 @@ struct PlayerGestures: View { singleTapAction: { singleTapAction() }, doubleTapAction: { player.backend.seek(relative: .secondsInDefaultTimescale(10), seekType: .userInteracted) - }, - anyTapAction: { - model.update() } ) } diff --git a/Shared/Player/PlayerOrientation.swift b/Shared/Player/PlayerOrientation.swift index 37aedb41..ed3b6826 100644 --- a/Shared/Player/PlayerOrientation.swift +++ b/Shared/Player/PlayerOrientation.swift @@ -13,7 +13,7 @@ extension VideoPlayerView { guard player.presentingPlayer else { return } DispatchQueue.main.async { - playerControls.presentingControls = false + player.controls.presentingControls = false player.enterFullScreen(showControls: false) } @@ -53,7 +53,7 @@ extension VideoPlayerView { orientationDebouncer.callback = { DispatchQueue.main.async { if orientation.isLandscape { - playerControls.presentingControls = false + player.controls.presentingControls = false player.enterFullScreen(showControls: false) Orientation.lockOrientation(OrientationTracker.shared.currentInterfaceOrientationMask, andRotateTo: orientation) } else { diff --git a/Shared/Player/VideoPlayerView.swift b/Shared/Player/VideoPlayerView.swift index c40dbd35..3cc45376 100644 --- a/Shared/Player/VideoPlayerView.swift +++ b/Shared/Player/VideoPlayerView.swift @@ -60,7 +60,6 @@ struct VideoPlayerView: View { @EnvironmentObject internal var accounts @EnvironmentObject internal var navigation @EnvironmentObject internal var player - @EnvironmentObject internal var playerControls @EnvironmentObject internal var recents #if os(macOS) @EnvironmentObject internal var search @@ -76,7 +75,7 @@ struct VideoPlayerView: View { videoPlayer .zIndex(-1) #if os(iOS) - .gesture(playerControls.presentingControlsOverlay ? videoPlayerCloseControlsOverlayGesture : nil) + .gesture(player.controls.presentingControlsOverlay ? videoPlayerCloseControlsOverlayGesture : nil) #endif overlay @@ -155,7 +154,7 @@ struct VideoPlayerView: View { Orientation.lockOrientation(.allButUpsideDown) } stopOrientationUpdates() - playerControls.hideOverlays() + player.controls.hideOverlays() player.lockedOrientation = nil } @@ -185,14 +184,14 @@ struct VideoPlayerView: View { var overlay: some View { VStack { - if playerControls.presentingControlsOverlay { + if player.controls.presentingControlsOverlay { HStack { HStack { ControlsOverlay() #if os(tvOS) .onExitCommand { - withAnimation(PlayerControls.animation) { - playerControls.hideOverlays() + withAnimation(Player.controls.animation) { + player.controls.hideOverlays() } } .onPlayPauseCommand { @@ -239,7 +238,7 @@ struct VideoPlayerView: View { var videoPlayerCloseControlsOverlayGesture: some Gesture { TapGesture().onEnded { withAnimation(PlayerControls.animation) { - playerControls.hideOverlays() + player.controls.hideOverlays() } } } @@ -298,17 +297,17 @@ struct VideoPlayerView: View { .frame(maxWidth: fullScreenLayout ? .infinity : nil, maxHeight: fullScreenLayout ? .infinity : nil) .onHover { hovering in hoveringPlayer = hovering - hovering ? playerControls.show() : playerControls.hide() + hovering ? player.controls.show() : player.controls.hide() } #if !os(tvOS) - .gesture(playerControls.presentingOverlays ? nil : playerDragGesture) + .gesture(player.controls.presentingOverlays ? nil : playerDragGesture) #endif #if os(macOS) .onAppear(perform: { NSEvent.addLocalMonitorForEvents(matching: [.mouseMoved]) { hoverThrottle.execute { if !player.currentItem.isNil, hoveringPlayer { - playerControls.resetTimer() + player.controls.resetTimer() } } @@ -347,16 +346,16 @@ struct VideoPlayerView: View { #if os(tvOS) .onMoveCommand { direction in if direction == .up { - playerControls.show() - } else if direction == .down, !playerControls.presentingControlsOverlay, !playerControls.presentingControls { - withAnimation(PlayerControls.animation) { - playerControls.presentingControlsOverlay = true + player.controls.show() + } else if direction == .down, !player.controls.presentingControlsOverlay, !player.controls.presentingControls { + withAnimation(Player.controls.animation) { + player.controls.presentingControlsOverlay = true } } - playerControls.resetTimer() + player.controls.resetTimer() - guard !playerControls.presentingControls else { return } + guard !player.controls.presentingControls else { return } if direction == .left { player.backend.seek(relative: .secondsInDefaultTimescale(-10), seekType: .userInteracted) @@ -369,11 +368,11 @@ struct VideoPlayerView: View { player.togglePlay() } .onExitCommand { - if playerControls.presentingOverlays { - playerControls.hideOverlays() + if player.controls.presentingOverlays { + player.controls.hideOverlays() } - if playerControls.presentingControls { - playerControls.hide() + if player.controls.presentingControls { + player.controls.hide() } else { player.hide() } @@ -397,7 +396,7 @@ struct VideoPlayerView: View { } } .onChange(of: fullScreenLayout) { newValue in - if !newValue { playerControls.hideOverlays() } + if !newValue { player.controls.hideOverlays() } } #if os(iOS) .statusBar(hidden: fullScreenLayout) diff --git a/Shared/YatteeApp.swift b/Shared/YatteeApp.swift index 825d1c65..fb9f69e1 100644 --- a/Shared/YatteeApp.swift +++ b/Shared/YatteeApp.swift @@ -39,7 +39,6 @@ struct YatteeApp: App { @StateObject private var navigation = NavigationModel() @StateObject private var networkState = NetworkStateModel() @StateObject private var player = PlayerModel() - @StateObject private var playerControls = PlayerControlsModel() @StateObject private var playlists = PlaylistsModel() @StateObject private var recents = RecentsModel() @StateObject private var search = SearchModel() @@ -49,6 +48,8 @@ struct YatteeApp: App { let persistenceController = PersistenceController.shared + var playerControls: PlayerControlsModel { .shared } + var body: some Scene { WindowGroup { ContentView()