mirror of
https://github.com/yattee/yattee.git
synced 2024-11-14 01:58:24 +00:00
322a550666
- iPad: rotate to device orientation on startup - fixed controls in portrait fullscreen - iOS: don’t call setNeedsDrawing multiple times - On iOS we call set needs drawing only once. - Added cooldown time to MPV.Client setNeedsDrawing to avoid multiple successive calls - make fullscreen animation smoother - dragGesture now calls toggleFullScreenAction - fix tvOS and macOS build Signed-off-by: Toni Förster <toni.foerster@gmail.com>
88 lines
4.1 KiB
Swift
88 lines
4.1 KiB
Swift
import Defaults
|
|
import Foundation
|
|
import Logging
|
|
import Repeat
|
|
import SwiftUI
|
|
|
|
final class OrientationModel {
|
|
static var shared = OrientationModel()
|
|
let logger = Logger(label: "stream.yattee.orientation.model")
|
|
|
|
var orientation = UIInterfaceOrientation.portrait
|
|
var lastOrientation: UIInterfaceOrientation?
|
|
var orientationDebouncer = Debouncer(.milliseconds(300))
|
|
var orientationObserver: Any?
|
|
|
|
private var player = PlayerModel.shared
|
|
|
|
func startOrientationUpdates() {
|
|
// Ensure the orientation observer is active
|
|
orientationObserver = NotificationCenter.default.addObserver(
|
|
forName: OrientationTracker.deviceOrientationChangedNotification,
|
|
object: nil,
|
|
queue: .main
|
|
) { _ in
|
|
self.logger.info("Notification received: Device orientation changed.")
|
|
|
|
// We only allow .portrait and are not showing the player
|
|
guard (!self.player.presentingPlayer && !Defaults[.lockPortraitWhenBrowsing]) || self.player.presentingPlayer
|
|
else {
|
|
return
|
|
}
|
|
|
|
let orientation = OrientationTracker.shared.currentInterfaceOrientation
|
|
self.logger.info("Current interface orientation: \(orientation)")
|
|
|
|
// Always update lastOrientation to keep track of the latest state
|
|
if self.lastOrientation != orientation {
|
|
self.lastOrientation = orientation
|
|
self.logger.info("Orientation changed to: \(orientation)")
|
|
} else {
|
|
self.logger.info("Orientation has not changed.")
|
|
}
|
|
|
|
// Only take action if the player is active and presenting
|
|
guard (!self.player.isOrientationLocked && !self.player.playingInPictureInPicture) || (!Defaults[.lockPortraitWhenBrowsing] && !self.player.presentingPlayer) || (!Defaults[.lockPortraitWhenBrowsing] && self.player.presentingPlayer && !self.player.isOrientationLocked)
|
|
else {
|
|
self.logger.info("Only updating orientation without actions.")
|
|
return
|
|
}
|
|
|
|
DispatchQueue.main.async {
|
|
self.orientationDebouncer.callback = {
|
|
DispatchQueue.main.async {
|
|
if orientation.isLandscape {
|
|
if Defaults[.enterFullscreenInLandscape], self.player.presentingPlayer {
|
|
self.logger.info("Entering fullscreen because orientation is landscape.")
|
|
self.player.controls.presentingControls = false
|
|
self.player.enterFullScreen(showControls: false)
|
|
}
|
|
Orientation.lockOrientation(OrientationTracker.shared.currentInterfaceOrientationMask, andRotateTo: orientation)
|
|
} else {
|
|
self.logger.info("Exiting fullscreen because orientation is portrait.")
|
|
if self.player.playingFullScreen {
|
|
self.player.exitFullScreen(showControls: false)
|
|
}
|
|
if Defaults[.lockPortraitWhenBrowsing] {
|
|
Orientation.lockOrientation(.portrait, andRotateTo: .portrait)
|
|
} else {
|
|
Orientation.lockOrientation(OrientationTracker.shared.currentInterfaceOrientationMask, andRotateTo: orientation)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
self.orientationDebouncer.call()
|
|
}
|
|
}
|
|
}
|
|
|
|
func lockOrientation(_ orientation: UIInterfaceOrientationMask, andRotateTo rotateOrientation: UIInterfaceOrientation? = nil) {
|
|
logger.info("Locking orientation to: \(orientation), rotating to: \(String(describing: rotateOrientation)).")
|
|
if let rotateOrientation {
|
|
self.orientation = rotateOrientation
|
|
lastOrientation = rotateOrientation
|
|
}
|
|
Orientation.lockOrientation(orientation, andRotateTo: rotateOrientation)
|
|
}
|
|
}
|