react-native-orientation-director 1.2.3 → 2.0.0

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.
Files changed (76) hide show
  1. package/README.md +42 -18
  2. package/android/build.gradle +13 -1
  3. package/android/gradle.properties +12 -0
  4. package/android/src/main/java/com/orientationdirector/OrientationDirectorPackage.kt +5 -4
  5. package/android/src/main/java/com/orientationdirector/implementation/EventManager.kt +0 -1
  6. package/android/src/main/java/com/orientationdirector/implementation/LifecycleListener.kt +1 -1
  7. package/android/src/main/java/com/orientationdirector/implementation/Orientation.kt +2 -0
  8. package/android/src/main/java/com/orientationdirector/implementation/{OrientationDirectorImpl.kt → OrientationDirectorModuleImpl.kt} +47 -33
  9. package/android/src/main/java/com/orientationdirector/implementation/OrientationSensorsEventListener.kt +79 -0
  10. package/android/src/main/java/com/orientationdirector/implementation/Utils.kt +28 -15
  11. package/android/src/newarch/OrientationDirectorModule.kt +59 -0
  12. package/android/src/oldarch/OrientationDirectorModule.kt +66 -0
  13. package/android/src/test/java/com/orientationdirector/implementation/OrientationDirectorModuleImplTest.kt +188 -0
  14. package/android/src/test/java/com/orientationdirector/implementation/UtilsTest.kt +314 -0
  15. package/ios/OrientationDirector.mm +12 -37
  16. package/lib/commonjs/EventEmitter.js +50 -0
  17. package/lib/commonjs/EventEmitter.js.map +1 -0
  18. package/lib/commonjs/NativeOrientationDirector.js.map +1 -1
  19. package/lib/commonjs/RNOrientationDirector.js +21 -12
  20. package/lib/commonjs/RNOrientationDirector.js.map +1 -1
  21. package/lib/commonjs/hooks/useDeviceOrientation.hook.js +1 -1
  22. package/lib/commonjs/hooks/useDeviceOrientation.hook.js.map +1 -1
  23. package/lib/commonjs/hooks/useInterfaceOrientation.hook.js +1 -1
  24. package/lib/commonjs/hooks/useInterfaceOrientation.hook.js.map +1 -1
  25. package/lib/commonjs/hooks/useIsInterfaceOrientationLocked.hook.js +1 -1
  26. package/lib/commonjs/hooks/useIsInterfaceOrientationLocked.hook.js.map +1 -1
  27. package/lib/commonjs/index.js +1 -1
  28. package/lib/commonjs/index.js.map +1 -1
  29. package/lib/commonjs/module.js +2 -2
  30. package/lib/commonjs/module.js.map +1 -1
  31. package/lib/commonjs/types/{InterfaceOrientationToLocalizedStringProvider.type.js → HumanReadableAutoRotationsResource.type.js} +1 -1
  32. package/lib/commonjs/types/HumanReadableAutoRotationsResource.type.js.map +1 -0
  33. package/lib/commonjs/types/HumanReadableOrientationsResource.type.js +6 -0
  34. package/lib/commonjs/types/HumanReadableOrientationsResource.type.js.map +1 -0
  35. package/lib/module/EventEmitter.js +41 -0
  36. package/lib/module/EventEmitter.js.map +1 -0
  37. package/lib/module/NativeOrientationDirector.js.map +1 -1
  38. package/lib/module/RNOrientationDirector.js +20 -9
  39. package/lib/module/RNOrientationDirector.js.map +1 -1
  40. package/lib/module/module.js +1 -1
  41. package/lib/module/module.js.map +1 -1
  42. package/lib/module/types/HumanReadableAutoRotationsResource.type.js +2 -0
  43. package/lib/module/types/HumanReadableAutoRotationsResource.type.js.map +1 -0
  44. package/lib/module/types/HumanReadableOrientationsResource.type.js +2 -0
  45. package/lib/module/types/HumanReadableOrientationsResource.type.js.map +1 -0
  46. package/lib/typescript/src/EventEmitter.d.ts +11 -0
  47. package/lib/typescript/src/EventEmitter.d.ts.map +1 -0
  48. package/lib/typescript/src/NativeOrientationDirector.d.ts +3 -1
  49. package/lib/typescript/src/NativeOrientationDirector.d.ts.map +1 -1
  50. package/lib/typescript/src/RNOrientationDirector.d.ts +8 -4
  51. package/lib/typescript/src/RNOrientationDirector.d.ts.map +1 -1
  52. package/lib/typescript/src/module.d.ts +1 -1
  53. package/lib/typescript/src/module.d.ts.map +1 -1
  54. package/lib/typescript/src/types/HumanReadableAutoRotationsResource.type.d.ts +3 -0
  55. package/lib/typescript/src/types/HumanReadableAutoRotationsResource.type.d.ts.map +1 -0
  56. package/lib/typescript/src/types/HumanReadableOrientationsResource.type.d.ts +3 -0
  57. package/lib/typescript/src/types/HumanReadableOrientationsResource.type.d.ts.map +1 -0
  58. package/lib/typescript/src/types/Orientation.enum.d.ts.map +1 -1
  59. package/package.json +3 -3
  60. package/src/EventEmitter.ts +70 -0
  61. package/src/NativeOrientationDirector.ts +12 -1
  62. package/src/RNOrientationDirector.ts +34 -21
  63. package/src/module.ts +1 -1
  64. package/src/types/HumanReadableAutoRotationsResource.type.ts +3 -0
  65. package/src/types/HumanReadableOrientationsResource.type.ts +3 -0
  66. package/src/types/Orientation.enum.ts +0 -2
  67. package/android/src/main/java/com/orientationdirector/OrientationDirectorModule.kt +0 -65
  68. package/android/src/main/java/com/orientationdirector/implementation/SensorListener.kt +0 -26
  69. package/android/src/newarch/OrientationDirectorSpec.kt +0 -7
  70. package/android/src/oldarch/OrientationDirectorSpec.kt +0 -18
  71. package/lib/commonjs/types/InterfaceOrientationToLocalizedStringProvider.type.js.map +0 -1
  72. package/lib/module/types/InterfaceOrientationToLocalizedStringProvider.type.js +0 -2
  73. package/lib/module/types/InterfaceOrientationToLocalizedStringProvider.type.js.map +0 -1
  74. package/lib/typescript/src/types/InterfaceOrientationToLocalizedStringProvider.type.d.ts +0 -3
  75. package/lib/typescript/src/types/InterfaceOrientationToLocalizedStringProvider.type.d.ts.map +0 -1
  76. package/src/types/InterfaceOrientationToLocalizedStringProvider.type.ts +0 -6
