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
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()
}
}

View File

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

View File

@ -12,7 +12,7 @@ struct ContentView: View {
@EnvironmentObject<CommentsModel> private var comments
@EnvironmentObject<InstancesModel> private var instances
@EnvironmentObject<NavigationModel> private var navigation
@EnvironmentObject<PlayerControlsModel> private var playerControls
@EnvironmentObject<PlayerModel> private var player
@EnvironmentObject<PlaylistsModel> private var playlists
@EnvironmentObject<RecentsModel> private var recents
@EnvironmentObject<SearchModel> 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

View File

@ -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) {

View File

@ -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()
}
)
}

View File

@ -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 {

View File

@ -60,7 +60,6 @@ struct VideoPlayerView: View {
@EnvironmentObject<AccountsModel> internal var accounts
@EnvironmentObject<NavigationModel> internal var navigation
@EnvironmentObject<PlayerModel> internal var player
@EnvironmentObject<PlayerControlsModel> internal var playerControls
@EnvironmentObject<RecentsModel> internal var recents
#if os(macOS)
@EnvironmentObject<SearchModel> 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)

View File

@ -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()