1
0
mirror of https://github.com/yattee/yattee.git synced 2025-04-27 15:30:33 +05:30

Model improvements

This commit is contained in:
Arkadiusz Fal 2022-09-01 20:01:01 +02:00
parent 02617a7c42
commit db98124de5
8 changed files with 34 additions and 45 deletions

View File

@ -5,6 +5,8 @@ import Foundation
import SwiftUI import SwiftUI
final class PlayerControlsModel: ObservableObject { final class PlayerControlsModel: ObservableObject {
static var shared = PlayerControlsModel()
@Published var isLoadingVideo = false @Published var isLoadingVideo = false
@Published var isPlaying = true @Published var isPlaying = true
@Published var presentingControls = false { didSet { handlePresentationChange() } } @Published var presentingControls = false { didSet { handlePresentationChange() } }
@ -33,7 +35,7 @@ final class PlayerControlsModel: ObservableObject {
self.presentingControlsOverlay = presentingControlsOverlay self.presentingControlsOverlay = presentingControlsOverlay
self.presentingDetailsOverlay = presentingDetailsOverlay self.presentingDetailsOverlay = presentingDetailsOverlay
self.timer = timer self.timer = timer
self.player = player self.player = player ?? .shared
} }
func handlePresentationChange() { func handlePresentationChange() {
@ -130,8 +132,4 @@ final class PlayerControlsModel: ObservableObject {
timer?.invalidate() timer?.invalidate()
timer = nil timer = nil
} }
func update() {
player?.backend.updateControls()
}
} }

View File

