|
|
@@ -19,6 +19,7 @@ import 'package:electronic_assistant/resource/string.gen.dart';
|
|
|
import 'package:electronic_assistant/utils/toast_util.dart';
|
|
|
import 'package:flutter/material.dart';
|
|
|
import 'package:flutter/services.dart';
|
|
|
+import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
|
|
|
import 'package:get/get.dart';
|
|
|
import 'package:pull_to_refresh/pull_to_refresh.dart';
|
|
|
import 'package:uuid/uuid.dart';
|
|
|
@@ -47,7 +48,8 @@ class ChatController extends BaseController {
|
|
|
|
|
|
bool isConsumeElectric = false;
|
|
|
|
|
|
- final Set<StreamSubscription> _streamChatSubscriptions = {};
|
|
|
+ StreamSubscription? _streamChatSubscription;
|
|
|
+ ProgressingChatItem? _nowProgressingChatItem;
|
|
|
|
|
|
@override
|
|
|
void onInit() {
|
|
|
@@ -113,7 +115,6 @@ class ChatController extends BaseController {
|
|
|
return;
|
|
|
}
|
|
|
String chatContent = inputController.text;
|
|
|
- inputController.clear();
|
|
|
_sendMessage(chatContent);
|
|
|
}
|
|
|
|
|
|
@@ -178,6 +179,14 @@ class ChatController extends BaseController {
|
|
|
}
|
|
|
|
|
|
void _sendMessage(String chatContent) {
|
|
|
+ final lastItem = chatItems.first;
|
|
|
+ if ((lastItem is ProgressingChatItem) &&
|
|
|
+ !(lastItem.isFinished.value || lastItem.isFailed.value)) {
|
|
|
+ ToastUtil.showToast(StringName.chatReplying.tr,
|
|
|
+ displayType: SmartToastType.onlyRefresh);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ inputController.clear();
|
|
|
chatItems.insert(0, createUserChatItem(chatContent));
|
|
|
|
|
|
ProgressingChatItem progressingChatItem = ProgressingChatItem(
|
|
|
@@ -189,6 +198,7 @@ class ChatController extends BaseController {
|
|
|
createTime: DateTime.now().toString(),
|
|
|
);
|
|
|
chatItems.insert(0, progressingChatItem);
|
|
|
+ _nowProgressingChatItem = progressingChatItem;
|
|
|
|
|
|
_scrollToBottom();
|
|
|
if (talkInfo.value == null) {
|
|
|
@@ -200,13 +210,13 @@ class ChatController extends BaseController {
|
|
|
EventId.id: EventId.id_001,
|
|
|
});
|
|
|
}
|
|
|
+ _streamChatSubscription?.cancel();
|
|
|
chatRepository
|
|
|
.streamChat(chatContent,
|
|
|
talkId: talkInfo.value?.id, agendaId: agenda.value?.id)
|
|
|
.then((stream) {
|
|
|
progressingChatItem.setGraduallyFinishedListener();
|
|
|
- StreamSubscription? sub;
|
|
|
- sub = stream.listen((event) {
|
|
|
+ _streamChatSubscription = stream.listen((event) {
|
|
|
try {
|
|
|
Map<String, dynamic> json = jsonDecode(event.data);
|
|
|
if (json.isEmpty) {
|
|
|
@@ -225,18 +235,15 @@ class ChatController extends BaseController {
|
|
|
chatAiTagId.value = progressingChatItem.id;
|
|
|
} catch (ignore) {}
|
|
|
}, onDone: () {
|
|
|
- _streamChatSubscriptions.remove(sub);
|
|
|
progressingChatItem.setAppendDone();
|
|
|
progressingChatItem.content =
|
|
|
progressingChatItem.graduallyController.graduallyTxt;
|
|
|
}, onError: (error) {
|
|
|
- _streamChatSubscriptions.remove(sub);
|
|
|
progressingChatItem.isFailed.value = true;
|
|
|
progressingChatItem.error.value = "网络错误,请检查网络连接";
|
|
|
debugPrint("error: $error");
|
|
|
debugPrintStack();
|
|
|
});
|
|
|
- _streamChatSubscriptions.add(sub);
|
|
|
}).catchError((error) {
|
|
|
progressingChatItem.isFailed.value = true;
|
|
|
if (error is ServerErrorException) {
|
|
|
@@ -332,8 +339,7 @@ class ChatController extends BaseController {
|
|
|
if (isConsumeElectric) {
|
|
|
accountRepository.refreshUserInfo();
|
|
|
}
|
|
|
- for (var sub in _streamChatSubscriptions) {
|
|
|
- sub.cancel();
|
|
|
- }
|
|
|
+ _nowProgressingChatItem?.graduallyController.dispose();
|
|
|
+ _streamChatSubscription?.cancel();
|
|
|
}
|
|
|
}
|