@@ -0,0 +1,70 @@
1
+ import { Platform, type EmitterSubscription } from 'react-native';
2
+ import Module, { ModuleEventEmitter } from './module';
3
+ import Event from './types/Event.enum';
4
+ import type { OrientationEvent } from './types/OrientationEvent.interface';
5
+ import type { LockedEvent } from './types/LockedEvent.interface';
6
+
7
+ class EventEmitter {
8
+ static addDeviceOrientationDidChangeListener(
9
+ callback: (orientation: OrientationEvent) => void
10
+ ) {
11
+ let listener = ModuleEventEmitter.addListener(
12
+ Event.DeviceOrientationDidChange,
13
+ callback
14
+ );
15
+
16
+ if (Platform.OS !== 'android') {
17
+ return listener;
18
+ }
19
+
20
+ const listenerCount = ModuleEventEmitter.listenerCount(
21
+ Event.DeviceOrientationDidChange
22
+ );
23
+
24
+ if (listenerCount === 1) {
25
+ Module.enableOrientationSensors();
26
+ }
27
+
28
+ return EventEmitter.createDeviceOrientationListenerProxy(listener);
29
+ }
30
+
31
+ static addInterfaceOrientationDidChangeListener(
32
+ callback: (orientation: OrientationEvent) => void
33
+ ) {
34
+ return ModuleEventEmitter.addListener(
35
+ Event.InterfaceOrientationDidChange,
36
+ callback
37
+ );
38
+ }
39
+
40
+ static addLockDidChangeListener(callback: (event: LockedEvent) => void) {
41
+ return ModuleEventEmitter.addListener(Event.LockDidChange, callback);
42
+ }
43
+
44
+ private static createDeviceOrientationListenerProxy(
45
+ listener: EmitterSubscription
46
+ ) {
47
+ const handler: ProxyHandler<EmitterSubscription> = {
48
+ get(target, propertyKey, receiver) {
49
+ if (propertyKey === 'remove') {
50
+ disableOrientationSensorsIfLastListener();
51
+ }
52
+ return Reflect.get(target, propertyKey, receiver);
53
+ },
54
+ };
55
+
56
+ return new Proxy(listener, handler);
57
+
58
+ function disableOrientationSensorsIfLastListener() {
59
+ const listenerCount = ModuleEventEmitter.listenerCount(
60
+ Event.DeviceOrientationDidChange
61
+ );
62
+
63
+ if (listenerCount === 1) {
64
+ Module.disableOrientationSensors();
65
+ }
66
+ }
67
+ }
68
+ }
69
+
70
+ export default EventEmitter;
@@ -7,9 +7,20 @@ export interface Spec extends TurboModule {
7
7
  lockTo(orientation: number): void;
8
8
  unlock(): void;
9
9
  isLocked(): boolean;
10
- isAutoRotationEnabled(): boolean;
11
10
  resetSupportedInterfaceOrientations(): void;
12
11
 
12
+ ////////////////////////////////////
13
+ //
14
+ // ANDROID ONLY
15
+ //
16
+
17
+ isAutoRotationEnabled(): boolean;
18
+ enableOrientationSensors(): void;
19
+ disableOrientationSensors(): void;
20
+
21
+ //
22
+ ////////////////////////////////////
23
+
13
24
  addListener: (eventType: string) => void;
14
25
  removeListeners: (count: number) => void;
15
26
  }
@@ -1,15 +1,16 @@
1
1
  import { Platform } from 'react-native';
2
- import Module, { EventEmitter } from './module';
3
- import Event from './types/Event.enum';
4
- import type { InterfaceOrientationToLocalizedStringProvider } from './types/InterfaceOrientationToLocalizedStringProvider.type';
2
+ import Module from './module';
3
+ import type { HumanReadableOrientationsResource } from './types/HumanReadableOrientationsResource.type';
5
4
  import { Orientation } from './types/Orientation.enum';
6
5
  import { AutoRotation } from './types/AutoRotation.enum';
7
6
  import type { OrientationEvent } from './types/OrientationEvent.interface';
8
7
  import type { LockableOrientation } from './types/LockableOrientation.type';
9
8
  import type { LockedEvent } from './types/LockedEvent.interface';
9
+ import type { HumanReadableAutoRotationsResource } from './types/HumanReadableAutoRotationsResource.type';
10
+ import EventEmitter from './EventEmitter';
10
11
 
11
12
  class RNOrientationDirector {
12
- private static _localizedStringProvider: InterfaceOrientationToLocalizedStringProvider =
13
+ private static _humanReadableOrientationsResource: HumanReadableOrientationsResource =
13
14
  {
14
15
  [Orientation.unknown]: 'Unknown',
15
16
  [Orientation.portrait]: 'Portrait',
@@ -20,10 +21,19 @@ class RNOrientationDirector {
20
21
  [Orientation.faceDown]: 'Face Down',
21
22
  };
22
23
 
23
- setLocalizedStringProvider(
24
- provider: InterfaceOrientationToLocalizedStringProvider
25
- ) {
26
- RNOrientationDirector._localizedStringProvider = provider;
24
+ private static _humanReadableAutoRotationsResource: HumanReadableAutoRotationsResource =
25
+ {
26
+ [AutoRotation.unknown]: 'Unknown',
27
+ [AutoRotation.enabled]: 'Enabled',
28
+ [AutoRotation.disabled]: 'Disabled',
29
+ };
30
+
31
+ setHumanReadableOrientations(resource: HumanReadableOrientationsResource) {
32
+ RNOrientationDirector._humanReadableOrientationsResource = resource;
33
+ }
34
+
35
+ setHumanReadableAutoRotations(resource: HumanReadableAutoRotationsResource) {
36
+ RNOrientationDirector._humanReadableAutoRotationsResource = resource;
27
37
  }
28
38
 
29
39
  static getInterfaceOrientation(): Promise<Orientation> {
@@ -46,7 +56,7 @@ class RNOrientationDirector {
46
56
  return Module.isLocked();
47
57
  }
48
58
 
49
- static isAutoRotationEnabled(): AutoRotation {
59
+ static isAutoRotationEnabled() {
50
60
  if (Platform.OS !== 'android') {
51
61
  return AutoRotation.unknown;
52
62
  }
@@ -55,33 +65,36 @@ class RNOrientationDirector {
55
65
  : AutoRotation.disabled;
56
66
  }
57
67
 
58
- static resetSupportedInterfaceOrientations(): void {
68
+ static resetSupportedInterfaceOrientations() {
59
69
  Module.resetSupportedInterfaceOrientations();
60
70
  }
61
71
 
62
72
  static listenForDeviceOrientationChanges(
63
73
  callback: (orientation: OrientationEvent) => void
64
74
  ) {
65
- return EventEmitter.addListener(Event.DeviceOrientationDidChange, callback);
75
+ return EventEmitter.addDeviceOrientationDidChangeListener(callback);
66
76
  }
67
77
 
68
78
  static listenForInterfaceOrientationChanges(
69
79
  callback: (orientation: OrientationEvent) => void
70
80
  ) {
71
- return EventEmitter.addListener(
72
- Event.InterfaceOrientationDidChange,
73
- callback
74
- );
81
+ return EventEmitter.addInterfaceOrientationDidChangeListener(callback);
82
+ }
83
+
84
+ static listenForLockChanges(callback: (event: LockedEvent) => void) {
85
+ return EventEmitter.addLockDidChangeListener(callback);
75
86
  }
76
87
 
77
- static listenForLockChanges(callback: (orientation: LockedEvent) => void) {
78
- return EventEmitter.addListener(Event.LockDidChange, callback);
88
+ static convertOrientationToHumanReadableString(orientation: Orientation) {
89
+ return RNOrientationDirector._humanReadableOrientationsResource[
90
+ orientation
91
+ ];
79
92
  }
80
93
 
81
- static convertOrientationToHumanReadableString(
82
- orientation: Orientation
83
- ): string {
84
- return RNOrientationDirector._localizedStringProvider[orientation];
94
+ static convertAutoRotationToHumanReadableString(autoRotation: AutoRotation) {
95
+ return RNOrientationDirector._humanReadableAutoRotationsResource[
96
+ autoRotation
97
+ ];
85
98
  }
86
99
  }
87
100
 
package/src/module.ts CHANGED
@@ -25,6 +25,6 @@ const OrientationDirectorModule = Module
25
25
  }
26
26
  );
27
27
 
28
- export const EventEmitter = new NativeEventEmitter(Module);
28
+ export const ModuleEventEmitter = new NativeEventEmitter(Module);
29
29
 
30
30
  export default OrientationDirectorModule as Spec;
@@ -0,0 +1,3 @@
1
+ import type { AutoRotation } from './AutoRotation.enum';
2
+
3
+ export type HumanReadableAutoRotationsResource = Record<AutoRotation, string>;
@@ -0,0 +1,3 @@
1
+ import type { Orientation } from './Orientation.enum';
2
+
3
+ export type HumanReadableOrientationsResource = Record<Orientation, string>;
@@ -4,8 +4,6 @@ export enum Orientation {
4
4
  landscapeRight = 2,
5
5
  portraitUpsideDown = 3,
6
6
  landscapeLeft = 4,
7
-
8
- // iOS only
9
7
  faceUp = 5,
10
8
  faceDown = 6,
11
9
  }
@@ -1,65 +0,0 @@
1
- package com.orientationdirector
2
-
3
- import com.facebook.react.bridge.ReactApplicationContext
4
- import com.facebook.react.bridge.ReactMethod
5
- import com.facebook.react.bridge.Promise
6
- import com.orientationdirector.implementation.OrientationDirectorImpl
7
-
8
- class OrientationDirectorModule internal constructor(context: ReactApplicationContext) :
9
- OrientationDirectorSpec(context) {
10
-
11
- private var orientationDirectorImpl: OrientationDirectorImpl
12
-
13
- override fun getName(): String {
14
- return NAME
15
- }
16
-
17
- init {
18
- orientationDirectorImpl = OrientationDirectorImpl(context)
19
- }
20
-
21
- @ReactMethod()
22
- override fun getInterfaceOrientation(promise: Promise) {
23
- promise.resolve(orientationDirectorImpl.getInterfaceOrientation().ordinal)
24
- }
25
-
26
- @ReactMethod()
27
- override fun getDeviceOrientation(promise: Promise) {
28
- promise.resolve(orientationDirectorImpl.getDeviceOrientation().ordinal)
29
- }
30
-
31
- @ReactMethod()
32
- override fun lockTo(orientation: Double) {
33
- orientationDirectorImpl.lockTo(orientation.toInt())
34
- }
35
-
36
- @ReactMethod()
37
- override fun unlock() {
38
- orientationDirectorImpl.unlock()
39
- }
40
-
41
- @ReactMethod()
42
- override fun resetSupportedInterfaceOrientations() {
43
- orientationDirectorImpl.resetSupportedInterfaceOrientations()
44
- }
45
-
46
- @ReactMethod(isBlockingSynchronousMethod = true)
47
- override fun isLocked(): Boolean {
48
- return orientationDirectorImpl.getIsLocked()
49
- }
50
-
51
- @ReactMethod(isBlockingSynchronousMethod = true)
52
- override fun isAutoRotationEnabled(): Boolean {
53
- return orientationDirectorImpl.getIsAutoRotationEnabled()
54
- }
55
-
56
- @ReactMethod()
57
- override fun addListener(eventName: String) {}
58
-
59
- @ReactMethod()
60
- override fun removeListeners(count: Double) {}
61
-
62
- companion object {
63
- const val NAME = "OrientationDirector"
64
- }
65
- }
@@ -1,26 +0,0 @@
1
- package com.orientationdirector.implementation
2
-
3
- import android.hardware.SensorManager
4
- import android.view.OrientationEventListener
5
- import com.facebook.react.bridge.ReactApplicationContext
6
-
7
- class SensorListener(
8
- context: ReactApplicationContext,
9
- ) : OrientationEventListener(context, SensorManager.SENSOR_DELAY_UI) {
10
-
11
- private var lastRotationDetected: Int? = null
12
- private var onOrientationChangedCallback: ((orientation: Int) -> Unit)? = null
13
-
14
- fun getLastRotationDetected(): Int? {
15
- return lastRotationDetected
16
- }
17
-
18
- fun setOnOrientationChangedCallback(callback: (orientation: Int) -> Unit) {
19
- onOrientationChangedCallback = callback
20
- }
21
-
22
- override fun onOrientationChanged(orientation: Int) {
23
- lastRotationDetected = orientation
24
- onOrientationChangedCallback?.invoke(orientation)
25
- }
26
- }
@@ -1,7 +0,0 @@
1
- package com.orientationdirector
2
-
3
- import com.facebook.react.bridge.ReactApplicationContext
4
-
5
- abstract class OrientationDirectorSpec internal constructor(context: ReactApplicationContext) :
6
- NativeOrientationDirectorSpec(context) {
7
- }
@@ -1,18 +0,0 @@
1
- package com.orientationdirector
2
-
3
- import com.facebook.react.bridge.ReactApplicationContext
4
- import com.facebook.react.bridge.ReactContextBaseJavaModule
5
- import com.facebook.react.bridge.Promise
6
-
7
- abstract class OrientationDirectorSpec internal constructor(context: ReactApplicationContext) :
8
- ReactContextBaseJavaModule(context) {
9
- abstract fun getInterfaceOrientation(promise: Promise)
10
- abstract fun getDeviceOrientation(promise: Promise)
11
- abstract fun lockTo(orientation: Double)
12
- abstract fun unlock()
13
- abstract fun resetSupportedInterfaceOrientations()
14
- abstract fun isLocked(): Boolean
15
- abstract fun isAutoRotationEnabled(): Boolean
16
- abstract fun addListener(eventName: String)
17
- abstract fun removeListeners(count: Double)
18
- }
@@ -1 +0,0 @@
1
- {"version":3,"names":[],"sourceRoot":"../../../src","sources":["types/InterfaceOrientationToLocalizedStringProvider.type.ts"],"mappings":"","ignoreList":[]}
@@ -1,2 +0,0 @@
1
- export {};
2
- //# sourceMappingURL=InterfaceOrientationToLocalizedStringProvider.type.js.map
@@ -1 +0,0 @@
1
- {"version":3,"names":[],"sourceRoot":"../../../src","sources":["types/InterfaceOrientationToLocalizedStringProvider.type.ts"],"mappings":"","ignoreList":[]}
@@ -1,3 +0,0 @@
1
- import type { Orientation } from './Orientation.enum';
2
- export type InterfaceOrientationToLocalizedStringProvider = Record<Orientation, string>;
3
- //# sourceMappingURL=InterfaceOrientationToLocalizedStringProvider.type.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"InterfaceOrientationToLocalizedStringProvider.type.d.ts","sourceRoot":"","sources":["../../../../src/types/InterfaceOrientationToLocalizedStringProvider.type.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,oBAAoB,CAAC;AAEtD,MAAM,MAAM,6CAA6C,GAAG,MAAM,CAChE,WAAW,EACX,MAAM,CACP,CAAC"}
@@ -1,6 +0,0 @@
1
- import type { Orientation } from './Orientation.enum';
2
-
3
- export type InterfaceOrientationToLocalizedStringProvider = Record<
4
- Orientation,
5
- string
6
- >;