@ -4,7 +4,7 @@ import SwiftUI
final class SeekModel: ObservableObject { final class SeekModel: ObservableObject {
static let shared = SeekModel() static let shared = SeekModel()
@Published var currentTime = CMTime.zero @Published var currentTime = CMTime.zero
@Published var duration = CMTime.zero @Published var duration = CMTime.zero

View File

@ -12,7 +12,7 @@ struct ContentView: View {
@EnvironmentObject<CommentsModel> private var comments @EnvironmentObject<CommentsModel> private var comments
@EnvironmentObject<InstancesModel> private var instances @EnvironmentObject<InstancesModel> private var instances
@EnvironmentObject<NavigationModel> private var navigation @EnvironmentObject<NavigationModel> private var navigation
@EnvironmentObject<PlayerControlsModel> private var playerControls @EnvironmentObject<PlayerModel> private var player
@EnvironmentObject<PlaylistsModel> private var playlists @EnvironmentObject<PlaylistsModel> private var playlists
@EnvironmentObject<RecentsModel> private var recents @EnvironmentObject<RecentsModel> private var recents
@EnvironmentObject<SearchModel> private var search @EnvironmentObject<SearchModel> private var search
@ -28,7 +28,7 @@ struct ContentView: View {
@State private var playerInitialized = false @State private var playerInitialized = false
private var player = PlayerModel.shared var playerControls: PlayerControlsModel { .shared }
let persistenceController = PersistenceController.shared let persistenceController = PersistenceController.shared

View File

@ -17,10 +17,10 @@ extension VideoPlayerView {
} }
.onChanged { value in .onChanged { value in
guard player.presentingPlayer, guard player.presentingPlayer,
!playerControls.presentingControlsOverlay else { return } !player.controls.presentingControlsOverlay else { return }
if playerControls.presentingControls, !player.musicMode { if player.controls.presentingControls, !player.musicMode {
playerControls.presentingControls = false player.controls.presentingControls = false
} }
if player.musicMode { if player.musicMode {
@ -83,7 +83,7 @@ extension VideoPlayerView {
isVerticalDrag = false isVerticalDrag = false
guard player.presentingPlayer, guard player.presentingPlayer,
!playerControls.presentingControlsOverlay else { return } !player.controls.presentingControlsOverlay else { return }
if viewDragOffset > 100 { if viewDragOffset > 100 {
withAnimation(Constants.overlayAnimation) { withAnimation(Constants.overlayAnimation) {

View File

@ -12,9 +12,6 @@ struct PlayerGestures: View {
singleTapAction: { singleTapAction() }, singleTapAction: { singleTapAction() },
doubleTapAction: { doubleTapAction: {
player.backend.seek(relative: .secondsInDefaultTimescale(-10), seekType: .userInteracted) player.backend.seek(relative: .secondsInDefaultTimescale(-10), seekType: .userInteracted)
},
anyTapAction: {
model.update()
} }
) )
@ -24,9 +21,6 @@ struct PlayerGestures: View {
singleTapAction: { singleTapAction() }, singleTapAction: { singleTapAction() },
doubleTapAction: { doubleTapAction: {
player.backend.togglePlay() player.backend.togglePlay()
},
anyTapAction: {
model.update()
} }
) )
@ -36,9 +30,6 @@ struct PlayerGestures: View {
singleTapAction: { singleTapAction() }, singleTapAction: { singleTapAction() },
doubleTapAction: { doubleTapAction: {
player.backend.seek(relative: .secondsInDefaultTimescale(10), seekType: .userInteracted) player.backend.seek(relative: .secondsInDefaultTimescale(10), seekType: .userInteracted)
},
anyTapAction: {
model.update()
} }
) )
} }

View File

@ -13,7 +13,7 @@ extension VideoPlayerView {
guard player.presentingPlayer else { return } guard player.presentingPlayer else { return }
DispatchQueue.main.async { DispatchQueue.main.async {
playerControls.presentingControls = false player.controls.presentingControls = false
player.enterFullScreen(showControls: false) player.enterFullScreen(showControls: false)
} }
@ -53,7 +53,7 @@ extension VideoPlayerView {
orientationDebouncer.callback = { orientationDebouncer.callback = {
DispatchQueue.main.async { DispatchQueue.main.async {
if orientation.isLandscape { if orientation.isLandscape {
playerControls.presentingControls = false player.controls.presentingControls = false
player.enterFullScreen(showControls: false) player.enterFullScreen(showControls: false)
Orientation.lockOrientation(OrientationTracker.shared.currentInterfaceOrientationMask, andRotateTo: orientation) Orientation.lockOrientation(OrientationTracker.shared.currentInterfaceOrientationMask, andRotateTo: orientation)
} else { } else {

View File

@ -60,7 +60,6 @@ struct VideoPlayerView: View {
@EnvironmentObject<AccountsModel> internal var accounts @EnvironmentObject<AccountsModel> internal var accounts
@EnvironmentObject<NavigationModel> internal var navigation @EnvironmentObject<NavigationModel> internal var navigation
@EnvironmentObject<PlayerModel> internal var player @EnvironmentObject<PlayerModel> internal var player
@EnvironmentObject<PlayerControlsModel> internal var playerControls
@EnvironmentObject<RecentsModel> internal var recents @EnvironmentObject<RecentsModel> internal var recents
#if os(macOS) #if os(macOS)
@EnvironmentObject<SearchModel> internal var search @EnvironmentObject<SearchModel> internal var search
@ -76,7 +75,7 @@ struct VideoPlayerView: View {
videoPlayer videoPlayer
.zIndex(-1) .zIndex(-1)
#if os(iOS) #if os(iOS)
.gesture(playerControls.presentingControlsOverlay ? videoPlayerCloseControlsOverlayGesture : nil) .gesture(player.controls.presentingControlsOverlay ? videoPlayerCloseControlsOverlayGesture : nil)
#endif #endif
overlay overlay
@ -155,7 +154,7 @@ struct VideoPlayerView: View {
Orientation.lockOrientation(.allButUpsideDown) Orientation.lockOrientation(.allButUpsideDown)
} }
stopOrientationUpdates() stopOrientationUpdates()
playerControls.hideOverlays() player.controls.hideOverlays()
player.lockedOrientation = nil player.lockedOrientation = nil
} }
@ -185,14 +184,14 @@ struct VideoPlayerView: View {
var overlay: some View { var overlay: some View {
VStack { VStack {
if playerControls.presentingControlsOverlay { if player.controls.presentingControlsOverlay {
HStack { HStack {
HStack { HStack {
ControlsOverlay() ControlsOverlay()
#if os(tvOS) #if os(tvOS)
.onExitCommand { .onExitCommand {
withAnimation(PlayerControls.animation) { withAnimation(Player.controls.animation) {
playerControls.hideOverlays() player.controls.hideOverlays()
} }
} }
.onPlayPauseCommand { .onPlayPauseCommand {
@ -239,7 +238,7 @@ struct VideoPlayerView: View {
var videoPlayerCloseControlsOverlayGesture: some Gesture { var videoPlayerCloseControlsOverlayGesture: some Gesture {
TapGesture().onEnded { TapGesture().onEnded {
withAnimation(PlayerControls.animation) { withAnimation(PlayerControls.animation) {
playerControls.hideOverlays() player.controls.hideOverlays()
} }
} }
} }
@ -298,17 +297,17 @@ struct VideoPlayerView: View {
.frame(maxWidth: fullScreenLayout ? .infinity : nil, maxHeight: fullScreenLayout ? .infinity : nil) .frame(maxWidth: fullScreenLayout ? .infinity : nil, maxHeight: fullScreenLayout ? .infinity : nil)
.onHover { hovering in .onHover { hovering in
hoveringPlayer = hovering hoveringPlayer = hovering
hovering ? playerControls.show() : playerControls.hide() hovering ? player.controls.show() : player.controls.hide()
} }
#if !os(tvOS) #if !os(tvOS)
.gesture(playerControls.presentingOverlays ? nil : playerDragGesture) .gesture(player.controls.presentingOverlays ? nil : playerDragGesture)
#endif #endif
#if os(macOS) #if os(macOS)
.onAppear(perform: { .onAppear(perform: {
NSEvent.addLocalMonitorForEvents(matching: [.mouseMoved]) { NSEvent.addLocalMonitorForEvents(matching: [.mouseMoved]) {
hoverThrottle.execute { hoverThrottle.execute {
if !player.currentItem.isNil, hoveringPlayer { if !player.currentItem.isNil, hoveringPlayer {
playerControls.resetTimer() player.controls.resetTimer()
} }
} }
@ -347,16 +346,16 @@ struct VideoPlayerView: View {
#if os(tvOS) #if os(tvOS)
.onMoveCommand { direction in .onMoveCommand { direction in
if direction == .up { if direction == .up {
playerControls.show() player.controls.show()
} else if direction == .down, !playerControls.presentingControlsOverlay, !playerControls.presentingControls { } else if direction == .down, !player.controls.presentingControlsOverlay, !player.controls.presentingControls {
withAnimation(PlayerControls.animation) { withAnimation(Player.controls.animation) {
playerControls.presentingControlsOverlay = true player.controls.presentingControlsOverlay = true
} }
} }
playerControls.resetTimer() player.controls.resetTimer()
guard !playerControls.presentingControls else { return } guard !player.controls.presentingControls else { return }
if direction == .left { if direction == .left {
player.backend.seek(relative: .secondsInDefaultTimescale(-10), seekType: .userInteracted) player.backend.seek(relative: .secondsInDefaultTimescale(-10), seekType: .userInteracted)
@ -369,11 +368,11 @@ struct VideoPlayerView: View {
player.togglePlay() player.togglePlay()
} }
.onExitCommand { .onExitCommand {
if playerControls.presentingOverlays { if player.controls.presentingOverlays {
playerControls.hideOverlays() player.controls.hideOverlays()
} }
if playerControls.presentingControls { if player.controls.presentingControls {
playerControls.hide() player.controls.hide()
} else { } else {
player.hide() player.hide()
} }
@ -397,7 +396,7 @@ struct VideoPlayerView: View {
} }
} }
.onChange(of: fullScreenLayout) { newValue in .onChange(of: fullScreenLayout) { newValue in
if !newValue { playerControls.hideOverlays() } if !newValue { player.controls.hideOverlays() }
} }
#if os(iOS) #if os(iOS)
.statusBar(hidden: fullScreenLayout) .statusBar(hidden: fullScreenLayout)

View File

@ -39,7 +39,6 @@ struct YatteeApp: App {
@StateObject private var navigation = NavigationModel() @StateObject private var navigation = NavigationModel()
@StateObject private var networkState = NetworkStateModel() @StateObject private var networkState = NetworkStateModel()
@StateObject private var player = PlayerModel() @StateObject private var player = PlayerModel()
@StateObject private var playerControls = PlayerControlsModel()
@StateObject private var playlists = PlaylistsModel() @StateObject private var playlists = PlaylistsModel()
@StateObject private var recents = RecentsModel() @StateObject private var recents = RecentsModel()
@StateObject private var search = SearchModel() @StateObject private var search = SearchModel()
@ -49,6 +48,8 @@ struct YatteeApp: App {
let persistenceController = PersistenceController.shared let persistenceController = PersistenceController.shared
var playerControls: PlayerControlsModel { .shared }
var body: some Scene { var body: some Scene {
WindowGroup { WindowGroup {
ContentView() ContentView()