222 lines
5.9 KiB
Dart
222 lines
5.9 KiB
Dart
// ignore_for_file: invalid_use_of_protected_member
|
|
|
|
part of 'home_screen.dart';
|
|
|
|
extension _HomeScreenInteractions on _HomeScreenState {
|
|
void _onHoldToSpeakCancel() {
|
|
if (_isRecordingStarting) {
|
|
_shouldStopWhenStartCompletes = false;
|
|
_shouldCancelWhenStartCompletes = true;
|
|
return;
|
|
}
|
|
_cancelRecording(showToast: false);
|
|
}
|
|
|
|
Future<void> _cancelRecording({bool showToast = true}) async {
|
|
try {
|
|
await _voiceRecorder.stop();
|
|
_listeningAnimationController.stop();
|
|
} catch (_) {}
|
|
if (!mounted) return;
|
|
setState(() {
|
|
_isRecording = false;
|
|
_isCancelGestureActive = false;
|
|
});
|
|
if (showToast) {
|
|
Toast.show(context, '已取消', type: ToastType.info);
|
|
}
|
|
}
|
|
|
|
Future<void> _sendMessage(
|
|
BuildContext context, {
|
|
String? overrideContent,
|
|
}) async {
|
|
if (_isSendingMessage) {
|
|
return;
|
|
}
|
|
|
|
final content = (overrideContent ?? _messageController.text).trim();
|
|
if (content.isEmpty && _selectedImages.isEmpty) return;
|
|
|
|
final images = List<XFile>.from(_selectedImages);
|
|
|
|
final currentFocus = FocusManager.instance.primaryFocus;
|
|
currentFocus?.unfocus();
|
|
_messageController.clear();
|
|
setState(() {
|
|
_isSendingMessage = true;
|
|
_selectedImages.clear();
|
|
});
|
|
|
|
try {
|
|
await _chatBloc.sendMessage(content, images: images);
|
|
} finally {
|
|
if (mounted) {
|
|
setState(() {
|
|
_isSendingMessage = false;
|
|
});
|
|
}
|
|
}
|
|
|
|
WidgetsBinding.instance.addPostFrameCallback((_) {
|
|
if (_scrollController.hasClients) {
|
|
_scrollController.animateTo(
|
|
_scrollController.position.maxScrollExtent,
|
|
duration: const Duration(milliseconds: _scrollDurationMs),
|
|
curve: Curves.easeOut,
|
|
);
|
|
}
|
|
});
|
|
}
|
|
|
|
Future<void> _onStopGenerating() async {
|
|
final canceled = await _chatBloc.cancelCurrentRun();
|
|
if (!mounted) {
|
|
return;
|
|
}
|
|
if (canceled) {
|
|
Toast.show(context, '已请求停止', type: ToastType.info);
|
|
}
|
|
}
|
|
|
|
Future<void> _startRecording() async {
|
|
if (_isRecording || _isRecordingStarting) {
|
|
return;
|
|
}
|
|
if (mounted) {
|
|
setState(() {
|
|
_isRecordingStarting = true;
|
|
_shouldCancelWhenStartCompletes = false;
|
|
_shouldStopWhenStartCompletes = false;
|
|
});
|
|
}
|
|
try {
|
|
await _voiceRecorder.start();
|
|
_listeningAnimationController.repeat();
|
|
if (!mounted) {
|
|
return;
|
|
}
|
|
if (_shouldStopWhenStartCompletes || _shouldCancelWhenStartCompletes) {
|
|
final shouldCancelAfterStart =
|
|
_shouldCancelWhenStartCompletes || _isCancelGestureActive;
|
|
setState(() {
|
|
_isRecordingStarting = false;
|
|
_shouldCancelWhenStartCompletes = false;
|
|
_shouldStopWhenStartCompletes = false;
|
|
_isRecording = true;
|
|
_isCancelGestureActive = false;
|
|
});
|
|
if (shouldCancelAfterStart) {
|
|
await _cancelRecording(showToast: false);
|
|
return;
|
|
}
|
|
await _stopRecording(autoSendAfterTranscribe: true);
|
|
return;
|
|
}
|
|
setState(() {
|
|
_isRecordingStarting = false;
|
|
_isRecording = true;
|
|
_isCancelGestureActive = false;
|
|
});
|
|
} catch (error) {
|
|
if (!mounted) {
|
|
return;
|
|
}
|
|
setState(() {
|
|
_isRecordingStarting = false;
|
|
_shouldCancelWhenStartCompletes = false;
|
|
_shouldStopWhenStartCompletes = false;
|
|
});
|
|
Toast.show(context, _readableError(error), type: ToastType.error);
|
|
}
|
|
}
|
|
|
|
Future<void> _stopRecording({bool autoSendAfterTranscribe = false}) async {
|
|
String? audioPath;
|
|
try {
|
|
audioPath = await _voiceRecorder.stop();
|
|
_listeningAnimationController.stop();
|
|
if (!mounted) {
|
|
return;
|
|
}
|
|
setState(() {
|
|
_isRecording = false;
|
|
_isTranscribing = true;
|
|
_isCancelGestureActive = false;
|
|
});
|
|
if (audioPath == null || audioPath.isEmpty) {
|
|
throw StateError('录音失败,请重试');
|
|
}
|
|
final transcript = await _transcribeAudio(audioPath);
|
|
if (!mounted) {
|
|
return;
|
|
}
|
|
final normalizedTranscript = transcript.trim();
|
|
if (normalizedTranscript.isEmpty) {
|
|
Toast.show(context, '未识别到有效语音,请靠近麦克风并连续说话后重试', type: ToastType.error);
|
|
return;
|
|
}
|
|
_messageController.text = normalizedTranscript;
|
|
_messageController.selection = TextSelection.fromPosition(
|
|
TextPosition(offset: normalizedTranscript.length),
|
|
);
|
|
if (autoSendAfterTranscribe) {
|
|
setState(() {
|
|
_isTranscribing = false;
|
|
});
|
|
await _sendMessage(context);
|
|
}
|
|
} catch (error) {
|
|
if (!mounted) {
|
|
return;
|
|
}
|
|
Toast.show(context, _readableError(error), type: ToastType.error);
|
|
} finally {
|
|
try {
|
|
if (audioPath != null) {
|
|
final file = File(audioPath);
|
|
if (await file.exists()) {
|
|
await file.delete();
|
|
}
|
|
}
|
|
} catch (_) {
|
|
// Ignore temp file cleanup errors to avoid blocking UI state recovery.
|
|
}
|
|
if (mounted) {
|
|
setState(() {
|
|
_isTranscribing = false;
|
|
});
|
|
}
|
|
}
|
|
}
|
|
|
|
String _readableError(Object error) {
|
|
if (error is ApiException) {
|
|
return error.message;
|
|
}
|
|
final raw = error.toString();
|
|
if (raw.startsWith('Instance of')) {
|
|
return '请求失败,请稍后重试';
|
|
}
|
|
return raw.replaceFirst('Bad state: ', '');
|
|
}
|
|
|
|
void _showBottomSheet(BuildContext context) {
|
|
showModalBottomSheet(
|
|
context: context,
|
|
backgroundColor: Colors.transparent,
|
|
isScrollControlled: true,
|
|
builder: (context) => HomeSheet(
|
|
onImagesSelected: (images) {
|
|
setState(() {
|
|
final remaining = 3 - _selectedImages.length;
|
|
if (remaining > 0) {
|
|
_selectedImages.addAll(images.take(remaining));
|
|
}
|
|
});
|
|
},
|
|
),
|
|
);
|
|
}
|
|
}
|