-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathOrientationDirectorImpl.swift
More file actions
199 lines (158 loc) · 7.79 KB
/
OrientationDirectorImpl.swift
File metadata and controls
199 lines (158 loc) · 7.79 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
//
// OrientationDirectorImpl.swift
// react-native-orientation-director
//
// Created by gladiuscode on 18/05/2024.
//
import Foundation
import UIKit
@objc public class OrientationDirectorImpl: NSObject {
private static let TAG = "OrientationDirectorImpl"
private let bundleManager: BundleManager = BundleManager()
private let utils: Utils = Utils()
private let sensorListener: SensorListener = SensorListener()
private let eventManager: EventManager = EventManager()
private var initialSupportedInterfaceOrientations: UIInterfaceOrientationMask = UIInterfaceOrientationMask.all
private var lastInterfaceOrientation = Orientation.UNKNOWN
private var lastDeviceOrientation = Orientation.UNKNOWN
private var isLocked = false
@objc public var supportedInterfaceOrientations: UIInterfaceOrientationMask = UIInterfaceOrientationMask.all
@objc public override init() {
super.init()
sensorListener.setOnOrientationDidChange(callback: self.onOrientationChanged)
initialSupportedInterfaceOrientations = initInitialSupportedInterfaceOrientations()
lastInterfaceOrientation = initInterfaceOrientation()
lastDeviceOrientation = initDeviceOrientation()
isLocked = initIsLocked()
supportedInterfaceOrientations = initialSupportedInterfaceOrientations
}
///////////////////////////////////////////////////////////////////////////////////////
/// EVENT EMITTER SETUP
@objc public func setEventManager(delegate: OrientationEventEmitterDelegate) {
self.eventManager.delegate = delegate
}
///
//////////////////////////////////////////////////////////////////////////////////////////
@objc public func getInterfaceOrientation() -> Orientation {
return lastInterfaceOrientation
}
@objc public func getDeviceOrientation() -> Orientation {
return lastDeviceOrientation
}
@objc public func getIsLocked() -> Bool {
return isLocked
}
@objc public func lockTo(jsValue: NSNumber) {
let jsOrientation = utils.convertToOrientationFrom(jsValue: jsValue)
let mask = utils.convertToMaskFrom(jsOrientation: jsOrientation)
self.requestInterfaceUpdateTo(mask: mask)
updateIsLockedTo(value: true)
let orientationCanBeUpdatedDirectly = jsOrientation != Orientation.LANDSCAPE
if orientationCanBeUpdatedDirectly {
updateLastInterfaceOrientationTo(value: jsOrientation)
return
}
let lastInterfaceOrientationIsAlreadyInLandscape = lastInterfaceOrientation == Orientation.LANDSCAPE_RIGHT || lastInterfaceOrientation == Orientation.LANDSCAPE_LEFT
if lastInterfaceOrientationIsAlreadyInLandscape {
updateLastInterfaceOrientationTo(value: lastInterfaceOrientation)
return
}
let systemDefaultLandscapeOrientation = Orientation.LANDSCAPE_RIGHT
updateLastInterfaceOrientationTo(value: systemDefaultLandscapeOrientation)
}
@objc public func unlock() {
self.requestInterfaceUpdateTo(mask: UIInterfaceOrientationMask.all)
updateIsLockedTo(value: false)
self.adaptInterfaceTo(deviceOrientation: lastDeviceOrientation)
}
@objc public func resetSupportedInterfaceOrientations() {
self.supportedInterfaceOrientations = self.initialSupportedInterfaceOrientations
self.requestInterfaceUpdateTo(mask: self.supportedInterfaceOrientations)
self.updateIsLockedTo(value: self.initIsLocked())
let lastMask = utils.convertToMaskFrom(jsOrientation: lastInterfaceOrientation)
let isLastMaskSupported = self.supportedInterfaceOrientations.contains(lastMask)
if isLastMaskSupported {
return
}
let supportedInterfaceOrientations = bundleManager.getSupportedInterfaceOrientations()
if supportedInterfaceOrientations.contains(UIInterfaceOrientationMask.portrait) {
self.updateLastInterfaceOrientationTo(value: Orientation.PORTRAIT)
return
}
if supportedInterfaceOrientations.contains(UIInterfaceOrientationMask.landscapeRight) {
self.updateLastInterfaceOrientationTo(value: Orientation.LANDSCAPE_RIGHT)
return
}
if supportedInterfaceOrientations.contains(UIInterfaceOrientationMask.landscapeLeft) {
self.updateLastInterfaceOrientationTo(value: Orientation.LANDSCAPE_LEFT)
return
}
self.updateLastInterfaceOrientationTo(value: Orientation.PORTRAIT_UPSIDE_DOWN)
}
private func initInitialSupportedInterfaceOrientations() -> UIInterfaceOrientationMask {
let supportedInterfaceOrientations = bundleManager.getSupportedInterfaceOrientations()
return supportedInterfaceOrientations.reduce(UIInterfaceOrientationMask()) { $0.union($1) }
}
private func initInterfaceOrientation() -> Orientation {
return self.getOrientationFromInterface()
}
private func initDeviceOrientation() -> Orientation {
return utils.convertToOrientationFrom(deviceOrientation: UIDevice.current.orientation)
}
private func initIsLocked() -> Bool {
let supportedOrientations = bundleManager.getSupportedInterfaceOrientations()
if supportedOrientations.count > 1 {
return false
}
return supportedOrientations.first != UIInterfaceOrientationMask.all
}
private func requestInterfaceUpdateTo(mask: UIInterfaceOrientationMask) {
self.supportedInterfaceOrientations = mask
if #available(iOS 16.0, *) {
let window = utils.getCurrentWindow()
guard let rootViewController = window?.rootViewController else {
return
}
guard let windowScene = window?.windowScene else {
return
}
rootViewController.setNeedsUpdateOfSupportedInterfaceOrientations()
windowScene.requestGeometryUpdate(.iOS(interfaceOrientations: mask)) { error in
print("\(OrientationDirectorImpl.TAG) - requestGeometryUpdate error", error)
}
} else {
let interfaceOrientation = self.utils.convertToInterfaceOrientationFrom(mask: mask)
UIDevice.current.setValue(interfaceOrientation.rawValue, forKey: "orientation")
UIViewController.attemptRotationToDeviceOrientation()
}
}
private func onOrientationChanged(uiDeviceOrientation: UIDeviceOrientation) {
let deviceOrientation = utils.convertToOrientationFrom(deviceOrientation: uiDeviceOrientation)
self.eventManager.sendDeviceOrientationDidChange(orientationValue: deviceOrientation.rawValue)
lastDeviceOrientation = deviceOrientation
adaptInterfaceTo(deviceOrientation: deviceOrientation)
}
private func adaptInterfaceTo(deviceOrientation: Orientation) {
let supportsLandscape = self.supportedInterfaceOrientations.contains(.landscape)
if isLocked && !supportsLandscape {
return
}
if deviceOrientation == Orientation.FACE_UP || deviceOrientation == Orientation.FACE_DOWN {
return
}
let newInterfaceOrientation = self.getOrientationFromInterface()
updateLastInterfaceOrientationTo(value: newInterfaceOrientation)
}
private func updateIsLockedTo(value: Bool) {
eventManager.sendLockDidChange(value: value)
isLocked = value
}
private func updateLastInterfaceOrientationTo(value: Orientation) {
self.eventManager.sendInterfaceOrientationDidChange(orientationValue: value.rawValue)
lastInterfaceOrientation = value
}
private func getOrientationFromInterface() -> Orientation {
let interfaceOrientation = utils.getInterfaceOrientation()
return utils.convertToOrientationFrom(uiInterfaceOrientation: interfaceOrientation)
}
}