Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add hasLightSensor react method (android) #3

Merged
merged 1 commit into from
Jun 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,17 @@ npm install react-native-ambient-light-sensor
## Usage

```js
import { startLightSensor, stopLightSensor } from 'react-native-ambient-light-sensor';
import { hasLightSensor, startLightSensor, stopLightSensor } from 'react-native-ambient-light-sensor';
import { View, Text, DeviceEventEmitter } from 'react-native';

// ...

export default function App() {
const [result, setResult] = React.useState<number | undefined>();
const [hasSensor, setHasSensor] = React.useState<boolean>();

useEffect(() => {
hasLightSensor().then(setHasSensor);
startLightSensor();

const subscription = DeviceEventEmitter.addListener(
Expand All @@ -35,7 +37,7 @@ export default function App() {

return (
<View>
<Text>Light Result Value: {result}</Text>
<Text>Device has sensor: {hasSensor ? 'YES' : 'NO'}. Light Result Value: {result}</Text>
</View>
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,12 @@ public final void onSensorChanged(SensorEvent sensorEvent) {
public final void onAccuracyChanged(Sensor sensor, int accuracy) {
}

@ReactMethod
public void hasLightSensor(Promise promise) {
boolean hasSensor = mSensorLight != null;
promise.resolve(hasSensor);
}

@ReactMethod
public void startLightSensor() {
if (mSensorLight == null) {
Expand Down
5 changes: 5 additions & 0 deletions lib/commonjs/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/commonjs/index.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions lib/module/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/module/index.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions lib/typescript/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export declare async function hasLightSensor(): Promise<boolean>;
export declare function startLightSensor(): void;
export declare function stopLightSensor(): void;
4 changes: 4 additions & 0 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ const AmbientLightSensor = NativeModules.AmbientLightSensor ? NativeModules.Amb
}
);

export async function hasLightSensor(): Promise<boolean> {
return AmbientLightSensor.hasLightSensor();
}

export function startLightSensor(): void {
return AmbientLightSensor.startLightSensor();
}
Expand Down