1
0
mirror of https://github.com/yattee/yattee.git synced 2024-12-13 22:00:31 +05:30
yattee/iOS/Orientation.swift

57 lines
2.3 KiB
Swift
Raw Normal View History

import CoreMotion
import Defaults
import Logging
import UIKit
2024-02-02 04:21:41 +05:30
enum Orientation {
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 19:57:01 +05:30
guard let rotateOrientation else {
return
}
2022-07-10 03:59:13 +05:30
let orientationString = rotateOrientation == .portrait ? "portrait" : rotateOrientation == .landscapeLeft ? "landscapeLeft" :
rotateOrientation == .landscapeRight ? "landscapeRight" : rotateOrientation == .portraitUpsideDown ? "portraitUpsideDown" : "allButUpsideDown"
logger.info("rotating to \(orientationString)")
2022-08-26 13:55:07 +05:30
if #available(iOS 16, *) {
guard let windowScene = Self.scene else { return }
2022-08-26 13:55:07 +05:30
let rotateOrientationMask = rotateOrientation == .portrait ? UIInterfaceOrientationMask.portrait :
rotateOrientation == .landscapeLeft ? .landscapeLeft :
rotateOrientation == .landscapeRight ? .landscapeRight :
.allButUpsideDown
windowScene.requestGeometryUpdate(.iOS(interfaceOrientations: rotateOrientationMask)) { error in
print("denied rotation \(error)")
}
} else {
UIDevice.current.setValue(rotateOrientation.rawValue, forKey: "orientation")
2022-08-26 13:55:07 +05:30
}
UINavigationController.attemptRotationToDeviceOrientation()
}
private static var scene: UIWindowScene? {
UIApplication.shared.connectedScenes
.filter { $0.activationState == .foregroundActive }
.compactMap { $0 as? UIWindowScene }
.first
}
}