You've already forked joplin
mirror of
https://github.com/laurent22/joplin.git
synced 2025-11-26 22:41:17 +02:00
Mobile: Improved Vosk error handling
This commit is contained in:
@@ -15,30 +15,39 @@ enum RecorderState {
|
|||||||
Loading = 1,
|
Loading = 1,
|
||||||
Recording = 2,
|
Recording = 2,
|
||||||
Processing = 3,
|
Processing = 3,
|
||||||
|
Error = 4,
|
||||||
}
|
}
|
||||||
|
|
||||||
const useVosk = (): Vosk|null => {
|
const useVosk = (): [Error | null, Vosk|null] => {
|
||||||
const [vosk, setVosk] = useState<Vosk>(null);
|
const [vosk, setVosk] = useState<Vosk>(null);
|
||||||
|
const [error, setError] = useState<Error>(null);
|
||||||
|
|
||||||
useAsyncEffect(async (event: AsyncEffectEvent) => {
|
useAsyncEffect(async (event: AsyncEffectEvent) => {
|
||||||
const v = await getVosk();
|
try {
|
||||||
if (event.cancelled) return;
|
const v = await getVosk();
|
||||||
setVosk(v);
|
if (event.cancelled) return;
|
||||||
|
setVosk(v);
|
||||||
|
} catch (error) {
|
||||||
|
setError(error);
|
||||||
|
}
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
return vosk;
|
return [error, vosk];
|
||||||
};
|
};
|
||||||
|
|
||||||
export default (props: Props) => {
|
export default (props: Props) => {
|
||||||
const [recorder, setRecorder] = useState<Recorder>(null);
|
const [recorder, setRecorder] = useState<Recorder>(null);
|
||||||
const [recorderState, setRecorderState] = useState<RecorderState>(RecorderState.Loading);
|
const [recorderState, setRecorderState] = useState<RecorderState>(RecorderState.Loading);
|
||||||
|
|
||||||
const vosk = useVosk();
|
const [voskError, vosk] = useVosk();
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!vosk) return;
|
if (voskError) {
|
||||||
setRecorderState(RecorderState.Recording);
|
setRecorderState(RecorderState.Error);
|
||||||
}, [vosk]);
|
} else if (vosk) {
|
||||||
|
setRecorderState(RecorderState.Recording);
|
||||||
|
}
|
||||||
|
}, [vosk, voskError]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (recorderState === RecorderState.Recording) {
|
if (recorderState === RecorderState.Recording) {
|
||||||
@@ -56,13 +65,14 @@ export default (props: Props) => {
|
|||||||
}, [recorder, props.onDismiss]);
|
}, [recorder, props.onDismiss]);
|
||||||
|
|
||||||
const renderContent = () => {
|
const renderContent = () => {
|
||||||
const components: Record<RecorderState, string> = {
|
const components: Record<RecorderState, Function> = {
|
||||||
[RecorderState.Loading]: _('Loading...'),
|
[RecorderState.Loading]: () => _('Loading...'),
|
||||||
[RecorderState.Recording]: _('Please record your voice...'),
|
[RecorderState.Recording]: () => _('Please record your voice...'),
|
||||||
[RecorderState.Processing]: _('Converting speech to text...'),
|
[RecorderState.Processing]: () => _('Converting speech to text...'),
|
||||||
|
[RecorderState.Error]: () => _('Error: %s', voskError.message),
|
||||||
};
|
};
|
||||||
|
|
||||||
return components[recorderState];
|
return components[recorderState]();
|
||||||
};
|
};
|
||||||
|
|
||||||
const renderIcon = () => {
|
const renderIcon = () => {
|
||||||
@@ -70,6 +80,7 @@ export default (props: Props) => {
|
|||||||
[RecorderState.Loading]: ({ size }: { size: number }) => <ActivityIndicator animating={true} style={{ width: size, height: size }} />,
|
[RecorderState.Loading]: ({ size }: { size: number }) => <ActivityIndicator animating={true} style={{ width: size, height: size }} />,
|
||||||
[RecorderState.Recording]: 'microphone',
|
[RecorderState.Recording]: 'microphone',
|
||||||
[RecorderState.Processing]: 'microphone',
|
[RecorderState.Processing]: 'microphone',
|
||||||
|
[RecorderState.Error]: 'alert-circle-outline',
|
||||||
};
|
};
|
||||||
|
|
||||||
return components[recorderState];
|
return components[recorderState];
|
||||||
|
|||||||
Reference in New Issue
Block a user