From 42fae1014123922bb5970720aee179e5f8d7c358 Mon Sep 17 00:00:00 2001 From: Caisking <3449750381@qq.com> Date: Fri, 12 Jul 2024 18:06:17 +0800 Subject: [PATCH] =?utf8?q?=E5=A2=9E=E5=8A=A0=E4=BF=A1=E6=81=AF=E6=96=87?= =?utf8?q?=E4=BB=B6=E5=85=B3=E6=9C=BA=E4=BF=9D=E5=AD=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .../uicomponents/UIComponents.cpp | 43 +++++++++++++++++++ .../uicomponents/UIComponents.h | 9 +++- .../message/src/MessageControlPresenter.cpp | 22 +++++++--- .../nativeui/message/src/MessageModel.cpp | 42 ++++++++++++++++-- .../message/src/MessageShowPresenter.cpp | 4 +- .../drivers/peripheral/utils/custom_utils.c | 5 +++ 6 files changed, 113 insertions(+), 12 deletions(-) diff --git a/code/sdk/application/wearable/CustomUIComponentTool/uicomponents/UIComponents.cpp b/code/sdk/application/wearable/CustomUIComponentTool/uicomponents/UIComponents.cpp index fb7b1a0dc..3c839e7eb 100644 --- a/code/sdk/application/wearable/CustomUIComponentTool/uicomponents/UIComponents.cpp +++ b/code/sdk/application/wearable/CustomUIComponentTool/uicomponents/UIComponents.cpp @@ -14,6 +14,49 @@ namespace OHOS #define DEFAULT_BACKGROUND_COLOR 0xff191919 #define PRESSED_BACKGROUND_COLOR 0xff249115 #define PRESSED_BORDER_COLOR 0xff0b1928 + /*** + * @description: 初始化标签 + * @param {UILabel} * + * @param {int} left + * @param {int} top + * @param {int} width + * @param {int} height + * @param {char} *viewId + * @param {char} *text + * @param {char} *fontName + * @param {int} fontSize + * @return + * @brief 初始化标签 + */ + bool InitUILabel(UILabel *&label, int left, int top, int width, int height, const char *viewId, const char *text, const char *fontName, int fontSize) + { + // 检查标签指针是否为空 + if (label == nullptr) + { + label = new UILabel(); + } + if (label == nullptr) + { + return false; // 内存分配失败,返回false + } + // 设置标签的位置和大小 + label->SetPosition(left, top); + label->Resize(width, height); + // 设置标签的视图 ID + label->SetViewId(viewId); + // 设置标签的样式 + label->SetStyle(STYLE_TEXT_FONT, fontSize); + label->SetFont(fontName, fontSize); + // 设置标签的文本和其他属性 + label->SetText(text); + // label->SetLineBreakMode(UILabel::LINE_BREAK_ELLIPSIS); + label->SetAlign(TEXT_ALIGNMENT_CENTER, TEXT_ALIGNMENT_CENTER); + // label->SetRollSpeed(0); + // 设置标签可见 + label->SetVisible(true); + // WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "InitLabelExt is succeed"); + return true; + } /*** * @description: 初始化标签 * @param {UILabel} * diff --git a/code/sdk/application/wearable/CustomUIComponentTool/uicomponents/UIComponents.h b/code/sdk/application/wearable/CustomUIComponentTool/uicomponents/UIComponents.h index b34dc3f60..4379cd419 100644 --- a/code/sdk/application/wearable/CustomUIComponentTool/uicomponents/UIComponents.h +++ b/code/sdk/application/wearable/CustomUIComponentTool/uicomponents/UIComponents.h @@ -1,3 +1,9 @@ +/*** + * @Date: 2024-05-30 15:59:37 + * @LastEditors: Caisking 3449750381@qq.com + * @LastEditTime: 2024-07-12 10:42:20 + * @FilePath: /FD230/code/sdk/application/wearable/CustomUIComponentTool/uicomponents/UIComponents.h + */ /*** * @Date: 2024-05-30 15:59:37 * @LastEditors: Caisking 3449750381@qq.com @@ -41,7 +47,8 @@ namespace OHOS { /*初始化label[可使用text]*/ bool InitLabel(UILabelExt *&label, int left, int top, int width, int height, const char *viewId, const char *text, const char *fontName, int fontSize); - + /*初始化label[可使用text]*/ + bool InitUILabel(UILabel *&label, int left, int top, int width, int height, const char *viewId, const char *text, const char *fontName, int fontSize); /*初始化button*/ bool InitButton(UIButton *&button, int left, int top, int width, int height, const char *viewId); /*初始化buttonExt*/ diff --git a/code/sdk/application/wearable/fd230app/nativeui/message/src/MessageControlPresenter.cpp b/code/sdk/application/wearable/fd230app/nativeui/message/src/MessageControlPresenter.cpp index 1db0397c9..f2b1c5526 100644 --- a/code/sdk/application/wearable/fd230app/nativeui/message/src/MessageControlPresenter.cpp +++ b/code/sdk/application/wearable/fd230app/nativeui/message/src/MessageControlPresenter.cpp @@ -1,3 +1,9 @@ +/*** + * @Date: 2024-06-22 11:36:16 + * @LastEditors: Caisking 3449750381@qq.com + * @LastEditTime: 2024-07-12 16:34:22 + * @FilePath: /FD230/code/sdk/application/wearable/fd230app/nativeui/message/src/MessageControlPresenter.cpp + */ /*** * @Date: 2024-06-22 11:36:16 * @LastEditors: Caisking 3449750381@qq.com @@ -63,13 +69,17 @@ namespace OHOS WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "Error: Invalid view type."); return false; } - MessageCardView::GetInstance()->GetList()->GetViewIndex(); - int index = p_MessageItemView->GetMessageListIndex(); - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "index is %d", index); + + int index1 = MessageCardView::GetInstance()->GetList()->GetViewIndex(); + int index2 = p_MessageItemView->GetMessageListIndex(); + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "index1 is %d, index2 is %d", index1, index2); - MessageModel::GetInstance().SetCurrentIndex(index); - // 切换到详情界面 - NativeAbility::GetInstance().ChangeSlice(VIEW_MESSAGE_SHOW); + if (index2 > -1 && index2 < 11) + { + MessageModel::GetInstance().SetCurrentIndex(index2); + // 切换到详情界面 + NativeAbility::GetInstance().ChangeSlice(VIEW_MESSAGE_SHOW); + } return true; // 返回 true 表示点击事件处理成功 } diff --git a/code/sdk/application/wearable/fd230app/nativeui/message/src/MessageModel.cpp b/code/sdk/application/wearable/fd230app/nativeui/message/src/MessageModel.cpp index 77b04e476..f417b0bc7 100644 --- a/code/sdk/application/wearable/fd230app/nativeui/message/src/MessageModel.cpp +++ b/code/sdk/application/wearable/fd230app/nativeui/message/src/MessageModel.cpp @@ -29,7 +29,7 @@ #include "broadcast_feature.h" #include "broadcast_service.h" -#define MESSAGE_JSON_FILE "user/userdata/message/ancs.json" +#define MESSAGE_JSON_FILE "user/userdata/ancs.json" #define MESSAGE_JSON_DIR "user/userdata/message/" #define PRINTFDATA 0 @@ -116,6 +116,14 @@ extern "C" CustomEventPublish(TOPIC_EVENT_MESSAGENOTE, NULL, 0); } } + + void saveancsdata() + { + printf("saveancs\r\n"); + OHOS::MessageModel::GetInstance().SaveMessagesToStorage(); + return; + } + #ifdef __cplusplus } #endif @@ -125,6 +133,7 @@ namespace OHOS MessageModel::MessageModel() { WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "MessageModel"); + LoadMessagesFromStorage(); } MessageModel::~MessageModel() { @@ -181,7 +190,7 @@ namespace OHOS } // 清空现有数据 - MessageVector.clear(); + // MessageVector.clear(); // 将JSON数据转换为MessageData并添加到消息数组中 int dataCount = cJSON_GetArraySize(messageArray); @@ -200,6 +209,16 @@ namespace OHOS data.labelDetails = labelDetails->valuestring; data.labelTitle = labelTitle->valuestring; data.timestamp = timestamp->valuedouble; + + char labelTime[20]; + if (uapi_get_TIME_STR(data.timestamp, labelTime, '.', ':', DATE_Y_M_D_H_M) != ERRCODE_SUCC) + { + data.labelTime = std::string("--"); + } + else + { + data.labelTime = std::string(labelTime); + } MessageVector.push_back(data); } @@ -212,6 +231,23 @@ namespace OHOS // 保存Message文件 bool MessageModel::SaveMessagesToStorage() { + if (MessageVector.empty()) + { + // 清空文件 + std::ofstream file(MESSAGE_JSON_FILE, std::ofstream::out | std::ofstream::trunc); + if (file.is_open()) + { + file.close(); + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "MessageVector is empty, existing file was cleared"); + return true; + } + else + { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "MessageVector is empty,Failed to open file for writing"); + return false; + } + } + cJSON *root = cJSON_CreateObject(); cJSON *messageArray = cJSON_CreateArray(); @@ -228,7 +264,7 @@ namespace OHOS cJSON_AddItemToObject(root, "messages", messageArray); - std::ofstream file(MESSAGE_JSON_FILE); + std::ofstream file(MESSAGE_JSON_FILE, std::ofstream::out | std::ofstream::trunc); if (!file.is_open()) { WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "Failed to open file for writing"); diff --git a/code/sdk/application/wearable/fd230app/nativeui/message/src/MessageShowPresenter.cpp b/code/sdk/application/wearable/fd230app/nativeui/message/src/MessageShowPresenter.cpp index ef2a34016..692688cfa 100644 --- a/code/sdk/application/wearable/fd230app/nativeui/message/src/MessageShowPresenter.cpp +++ b/code/sdk/application/wearable/fd230app/nativeui/message/src/MessageShowPresenter.cpp @@ -1,4 +1,4 @@ -/*** +/*** * @Date: 2024-06-17 19:39:36 * @LastEditors: Caisking 3449750381@qq.com * @LastEditTime: 2024-07-11 18:59:45 @@ -75,12 +75,12 @@ namespace OHOS bool MessageShowPresenter::OnDrag(UIView &view, const DragEvent &event) { - if (event.GetDragDirection() == DragEvent::DIRECTION_LEFT_TO_RIGHT && (event.GetDeltaX() > 20) && (event.GetDeltaX() > (2 * MATH_ABS(event.GetDeltaY())))) { NativeAbility::GetInstance().ChangeSlice(VIEW_MESSAGE); } + return true; } diff --git a/code/sdk/drivers/peripheral/utils/custom_utils.c b/code/sdk/drivers/peripheral/utils/custom_utils.c index ae59e87ac..f3b1726d5 100644 --- a/code/sdk/drivers/peripheral/utils/custom_utils.c +++ b/code/sdk/drivers/peripheral/utils/custom_utils.c @@ -150,10 +150,14 @@ void uapi_reboot_device(void) void shutdown_reboot_prejobs_task(void) { + //保存步数 do_cywee_service_check_msg(CYWEE_SERVICE_CMD_SAVE_DATA,NULL); addressbookTowrite(); + + //保存信息文件 + saveancsdata(); dial_file_save(); //同步所有需要保存实时数据到config文件 @@ -162,6 +166,7 @@ void shutdown_reboot_prejobs_task(void) cm_sync_all_config_to_disk(); //重启前要保存时间 uapi_calendar_store_date(); + } static bool doing_shutdown_or_reboot = false; void shutdown_reboot_prejobs(void) -- 2.17.1