bool AiVoiceModel::UpdateAiAnswerStr()
{
WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "AiVoiceModel::UpdateAiAnswerStr");
- int index = GetChatContentListCount() - 1;
- if (index < 0 || index >= MaxChatContentListCount)
- {
- WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "Invalid index: %d", index);
- return false;
- }
- if (chatTtems[index].isAiAnswer) // 处理ai1的多段回答
+ // 合并节流:上一次未结束则仅记录一次待处理
+ if (isAiAnswerUpdating)
{
- ChatContentListCount--;
+ WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "isAiAnswerUpdating");
+ // hasPendingAiAnswerUpdate = true;
+ return true;
}
+ isAiAnswerUpdating = true;
- if (!AddAiAnswerToChatItems())
+ bool success = true;
+ do
{
- WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "AddAiAnswerToChatItems fail!!");
- return false;
- }
+ int index = GetChatContentListCount() - 1;
+ if (index < 0 || index >= MaxChatContentListCount)
+ {
+ WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "Invalid index: %d", index);
+ success = false;
+ break;
+ }
- // 更新显示列表
- AivoiceReturnResultTextView *p_AivoiceReturnResultTextView = AivoiceReturnResultTextView::GetInstance();
- if (!p_AivoiceReturnResultTextView)
- {
- WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "AivoiceReturnResultTextView::GetInstance fail!!");
- return false;
- }
+ if (chatTtems[index].isAiAnswer) // 处理ai1的多段回答
+ {
+ ChatContentListCount--;
+ }
- AivoiceChatContentListAdapter *adapter = static_cast<AivoiceChatContentListAdapter *>(p_AivoiceReturnResultTextView->GetAivoiceChatContentListAdapter());
- if (adapter != nullptr && !adapter->GetChatContentItem().empty())
- {
- // 如果最后一条是 AI 回答,则移除
- if (adapter->GetChatContentItem().back().isAiAnswer)
+ if (!AddAiAnswerToChatItems())
{
- adapter->GetChatContentItem().pop_back();
+ WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "AddAiAnswerToChatItems fail!!");
+ success = false;
+ break;
}
- WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "GetChatContentListCount() - 1 = %d,line = %d", GetChatContentListCount() - 1, __LINE__);
- // 将更新后的 AI 回答内容添加到列表
- adapter->GetChatContentItem().push_back(chatTtems[GetChatContentListCount() - 1]);
- if (AiVoicePresenter::GetInstance())
- AiVoicePresenter::GetInstance()->calculateChatContentItemViewHeight(chatTtems[GetChatContentListCount() - 1].text, true);
- p_AivoiceReturnResultTextView->GetChatContentList()->RefreshList();
- }
- else
- {
- WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "Chat content adapter is nullptr!!");
- return false;
- }
- return true;
+ // 更新显示列表
+ AivoiceReturnResultTextView *p_AivoiceReturnResultTextView = AivoiceReturnResultTextView::GetInstance();
+ if (!p_AivoiceReturnResultTextView)
+ {
+ WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "AivoiceReturnResultTextView::GetInstance fail!!");
+ success = false;
+ break;
+ }
+
+ AivoiceChatContentListAdapter *adapter = static_cast<AivoiceChatContentListAdapter *>(p_AivoiceReturnResultTextView->GetAivoiceChatContentListAdapter());
+ if (adapter != nullptr && !adapter->GetChatContentItem().empty())
+ {
+ // 如果最后一条是 AI 回答,则移除
+ if (adapter->GetChatContentItem().back().isAiAnswer)
+ {
+ adapter->GetChatContentItem().pop_back();
+ }
+ WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "GetChatContentListCount() - 1 = %d,line = %d", GetChatContentListCount() - 1, __LINE__);
+ // 将更新后的 AI 回答内容添加到列表
+ adapter->GetChatContentItem().push_back(chatTtems[GetChatContentListCount() - 1]);
+ if (AiVoicePresenter::GetInstance())
+ AiVoicePresenter::GetInstance()->calculateChatContentItemViewHeight(chatTtems[GetChatContentListCount() - 1].text, true);
+ p_AivoiceReturnResultTextView->GetChatContentList()->RefreshList();
+ }
+ else
+ {
+ WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "Chat content adapter is nullptr!!");
+ success = false;
+ break;
+ }
+ } while (0);
+
+ // 标记本次结束
+ isAiAnswerUpdating = false;
+
+ // 若期间丢弃过调用,合并为一次刷新
+ // if (hasPendingAiAnswerUpdate)
+ // {
+ // hasPendingAiAnswerUpdate = false;
+ // SendMsgToUIService(UI_UPDATE_AI_ANSWER_STR, 0, nullptr, 0);
+ // }
+
+ return success;
}
void AiVoiceModel::SetIsClickable(bool value)
if (!OHOS::AiVoiceModel::GetInstance().GetIsLimitDisplayed()) // 如果ai答案没有显示到极限了,就更新ai答案字符串
{
aiAnswerStr += str;
- WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "接受字的符串:%s", aiAnswerStr.c_str());
+ WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "接受字的符串:%s", aiAnswerStr.c_str());
// SendMsgToUIService(UI_UPDATE_AI_ANSWER_STR, 0, nullptr, 0);
}
}
if (errnote.empty()) // 如果没有回应数据
{
WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "AiVoiceModel::GetErrnote is empty!!");
- AiVoiceModel::GetInstance().SetErrnote("响应内容为空");
- std::string str = AiVoiceModel::GetInstance().GetErrnote();
- SendMsgToUIService(UI_AI_JUMP_TO_RESPONDERR_VIEW, 0, (void *)str.c_str(), str.length());
+ // AiVoiceModel::GetInstance().SetErrnote("响应内容为空");
+ // std::string str = AiVoiceModel::GetInstance().GetErrnote();
+ // SendMsgToUIService(UI_AI_JUMP_TO_RESPONDERR_VIEW, 0, (void *)str.c_str(), str.length());
return;
}
}