mirror of
https://github.com/yattee/yattee.git
synced 2025-10-09 17:08:15 +00:00
@@ -31,7 +31,7 @@ struct Orientation {
|
||||
logger.info("rotating to \(orientationString)")
|
||||
|
||||
if #available(iOS 16, *) {
|
||||
guard let windowScene = SafeArea.scene else { return }
|
||||
guard let windowScene = Self.scene else { return }
|
||||
let rotateOrientationMask = rotateOrientation == .portrait ? UIInterfaceOrientationMask.portrait :
|
||||
rotateOrientation == .landscapeLeft ? .landscapeLeft :
|
||||
rotateOrientation == .landscapeRight ? .landscapeRight :
|
||||
@@ -46,4 +46,11 @@ struct Orientation {
|
||||
|
||||
UINavigationController.attemptRotationToDeviceOrientation()
|
||||
}
|
||||
|
||||
private static var scene: UIWindowScene? {
|
||||
UIApplication.shared.connectedScenes
|
||||
.filter { $0.activationState == .foregroundActive }
|
||||
.compactMap { $0 as? UIWindowScene }
|
||||
.first
|
||||
}
|
||||
}
|
||||
|
95
iOS/OrientationModel.swift
Normal file
95
iOS/OrientationModel.swift
Normal file
@@ -0,0 +1,95 @@
|
||||
import Defaults
|
||||
import Foundation
|
||||
import Repeat
|
||||
import SwiftUI
|
||||
|
||||
final class OrientationModel {
|
||||
static var shared = OrientationModel()
|
||||
|
||||
var orientation = UIInterfaceOrientation.portrait
|
||||
var lastOrientation: UIInterfaceOrientation?
|
||||
var orientationDebouncer = Debouncer(.milliseconds(300))
|
||||
internal var orientationObserver: Any?
|
||||
|
||||
private var player = PlayerModel.shared
|
||||
|
||||
func configureOrientationUpdatesBasedOnAccelerometer() {
|
||||
let currentOrientation = OrientationTracker.shared.currentInterfaceOrientation
|
||||
if currentOrientation.isLandscape,
|
||||
Defaults[.enterFullscreenInLandscape],
|
||||
!Defaults[.honorSystemOrientationLock],
|
||||
!player.playingFullScreen,
|
||||
!player.currentItem.isNil,
|
||||
player.lockedOrientation.isNil || player.lockedOrientation!.contains(.landscape),
|
||||
!player.playingInPictureInPicture,
|
||||
player.presentingPlayer
|
||||
{
|
||||
DispatchQueue.main.async {
|
||||
self.player.controls.presentingControls = false
|
||||
self.player.enterFullScreen(showControls: false)
|
||||
}
|
||||
|
||||
player.onPresentPlayer.append {
|
||||
Orientation.lockOrientation(.allButUpsideDown, andRotateTo: currentOrientation)
|
||||
}
|
||||
}
|
||||
|
||||
orientationObserver = NotificationCenter.default.addObserver(
|
||||
forName: OrientationTracker.deviceOrientationChangedNotification,
|
||||
object: nil,
|
||||
queue: .main
|
||||
) { _ in
|
||||
guard !Defaults[.honorSystemOrientationLock],
|
||||
self.player.presentingPlayer,
|
||||
!self.player.playingInPictureInPicture,
|
||||
self.player.lockedOrientation.isNil
|
||||
else {
|
||||
return
|
||||
}
|
||||
|
||||
let orientation = OrientationTracker.shared.currentInterfaceOrientation
|
||||
|
||||
guard self.lastOrientation != orientation else {
|
||||
return
|
||||
}
|
||||
|
||||
self.lastOrientation = orientation
|
||||
|
||||
DispatchQueue.main.async {
|
||||
guard Defaults[.enterFullscreenInLandscape],
|
||||
self.player.presentingPlayer
|
||||
else {
|
||||
return
|
||||
}
|
||||
|
||||
self.orientationDebouncer.callback = {
|
||||
DispatchQueue.main.async {
|
||||
if orientation.isLandscape {
|
||||
self.player.controls.presentingControls = false
|
||||
self.player.enterFullScreen(showControls: false)
|
||||
Orientation.lockOrientation(OrientationTracker.shared.currentInterfaceOrientationMask, andRotateTo: orientation)
|
||||
} else {
|
||||
self.player.exitFullScreen(showControls: false)
|
||||
Orientation.lockOrientation(.allButUpsideDown, andRotateTo: .portrait)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
self.orientationDebouncer.call()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func stopOrientationUpdates() {
|
||||
guard let observer = orientationObserver else { return }
|
||||
NotificationCenter.default.removeObserver(observer)
|
||||
}
|
||||
|
||||
func lockOrientation(_ orientation: UIInterfaceOrientationMask, andRotateTo rotateOrientation: UIInterfaceOrientation? = nil) {
|
||||
if let rotateOrientation {
|
||||
self.orientation = rotateOrientation
|
||||
lastOrientation = rotateOrientation
|
||||
}
|
||||
Orientation.lockOrientation(orientation, andRotateTo: rotateOrientation)
|
||||
}
|
||||
}
|
7
iOS/SafeAreaModel.swift
Normal file
7
iOS/SafeAreaModel.swift
Normal file
@@ -0,0 +1,7 @@
|
||||
import Foundation
|
||||
import SwiftUI
|
||||
|
||||
final class SafeAreaModel: ObservableObject {
|
||||
static var shared = SafeAreaModel()
|
||||
@Published var safeArea = EdgeInsets()
|
||||
}
|
Reference in New Issue
Block a user