2022-08-28 22:48:49 +05:30
|
|
|
import Defaults
|
|
|
|
import Foundation
|
2023-05-20 19:34:58 +05:30
|
|
|
import Repeat
|
2022-08-28 22:48:49 +05:30
|
|
|
import SwiftUI
|
|
|
|
|
2023-05-20 19:34:58 +05:30
|
|
|
final class OrientationModel {
|
|
|
|
static var shared = OrientationModel()
|
|
|
|
|
|
|
|
var orientation = UIInterfaceOrientation.portrait
|
|
|
|
var lastOrientation: UIInterfaceOrientation?
|
|
|
|
var orientationDebouncer = Debouncer(.milliseconds(300))
|
2023-09-23 18:37:27 +05:30
|
|
|
var orientationObserver: Any?
|
2023-05-20 19:34:58 +05:30
|
|
|
|
|
|
|
private var player = PlayerModel.shared
|
|
|
|
|
2022-08-28 22:48:49 +05:30
|
|
|
func configureOrientationUpdatesBasedOnAccelerometer() {
|
|
|
|
let currentOrientation = OrientationTracker.shared.currentInterfaceOrientation
|
|
|
|
if currentOrientation.isLandscape,
|
|
|
|
Defaults[.enterFullscreenInLandscape],
|
2022-11-13 17:45:44 +05:30
|
|
|
!Defaults[.honorSystemOrientationLock],
|
2022-08-28 22:48:49 +05:30
|
|
|
!player.playingFullScreen,
|
2022-11-13 17:45:44 +05:30
|
|
|
!player.currentItem.isNil,
|
|
|
|
player.lockedOrientation.isNil || player.lockedOrientation!.contains(.landscape),
|
|
|
|
!player.playingInPictureInPicture,
|
|
|
|
player.presentingPlayer
|
2022-08-28 22:48:49 +05:30
|
|
|
{
|
|
|
|
DispatchQueue.main.async {
|
2023-05-20 19:34:58 +05:30
|
|
|
self.player.controls.presentingControls = false
|
|
|
|
self.player.enterFullScreen(showControls: false)
|
2022-08-28 22:48:49 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
player.onPresentPlayer.append {
|
|
|
|
Orientation.lockOrientation(.allButUpsideDown, andRotateTo: currentOrientation)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
orientationObserver = NotificationCenter.default.addObserver(
|
|
|
|
forName: OrientationTracker.deviceOrientationChangedNotification,
|
|
|
|
object: nil,
|
|
|
|
queue: .main
|
|
|
|
) { _ in
|
2022-11-13 17:19:39 +05:30
|
|
|
guard !Defaults[.honorSystemOrientationLock],
|
2023-05-20 19:34:58 +05:30
|
|
|
self.player.presentingPlayer,
|
|
|
|
!self.player.playingInPictureInPicture,
|
|
|
|
self.player.lockedOrientation.isNil
|
2022-08-28 22:48:49 +05:30
|
|
|
else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
let orientation = OrientationTracker.shared.currentInterfaceOrientation
|
|
|
|
|
2023-05-20 19:34:58 +05:30
|
|
|
guard self.lastOrientation != orientation else {
|
2022-08-28 22:48:49 +05:30
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-05-20 19:34:58 +05:30
|
|
|
self.lastOrientation = orientation
|
2022-08-28 22:48:49 +05:30
|
|
|
|
|
|
|
DispatchQueue.main.async {
|
|
|
|
guard Defaults[.enterFullscreenInLandscape],
|
2023-05-20 19:34:58 +05:30
|
|
|
self.player.presentingPlayer
|
2022-08-28 22:48:49 +05:30
|
|
|
else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-05-20 19:34:58 +05:30
|
|
|
self.orientationDebouncer.callback = {
|
2022-09-01 00:54:46 +05:30
|
|
|
DispatchQueue.main.async {
|
|
|
|
if orientation.isLandscape {
|
2023-05-20 19:34:58 +05:30
|
|
|
self.player.controls.presentingControls = false
|
|
|
|
self.player.enterFullScreen(showControls: false)
|
2022-09-01 00:54:46 +05:30
|
|
|
Orientation.lockOrientation(OrientationTracker.shared.currentInterfaceOrientationMask, andRotateTo: orientation)
|
|
|
|
} else {
|
2023-05-20 19:34:58 +05:30
|
|
|
self.player.exitFullScreen(showControls: false)
|
2022-09-01 00:54:46 +05:30
|
|
|
Orientation.lockOrientation(.allButUpsideDown, andRotateTo: .portrait)
|
|
|
|
}
|
|
|
|
}
|
2022-08-28 22:48:49 +05:30
|
|
|
}
|
2022-09-01 00:54:46 +05:30
|
|
|
|
2023-05-20 19:34:58 +05:30
|
|
|
self.orientationDebouncer.call()
|
2022-08-28 22:48:49 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func stopOrientationUpdates() {
|
|
|
|
guard let observer = orientationObserver else { return }
|
|
|
|
NotificationCenter.default.removeObserver(observer)
|
|
|
|
}
|
2023-05-20 19:34:58 +05:30
|
|
|
|
|
|
|
func lockOrientation(_ orientation: UIInterfaceOrientationMask, andRotateTo rotateOrientation: UIInterfaceOrientation? = nil) {
|
|
|
|
if let rotateOrientation {
|
|
|
|
self.orientation = rotateOrientation
|
|
|
|
lastOrientation = rotateOrientation
|
|
|
|
}
|
|
|
|
Orientation.lockOrientation(orientation, andRotateTo: rotateOrientation)
|
|
|
|
}
|
2022-08-28 22:48:49 +05:30
|
|
|
}
|