react-native-orientation-director 1.2.1 → 1.2.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -22,6 +22,8 @@ This library takes inspiration from and builds upon the following amazing altern
22
22
  - [x] Listen to interface orientation status changes
23
23
  - [x] Lock the interface orientation to a specific orientation
24
24
  - [x] Unlock the interface orientation
25
+ - [x] Reset supported interface orientations to settings
26
+ - [x] Check if autorotation is enabled (Android only)
25
27
 
26
28
  ## Installation
27
29
 
@@ -65,11 +67,13 @@ This library exports a class called: [RNOrientationDirector](https://github.com/
65
67
  | lockTo | Locks the interface to a specific orientation |
66
68
  | unlock | Unlock the interface |
67
69
  | isLocked | Returns the current interface orientation status (locked / unlocked) |
70
+ | isAutoRotationEnabled | (Android Only) Returns if auto rotation is enabled |
68
71
  | listenForDeviceOrientationChanges | Triggers a provided callback each time the device orientation changes |
69
72
  | listenForInterfaceOrientationChanges | Triggers a provided callback each time the interface orientation changes |
70
73
  | listenForLockChanges | Triggers a provided callback each time the interface orientation status changes |
71
74
  | convertOrientationToHumanReadableString | Returns a human readable string based on the given orientation |
72
75
  | setLocalizedStringProvider | Sets the mapping needed to convert orientation values to human readable strings |
76
+ | resetSupportedInterfaceOrientations | Resets the supported interface orientations to settings |
73
77
 
74
78
  In addition, the library exposes the following hooks:
75
79
 
@@ -3,10 +3,9 @@ package com.orientationdirector.implementation
3
3
  import android.database.ContentObserver
4
4
  import android.os.Handler
5
5
  import android.provider.Settings
6
- import android.util.Log
7
6
  import com.facebook.react.bridge.ReactContext
8
7
 
9
- class OrientationAutoRotationObserver(val context: ReactContext, handler: Handler?) : ContentObserver(handler) {
8
+ class AutoRotationObserver(private val context: ReactContext, handler: Handler?) : ContentObserver(handler) {
10
9
  private var lastAutoRotationStatus: Boolean = isAutoRotationEnabled()
11
10
 
12
11
  fun getLastAutoRotationStatus(): Boolean {
@@ -12,7 +12,7 @@ enum class Event {
12
12
  LockDidChange,
13
13
  }
14
14
 
15
- class OrientationEventManager(private val context: ReactApplicationContext) {
15
+ class EventManager(private val context: ReactApplicationContext) {
16
16
 
17
17
  fun sendDeviceOrientationDidChange(orientationValue: Int) {
18
18
  val params = Arguments.createMap().apply {
@@ -36,15 +36,13 @@ class OrientationEventManager(private val context: ReactApplicationContext) {
36
36
  }
37
37
 
38
38
  private fun sendEvent(eventName: Event, params: WritableMap?) {
39
- Log.d(NAME, "sendEvent - $eventName")
40
- Log.d(NAME, "sendEvent - $params")
41
39
  context
42
40
  .getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter::class.java)
43
41
  .emit(eventName.name, params)
44
42
  }
45
43
 
46
44
  companion object {
47
- val NAME = "OrientationEventManager"
45
+ const val NAME = "OrientationEventManager"
48
46
  }
49
47
 
50
48
  }
@@ -2,7 +2,7 @@ package com.orientationdirector.implementation
2
2
 
3
3
  import com.facebook.react.bridge.LifecycleEventListener
4
4
 
5
- class OrientationLifecycleListener() : LifecycleEventListener {
5
+ class LifecycleListener() : LifecycleEventListener {
6
6
 
7
7
  private var onHostResumeCallback: (() -> Unit)? = null
8
8
  private var onHostPauseCallback: (() -> Unit)? = null
@@ -12,7 +12,7 @@ class OrientationLifecycleListener() : LifecycleEventListener {
12
12
  this.onHostResumeCallback = callback
13
13
  }
14
14
  fun setOnHostPauseCallback(callback: () -> Unit) {
15
- this.onHostResumeCallback = callback
15
+ this.onHostPauseCallback = callback
16
16
  }
17
17
  fun setOnHostDestroyCallback(callback: () -> Unit) {
18
18
  this.onHostDestroyCallback = callback
@@ -3,18 +3,19 @@ package com.orientationdirector.implementation
3
3
  import android.content.pm.ActivityInfo
4
4
  import android.os.Handler
5
5
  import android.os.Looper
6
+ import android.view.OrientationEventListener.ORIENTATION_UNKNOWN
6
7
  import com.facebook.react.bridge.ReactApplicationContext
7
8
 
8
9
  class OrientationDirectorImpl internal constructor(private val context: ReactApplicationContext) {
9
- private var mUtils = OrientationDirectorUtilsImpl(context)
10
- private var mEventEmitter = OrientationEventManager(context)
11
- private var mSensorListener = OrientationSensorListener(context)
12
- private var mAutoRotationObserver = OrientationAutoRotationObserver(
10
+ private var mUtils = Utils(context)
11
+ private var mEventManager = EventManager(context)
12
+ private var mSensorListener = SensorListener(context)
13
+ private var mAutoRotationObserver = AutoRotationObserver(
13
14
  context, Handler(
14
15
  Looper.getMainLooper()
15
16
  )
16
17
  )
17
- private var mLifecycleListener = OrientationLifecycleListener()
18
+ private var mLifecycleListener = LifecycleListener()
18
19
 
19
20
  private var initialSupportedInterfaceOrientations = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED
20
21
  private var lastInterfaceOrientation = Orientation.UNKNOWN
@@ -79,10 +80,10 @@ class OrientationDirectorImpl internal constructor(private val context: ReactApp
79
80
  return mAutoRotationObserver.getLastAutoRotationStatus()
80
81
  }
81
82
 
82
- fun lockTo(rawJsOrientation: Int) {
83
- val jsOrientation = mUtils.getOrientationFromJsOrientation(rawJsOrientation)
83
+ fun lockTo(jsValue: Int) {
84
+ val jsOrientation = mUtils.convertToOrientationFromJsValue(jsValue)
84
85
  val screenOrientation =
85
- mUtils.getActivityOrientationFrom(jsOrientation)
86
+ mUtils.convertToActivityOrientationFrom(jsOrientation)
86
87
  context.currentActivity?.requestedOrientation = screenOrientation
87
88
 
88
89
  updateIsLockedTo(true)
@@ -104,14 +105,14 @@ class OrientationDirectorImpl internal constructor(private val context: ReactApp
104
105
 
105
106
  private fun initInterfaceOrientation(): Orientation {
106
107
  val rotation = mUtils.getInterfaceRotation()
107
- return mUtils.getOrientationFromRotation(rotation)
108
+ return mUtils.convertToOrientationFromScreenRotation(rotation)
108
109
  }
109
110
 
110
111
  private fun initDeviceOrientation(): Orientation {
111
112
  val lastRotationDetected = mSensorListener.getLastRotationDetected()
112
113
  ?: return Orientation.UNKNOWN
113
114
 
114
- return mUtils.getDeviceOrientationFrom(lastRotationDetected)
115
+ return mUtils.convertToDeviceOrientationFrom(lastRotationDetected)
115
116
  }
116
117
 
117
118
  private fun initIsLocked(): Boolean {
@@ -126,9 +127,19 @@ class OrientationDirectorImpl internal constructor(private val context: ReactApp
126
127
  }
127
128
 
128
129
  private fun onOrientationChanged(rawDeviceOrientation: Int) {
129
- val deviceOrientation = mUtils.getDeviceOrientationFrom(rawDeviceOrientation)
130
- mEventEmitter.sendDeviceOrientationDidChange(deviceOrientation.ordinal)
130
+ val deviceOrientation = mUtils.convertToDeviceOrientationFrom(rawDeviceOrientation)
131
+
132
+ if (rawDeviceOrientation != ORIENTATION_UNKNOWN && deviceOrientation == Orientation.UNKNOWN) {
133
+ return;
134
+ }
135
+
136
+ if (lastDeviceOrientation == deviceOrientation) {
137
+ return
138
+ }
139
+
140
+ mEventManager.sendDeviceOrientationDidChange(deviceOrientation.ordinal)
131
141
  lastDeviceOrientation = deviceOrientation
142
+
132
143
  adaptInterfaceTo(deviceOrientation)
133
144
  }
134
145
 
@@ -141,7 +152,7 @@ class OrientationDirectorImpl internal constructor(private val context: ReactApp
141
152
  return
142
153
  }
143
154
 
144
- val newInterfaceOrientation = mUtils.getInterfaceOrientationFromDeviceOrientation(deviceOrientation);
155
+ val newInterfaceOrientation = mUtils.convertToInterfaceOrientationFrom(deviceOrientation);
145
156
  if (newInterfaceOrientation == lastInterfaceOrientation) {
146
157
  return
147
158
  }
@@ -150,13 +161,13 @@ class OrientationDirectorImpl internal constructor(private val context: ReactApp
150
161
  }
151
162
 
152
163
  private fun updateIsLockedTo(value: Boolean) {
153
- mEventEmitter.sendLockDidChange(value)
164
+ mEventManager.sendLockDidChange(value)
154
165
  isLocked = value
155
166
  }
156
167
 
157
168
  private fun updateLastInterfaceOrientationTo(value: Orientation) {
158
169
  lastInterfaceOrientation = value
159
- mEventEmitter.sendInterfaceOrientationDidChange(value.ordinal)
170
+ mEventManager.sendInterfaceOrientationDidChange(value.ordinal)
160
171
  }
161
172
 
162
173
  companion object {
@@ -4,7 +4,7 @@ import android.hardware.SensorManager
4
4
  import android.view.OrientationEventListener
5
5
  import com.facebook.react.bridge.ReactApplicationContext
6
6
 
7
- class OrientationSensorListener(
7
+ class SensorListener(
8
8
  context: ReactApplicationContext,
9
9
  ) : OrientationEventListener(context, SensorManager.SENSOR_DELAY_UI) {
10
10
 
@@ -3,12 +3,11 @@ package com.orientationdirector.implementation
3
3
  import android.content.Context
4
4
  import android.content.pm.ActivityInfo
5
5
  import android.os.Build
6
- import android.provider.Settings
7
6
  import android.view.Surface
8
7
  import android.view.WindowManager
9
8
  import com.facebook.react.bridge.ReactContext
10
9
 
11
- class OrientationDirectorUtilsImpl(val context: ReactContext) {
10
+ class Utils(private val context: ReactContext) {
12
11
 
13
12
  fun getInterfaceRotation(): Int {
14
13
  return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
@@ -20,26 +19,24 @@ class OrientationDirectorUtilsImpl(val context: ReactContext) {
20
19
  }
21
20
  }
22
21
 
23
- fun getDeviceOrientationFrom(rotation: Int): Orientation {
24
- var orientation = Orientation.UNKNOWN
25
-
26
- if (rotation == -1) {
27
- orientation = Orientation.UNKNOWN
28
- } else if (rotation > 355 || rotation < 5) {
29
- orientation = Orientation.PORTRAIT
30
- } else if (rotation in 86..94) {
31
- orientation = Orientation.LANDSCAPE_RIGHT
32
- } else if (rotation in 176..184) {
33
- orientation = Orientation.PORTRAIT_UPSIDE_DOWN
34
- } else if (rotation in 266..274) {
35
- orientation = Orientation.LANDSCAPE_LEFT
22
+ fun convertToDeviceOrientationFrom(deviceRotation: Int): Orientation {
23
+ return if (deviceRotation == -1) {
24
+ Orientation.UNKNOWN
25
+ } else if (deviceRotation > 355 || deviceRotation < 5) {
26
+ Orientation.PORTRAIT
27
+ } else if (deviceRotation in 86..94) {
28
+ Orientation.LANDSCAPE_RIGHT
29
+ } else if (deviceRotation in 176..184) {
30
+ Orientation.PORTRAIT_UPSIDE_DOWN
31
+ } else if (deviceRotation in 266..274) {
32
+ Orientation.LANDSCAPE_LEFT
33
+ } else {
34
+ return Orientation.UNKNOWN
36
35
  }
37
-
38
- return orientation
39
36
  }
40
37
 
41
- fun getActivityOrientationFrom(interfaceOrientation: Orientation): Int {
42
- return when (interfaceOrientation) {
38
+ fun convertToActivityOrientationFrom(orientation: Orientation): Int {
39
+ return when (orientation) {
43
40
  Orientation.LANDSCAPE_RIGHT -> ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE
44
41
  Orientation.PORTRAIT_UPSIDE_DOWN -> ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT
45
42
  Orientation.LANDSCAPE_LEFT -> ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
@@ -47,8 +44,8 @@ class OrientationDirectorUtilsImpl(val context: ReactContext) {
47
44
  }
48
45
  }
49
46
 
50
- fun getOrientationFromJsOrientation(jsOrientation: Int): Orientation {
51
- return when (jsOrientation) {
47
+ fun convertToOrientationFromJsValue(jsValue: Int): Orientation {
48
+ return when (jsValue) {
52
49
  2 -> Orientation.LANDSCAPE_RIGHT
53
50
  3 -> Orientation.PORTRAIT_UPSIDE_DOWN
54
51
  4 -> Orientation.LANDSCAPE_LEFT
@@ -56,8 +53,8 @@ class OrientationDirectorUtilsImpl(val context: ReactContext) {
56
53
  }
57
54
  }
58
55
 
59
- fun getOrientationFromRotation(rotation: Int): Orientation {
60
- return when(rotation) {
56
+ fun convertToOrientationFromScreenRotation(screenRotation: Int): Orientation {
57
+ return when(screenRotation) {
61
58
  Surface.ROTATION_270 -> Orientation.LANDSCAPE_RIGHT
62
59
  Surface.ROTATION_90 -> Orientation.LANDSCAPE_LEFT
63
60
  Surface.ROTATION_180 -> Orientation.PORTRAIT_UPSIDE_DOWN
@@ -65,7 +62,7 @@ class OrientationDirectorUtilsImpl(val context: ReactContext) {
65
62
  }
66
63
  }
67
64
 
68
- fun getInterfaceOrientationFromDeviceOrientation(deviceOrientation: Orientation): Orientation {
65
+ fun convertToInterfaceOrientationFrom(deviceOrientation: Orientation): Orientation {
69
66
  return when(deviceOrientation) {
70
67
  Orientation.PORTRAIT -> Orientation.PORTRAIT
71
68
  Orientation.LANDSCAPE_RIGHT -> Orientation.LANDSCAPE_LEFT
@@ -53,7 +53,7 @@ RCT_EXPORT_MODULE()
53
53
  }
54
54
 
55
55
  - (NSArray<NSString *> *)supportedEvents {
56
- return [OrientationEventManager supportedEvents];
56
+ return [EventManager supportedEvents];
57
57
  }
58
58
 
59
59
  - (void)sendEventWithName:(NSString * _Nonnull)name params:(NSDictionary *)params {
@@ -91,6 +91,15 @@ RCT_EXPORT_METHOD(getDeviceOrientation:(RCTPromiseResolveBlock)resolve
91
91
  });
92
92
  }
93
93
 
94
+ #ifdef RCT_NEW_ARCH_ENABLED
95
+ - (NSNumber *)isLocked
96
+ #else
97
+ RCT_EXPORT_BLOCKING_SYNCHRONOUS_METHOD(isLocked)
98
+ #endif
99
+ {
100
+ return @([_director getIsLocked]);
101
+ }
102
+
94
103
  #ifdef RCT_NEW_ARCH_ENABLED
95
104
  - (void)lockTo:(double)jsOrientation
96
105
  #else
@@ -99,7 +108,7 @@ RCT_EXPORT_METHOD(lockTo:(double)jsOrientation)
99
108
  {
100
109
  NSNumber *jsOrientationNumber = @(jsOrientation);
101
110
  dispatch_async(dispatch_get_main_queue(), ^{
102
- [_director lockToRawJsOrientation:jsOrientationNumber];
111
+ [_director lockToJsValue:jsOrientationNumber];
103
112
  });
104
113
  }
105
114
 
@@ -115,26 +124,30 @@ RCT_EXPORT_METHOD(unlock)
115
124
  }
116
125
 
117
126
  #ifdef RCT_NEW_ARCH_ENABLED
118
- - (NSNumber *)isLocked
127
+ - (void)resetSupportedInterfaceOrientations
119
128
  #else
120
- RCT_EXPORT_BLOCKING_SYNCHRONOUS_METHOD(isLocked)
129
+ RCT_EXPORT_METHOD(resetSupportedInterfaceOrientations)
121
130
  #endif
122
131
  {
123
- return @([_director isLocked]);
132
+ dispatch_async(dispatch_get_main_queue(), ^{
133
+ [_director resetSupportedInterfaceOrientations];
134
+ });
124
135
  }
125
136
 
137
+ /**
138
+ This method is a pure stub since we cannot access auto rotation setting in iOS
139
+ */
126
140
  #ifdef RCT_NEW_ARCH_ENABLED
127
- - (void)resetSupportedInterfaceOrientations
141
+ - (NSNumber *)isAutoRotationEnabled
128
142
  #else
129
- RCT_EXPORT_BLOCKING_SYNCHRONOUS_METHOD(resetSupportedInterfaceOrientations)
143
+ RCT_EXPORT_BLOCKING_SYNCHRONOUS_METHOD(isAutoRotationEnabled)
130
144
  #endif
131
145
  {
132
- dispatch_async(dispatch_get_main_queue(), ^{
133
- [_director resetSupportedInterfaceOrientations];
134
- });
146
+ return @(NO);
135
147
  }
136
148
 
137
149
 
150
+
138
151
  // Don't compile this code when we build for the old architecture.
139
152
  #ifdef RCT_NEW_ARCH_ENABLED
140
153
  - (std::shared_ptr<facebook::react::TurboModule>)getTurboModule:
@@ -0,0 +1,45 @@
1
+ //
2
+ // BundleManager.swift
3
+ // react-native-orientation-director
4
+ //
5
+ // Created by gladiuscode on 26/05/2024.
6
+ //
7
+
8
+ import Foundation
9
+
10
+ class BundleManager {
11
+
12
+ private var supportedInterfaceOrientations: [UIInterfaceOrientationMask] = [UIInterfaceOrientationMask.all]
13
+
14
+ init() {
15
+ supportedInterfaceOrientations = readSupportedInterfaceOrientations()
16
+ }
17
+
18
+ func getSupportedInterfaceOrientations() -> [UIInterfaceOrientationMask] {
19
+ return supportedInterfaceOrientations
20
+ }
21
+
22
+ private func readSupportedInterfaceOrientations() -> [UIInterfaceOrientationMask] {
23
+ let orientations = Bundle.main.object(forInfoDictionaryKey: "UISupportedInterfaceOrientations") as? [String]
24
+
25
+ guard let orientations = orientations else {
26
+ return [UIInterfaceOrientationMask.all]
27
+ }
28
+
29
+ return orientations.compactMap { orientation in
30
+ switch orientation {
31
+ case "UIInterfaceOrientationPortrait":
32
+ return UIInterfaceOrientationMask.portrait
33
+ case "UIInterfaceOrientationLandscapeLeft":
34
+ return UIInterfaceOrientationMask.landscapeLeft
35
+ case "UIInterfaceOrientationLandscapeRight":
36
+ return UIInterfaceOrientationMask.landscapeRight
37
+ case "UIInterfaceOrientationPortraitUpsideDown":
38
+ return UIInterfaceOrientationMask.portraitUpsideDown
39
+ default:
40
+ return UIInterfaceOrientationMask.allButUpsideDown
41
+ }
42
+ }
43
+ }
44
+
45
+ }
@@ -7,7 +7,7 @@
7
7
 
8
8
  import Foundation
9
9
 
10
- @objc public class OrientationEventManager : NSObject {
10
+ @objc public class EventManager : NSObject {
11
11
  @objc public weak var delegate: OrientationEventEmitterDelegate? = nil
12
12
 
13
13
  func sendDeviceOrientationDidChange(orientationValue: Int) {
@@ -56,7 +56,7 @@ import Foundation
56
56
  func sendEvent(name: String, params: NSDictionary)
57
57
  }
58
58
 
59
- public extension OrientationEventManager {
59
+ public extension EventManager {
60
60
 
61
61
  enum Event: String, CaseIterable {
62
62
  case DeviceOrientationDidChange
@@ -10,20 +10,23 @@ import UIKit
10
10
 
11
11
  @objc public class OrientationDirectorImpl : NSObject {
12
12
  private static let TAG = "OrientationDirectorImpl"
13
- private let sensorListener: OrientationSensorListener
14
- private let eventManager: OrientationEventManager
13
+
14
+ private let bundleManager: BundleManager = BundleManager()
15
+ private let utils: Utils = Utils()
16
+ private let sensorListener: SensorListener = SensorListener()
17
+ private let eventManager: EventManager = EventManager()
15
18
  private var initialSupportedInterfaceOrientations: UIInterfaceOrientationMask = UIInterfaceOrientationMask.all
16
19
  private var lastInterfaceOrientation = Orientation.UNKNOWN
17
20
  private var lastDeviceOrientation = Orientation.UNKNOWN
21
+ private var isLocked = false
18
22
 
19
23
  @objc public var supportedInterfaceOrientations: UIInterfaceOrientationMask = UIInterfaceOrientationMask.all
20
- @objc public var isLocked = false
21
24
 
22
25
  @objc public override init() {
23
- eventManager = OrientationEventManager()
24
- sensorListener = OrientationSensorListener()
25
26
  super.init()
26
- sensorListener.setOnOrientationChanged(callback: self.onOrientationChanged)
27
+
28
+ sensorListener.setOnOrientationDidChange(callback: self.onOrientationChanged)
29
+
27
30
  initialSupportedInterfaceOrientations = initInitialSupportedInterfaceOrientations()
28
31
  lastInterfaceOrientation = initInterfaceOrientation()
29
32
  lastDeviceOrientation = initDeviceOrientation()
@@ -47,10 +50,14 @@ import UIKit
47
50
  @objc public func getDeviceOrientation() -> Orientation {
48
51
  return lastDeviceOrientation
49
52
  }
53
+
54
+ @objc public func getIsLocked() -> Bool {
55
+ return isLocked
56
+ }
50
57
 
51
- @objc public func lockTo(rawJsOrientation: NSNumber) {
52
- let jsOrientation = OrientationDirectorUtils.getOrientationFrom(jsOrientation: rawJsOrientation)
53
- let mask = OrientationDirectorUtils.getMaskFrom(jsOrientation: jsOrientation)
58
+ @objc public func lockTo(jsValue: NSNumber) {
59
+ let jsOrientation = utils.convertToOrientationFrom(jsValue: jsValue)
60
+ let mask = utils.convertToMaskFrom(jsOrientation: jsOrientation)
54
61
  self.requestInterfaceUpdateTo(mask: mask)
55
62
 
56
63
  updateIsLockedTo(value: true)
@@ -69,13 +76,13 @@ import UIKit
69
76
  self.requestInterfaceUpdateTo(mask: self.supportedInterfaceOrientations)
70
77
  self.updateIsLockedTo(value: self.initIsLocked())
71
78
 
72
- let lastMask = OrientationDirectorUtils.getMaskFrom(jsOrientation: lastInterfaceOrientation)
79
+ let lastMask = utils.convertToMaskFrom(jsOrientation: lastInterfaceOrientation)
73
80
  let isLastMaskSupported = self.supportedInterfaceOrientations.contains(lastMask)
74
81
  if (isLastMaskSupported) {
75
82
  return
76
83
  }
77
84
 
78
- let supportedInterfaceOrientations = OrientationDirectorUtils.readSupportedInterfaceOrientationsFromBundle()
85
+ let supportedInterfaceOrientations = bundleManager.getSupportedInterfaceOrientations()
79
86
  if (supportedInterfaceOrientations.contains(UIInterfaceOrientationMask.portrait)) {
80
87
  self.updateLastInterfaceOrientationTo(value: Orientation.PORTRAIT)
81
88
  return
@@ -93,21 +100,22 @@ import UIKit
93
100
  }
94
101
 
95
102
  private func initInitialSupportedInterfaceOrientations() -> UIInterfaceOrientationMask {
96
- let supportedInterfaceOrientations = OrientationDirectorUtils.readSupportedInterfaceOrientationsFromBundle()
103
+ let supportedInterfaceOrientations = bundleManager.getSupportedInterfaceOrientations()
97
104
  return supportedInterfaceOrientations.reduce(UIInterfaceOrientationMask()) { $0.union($1) }
98
105
  }
99
106
 
107
+ // TODO: FIX BECAUSE IT ALWAYS RETURNS PORTRAIT AND ITS BROKEN
100
108
  private func initInterfaceOrientation() -> Orientation {
101
- let interfaceOrientation = OrientationDirectorUtils.getInterfaceOrientation()
102
- return OrientationDirectorUtils.getOrientationFrom(uiInterfaceOrientation: interfaceOrientation)
109
+ let interfaceOrientation = utils.getInterfaceOrientation()
110
+ return utils.convertToOrientationFrom(uiInterfaceOrientation: interfaceOrientation)
103
111
  }
104
112
 
105
113
  private func initDeviceOrientation() -> Orientation {
106
- return OrientationDirectorUtils.getOrientationFrom(deviceOrientation: UIDevice.current.orientation)
114
+ return utils.convertToOrientationFrom(deviceOrientation: UIDevice.current.orientation)
107
115
  }
108
116
 
109
117
  private func initIsLocked() -> Bool {
110
- let supportedOrientations = OrientationDirectorUtils.readSupportedInterfaceOrientationsFromBundle()
118
+ let supportedOrientations = bundleManager.getSupportedInterfaceOrientations()
111
119
  if (supportedOrientations.count > 1) {
112
120
  return false
113
121
  }
@@ -119,7 +127,7 @@ import UIKit
119
127
  self.supportedInterfaceOrientations = mask
120
128
 
121
129
  if #available(iOS 16.0, *) {
122
- let window = OrientationDirectorUtils.getCurrentWindow()
130
+ let window = utils.getCurrentWindow()
123
131
 
124
132
  guard let rootViewController = window?.rootViewController else {
125
133
  return
@@ -141,7 +149,7 @@ import UIKit
141
149
  }
142
150
 
143
151
  private func onOrientationChanged(uiDeviceOrientation: UIDeviceOrientation) {
144
- let deviceOrientation = OrientationDirectorUtils.getOrientationFrom(deviceOrientation: uiDeviceOrientation)
152
+ let deviceOrientation = utils.convertToOrientationFrom(deviceOrientation: uiDeviceOrientation)
145
153
  self.eventManager.sendDeviceOrientationDidChange(orientationValue: deviceOrientation.rawValue)
146
154
  lastDeviceOrientation = deviceOrientation
147
155
  adaptInterfaceTo(deviceOrientation: deviceOrientation)
@@ -151,22 +159,22 @@ import UIKit
151
159
  if (isLocked) {
152
160
  return
153
161
  }
154
-
162
+
155
163
  if (deviceOrientation == Orientation.FACE_UP || deviceOrientation == Orientation.FACE_DOWN) {
156
164
  return
157
165
  }
158
166
 
159
- let newInterfaceOrientationMask = OrientationDirectorUtils.getMaskFrom(deviceOrientation: deviceOrientation)
167
+ let newInterfaceOrientationMask = utils.convertToMaskFrom(deviceOrientation: deviceOrientation)
160
168
  let isSupported = self.supportedInterfaceOrientations.contains(newInterfaceOrientationMask)
161
169
  if (!isSupported) {
162
170
  return
163
171
  }
164
172
 
165
- let newInterfaceOrientation = OrientationDirectorUtils.getOrientationFrom(mask: newInterfaceOrientationMask)
173
+ let newInterfaceOrientation = utils.convertToOrientationFrom(mask: newInterfaceOrientationMask)
166
174
  if (newInterfaceOrientation == lastInterfaceOrientation) {
167
175
  return
168
176
  }
169
-
177
+
170
178
  updateLastInterfaceOrientationTo(value: newInterfaceOrientation)
171
179
  }
172
180
 
@@ -7,8 +7,8 @@
7
7
 
8
8
  import Foundation
9
9
 
10
- public class OrientationSensorListener {
11
- private var onOrientationChangedCallback: ((_ deviceOrientation: UIDeviceOrientation) -> Void)? = nil
10
+ public class SensorListener {
11
+ private var onOrientationDidChangeCallback: ((_ deviceOrientation: UIDeviceOrientation) -> Void)? = nil
12
12
 
13
13
  init() {
14
14
  NotificationCenter.default.addObserver(
@@ -23,15 +23,15 @@ public class OrientationSensorListener {
23
23
  NotificationCenter.default.removeObserver(self)
24
24
  }
25
25
 
26
- func setOnOrientationChanged(callback: @escaping (_ deviceOrientation: UIDeviceOrientation) -> Void) {
27
- self.onOrientationChangedCallback = callback
26
+ func setOnOrientationDidChange(callback: @escaping (_ deviceOrientation: UIDeviceOrientation) -> Void) {
27
+ self.onOrientationDidChangeCallback = callback
28
28
  }
29
29
 
30
30
  @objc func orientationDidChange(_ notification: Notification) {
31
- guard let onOrientationChangedCallback = self.onOrientationChangedCallback else {
31
+ guard let onOrientationDidChangeCallback = self.onOrientationDidChangeCallback else {
32
32
  return
33
33
  }
34
34
 
35
- onOrientationChangedCallback(UIDevice.current.orientation)
35
+ onOrientationDidChangeCallback(UIDevice.current.orientation)
36
36
  }
37
37
  }
@@ -0,0 +1,130 @@
1
+ //
2
+ // OrientationDirectorUtils.swift
3
+ // react-native-orientation-director
4
+ //
5
+ // Created by gladiuscode on 18/05/2024.
6
+ //
7
+
8
+ import Foundation
9
+
10
+ class Utils {
11
+
12
+ private static let TAG = "Utils"
13
+
14
+ // TODO: Add .unknown
15
+ public func convertToOrientationFrom(uiInterfaceOrientation: UIInterfaceOrientation) -> Orientation {
16
+ switch(uiInterfaceOrientation) {
17
+ case .landscapeRight:
18
+ return .LANDSCAPE_RIGHT
19
+ case .portraitUpsideDown:
20
+ return .PORTRAIT_UPSIDE_DOWN
21
+ case .landscapeLeft:
22
+ return .LANDSCAPE_LEFT
23
+ default:
24
+ return .PORTRAIT
25
+ }
26
+ }
27
+
28
+ public func convertToOrientationFrom(deviceOrientation: UIDeviceOrientation) -> Orientation {
29
+ switch(deviceOrientation) {
30
+ case .landscapeRight:
31
+ return .LANDSCAPE_RIGHT
32
+ case .portraitUpsideDown:
33
+ return .PORTRAIT_UPSIDE_DOWN
34
+ case .landscapeLeft:
35
+ return .LANDSCAPE_LEFT
36
+ case .faceUp:
37
+ return .FACE_UP
38
+ case .faceDown:
39
+ return .FACE_DOWN
40
+ default:
41
+ return .PORTRAIT
42
+ }
43
+ }
44
+
45
+ public func convertToOrientationFrom(jsValue: NSNumber) -> Orientation {
46
+ switch(jsValue) {
47
+ case 2:
48
+ return .LANDSCAPE_RIGHT
49
+ case 3:
50
+ return .PORTRAIT_UPSIDE_DOWN
51
+ case 4:
52
+ return .LANDSCAPE_LEFT
53
+ default:
54
+ return .PORTRAIT
55
+ }
56
+ }
57
+
58
+ public func convertToOrientationFrom(mask: UIInterfaceOrientationMask) -> Orientation {
59
+ switch(mask) {
60
+ case .portraitUpsideDown:
61
+ return .PORTRAIT_UPSIDE_DOWN
62
+ case .landscapeRight:
63
+ return .LANDSCAPE_RIGHT
64
+ case .landscapeLeft:
65
+ return .LANDSCAPE_LEFT
66
+ default:
67
+ return .PORTRAIT
68
+ }
69
+ }
70
+
71
+ /**
72
+ Note: .portraitUpsideDown only works for devices with home button and iPads
73
+ https://developer.apple.com/documentation/uikit/uiviewcontroller/1621435-supportedinterfaceorientations
74
+ */
75
+ public func convertToMaskFrom(jsOrientation: Orientation) -> UIInterfaceOrientationMask {
76
+ switch(jsOrientation) {
77
+ case .PORTRAIT:
78
+ return .portrait
79
+ case .LANDSCAPE_RIGHT:
80
+ return .landscapeRight
81
+ case .PORTRAIT_UPSIDE_DOWN:
82
+ return .portraitUpsideDown
83
+ case .LANDSCAPE_LEFT:
84
+ return .landscapeLeft
85
+ default:
86
+ return .all
87
+ }
88
+ }
89
+
90
+ /**
91
+ Note: .portraitUpsideDown only works for devices with home button and iPads
92
+ https://developer.apple.com/documentation/uikit/uiviewcontroller/1621435-supportedinterfaceorientations
93
+ */
94
+ public func convertToMaskFrom(deviceOrientation: Orientation) -> UIInterfaceOrientationMask {
95
+ switch(deviceOrientation) {
96
+ case .PORTRAIT:
97
+ return .portrait
98
+ case .LANDSCAPE_RIGHT:
99
+ return .landscapeLeft
100
+ case .PORTRAIT_UPSIDE_DOWN:
101
+ return .portraitUpsideDown
102
+ case .LANDSCAPE_LEFT:
103
+ return .landscapeRight
104
+ default:
105
+ return .all
106
+ }
107
+ }
108
+
109
+ public func getInterfaceOrientation() -> UIInterfaceOrientation {
110
+ guard let windowScene = self.getCurrentWindow()?.windowScene else {
111
+ return UIInterfaceOrientation.unknown
112
+ }
113
+
114
+ return windowScene.interfaceOrientation;
115
+ }
116
+
117
+ /* This function is needed to get the current available window.
118
+ Here in React Native we should have only one window tho.
119
+ https://stackoverflow.com/questions/57134259/how-to-resolve-keywindow-was-deprecated-in-ios-13-0/58031897#58031897
120
+ */
121
+ public func getCurrentWindow() -> UIWindow? {
122
+ return UIApplication
123
+ .shared
124
+ .connectedScenes
125
+ .compactMap { $0 as? UIWindowScene }
126
+ .flatMap { $0.windows }
127
+ .last { $0.isKeyWindow }
128
+ }
129
+
130
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-native-orientation-director",
3
- "version": "1.2.1",
3
+ "version": "1.2.3",
4
4
  "description": "A Modern React Native library that allows you to access orientation",
5
5
  "main": "lib/commonjs/index",
6
6
  "module": "lib/module/index",
@@ -1,166 +0,0 @@
1
- //
2
- // OrientationDirectorUtils.swift
3
- // react-native-orientation-director
4
- //
5
- // Created by gladiuscode on 18/05/2024.
6
- //
7
-
8
- import Foundation
9
-
10
- class OrientationDirectorUtils {
11
-
12
- private static let TAG = "OrientationDirectorUtils"
13
-
14
- public static func getOrientationFrom(uiInterfaceOrientation: UIInterfaceOrientation) -> Orientation {
15
- var orientation = Orientation.UNKNOWN
16
-
17
- switch(uiInterfaceOrientation) {
18
- case UIInterfaceOrientation.landscapeRight:
19
- orientation = Orientation.LANDSCAPE_RIGHT
20
- case UIInterfaceOrientation.portraitUpsideDown:
21
- orientation = Orientation.PORTRAIT_UPSIDE_DOWN
22
- case UIInterfaceOrientation.landscapeLeft:
23
- orientation = Orientation.LANDSCAPE_LEFT
24
- default:
25
- orientation = Orientation.PORTRAIT
26
- }
27
-
28
- return orientation
29
- }
30
-
31
- public static func getOrientationFrom(deviceOrientation: UIDeviceOrientation) -> Orientation {
32
- var orientation = Orientation.UNKNOWN
33
-
34
- switch(deviceOrientation) {
35
- case UIDeviceOrientation.landscapeRight:
36
- orientation = Orientation.LANDSCAPE_RIGHT
37
- case UIDeviceOrientation.portraitUpsideDown:
38
- orientation = Orientation.PORTRAIT_UPSIDE_DOWN
39
- case UIDeviceOrientation.landscapeLeft:
40
- orientation = Orientation.LANDSCAPE_LEFT
41
- case UIDeviceOrientation.faceUp:
42
- orientation = Orientation.FACE_UP
43
- case UIDeviceOrientation.faceDown:
44
- orientation = Orientation.FACE_DOWN
45
- default:
46
- orientation = Orientation.PORTRAIT
47
- }
48
-
49
- return orientation
50
- }
51
-
52
- public static func getOrientationFrom(jsOrientation: NSNumber) -> Orientation {
53
- var orientation = Orientation.UNKNOWN
54
-
55
- switch(jsOrientation) {
56
- case 2:
57
- orientation = Orientation.LANDSCAPE_RIGHT
58
- case 3:
59
- orientation = Orientation.PORTRAIT_UPSIDE_DOWN
60
- case 4:
61
- orientation = Orientation.LANDSCAPE_LEFT
62
- default:
63
- orientation = Orientation.PORTRAIT
64
- }
65
-
66
- return orientation
67
- }
68
-
69
- public static func getOrientationFrom(mask: UIInterfaceOrientationMask) -> Orientation {
70
- var orientation = Orientation.UNKNOWN
71
-
72
- switch(mask) {
73
- case UIInterfaceOrientationMask.portraitUpsideDown:
74
- orientation = Orientation.PORTRAIT_UPSIDE_DOWN
75
- case UIInterfaceOrientationMask.landscapeRight:
76
- orientation = Orientation.LANDSCAPE_RIGHT
77
- case UIInterfaceOrientationMask.landscapeLeft:
78
- orientation = Orientation.LANDSCAPE_LEFT
79
- default:
80
- orientation = Orientation.PORTRAIT
81
- }
82
-
83
- return orientation
84
- }
85
-
86
- /**
87
- Note: .portraitUpsideDown only works for devices with home button and iPads
88
- https://developer.apple.com/documentation/uikit/uiviewcontroller/1621435-supportedinterfaceorientations
89
- */
90
- public static func getMaskFrom(jsOrientation: Orientation) -> UIInterfaceOrientationMask {
91
- switch(jsOrientation) {
92
- case Orientation.PORTRAIT:
93
- return UIInterfaceOrientationMask.portrait
94
- case Orientation.LANDSCAPE_RIGHT:
95
- return UIInterfaceOrientationMask.landscapeRight
96
- case Orientation.PORTRAIT_UPSIDE_DOWN:
97
- return UIInterfaceOrientationMask.portraitUpsideDown
98
- case Orientation.LANDSCAPE_LEFT:
99
- return UIInterfaceOrientationMask.landscapeLeft
100
- default:
101
- return UIInterfaceOrientationMask.all
102
- }
103
- }
104
-
105
- /**
106
- Note: .portraitUpsideDown only works for devices with home button and iPads
107
- https://developer.apple.com/documentation/uikit/uiviewcontroller/1621435-supportedinterfaceorientations
108
- */
109
- public static func getMaskFrom(deviceOrientation: Orientation) -> UIInterfaceOrientationMask {
110
- switch(deviceOrientation) {
111
- case Orientation.PORTRAIT:
112
- return UIInterfaceOrientationMask.portrait
113
- case Orientation.LANDSCAPE_RIGHT:
114
- return UIInterfaceOrientationMask.landscapeLeft
115
- case Orientation.PORTRAIT_UPSIDE_DOWN:
116
- return UIInterfaceOrientationMask.portraitUpsideDown
117
- case Orientation.LANDSCAPE_LEFT:
118
- return UIInterfaceOrientationMask.landscapeRight
119
- default:
120
- return UIInterfaceOrientationMask.all
121
- }
122
- }
123
-
124
- public static func getInterfaceOrientation() -> UIInterfaceOrientation {
125
- guard let windowScene = self.getCurrentWindow()?.windowScene else {
126
- return UIInterfaceOrientation.unknown
127
- }
128
-
129
- return windowScene.interfaceOrientation;
130
- }
131
-
132
- /* This function is needed to get the current available window.
133
- Here in React Native we should have only one window tho.
134
- https://stackoverflow.com/questions/57134259/how-to-resolve-keywindow-was-deprecated-in-ios-13-0/58031897#58031897
135
- */
136
- public static func getCurrentWindow() -> UIWindow? {
137
- return UIApplication
138
- .shared
139
- .connectedScenes
140
- .compactMap { $0 as? UIWindowScene }
141
- .flatMap { $0.windows }
142
- .last { $0.isKeyWindow }
143
- }
144
-
145
- public static func readSupportedInterfaceOrientationsFromBundle() -> [UIInterfaceOrientationMask] {
146
- guard let rawOrientations = Bundle.main.object(forInfoDictionaryKey: "UISupportedInterfaceOrientations") as? [String] else {
147
- return [UIInterfaceOrientationMask.all]
148
- }
149
-
150
- return rawOrientations.compactMap { orientation in
151
- switch orientation {
152
- case "UIInterfaceOrientationPortrait":
153
- return UIInterfaceOrientationMask.portrait
154
- case "UIInterfaceOrientationLandscapeLeft":
155
- return UIInterfaceOrientationMask.landscapeLeft
156
- case "UIInterfaceOrientationLandscapeRight":
157
- return UIInterfaceOrientationMask.landscapeRight
158
- case "UIInterfaceOrientationPortraitUpsideDown":
159
- return UIInterfaceOrientationMask.portraitUpsideDown
160
- default:
161
- return UIInterfaceOrientationMask.allButUpsideDown
162
- }
163
- }
164
- }
165
-
166
- }