1import { Audio } from 'expo-av';
2import { RecordingInput } from 'expo-av/build/Audio/Recording.types';
3import React, { useCallback, useEffect, useState } from 'react';
4import { Text, View } from 'react-native';
5
6import ListButton from '../../components/ListButton';
7
8type Props = {
9  recordingObject?: Audio.Recording;
10};
11
12function AudioInputSelector({ recordingObject }: Props) {
13  const [availableInputs, setAvailableInputs] = useState<RecordingInput[]>([]);
14  const [currentInput, setCurrentInput] = useState<RecordingInput | null>(null);
15
16  const checkInputs = useCallback(async () => {
17    if (recordingObject) {
18      const availInputs = await recordingObject.getAvailableInputs();
19      setAvailableInputs(availInputs);
20      const curtInput = await recordingObject.getCurrentInput();
21      setCurrentInput(curtInput);
22    }
23  }, [recordingObject]);
24
25  useEffect(() => {
26    checkInputs();
27  }, [checkInputs]);
28
29  return (
30    <View>
31      <Text>Recording Inputs:</Text>
32      {availableInputs.length ? (
33        availableInputs.map((input) => {
34          const isSelected = input.uid === currentInput?.uid;
35          const title = input.name;
36          return (
37            <ListButton
38              key={`input-${input.uid}`}
39              title={`${isSelected ? '✓ ' : ''}${title}`}
40              onPress={async () => {
41                await recordingObject?.setInput(input.uid);
42                checkInputs();
43              }}
44            />
45          );
46        })
47      ) : (
48        <Text>
49          Inputs cannot be populated until a recording object is created. Begin recording to view
50          inputs.
51        </Text>
52      )}
53    </View>
54  );
55}
56
57export default AudioInputSelector;
58