Use handleError for audio recording actions

Refactor RapidOrderBloc to wrap start/stop recording logic with handleError (adds onError handling and standardized emit closure). Rename isListening var to alreadyListening and tweak comments. Remove the overridden close() that disposed the audio recorder (disposal likely handled elsewhere). Also remove an unused import (krow_domain) from create_order_view to clean up warnings.
This commit is contained in:
Achintha Isuru
2026-02-27 13:25:51 -05:00
parent 2d4eee576c
commit c54b817ac4
2 changed files with 13 additions and 14 deletions

View File

@@ -47,18 +47,24 @@ class RapidOrderBloc extends Bloc<RapidOrderEvent, RapidOrderState>
) async {
if (state is RapidOrderInitial) {
final RapidOrderInitial currentState = state as RapidOrderInitial;
final bool newListeningState = !currentState.isListening;
final bool alreadyListening = currentState.isListening;
if (newListeningState) {
// Start Recording
await _audioRecorderService.startRecording();
emit(currentState.copyWith(isListening: true));
if (!alreadyListening) {
// Start recording
await handleError(
emit: (RapidOrderState s) => emit(s),
action: () async {
await _audioRecorderService.startRecording();
emit(currentState.copyWith(isListening: true));
},
onError: (String errorKey) => RapidOrderFailure(errorKey),
);
} else {
// Stop Recording and Transcribe
// Stop and transcribe
emit(currentState.copyWith(isListening: false));
await handleError(
emit: emit.call,
emit: (RapidOrderState s) => emit(s),
action: () async {
final String? path = await _audioRecorderService.stopRecording();
if (path != null) {
@@ -109,10 +115,4 @@ class RapidOrderBloc extends Bloc<RapidOrderEvent, RapidOrderState>
emit((state as RapidOrderInitial).copyWith(message: cleanedExample));
}
}
@override
Future<void> close() {
_audioRecorderService.dispose();
return super.close();
}
}

View File

@@ -3,7 +3,6 @@ import 'package:design_system/design_system.dart';
import 'package:flutter/material.dart';
import 'package:flutter_modular/flutter_modular.dart';
import 'package:krow_core/core.dart';
import 'package:krow_domain/krow_domain.dart';
import '../../utils/constants/order_types.dart';
import '../../utils/ui_entities/order_type_ui_metadata.dart';
import '../order_type_card.dart';