Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/pkolt/react-native-lantern
Flashlight support on React Native
https://github.com/pkolt/react-native-lantern
Last synced: 7 days ago
JSON representation
Flashlight support on React Native
- Host: GitHub
- URL: https://github.com/pkolt/react-native-lantern
- Owner: pkolt
- License: mit
- Created: 2020-05-04T18:02:29.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2020-05-23T19:28:25.000Z (over 4 years ago)
- Last Synced: 2024-10-15T12:10:25.026Z (about 1 month ago)
- Language: Java
- Homepage:
- Size: 104 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# react-native-lantern
Flashlight support on React Native
## Warning!!! Support only Android (>= API 23 (>= Android 6.0))
## Getting started
`npm i react-native-lantern`
### Automatic installation
`npx react-native link react-native-lantern` (use `npx react-native unlink react-native-lantern` to uninstall)
### Manual installation
#### Android
1. Open up `android/app/src/main/java/[...]/MainApplication.java`
- Add `import com.reactnative.lantern.ReactNativeLanternPackage;` to the imports at the top of the file
2. Append the following lines to `android/settings.gradle`:
```
include ':react-native-lantern'
project(':react-native-lantern').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-lantern/android')
```
3. Insert the following lines inside the dependencies block in `android/app/build.gradle`:
```
compile project(':react-native-lantern')
```## Usage
```javascript
import React, { useState, useEffect, useCallback } from 'react';
import { View, Button } from 'react-native';
import lantern from 'react-native-lantern';const Main = () => {
const [isDisabled, setDisabled] = useState(true);
const [isTurnOn, setTurnState] = useState(false);useEffect(() => {
// call on change turn state (fire on subscribe, return current turn state)
const unsubscribe = lantern.subscribe('onTurn', (event) => setTurnState(event.value));
return unsubscribe;
}, []);useEffect(() => {
(async () => {
// initialize module
await lantern.ready();
setDisabled(false);
})();
}, []);const onPress = useCallback(async () => {
if (isTurnOn) {
await lantern.turnOff();
} else {
await lantern.turnOn();
}
// or `await lantern.turn(!isTurnOn)`
}, [isTurnOn]);return (
);
}
```## API
### ready() -> Promise
Initialize flashlight. This method should be called at the very beginning, before calling other methods.
### turn(turnState) -> Promise
Change turn (on/off).
### turnOn() -> Promise
Turn on flashlight.
### turnOff() -> Promise
Turn off flashlight.
### subscribe(eventName, callback) -> unsubscribe
Subscribing to event. Use the `onTurn` event to subscribe to a state change `turnState`.
## FAQ
### When running Jest tests, an error occurs `react-native-lantern: NativeModule.ReactNativeLantern is null`.
This is due to the fact that when running Jest tests, there is no native implementation of the module.
You need to add a mock file:```
// __mocks__/react-native-lantern.ts
const lanternMockModule = {
ready: jest.fn(() => Promise.resolve()),
turn: jest.fn(() => Promise.resolve()),
turnOn: jest.fn(() => Promise.resolve()),
turnOff: jest.fn(() => Promise.resolve()),
subscribe: jest.fn(() => () => {}),
};export default lanternMockModule;
```## License
[MIT](LICENSE.md)