Skip to content

Commit

Permalink
Merge pull request #3 from kvs-coder/main
Browse files Browse the repository at this point in the history
feat: add hasLightSensor react method (android)
  • Loading branch information
Cshayan committed Jun 5, 2023
2 parents 5504494 + 510d49c commit d1797d8
Show file tree
Hide file tree
Showing 8 changed files with 25 additions and 4 deletions.
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

0 comments on commit d1797d8

Please sign in to comment.