From d9f5787407c73e34c74626395facf0eaa7dc7c8d Mon Sep 17 00:00:00 2001 From: luozheng <1732036298@qq.com> Date: Tue, 16 Jul 2024 11:29:58 +0800 Subject: [PATCH] =?utf8?q?Ai=E8=AF=AD=E9=9F=B3=E8=BF=94=E5=9B=9E=E7=BB=93?= =?utf8?q?=E6=9E=9C=E7=95=8C=E9=9D=A2=E4=BC=98=E5=8C=96=EF=BC=9A=E7=8E=B0?= =?utf8?q?=E5=9C=A8=E5=AF=B9=E8=AF=9D=E4=B8=BA=E4=B8=80=E5=B7=A6=E4=B8=80?= =?utf8?q?=E5=8F=B3=E5=8C=BA=E5=88=86=E6=9B=B4=E6=98=8E=E6=98=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .../include/AivoiceChatContentListItemView.h | 4 +- .../nativeui/aivoice/src/AiVoiceModel.cpp | 39 ++++++++++--------- .../src/AivoiceChatContentListItemView.cpp | 39 ++++++++++++++----- .../nativeui/uiservice/include/ui_service.h | 7 ++-- .../nativeui/uiservice/src/ui_service.cpp | 26 +++++++++---- 5 files changed, 75 insertions(+), 40 deletions(-) diff --git a/code/sdk/application/wearable/fd230app/nativeui/aivoice/include/AivoiceChatContentListItemView.h b/code/sdk/application/wearable/fd230app/nativeui/aivoice/include/AivoiceChatContentListItemView.h index d2113865a..080df6c4a 100644 --- a/code/sdk/application/wearable/fd230app/nativeui/aivoice/include/AivoiceChatContentListItemView.h +++ b/code/sdk/application/wearable/fd230app/nativeui/aivoice/include/AivoiceChatContentListItemView.h @@ -2,7 +2,7 @@ * @Author: luozheng 1732036298@qq.com * @Date: 2024-06-29 17:26:19 * @LastEditors: luozheng 1732036298@qq.com - * @LastEditTime: 2024-07-15 20:21:16 + * @LastEditTime: 2024-07-16 10:14:28 * @FilePath: /FD230/code/sdk/application/wearable/fd230app/nativeui/aivoice/include/AivoiceChatContentListItemView.h */ @@ -12,6 +12,7 @@ #include "components/ui_view_group.h" #include "components/ui_label_ext.h" #include "components/ui_scroll_view.h" +#include "components/ui_image_view.h" #include "View.h" #include "iostream" #include "ChatContentItem.h" @@ -28,6 +29,7 @@ namespace OHOS private: UILabelExt chatContentLabel; + UIImageView chatContentImage; UIScrollView chatContentScrollView; int m_viewHeight; bool m_isAiAnswerFalg{false}; diff --git a/code/sdk/application/wearable/fd230app/nativeui/aivoice/src/AiVoiceModel.cpp b/code/sdk/application/wearable/fd230app/nativeui/aivoice/src/AiVoiceModel.cpp index 4533eb756..f00c8e09a 100644 --- a/code/sdk/application/wearable/fd230app/nativeui/aivoice/src/AiVoiceModel.cpp +++ b/code/sdk/application/wearable/fd230app/nativeui/aivoice/src/AiVoiceModel.cpp @@ -2,7 +2,7 @@ * @Author: luozheng 1732036298@qq.com * @Date: 2024-06-20 15:14:50 * @LastEditors: luozheng 1732036298@qq.com - * @LastEditTime: 2024-07-15 20:28:07 + * @LastEditTime: 2024-07-16 10:00:58 * @FilePath: /FD230/code/sdk/application/wearable/fd230app/nativeui/aivoice/src/AiVoiceModel.cpp */ #include "aivoice/include/AiVoiceModel.h" @@ -107,7 +107,7 @@ namespace OHOS /*添加聊天内容项到数组*/ bool AiVoiceModel::AddChatContentToItemArray(const ChatContentItem &item) { - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "ChatContentListCount = %d", ChatContentListCount); + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "ChatContentListCount = %d", ChatContentListCount); // 检查 ChatContentListCount 是否超出范围 if (ChatContentListCount >= MaxChatContentListCount) { @@ -144,7 +144,7 @@ namespace OHOS /*初始化历史对话数据*/ bool AiVoiceModel::InitchatContentItems(void) { - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "AiVoiceModel::InitchatContentItems"); + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "AiVoiceModel::InitchatContentItems"); // 获取单例对象 AivoiceReturnResultTextView *p_AivoiceReturnResultTextView = AivoiceReturnResultTextView::GetInstance(); @@ -195,7 +195,7 @@ namespace OHOS // 函数定义:移除列表的最后两项或清空列表 void AiVoiceModel::RemoveLastTwoItemsOrClear() { - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "AiVoiceModel::RemoveLastTwoItemsOrClear"); + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "AiVoiceModel::RemoveLastTwoItemsOrClear"); AivoiceReturnResultTextView *p_AivoiceReturnResultTextView = AivoiceReturnResultTextView::GetInstance(); if (p_AivoiceReturnResultTextView == nullptr) { @@ -229,7 +229,7 @@ namespace OHOS /* 更新用户问题字符串 */ bool AiVoiceModel::UpdateUserQuestionStr() { - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "AiVoiceModel::UpdateUserQuestionStr"); + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "AiVoiceModel::UpdateUserQuestionStr"); RemoveLastTwoItemsOrClear(); @@ -250,7 +250,7 @@ namespace OHOS AivoiceChatContentListAdapter *adapter = static_cast(p_AivoiceReturnResultTextView->GetAivoiceChatContentListAdapter()); if (adapter) { - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "GetChatContentListCount() - 1 = %d,line =%d", GetChatContentListCount() - 1, __LINE__); + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "GetChatContentListCount() - 1 = %d,line =%d", GetChatContentListCount() - 1, __LINE__); adapter->GetChatContentItem().push_back(chatTtems[GetChatContentListCount() - 1]); p_AivoiceReturnResultTextView->GetChatContentList()->RefreshList(); } @@ -266,7 +266,7 @@ namespace OHOS /*更新Ai答案字符串*/ bool AiVoiceModel::UpdateAiAnswerStr() { - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "AiVoiceModel::UpdateAiAnswerStr"); + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "AiVoiceModel::UpdateAiAnswerStr"); int index = GetChatContentListCount() - 1; if (index < 0 || index >= MaxChatContentListCount) @@ -302,7 +302,7 @@ namespace OHOS { adapter->GetChatContentItem().pop_back(); } - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "GetChatContentListCount() - 1 = %d,line = %d", GetChatContentListCount() - 1, __LINE__); + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "GetChatContentListCount() - 1 = %d,line = %d", GetChatContentListCount() - 1, __LINE__); // 将更新后的 AI 回答内容添加到列表 adapter->GetChatContentItem().push_back(chatTtems[GetChatContentListCount() - 1]); p_AivoiceReturnResultTextView->GetChatContentList()->RefreshList(); @@ -320,7 +320,7 @@ namespace OHOS std::string AiVoiceModel::GetUserQuestionStr(void) { // userQuestionStr = "写一篇100字文章"; - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "GetUserQuestionStr:%s\n", userQuestionStr.c_str()); + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "GetUserQuestionStr:%s\n", userQuestionStr.c_str()); return userQuestionStr; } @@ -329,24 +329,25 @@ namespace OHOS { if (!entryTextViewState) { - SendMsgToUIService(UI_AI_RESPONSE_USERDATA, 0, nullptr, 0); + SendMsgToUIService(UI_AI_JUMP_TO_RESULT_VIEW, 0, nullptr, 0); entryTextViewState = true; // 进入返回结果界面的标志 避免重复发消息 usleep(50000); // 延时等待创建完成,0.05秒 } /* -------------------------------------------------------------------------- */ userQuestionStr += str; /* -------------------------------------------------------------------------- */ - if (!UpdateUserQuestionStr()) - { - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "AiVoiceModel::UpdateUserQuestionStr fail!!"); - } + // if (!UpdateUserQuestionStr()) + // { + // WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "AiVoiceModel::UpdateUserQuestionStr fail!!"); + // } + SendMsgToUIService(UI_UPDATE_USER_QUESTION_STR, 0, nullptr, 0); } /*获取Ai回答的字符串*/ std::string AiVoiceModel::GetAiAnswerStr(void) { // aiAnswerStr = "春风化雨,时光荏苒。百年之内,生命如白驹过隙,转瞬即逝。唯有心存善念,用爱化解芥蒂,用智慧建构未来,才是人生的真正意义。让我们携手共进,用每一分时光去关爱他人,用每一滴汗水去创造奇迹,用每一个眼神去传递温暖。让这个世界因我们的存在而变得更加美好。生命短暂,但只要我们怀着虔诚的心,坚韧前行,我们就一定能在有限的时光里留下无限的可能。"; - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "GetAiAnswerStr:%s\n", aiAnswerStr.c_str()); + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "GetAiAnswerStr:%s\n", aiAnswerStr.c_str()); return aiAnswerStr; } @@ -365,7 +366,7 @@ namespace OHOS // { // WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "AiVoiceModel::UpdateAiAnswerStr fail!!"); // } - SendMsgToUIService(UI_UPDATE_AI_QUESTION_STR, 0, nullptr, 0); + SendMsgToUIService(UI_UPDATE_AI_ANSWER_STR, 0, nullptr, 0); } /*清空上次用户问题和Ai回答字符串*/ @@ -394,7 +395,7 @@ namespace OHOS // 开始聆听并录取用户问题 void AiVoiceModel::StartListenAndRecordingUserQuestion(void) { - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "AiVoiceModel::StartListenAndRecordingUserQuestion"); + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "AiVoiceModel::StartListenAndRecordingUserQuestion"); AiVoiceView *view_ = AiVoiceView::GetInstance(); if (view_ == nullptr) { @@ -428,7 +429,7 @@ namespace OHOS // 停止聆听用户问题 void AiVoiceModel::StopListenAndRecordingUserQuestion(void) { - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "AiVoiceModel::StopListenAndRecordingUserQuestion"); + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "AiVoiceModel::StopListenAndRecordingUserQuestion"); int32_t argc = 1; uint8_t *argv = (uint8_t *)"stop"; AudioCaptureSample(argc, (const char **)&argv); @@ -438,7 +439,7 @@ namespace OHOS /*检查录音文件是否存在并发送语音准备操作*/ void AiVoiceModel::CheckFileAndSendAIIsReady(void) { - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "AiVoiceModel::CheckFileAndSendAIIsReady"); + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "AiVoiceModel::CheckFileAndSendAIIsReady"); const std::string &filePath = AIAIAudioPath; FILE *file = fopen(filePath.c_str(), "r"); if (file != nullptr) diff --git a/code/sdk/application/wearable/fd230app/nativeui/aivoice/src/AivoiceChatContentListItemView.cpp b/code/sdk/application/wearable/fd230app/nativeui/aivoice/src/AivoiceChatContentListItemView.cpp index 88ec02796..f0cbc2e8c 100644 --- a/code/sdk/application/wearable/fd230app/nativeui/aivoice/src/AivoiceChatContentListItemView.cpp +++ b/code/sdk/application/wearable/fd230app/nativeui/aivoice/src/AivoiceChatContentListItemView.cpp @@ -2,7 +2,7 @@ * @Author: luozheng 1732036298@qq.com * @Date: 2024-06-29 17:27:25 * @LastEditors: luozheng 1732036298@qq.com - * @LastEditTime: 2024-07-15 20:23:27 + * @LastEditTime: 2024-07-16 11:19:28 * @FilePath: /FD230/code/sdk/application/wearable/fd230app/nativeui/aivoice/src/AivoiceChatContentListItemView.cpp */ @@ -24,6 +24,21 @@ namespace OHOS static constexpr const int64_t RADIUS = 20; + static constexpr const int16_t QUESTION_CHATCONTENTIMAGE_X = 0; + static constexpr const int16_t ANSWER_CHATCONTENTIMAGE_X = 289; + + static constexpr const int16_t QUESTION_CHATCONTENTSCROLLVIEW_X = 20; + static constexpr const int16_t ANSWER_CHATCONTENTSCROLLVIEW_X = 59; + + static constexpr const int16_t QUESTION_CHATCONTENTSCROLLVIEW_W = 340; + static constexpr const int16_t ANSWER_CHATCONTENTSCROLLVIEW_W = 280; + + static constexpr const int16_t QUESTION_CHATCONTENTLABEL_W = 260; + static constexpr const int16_t ANSWER_CHATCONTENTLABEL_W = 200; + + static constexpr const char *QUESTION_ITEM_IMAGE_SRC = "/user/res/AIVoice/AI_RecGreen.bin"; + static constexpr const char *ANSWER_ITEM_IMAGE_SRC = "/user/res/AIVoice/AI_RecBlack.bin"; + static constexpr const int64_t QUESTION_ITEM_VIEW_COLOR = 0xff4fc34c; /*0xff4fc34c*/ static constexpr const int64_t ANSWER_ITEM_VIEW_COLOR = 0xff191919; /*0xff191919*/ @@ -36,12 +51,14 @@ namespace OHOS SetStyle(STYLE_BACKGROUND_OPA, 0xff); SetTouchable(true); SetUpChild(); + Add(&chatContentImage); chatContentScrollView.Add(&chatContentLabel); Add(&chatContentScrollView); } AivoiceChatContentListItemView::~AivoiceChatContentListItemView() { + Remove(&chatContentImage); chatContentScrollView.Remove(&chatContentLabel); Remove(&chatContentScrollView); } @@ -55,26 +72,30 @@ namespace OHOS { Resize(ITEM_VIEW_W, viewHeight + ITEM_VIEW_H); - chatContentScrollView.Resize(isAiAnswerFlag ? 360 : 280, GetHeight() - 30); + chatContentImage.SetPosition(isAiAnswerFlag ? QUESTION_CHATCONTENTIMAGE_X : ANSWER_CHATCONTENTIMAGE_X, 0, 70, 30); + chatContentImage.SetSrc(isAiAnswerFlag ? ANSWER_ITEM_IMAGE_SRC : QUESTION_ITEM_IMAGE_SRC); + + chatContentScrollView.SetPosition(isAiAnswerFlag ? QUESTION_CHATCONTENTSCROLLVIEW_X : ANSWER_CHATCONTENTSCROLLVIEW_X, 0); + chatContentScrollView.Resize(isAiAnswerFlag ? QUESTION_CHATCONTENTSCROLLVIEW_W : ANSWER_CHATCONTENTSCROLLVIEW_W, GetHeight() - 30); chatContentScrollView.SetStyle(STYLE_BACKGROUND_COLOR, isAiAnswerFlag ? ANSWER_ITEM_VIEW_COLOR : QUESTION_ITEM_VIEW_COLOR); - chatContentLabel.Resize(isAiAnswerFlag ? 280 : 200, viewHeight); + chatContentLabel.Resize(isAiAnswerFlag ? QUESTION_CHATCONTENTLABEL_W : ANSWER_CHATCONTENTLABEL_W, viewHeight); } void AivoiceChatContentListItemView::SetUpChild() { - int16_t scrollViewWidth = m_isAiAnswerFalg ? 360 : 280; + chatContentImage.SetPosition(m_isAiAnswerFalg ? QUESTION_CHATCONTENTIMAGE_X : ANSWER_CHATCONTENTIMAGE_X, 0, 70, 30); + chatContentImage.SetSrc(m_isAiAnswerFalg ? ANSWER_ITEM_IMAGE_SRC : QUESTION_ITEM_IMAGE_SRC); - chatContentScrollView.SetPosition(m_isAiAnswerFalg ? 0 : 59, 0); - chatContentScrollView.Resize(scrollViewWidth, GetHeight() - 30); - chatContentScrollView.SetStyle(STYLE_BACKGROUND_COLOR, - m_isAiAnswerFalg ? ANSWER_ITEM_VIEW_COLOR : QUESTION_ITEM_VIEW_COLOR); + chatContentScrollView.SetPosition(m_isAiAnswerFalg ? QUESTION_CHATCONTENTSCROLLVIEW_X : ANSWER_CHATCONTENTSCROLLVIEW_X, 0); + chatContentScrollView.Resize(m_isAiAnswerFalg ? QUESTION_CHATCONTENTSCROLLVIEW_W : ANSWER_CHATCONTENTSCROLLVIEW_W, GetHeight() - 30); + chatContentScrollView.SetStyle(STYLE_BACKGROUND_COLOR, m_isAiAnswerFalg ? ANSWER_ITEM_VIEW_COLOR : QUESTION_ITEM_VIEW_COLOR); chatContentScrollView.SetStyle(STYLE_BACKGROUND_OPA, 0xff); chatContentScrollView.SetStyle(STYLE_BORDER_RADIUS, RADIUS); chatContentScrollView.SetTouchable(false); chatContentLabel.SetPosition(CHATCONTENTLABEL_X, CHATCONTENTLABEL_Y); - chatContentLabel.Resize(m_isAiAnswerFalg ? 280 : 200, m_viewHeight); + chatContentLabel.Resize(m_isAiAnswerFalg ? QUESTION_CHATCONTENTLABEL_W : ANSWER_CHATCONTENTLABEL_W, m_viewHeight); chatContentLabel.SetStyle(STYLE_TEXT_FONT, CHATCONTENTLABEL_TEXT_FONT); chatContentLabel.SetStyle(STYLE_LINE_HEIGHT, CHATCONTENTLABEL_LINE_HEIGHT); chatContentLabel.SetLineBreakMode(UILabel::LINE_BREAK_WRAP); diff --git a/code/sdk/application/wearable/fd230app/nativeui/uiservice/include/ui_service.h b/code/sdk/application/wearable/fd230app/nativeui/uiservice/include/ui_service.h index 29e2d708f..f2fbfb1f8 100644 --- a/code/sdk/application/wearable/fd230app/nativeui/uiservice/include/ui_service.h +++ b/code/sdk/application/wearable/fd230app/nativeui/uiservice/include/ui_service.h @@ -8,7 +8,7 @@ * @Author: luozheng 1732036298@qq.com * @Date: 2024-06-12 11:44:04 * @LastEditors: luozheng 1732036298@qq.com - * @LastEditTime: 2024-07-12 18:56:55 + * @LastEditTime: 2024-07-16 09:38:05 * @FilePath: /FD230/code/sdk/application/wearable/nativeapp/nativeui/uiservice/include/ui_service.h */ /* @@ -77,8 +77,9 @@ typedef enum { UI_UPDATE_WEATHER_DATA, //天气数据更新 UI_UPDATE_SLEEP_DATA, //睡眠数据更新 - UI_AI_RESPONSE_USERDATA, //跳转到返回结果视图 - UI_UPDATE_AI_QUESTION_STR, //更新AI答案的字符串 + UI_AI_JUMP_TO_RESULT_VIEW, //跳转到返回结果视图 + UI_UPDATE_USER_QUESTION_STR,//更新用户问题的字符串 + UI_UPDATE_AI_ANSWER_STR, //更新AI答案的字符串 MAX_NUM_HANDLER, } UiServiceMsgId; diff --git a/code/sdk/application/wearable/fd230app/nativeui/uiservice/src/ui_service.cpp b/code/sdk/application/wearable/fd230app/nativeui/uiservice/src/ui_service.cpp index d1943619e..1056e0084 100644 --- a/code/sdk/application/wearable/fd230app/nativeui/uiservice/src/ui_service.cpp +++ b/code/sdk/application/wearable/fd230app/nativeui/uiservice/src/ui_service.cpp @@ -103,8 +103,9 @@ static void HandleUpdateSystemVolume(Request *request); static void HandleUpdateCallVolume(Request *request); static void HandleUpdateVibrationMode(Request *request); static void HandleUpdateSilentMode(Request *request); -static void HandleAiResponseUserData(Request *request); -static void HandleUpdateAIQuestionStr(Request *request); +static void HandleAiJumpToResultView(Request *request); +static void HandleUpdateUserQuestionStr(Request *request); +static void HandleUpdateAIAnswerStr(Request *request); static void HandleUpdateSleep(Request *request); @@ -131,8 +132,9 @@ MsgHandlerEntry msgHandlers[] = { {UI_UPDATE_CALL_VOLUME, HandleUpdateCallVolume}, {UI_UPDATE_VIBRATION_MODE, HandleUpdateVibrationMode}, {UI_UPDATE_SILENT_MODE, HandleUpdateSilentMode}, - {UI_AI_RESPONSE_USERDATA, HandleAiResponseUserData}, - {UI_UPDATE_AI_QUESTION_STR, HandleUpdateAIQuestionStr}, + {UI_AI_JUMP_TO_RESULT_VIEW, HandleAiJumpToResultView}, + {UI_UPDATE_USER_QUESTION_STR,HandleUpdateUserQuestionStr}, + {UI_UPDATE_AI_ANSWER_STR, HandleUpdateAIAnswerStr}, {UI_UPDATE_SLEEP_DATA, HandleUpdateSleep}, }; @@ -520,17 +522,25 @@ void HandleUpdateSilentMode(Request *request) } //处理Ai回应的用户数据 -void HandleAiResponseUserData(Request *request) +void HandleAiJumpToResultView(Request *request) { - WEARABLE_LOGI(WEARABLE_LOG_MODULE_UI_SERVICE, "HandleAiResponseUserData"); + WEARABLE_LOGI(WEARABLE_LOG_MODULE_UI_SERVICE, "HandleAiJumpToResultView"); GraphicService::GetInstance()->PostGraphicEvent( std::bind(&OHOS::AiVoiceModel::ChangeToReturnResultTextView, &OHOS::AiVoiceModel::GetInstance())); } +//处理更新用户问题字符串 +void HandleUpdateUserQuestionStr(Request *request) +{ + WEARABLE_LOGI(WEARABLE_LOG_MODULE_UI_SERVICE, "HandleUpdateUserQuestionStr"); + GraphicService::GetInstance()->PostGraphicEvent( + std::bind(&OHOS::AiVoiceModel::UpdateUserQuestionStr, &OHOS::AiVoiceModel::GetInstance())); +} + //处理更新AI答案字符串 -void HandleUpdateAIQuestionStr(Request *request) +void HandleUpdateAIAnswerStr(Request *request) { - WEARABLE_LOGI(WEARABLE_LOG_MODULE_UI_SERVICE, "HandleUpdateAIQuestionStr"); + WEARABLE_LOGI(WEARABLE_LOG_MODULE_UI_SERVICE, "HandleUpdateAIAnswerStr"); GraphicService::GetInstance()->PostGraphicEvent( std::bind(&OHOS::AiVoiceModel::UpdateAiAnswerStr, &OHOS::AiVoiceModel::GetInstance())); } -- 2.17.1