react-native-orientation-director 1.3.0 → 2.1.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 (70) hide show
  1. package/README.md +51 -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/ios/implementation/OrientationDirectorImpl.swift +8 -15
  17. package/ios/implementation/Utils.swift +0 -32
  18. package/lib/commonjs/EventEmitter.js +50 -0
  19. package/lib/commonjs/EventEmitter.js.map +1 -0
  20. package/lib/commonjs/NativeOrientationDirector.js.map +1 -1
  21. package/lib/commonjs/RNOrientationDirector.js +58 -9
  22. package/lib/commonjs/RNOrientationDirector.js.map +1 -1
  23. package/lib/commonjs/hooks/useDeviceOrientation.hook.js +1 -1
  24. package/lib/commonjs/hooks/useDeviceOrientation.hook.js.map +1 -1
  25. package/lib/commonjs/hooks/useInterfaceOrientation.hook.js +1 -1
  26. package/lib/commonjs/hooks/useInterfaceOrientation.hook.js.map +1 -1
  27. package/lib/commonjs/hooks/useIsInterfaceOrientationLocked.hook.js +1 -1
  28. package/lib/commonjs/hooks/useIsInterfaceOrientationLocked.hook.js.map +1 -1
  29. package/lib/commonjs/index.js +8 -1
  30. package/lib/commonjs/index.js.map +1 -1
  31. package/lib/commonjs/module.js +2 -2
  32. package/lib/commonjs/module.js.map +1 -1
  33. package/lib/commonjs/types/OrientationType.enum.js +12 -0
  34. package/lib/commonjs/types/OrientationType.enum.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 +57 -6
  39. package/lib/module/RNOrientationDirector.js.map +1 -1
  40. package/lib/module/index.js +1 -0
  41. package/lib/module/index.js.map +1 -1
  42. package/lib/module/module.js +1 -1
  43. package/lib/module/module.js.map +1 -1
  44. package/lib/module/types/OrientationType.enum.js +6 -0
  45. package/lib/module/types/OrientationType.enum.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 +37 -2
  51. package/lib/typescript/src/RNOrientationDirector.d.ts.map +1 -1
  52. package/lib/typescript/src/index.d.ts +1 -0
  53. package/lib/typescript/src/index.d.ts.map +1 -1
  54. package/lib/typescript/src/module.d.ts +1 -1
  55. package/lib/typescript/src/module.d.ts.map +1 -1
  56. package/lib/typescript/src/types/Orientation.enum.d.ts.map +1 -1
  57. package/lib/typescript/src/types/OrientationType.enum.d.ts +5 -0
  58. package/lib/typescript/src/types/OrientationType.enum.d.ts.map +1 -0
  59. package/package.json +1 -1
  60. package/src/EventEmitter.ts +70 -0
  61. package/src/NativeOrientationDirector.ts +12 -1
  62. package/src/RNOrientationDirector.ts +69 -10
  63. package/src/index.tsx +1 -0
  64. package/src/module.ts +1 -1
  65. package/src/types/Orientation.enum.ts +0 -2
  66. package/src/types/OrientationType.enum.ts +4 -0
  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
@@ -1,6 +1,7 @@
1
1
  import type { HumanReadableOrientationsResource } from './types/HumanReadableOrientationsResource.type';
2
2
  import { Orientation } from './types/Orientation.enum';
3
3
  import { AutoRotation } from './types/AutoRotation.enum';
4
+ import { OrientationType } from './types/OrientationType.enum';
4
5
  import type { OrientationEvent } from './types/OrientationEvent.interface';
5
6
  import type { LockableOrientation } from './types/LockableOrientation.type';
6
7
  import type { LockedEvent } from './types/LockedEvent.interface';
