-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathUtils.swift
More file actions
102 lines (91 loc) · 2.92 KB
/
Utils.swift
File metadata and controls
102 lines (91 loc) · 2.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
//
// OrientationDirectorUtils.swift
// react-native-orientation-director
//
// Created by gladiuscode on 18/05/2024.
//
import Foundation
class Utils {
private static let TAG = "Utils"
// TODO: Add .unknown
public func convertToOrientationFrom(uiInterfaceOrientation: UIInterfaceOrientation) -> Orientation {
switch uiInterfaceOrientation {
case .landscapeRight:
return .LANDSCAPE_RIGHT
case .portraitUpsideDown:
return .PORTRAIT_UPSIDE_DOWN
case .landscapeLeft:
return .LANDSCAPE_LEFT
default:
return .PORTRAIT
}
}
public func convertToOrientationFrom(deviceOrientation: UIDeviceOrientation) -> Orientation {
switch deviceOrientation {
case .landscapeRight:
return .LANDSCAPE_RIGHT
case .portraitUpsideDown:
return .PORTRAIT_UPSIDE_DOWN
case .landscapeLeft:
return .LANDSCAPE_LEFT
case .faceUp:
return .FACE_UP
case .faceDown:
return .FACE_DOWN
default:
return .PORTRAIT
}
}
public func convertToOrientationFrom(jsValue: NSNumber) -> Orientation {
switch jsValue {
case 2:
return .LANDSCAPE_RIGHT
case 3:
return .PORTRAIT_UPSIDE_DOWN
case 4:
return .LANDSCAPE_LEFT
case 5:
return .LANDSCAPE
default:
return .PORTRAIT
}
}
/**
Note: .portraitUpsideDown only works for devices with home button and iPads
https://developer.apple.com/documentation/uikit/uiviewcontroller/1621435-supportedinterfaceorientations
*/
public func convertToMaskFrom(jsOrientation: Orientation) -> UIInterfaceOrientationMask {
switch jsOrientation {
case .PORTRAIT:
return .portrait
case .LANDSCAPE_RIGHT:
return .landscapeRight
case .PORTRAIT_UPSIDE_DOWN:
return .portraitUpsideDown
case .LANDSCAPE_LEFT:
return .landscapeLeft
case .LANDSCAPE:
return .landscape
default:
return .all
}
}
public func getInterfaceOrientation() -> UIInterfaceOrientation {
guard let windowScene = self.getCurrentWindow()?.windowScene else {
return UIInterfaceOrientation.unknown
}
return windowScene.interfaceOrientation
}
/* This function is needed to get the current available window.
Here in React Native we should have only one window tho.
https://stackoverflow.com/questions/57134259/how-to-resolve-keywindow-was-deprecated-in-ios-13-0/58031897#58031897
*/
public func getCurrentWindow() -> UIWindow? {
return UIApplication
.shared
.connectedScenes
.compactMap { $0 as? UIWindowScene }
.flatMap { $0.windows }
.last { $0.isKeyWindow }
}
}