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.
- package/README.md +42 -18
- package/android/build.gradle +13 -1
- package/android/gradle.properties +12 -0
- package/android/src/main/java/com/orientationdirector/OrientationDirectorPackage.kt +5 -4
- package/android/src/main/java/com/orientationdirector/implementation/EventManager.kt +0 -1
- package/android/src/main/java/com/orientationdirector/implementation/LifecycleListener.kt +1 -1
- package/android/src/main/java/com/orientationdirector/implementation/Orientation.kt +2 -0
- package/android/src/main/java/com/orientationdirector/implementation/{OrientationDirectorImpl.kt → OrientationDirectorModuleImpl.kt} +47 -33
- package/android/src/main/java/com/orientationdirector/implementation/OrientationSensorsEventListener.kt +79 -0
- package/android/src/main/java/com/orientationdirector/implementation/Utils.kt +28 -15
- package/android/src/newarch/OrientationDirectorModule.kt +59 -0
- package/android/src/oldarch/OrientationDirectorModule.kt +66 -0
- package/android/src/test/java/com/orientationdirector/implementation/OrientationDirectorModuleImplTest.kt +188 -0
- package/android/src/test/java/com/orientationdirector/implementation/UtilsTest.kt +314 -0
- package/ios/OrientationDirector.mm +12 -37
- package/lib/commonjs/EventEmitter.js +50 -0
- package/lib/commonjs/EventEmitter.js.map +1 -0
- package/lib/commonjs/NativeOrientationDirector.js.map +1 -1
- package/lib/commonjs/RNOrientationDirector.js +21 -12
- package/lib/commonjs/RNOrientationDirector.js.map +1 -1
- package/lib/commonjs/hooks/useDeviceOrientation.hook.js +1 -1
- package/lib/commonjs/hooks/useDeviceOrientation.hook.js.map +1 -1
- package/lib/commonjs/hooks/useInterfaceOrientation.hook.js +1 -1
- package/lib/commonjs/hooks/useInterfaceOrientation.hook.js.map +1 -1
- package/lib/commonjs/hooks/useIsInterfaceOrientationLocked.hook.js +1 -1
- package/lib/commonjs/hooks/useIsInterfaceOrientationLocked.hook.js.map +1 -1
- package/lib/commonjs/index.js +1 -1
- package/lib/commonjs/index.js.map +1 -1
- package/lib/commonjs/module.js +2 -2
- package/lib/commonjs/module.js.map +1 -1
- package/lib/commonjs/types/{InterfaceOrientationToLocalizedStringProvider.type.js → HumanReadableAutoRotationsResource.type.js} +1 -1
- package/lib/commonjs/types/HumanReadableAutoRotationsResource.type.js.map +1 -0
- package/lib/commonjs/types/HumanReadableOrientationsResource.type.js +6 -0
- package/lib/commonjs/types/HumanReadableOrientationsResource.type.js.map +1 -0
- package/lib/module/EventEmitter.js +41 -0
- package/lib/module/EventEmitter.js.map +1 -0
- package/lib/module/NativeOrientationDirector.js.map +1 -1
- package/lib/module/RNOrientationDirector.js +20 -9
- package/lib/module/RNOrientationDirector.js.map +1 -1
- package/lib/module/module.js +1 -1
- package/lib/module/module.js.map +1 -1
- package/lib/module/types/HumanReadableAutoRotationsResource.type.js +2 -0
- package/lib/module/types/HumanReadableAutoRotationsResource.type.js.map +1 -0
- package/lib/module/types/HumanReadableOrientationsResource.type.js +2 -0
- package/lib/module/types/HumanReadableOrientationsResource.type.js.map +1 -0
- package/lib/typescript/src/EventEmitter.d.ts +11 -0
- package/lib/typescript/src/EventEmitter.d.ts.map +1 -0
- package/lib/typescript/src/NativeOrientationDirector.d.ts +3 -1
- package/lib/typescript/src/NativeOrientationDirector.d.ts.map +1 -1
- package/lib/typescript/src/RNOrientationDirector.d.ts +8 -4
- package/lib/typescript/src/RNOrientationDirector.d.ts.map +1 -1
- package/lib/typescript/src/module.d.ts +1 -1
- package/lib/typescript/src/module.d.ts.map +1 -1
- package/lib/typescript/src/types/HumanReadableAutoRotationsResource.type.d.ts +3 -0
- package/lib/typescript/src/types/HumanReadableAutoRotationsResource.type.d.ts.map +1 -0
- package/lib/typescript/src/types/HumanReadableOrientationsResource.type.d.ts +3 -0
- package/lib/typescript/src/types/HumanReadableOrientationsResource.type.d.ts.map +1 -0
- package/lib/typescript/src/types/Orientation.enum.d.ts.map +1 -1
- package/package.json +3 -3
- package/src/EventEmitter.ts +70 -0
- package/src/NativeOrientationDirector.ts +12 -1
- package/src/RNOrientationDirector.ts +34 -21
- package/src/module.ts +1 -1
- package/src/types/HumanReadableAutoRotationsResource.type.ts +3 -0
- package/src/types/HumanReadableOrientationsResource.type.ts +3 -0
- package/src/types/Orientation.enum.ts +0 -2
- package/android/src/main/java/com/orientationdirector/OrientationDirectorModule.kt +0 -65
- package/android/src/main/java/com/orientationdirector/implementation/SensorListener.kt +0 -26
- package/android/src/newarch/OrientationDirectorSpec.kt +0 -7
- package/android/src/oldarch/OrientationDirectorSpec.kt +0 -18
- package/lib/commonjs/types/InterfaceOrientationToLocalizedStringProvider.type.js.map +0 -1
- package/lib/module/types/InterfaceOrientationToLocalizedStringProvider.type.js +0 -2
- package/lib/module/types/InterfaceOrientationToLocalizedStringProvider.type.js.map +0 -1
- package/lib/typescript/src/types/InterfaceOrientationToLocalizedStringProvider.type.d.ts +0 -3
- package/lib/typescript/src/types/InterfaceOrientationToLocalizedStringProvider.type.d.ts.map +0 -1
- 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
|
|
3
|
-
import
|
|
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
|
|
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
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
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()
|
|
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()
|
|
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.
|
|
75
|
+
return EventEmitter.addDeviceOrientationDidChangeListener(callback);
|
|
66
76
|
}
|
|
67
77
|
|
|
68
78
|
static listenForInterfaceOrientationChanges(
|
|
69
79
|
callback: (orientation: OrientationEvent) => void
|
|
70
80
|
) {
|
|
71
|
-
return EventEmitter.
|
|
72
|
-
|
|
73
|
-
|
|
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
|
|
78
|
-
return
|
|
88
|
+
static convertOrientationToHumanReadableString(orientation: Orientation) {
|
|
89
|
+
return RNOrientationDirector._humanReadableOrientationsResource[
|
|
90
|
+
orientation
|
|
91
|
+
];
|
|
79
92
|
}
|
|
80
93
|
|
|
81
|
-
static
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
94
|
+
static convertAutoRotationToHumanReadableString(autoRotation: AutoRotation) {
|
|
95
|
+
return RNOrientationDirector._humanReadableAutoRotationsResource[
|
|
96
|
+
autoRotation
|
|
97
|
+
];
|
|
85
98
|
}
|
|
86
99
|
}
|
|
87
100
|
|
package/src/module.ts
CHANGED
|
@@ -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,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 +0,0 @@
|
|
|
1
|
-
{"version":3,"names":[],"sourceRoot":"../../../src","sources":["types/InterfaceOrientationToLocalizedStringProvider.type.ts"],"mappings":"","ignoreList":[]}
|
package/lib/typescript/src/types/InterfaceOrientationToLocalizedStringProvider.type.d.ts.map
DELETED
|
@@ -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"}
|