@@ -12,16 +13,50 @@ declare class RNOrientationDirector {
12
13
  setHumanReadableAutoRotations(resource: HumanReadableAutoRotationsResource): void;
13
14
  static getInterfaceOrientation(): Promise<Orientation>;
14
15
  static getDeviceOrientation(): Promise<Orientation>;
15
- static lockTo(orientation: LockableOrientation): void;
16
+ /**
17
+ * Please be aware that device orientation is not the
18
+ * same as interface orientation.
19
+ *
20
+ * Specifically, landscape left and right are inverted:
21
+ *
22
+ * - landscapeLeft in device orientation is landscapeRight in interface orientation
23
+ * - landscapeRight in device orientation is landscapeLeft in interface orientation
24
+ *
25
+ * This is a behavior of the native API.
26
+ *
27
+ * When you pass an orientation value, do provide orientationType
28
+ * as well if the orientation value is not an interface orientation.
29
+ * Example: when using listenForDeviceOrientationChanges.
30
+ *
31
+ * @param orientation any lockable orientation enum value
32
+ * @param orientationType any orientation type enum value
33
+ */
34
+ static lockTo(orientation: LockableOrientation, orientationType?: OrientationType): void;
16
35
  static unlock(): void;
17
36
  static isLocked(): boolean;
18
37
  static isAutoRotationEnabled(): AutoRotation;
19
38
  static resetSupportedInterfaceOrientations(): void;
20
39
  static listenForDeviceOrientationChanges(callback: (orientation: OrientationEvent) => void): import("react-native").EmitterSubscription;
21
40
  static listenForInterfaceOrientationChanges(callback: (orientation: OrientationEvent) => void): import("react-native").EmitterSubscription;
22
- static listenForLockChanges(callback: (orientation: LockedEvent) => void): import("react-native").EmitterSubscription;
41
+ static listenForLockChanges(callback: (event: LockedEvent) => void): import("react-native").EmitterSubscription;
23
42
  static convertOrientationToHumanReadableString(orientation: Orientation): string;
24
43
  static convertAutoRotationToHumanReadableString(autoRotation: AutoRotation): string;
44
+ /**
45
+ * This method checks if the given orientation is lockable
46
+ * by interface perspective.
47
+ *
48
+ * All orientations are lockable except for unknown, faceUp
49
+ * and faceDown.
50
+ *
51
+ * This method is useful when you want to lock the interface
52
+ * orientation from a given device orientation.
53
+ *
54
+ * Example: with listenForDeviceOrientationChanges
55
+ *
56
+ * @param orientation any orientation enum value
57
+ * @returns true if the orientation is lockable
58
+ */
59
+ static isLockableOrientation(orientation: Orientation): orientation is LockableOrientation;
25
60
  }
26
61
  export default RNOrientationDirector;
27
62
  //# sourceMappingURL=RNOrientationDirector.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"RNOrientationDirector.d.ts","sourceRoot":"","sources":["../../../src/RNOrientationDirector.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,iCAAiC,EAAE,MAAM,gDAAgD,CAAC;AACxG,OAAO,EAAE,WAAW,EAAE,MAAM,0BAA0B,CAAC;AACvD,OAAO,EAAE,YAAY,EAAE,MAAM,2BAA2B,CAAC;AACzD,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,oCAAoC,CAAC;AAC3E,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,kCAAkC,CAAC;AAC5E,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,+BAA+B,CAAC;AACjE,OAAO,KAAK,EAAE,kCAAkC,EAAE,MAAM,iDAAiD,CAAC;AAE1G,cAAM,qBAAqB;IACzB,OAAO,CAAC,MAAM,CAA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OrientationType } from './types/OrientationType.enum';
3
4
  import useDeviceOrientation from './hooks/useDeviceOrientation.hook';
4
5
  export { useDeviceOrientation };
5
6
  import useInterfaceOrientation from './hooks/useInterfaceOrientation.hook';
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/index.tsx"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,0BAA0B,CAAC;AACvD,OAAO,EAAE,YAAY,EAAE,MAAM,2BAA2B,CAAC;AAEzD,OAAO,oBAAoB,MAAM,mCAAmC,CAAC;AACrE,OAAO,EAAE,oBAAoB,EAAE,CAAC;AAEhC,OAAO,uBAAuB,MAAM,sCAAsC,CAAC;AAC3E,OAAO,EAAE,uBAAuB,EAAE,CAAC;AAEnC,OAAO,+BAA+B,MAAM,8CAA8C,CAAC;AAC3F,OAAO,EAAE,+BAA+B,EAAE,CAAC;AAE3C,OAAO,qBAAqB,MAAM,yBAAyB,CAAC;AAC5D,eAAe,qBAAqB,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/index.tsx"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,0BAA0B,CAAC;AACvD,OAAO,EAAE,YAAY,EAAE,MAAM,2BAA2B,CAAC;AACzD,OAAO,EAAE,eAAe,EAAE,MAAM,8BAA8B,CAAC;AAE/D,OAAO,oBAAoB,MAAM,mCAAmC,CAAC;AACrE,OAAO,EAAE,oBAAoB,EAAE,CAAC;AAEhC,OAAO,uBAAuB,MAAM,sCAAsC,CAAC;AAC3E,OAAO,EAAE,uBAAuB,EAAE,CAAC;AAEnC,OAAO,+BAA+B,MAAM,8CAA8C,CAAC;AAC3F,OAAO,EAAE,+BAA+B,EAAE,CAAC;AAE3C,OAAO,qBAAqB,MAAM,yBAAyB,CAAC;AAC5D,eAAe,qBAAqB,CAAC"}
@@ -1,6 +1,6 @@
1
1
  import { NativeEventEmitter } from 'react-native';
