2023-09-26 03:39:17 -06:00
|
|
|
import { NativeModules, NativeEventEmitter } from 'react-native'
|
|
|
|
import { CameraDevice } from './CameraDevice'
|
2023-09-21 03:20:33 -06:00
|
|
|
|
|
|
|
const CameraDevicesManager = NativeModules.CameraDevices as {
|
|
|
|
getConstants: () => {
|
2023-09-26 03:39:17 -06:00
|
|
|
availableCameraDevices: CameraDevice[]
|
|
|
|
userPreferredCameraDevice: CameraDevice | undefined
|
|
|
|
}
|
|
|
|
}
|
2023-09-21 03:20:33 -06:00
|
|
|
|
2023-09-26 03:39:17 -06:00
|
|
|
const constants = CameraDevicesManager.getConstants()
|
|
|
|
let devices = constants.availableCameraDevices
|
2023-09-21 03:20:33 -06:00
|
|
|
|
2023-09-26 03:39:17 -06:00
|
|
|
const DEVICES_CHANGED_NAME = 'CameraDevicesChanged'
|
2023-09-21 03:20:33 -06:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
2023-09-26 03:39:17 -06:00
|
|
|
const eventEmitter = new NativeEventEmitter(CameraDevicesManager as any)
|
2023-09-21 03:20:33 -06:00
|
|
|
eventEmitter.addListener(DEVICES_CHANGED_NAME, (newDevices: CameraDevice[]) => {
|
2023-09-26 03:39:17 -06:00
|
|
|
devices = newDevices
|
|
|
|
})
|
2023-09-21 03:20:33 -06:00
|
|
|
|
|
|
|
export const CameraDevices = {
|
2023-09-21 08:29:46 -06:00
|
|
|
userPreferredCameraDevice: constants.userPreferredCameraDevice,
|
2023-09-21 03:20:33 -06:00
|
|
|
getAvailableCameraDevices: () => devices,
|
|
|
|
addCameraDevicesChangedListener: (callback: (newDevices: CameraDevice[]) => void) => {
|
2023-09-26 03:39:17 -06:00
|
|
|
return eventEmitter.addListener(DEVICES_CHANGED_NAME, callback)
|
2023-09-21 03:20:33 -06:00
|
|
|
},
|
2023-09-26 03:39:17 -06:00
|
|
|
}
|