2022-01-02 19:41:04 +00:00
|
|
|
import CoreMotion
|
|
|
|
import Logging
|
|
|
|
import UIKit
|
|
|
|
|
2024-02-01 22:51:41 +00:00
|
|
|
enum Orientation {
|
2022-01-02 19:41:04 +00:00
|
|
|
static var logger = Logger(label: "stream.yattee.orientation")
|
|
|
|
|
|
|
|
static func lockOrientation(_ orientation: UIInterfaceOrientationMask) {
|
|
|
|
if let delegate = AppDelegate.instance {
|
|
|
|
delegate.orientationLock = orientation
|
|
|
|
|
|
|
|
let orientationString = orientation == .portrait ? "portrait" : orientation == .landscapeLeft ? "landscapeLeft" :
|
|
|
|
orientation == .landscapeRight ? "landscapeRight" : orientation == .portraitUpsideDown ? "portraitUpsideDown" :
|
|
|
|
orientation == .landscape ? "landscape" : orientation == .all ? "all" : "allButUpsideDown"
|
|
|
|
|
|
|
|
logger.info("locking \(orientationString)")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static func lockOrientation(_ orientation: UIInterfaceOrientationMask, andRotateTo rotateOrientation: UIInterfaceOrientation? = nil) {
|
|
|
|
lockOrientation(orientation)
|
|
|
|
|
2022-09-28 14:27:01 +00:00
|
|
|
guard let rotateOrientation else {
|
2022-01-02 19:41:04 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-07-09 22:29:13 +00:00
|
|
|
let orientationString = rotateOrientation == .portrait ? "portrait" : rotateOrientation == .landscapeLeft ? "landscapeLeft" :
|
|
|
|
rotateOrientation == .landscapeRight ? "landscapeRight" : rotateOrientation == .portraitUpsideDown ? "portraitUpsideDown" : "allButUpsideDown"
|
|
|
|
|
|
|
|
logger.info("rotating to \(orientationString)")
|
|
|
|
|
2022-08-26 08:25:07 +00:00
|
|
|
if #available(iOS 16, *) {
|
2023-05-20 14:04:58 +00:00
|
|
|
guard let windowScene = Self.scene else { return }
|
2022-08-26 08:25:07 +00:00
|
|
|
let rotateOrientationMask = rotateOrientation == .portrait ? UIInterfaceOrientationMask.portrait :
|
|
|
|
rotateOrientation == .landscapeLeft ? .landscapeLeft :
|
|
|
|
rotateOrientation == .landscapeRight ? .landscapeRight :
|
2024-09-06 14:47:03 +00:00
|
|
|
.all
|
2022-08-26 08:25:07 +00:00
|
|
|
|
|
|
|
windowScene.requestGeometryUpdate(.iOS(interfaceOrientations: rotateOrientationMask)) { error in
|
|
|
|
print("denied rotation \(error)")
|
|
|
|
}
|
2022-11-11 13:25:56 +00:00
|
|
|
} else {
|
|
|
|
UIDevice.current.setValue(rotateOrientation.rawValue, forKey: "orientation")
|
2022-08-26 08:25:07 +00:00
|
|
|
}
|
2022-11-11 13:25:56 +00:00
|
|
|
|
|
|
|
UINavigationController.attemptRotationToDeviceOrientation()
|
2022-01-02 19:41:04 +00:00
|
|
|
}
|
2023-05-20 14:04:58 +00:00
|
|
|
|
|
|
|
private static var scene: UIWindowScene? {
|
|
|
|
UIApplication.shared.connectedScenes
|
|
|
|
.filter { $0.activationState == .foregroundActive }
|
|
|
|
.compactMap { $0 as? UIWindowScene }
|
|
|
|
.first
|
|
|
|
}
|
2022-01-02 19:41:04 +00:00
|
|
|
}
|