2
2
  import type { Spec } from './NativeOrientationDirector';
3
- export declare const EventEmitter: NativeEventEmitter;
3
+ export declare const ModuleEventEmitter: NativeEventEmitter;
4
4
  declare const _default: Spec;
5
5
  export default _default;
6
6
  //# sourceMappingURL=module.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"module.d.ts","sourceRoot":"","sources":["../../../src/module.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,kBAAkB,EAA2B,MAAM,cAAc,CAAC;AAC3E,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,6BAA6B,CAAC;AA0BxD,eAAO,MAAM,YAAY,oBAAiC,CAAC;;AAE3D,wBAAiD"}
1
+ {"version":3,"file":"module.d.ts","sourceRoot":"","sources":["../../../src/module.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,kBAAkB,EAA2B,MAAM,cAAc,CAAC;AAC3E,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,6BAA6B,CAAC;AA0BxD,eAAO,MAAM,kBAAkB,oBAAiC,CAAC;wBAErB,IAAI;AAAhD,wBAAiD"}
@@ -1 +1 @@
1
- {"version":3,"file":"Orientation.enum.d.ts","sourceRoot":"","sources":["../../../../src/types/Orientation.enum.ts"],"names":[],"mappings":"AAAA,oBAAY,WAAW;IACrB,OAAO,IAAI;IACX,QAAQ,IAAI;IACZ,cAAc,IAAI;IAClB,kBAAkB,IAAI;IACtB,aAAa,IAAI;IAGjB,MAAM,IAAI;IACV,QAAQ,IAAI;CACb"}
1
+ {"version":3,"file":"Orientation.enum.d.ts","sourceRoot":"","sources":["../../../../src/types/Orientation.enum.ts"],"names":[],"mappings":"AAAA,oBAAY,WAAW;IACrB,OAAO,IAAI;IACX,QAAQ,IAAI;IACZ,cAAc,IAAI;IAClB,kBAAkB,IAAI;IACtB,aAAa,IAAI;IACjB,MAAM,IAAI;IACV,QAAQ,IAAI;CACb"}
@@ -0,0 +1,5 @@
1
+ export declare enum OrientationType {
2
+ device = "device",
3
+ interface = "interface"
4
+ }
5
+ //# sourceMappingURL=OrientationType.enum.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"OrientationType.enum.d.ts","sourceRoot":"","sources":["../../../../src/types/OrientationType.enum.ts"],"names":[],"mappings":"AAAA,oBAAY,eAAe;IACzB,MAAM,WAAW;IACjB,SAAS,cAAc;CACxB"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-native-orientation-director",
3
- "version": "1.3.0",
3
+ "version": "2.1.0",
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",
@@ -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,13 +1,14 @@
1
1
  import { Platform } from 'react-native';
2
- import Module, { EventEmitter } from './module';
3
- import Event from './types/Event.enum';
2
+ import Module from './module';
4
3
  import type { HumanReadableOrientationsResource } from './types/HumanReadableOrientationsResource.type';
5
4
  import { Orientation } from './types/Orientation.enum';
6
5
  import { AutoRotation } from './types/AutoRotation.enum';
6
+ import { OrientationType } from './types/OrientationType.enum';
7
7
  import type { OrientationEvent } from './types/OrientationEvent.interface';
8
8
  import type { LockableOrientation } from './types/LockableOrientation.type';
9
9
  import type { LockedEvent } from './types/LockedEvent.interface';
10
10
  import type { HumanReadableAutoRotationsResource } from './types/HumanReadableAutoRotationsResource.type';
11
+ import EventEmitter from './EventEmitter';
11
12
 
12
13
  class RNOrientationDirector {
13
14
  private static _humanReadableOrientationsResource: HumanReadableOrientationsResource =
@@ -44,7 +45,43 @@ class RNOrientationDirector {
44
45
  return Module.getDeviceOrientation();
45
46
  }
46
47
 
47
- static lockTo(orientation: LockableOrientation) {
48
+ /**
49
+ * Please be aware that device orientation is not the
50
+ * same as interface orientation.
51
+ *
52
+ * Specifically, landscape left and right are inverted:
53
+ *
54
+ * - landscapeLeft in device orientation is landscapeRight in interface orientation
55
+ * - landscapeRight in device orientation is landscapeLeft in interface orientation
56
+ *
57
+ * This is a behavior of the native API.
58
+ *
59
+ * When you pass an orientation value, do provide orientationType
60
+ * as well if the orientation value is not an interface orientation.
61
+ * Example: when using listenForDeviceOrientationChanges.
62
+ *
63
+ * @param orientation any lockable orientation enum value
64
+ * @param orientationType any orientation type enum value
65
+ */
66
+ static lockTo(
67
+ orientation: LockableOrientation,
68
+ orientationType: OrientationType = OrientationType.interface
69
+ ) {
70
+ if (orientationType === OrientationType.interface) {
71
+ Module.lockTo(orientation);
72
+ return;
73
+ }
74
+
75
+ if (orientation === Orientation.landscapeLeft) {
76
+ Module.lockTo(Orientation.landscapeRight);
77
+ return;
78
+ }
79
+
80
+ if (orientation === Orientation.landscapeRight) {
81
+ Module.lockTo(Orientation.landscapeLeft);
82
+ return;
83
+ }
84
+
48
85
  Module.lockTo(orientation);
49
86
  }
50
87
 
@@ -72,20 +109,17 @@ class RNOrientationDirector {
72
109
  static listenForDeviceOrientationChanges(
73
110
  callback: (orientation: OrientationEvent) => void
74
111
  ) {
75
- return EventEmitter.addListener(Event.DeviceOrientationDidChange, callback);
112
+ return EventEmitter.addDeviceOrientationDidChangeListener(callback);
76
113
  }
77
114
 
78
115
  static listenForInterfaceOrientationChanges(
79
116
  callback: (orientation: OrientationEvent) => void
80
117
  ) {
81
- return EventEmitter.addListener(
82
- Event.InterfaceOrientationDidChange,
83
- callback
84
- );
118
+ return EventEmitter.addInterfaceOrientationDidChangeListener(callback);
85
119
  }
86
120
 
87
- static listenForLockChanges(callback: (orientation: LockedEvent) => void) {
88
- return EventEmitter.addListener(Event.LockDidChange, callback);
121
+ static listenForLockChanges(callback: (event: LockedEvent) => void) {
122
+ return EventEmitter.addLockDidChangeListener(callback);
89
123
  }
90
124
 
91
125
  static convertOrientationToHumanReadableString(orientation: Orientation) {
@@ -99,6 +133,31 @@ class RNOrientationDirector {
99
133
  autoRotation
100
134
  ];
101
135
  }
136
+
137
+ /**
138
+ * This method checks if the given orientation is lockable
139
+ * by interface perspective.
140
+ *
141
+ * All orientations are lockable except for unknown, faceUp
142
+ * and faceDown.
143
+ *
144
+ * This method is useful when you want to lock the interface
145
+ * orientation from a given device orientation.
146
+ *
147
+ * Example: with listenForDeviceOrientationChanges
148
+ *
149
+ * @param orientation any orientation enum value
150
+ * @returns true if the orientation is lockable
151
+ */
152
+ static isLockableOrientation(
153
+ orientation: Orientation
154
+ ): orientation is LockableOrientation {
155
+ return !(
156
+ orientation === Orientation.unknown ||
157
+ orientation === Orientation.faceUp ||
158
+ orientation === Orientation.faceDown
159
+ );
160
+ }
102
161
  }
103
162
 
104
163
  export default RNOrientationDirector;
package/src/index.tsx CHANGED
@@ -1,5 +1,6 @@
1
1
  export { Orientation } from './types/Orientation.enum';
2
2
  export { AutoRotation } from './types/AutoRotation.enum';
3
+ export { OrientationType } from './types/OrientationType.enum';
3
4
 
4
5
  import useDeviceOrientation from './hooks/useDeviceOrientation.hook';
5
6
  export { useDeviceOrientation };
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;
@@ -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
  }
@@ -0,0 +1,4 @@
1
+ export enum OrientationType {
2
+ device = 'device',
3
+ interface = 'interface',
4
+ }
@@ -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
- }