Skip to content

Commit

Permalink
feat: add convert auto rotation to human readable string (#27)
Browse files Browse the repository at this point in the history
  • Loading branch information
gladiuscode authored Sep 18, 2024
1 parent a39f322 commit 178fb4b
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 25 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,9 @@ This library exports a class called: [RNOrientationDirector](https://github.com/
| listenForInterfaceOrientationChanges | Triggers a provided callback each time the interface orientation changes |
| listenForLockChanges | Triggers a provided callback each time the interface orientation status changes |
| convertOrientationToHumanReadableString | Returns a human readable string based on the given orientation |
| setLocalizedStringProvider | Sets the mapping needed to convert orientation values to human readable strings |
| convertAutoRotationToHumanReadableString | Returns a human readable string based on the given auto rotation |
| setHumanReadableOrientations | Sets the mapping needed to convert orientation values to human readable strings |
| setHumanReadableAutoRotations | Sets the mapping needed to convert auto rotation values to human readable strings |
| resetSupportedInterfaceOrientations | Resets the supported interface orientations to settings |

In addition, the library exposes the following hooks:
Expand Down
25 changes: 19 additions & 6 deletions example/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,12 @@ export default function App() {
onPress={() => {
RNOrientationDirector.getInterfaceOrientation().then(
(orientation) => {
console.log('Current Interface Orientation:', orientation);
console.log(
'Current Interface Orientation:',
RNOrientationDirector.convertOrientationToHumanReadableString(
orientation
)
);
}
);
}}
Expand All @@ -49,7 +54,12 @@ export default function App() {
title={'Log Device Orientation'}
onPress={() => {
RNOrientationDirector.getDeviceOrientation().then((orientation) => {
console.log('Current Device Orientation:', orientation);
console.log(
'Current Device Orientation:',
RNOrientationDirector.convertOrientationToHumanReadableString(
orientation
)
);
});
}}
/>
Expand All @@ -64,10 +74,13 @@ export default function App() {
<Button
title={'Log is Auto Rotation Enabled'}
onPress={() => {
console.log(
'isAutoRotationEnabled: ',
RNOrientationDirector.isAutoRotationEnabled()
);
const isAutoRotationEnabled =
RNOrientationDirector.isAutoRotationEnabled();
const humanReadableAutoRotation =
RNOrientationDirector.convertAutoRotationToHumanReadableString(
isAutoRotationEnabled
);
console.log('isAutoRotationEnabled: ', humanReadableAutoRotation);
}}
/>
<View style={styles.marginBottom} />
Expand Down
40 changes: 28 additions & 12 deletions src/RNOrientationDirector.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import { Platform } from 'react-native';
import Module, { EventEmitter } from './module';
import Event from './types/Event.enum';
import type { InterfaceOrientationToLocalizedStringProvider } from './types/InterfaceOrientationToLocalizedStringProvider.type';
import type { HumanReadableOrientationsResource } from './types/HumanReadableOrientationsResource.type';
import { Orientation } from './types/Orientation.enum';
import { AutoRotation } from './types/AutoRotation.enum';
import type { OrientationEvent } from './types/OrientationEvent.interface';
import type { LockableOrientation } from './types/LockableOrientation.type';
import type { LockedEvent } from './types/LockedEvent.interface';
import type { HumanReadableAutoRotationsResource } from './types/HumanReadableAutoRotationsResource.type';

class RNOrientationDirector {
private static _localizedStringProvider: InterfaceOrientationToLocalizedStringProvider =
private static _humanReadableOrientationsResource: HumanReadableOrientationsResource =
{
[Orientation.unknown]: 'Unknown',
[Orientation.portrait]: 'Portrait',
Expand All @@ -20,10 +21,19 @@ class RNOrientationDirector {
[Orientation.faceDown]: 'Face Down',
};

setLocalizedStringProvider(
provider: InterfaceOrientationToLocalizedStringProvider
) {
RNOrientationDirector._localizedStringProvider = provider;
private static _humanReadableAutoRotationsResource: HumanReadableAutoRotationsResource =
{
[AutoRotation.unknown]: 'Unknown',
[AutoRotation.enabled]: 'Enabled',
[AutoRotation.disabled]: 'Disabled',
};

setHumanReadableOrientations(resource: HumanReadableOrientationsResource) {
RNOrientationDirector._humanReadableOrientationsResource = resource;
}

setHumanReadableAutoRotations(resource: HumanReadableAutoRotationsResource) {
RNOrientationDirector._humanReadableAutoRotationsResource = resource;
}

static getInterfaceOrientation(): Promise<Orientation> {
Expand All @@ -46,7 +56,7 @@ class RNOrientationDirector {
return Module.isLocked();
}

static isAutoRotationEnabled(): AutoRotation {
static isAutoRotationEnabled() {
if (Platform.OS !== 'android') {
return AutoRotation.unknown;
}
Expand All @@ -55,7 +65,7 @@ class RNOrientationDirector {
: AutoRotation.disabled;
}

static resetSupportedInterfaceOrientations(): void {
static resetSupportedInterfaceOrientations() {
Module.resetSupportedInterfaceOrientations();
}

Expand All @@ -78,10 +88,16 @@ class RNOrientationDirector {
return EventEmitter.addListener(Event.LockDidChange, callback);
}

static convertOrientationToHumanReadableString(
orientation: Orientation
): string {
return RNOrientationDirector._localizedStringProvider[orientation];
static convertOrientationToHumanReadableString(orientation: Orientation) {
return RNOrientationDirector._humanReadableOrientationsResource[
orientation
];
}

static convertAutoRotationToHumanReadableString(autoRotation: AutoRotation) {
return RNOrientationDirector._humanReadableAutoRotationsResource[
autoRotation
];
}
}

Expand Down
3 changes: 3 additions & 0 deletions src/types/HumanReadableAutoRotationsResource.type.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import type { AutoRotation } from './AutoRotation.enum';

export type HumanReadableAutoRotationsResource = Record<AutoRotation, string>;
3 changes: 3 additions & 0 deletions src/types/HumanReadableOrientationsResource.type.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import type { Orientation } from './Orientation.enum';

export type HumanReadableOrientationsResource = Record<Orientation, string>;

This file was deleted.

0 comments on commit 178fb4b

Please sign in to comment.