forked from CityOfZion/neon-wallet
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[CityOfZion#1318] Commit 1/2: Enable “Scan QR” button only if camera …
…available
- Loading branch information
Showing
5 changed files
with
99 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
// @flow | ||
import React from 'react' | ||
import { filter } from 'lodash-es' | ||
import poll from '../util/poll' | ||
|
||
const POLL_FREQUENCY = 1000 | ||
|
||
type State = { | ||
avail: boolean | ||
} | ||
|
||
type Props = {} | ||
|
||
// $FlowFixMe | ||
export default function withCameraAvailability(Component) { | ||
class CameraAvailability extends React.Component<Props, State> { | ||
config = { frequency: POLL_FREQUENCY } | ||
|
||
state = { | ||
avail: false | ||
} | ||
|
||
componentDidMount() { | ||
this.awaitAvailable() | ||
} | ||
|
||
componentDidUpdate(_: Props, prevState: State) { | ||
if (prevState.avail) { | ||
this.awaitNotAvailable() | ||
} else { | ||
this.awaitAvailable() | ||
} | ||
} | ||
|
||
// wait till camera is available | ||
awaitAvailable = async () => { | ||
await poll(this.isCameraAvailable, this.config) | ||
this.setState({ avail: true }) | ||
} | ||
|
||
// wait till camera is no longer available | ||
awaitNotAvailable = async () => { | ||
await poll(this.noCameraAvailable, this.config) | ||
this.setState({ avail: false }) | ||
} | ||
|
||
// check if any camera devices are available | ||
isCameraAvailable = async (): Promise<*> => { | ||
const cameras = await this.getCameras() | ||
return cameras.length > 0 ? Promise.resolve() : Promise.reject() | ||
} | ||
|
||
// check if no camera devices are available | ||
noCameraAvailable = async (): Promise<*> => { | ||
const cameras = await this.getCameras() | ||
return cameras.length === 0 ? Promise.resolve() : Promise.reject() | ||
} | ||
|
||
// get available cameras | ||
getCameras = async (): Promise<Array<MediaDeviceInfoType>> => { | ||
const devices = await navigator.mediaDevices.enumerateDevices() | ||
return filter(devices, { kind: 'videoinput' }) | ||
} | ||
|
||
render() { | ||
return <Component cameraAvailable={this.state.avail} {...this.props} /> | ||
} | ||
} | ||
|
||
return CameraAvailability | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters