-/***
- * @Date: 2024-06-29 18:03:40
- * @LastEditors: Caisking 3449750381@qq.com
- * @LastEditTime: 2024-07-10 21:34:39
- * @FilePath: /FD230/code/sdk/application/wearable/fd230app/nativeui/message/include/MessageModel.h
- */
-/***
- * @Date: 2024-06-29 18:03:40
- * @LastEditors: Caisking 3449750381@qq.com
- * @LastEditTime: 2024-07-10 17:18:42
- * @FilePath: /FD230/code/sdk/application/wearable/fd230app/nativeui/message/include/MessageModel.h
- */
-/***
- * @Date: 2024-06-21 09:56:18
- * @LastEditors: Caisking 3449750381@qq.com
- * @LastEditTime: 2024-07-08 09:22:12
- * @FilePath: /FD230/code/sdk/application/wearable/fd230app/nativeui/message/include/MessageModel.h
- */
-/***
- * @Date: 2024-04-15 10:55:58
- * @LastEditors: Caisking 3449750381@qq.com
- * @LastEditTime: 2024-06-20 10:02:36
- * @FilePath: /XTS_FD230/FD230/code/sdk/application/wearable/fd230app/nativeui/message/include/MessageModel.h
- */
+
#ifndef MESSAGE_MODEL_H
#define MESSAGE_MODEL_H
#include "UiConfig.h"
#include "MessagePresenter.h"
#include "message/include/MessageAdapter.h"
+#include "bletest.h"
+#include <vector>
namespace OHOS
{
class MessageModel
{
public:
+ MessageModel();
+ ~MessageModel();
+ void Init();
+
static MessageModel &GetInstance();
void InitMessageListItems(); // 初始化消息列表项
const MessageListItem *GetMessageListItems() const; // 获取消息列表项
- // uint8_t GetMessageListCount() const; // 获取消息列表数量
+ uint8_t GetMessageCount() const; // 获取消息数量
+ void AddMessageData(ancs_data_info_t *messageInfo); // 增加消息项
+ bool LoadMessagesFromStorage(); // 初始化信息数组 {开机调用一次}
+ bool SaveMessagesToStorage(); // 保存信息数组 {关机时候调用}
+ bool SetMessageAsRead(); // 设置当前项已读
+ void SetCurrentIndex(uint8_t index); // 设置当前下标
+ uint8_t GetCurrentIndex(); // 获取当前下标
+ MessageData *GetCurrentMessageData(); // 获取当前项
+ bool DeleteCurrentMessage(); // 删除当前项
+ bool ClearAllMessages(); // 删除所有数据
+ MessageData *GetNewMessage(); // 获取最新信息
+ void SetPopupViewState(bool state); // 设置弹出视图状态
+ bool GetPopupViewState(); // 获取弹出视图状态
+ bool HasDataVector(); // 判断数组是否存在数据
- bool HasDataJson(); // 判断是否存在数据Json
- bool GetJsonLoadStatus(); // 获取Json加载状态
- std::vector<MessageData> LoadMessageData(); // 加载消息数据
- uint8_t GetMessageCount() const; // 获取消息数量
- void SetPopupViewState(bool state); // 设置弹出视图状态
- bool GetPopupViewState(); // 获取弹出视图状态
- void PrintData(const MessageData messageData); // 打印数据
- MessageData *GetCurrentMessageData(); // 获取当前消息数据
- void SetCurrentMessageData(MessageData *data); // 设置当前消息数据
- bool ClearAllData(); // 清除所有数据
- bool DeleteSingleItem(int currentIndex); // 删除单个项
- MessageData *GetNewestData(); // 获取最新信息项
- void SetCurrentIndex(uint8_t index); // 设置当前项
- uint8_t GetCurrentIndex(); // 获取当前项
- bool SetMessageIsread(); // 设置当前项已读
- void DeleteExcessFiles(std::vector<std::string> &filenames); // 删除文件
- void SaveMessageData(const MessageData data, const int index); // 保存json数据
private:
- MessageModel();
- MessageModel(const MessageModel &);
MessageModel &operator=(const MessageModel &);
- virtual ~MessageModel();
- uint8_t numberOfItems; // 项数
+ uint16_t numberOfItems; // 项数
static const uint16_t MaxMessageListCount = 10; // 消息列表最大数量
MessageListItem Messages[MaxMessageListCount] = {0, false, nullptr, nullptr, nullptr}; // 消息列表
- uint8_t indexCurrent; // 选择的项
- std::map<int, std::string> fileIndexMap;
+ uint8_t currentIndex_; // 选择的项
+ std::vector<MessageData> MessageVector;
- bool jsonDataExists{false}; // Json数据存在标志
- bool popupViewState{false}; // 弹出视图状态
+ bool popupViewState_{false}; // 弹出视图状态
};
}
-#endif
\ No newline at end of file
+#endif
+
+#if 0
+ bool HasDataJson(); // 判断是否存在数据Json
+ bool GetJsonLoadStatus(); // 获取Json加载状态
+ std::vector<MessageData> LoadMessageData(); // 加载消息数据
+ uint8_t GetMessageCount() const; // 获取消息数量
+ void SetPopupViewState(bool state); // 设置弹出视图状态
+ bool GetPopupViewState(); // 获取弹出视图状态
+ void PrintData(const MessageData messageData); // 打印数据
+ MessageData *GetCurrentMessageData(); // 获取当前消息数据
+ void SetCurrentMessageData(MessageData *data); // 设置当前消息数据
+ bool ClearAllData(); // 清除所有数据
+ bool DeleteSingleItem(int currentIndex); // 删除单个项
+ MessageData *GetNewestData(int index); // 获取最新信息项
+ void SetCurrentIndex(uint8_t index); // 设置当前项
+ uint8_t GetCurrentIndex(); // 获取当前项
+ bool SetMessageIsread(); // 设置当前项已读
+ void DeleteExcessFiles(std::vector<std::string> &filenames); // 删除文件
+ void SaveMessageData(const MessageData data, const int index); // 保存json数据
+#endif
* @LastEditTime: 2024-06-22 15:32:37
* @FilePath: /XTS_FD230/FD230/code/sdk/application/wearable/fd230app/nativeui/message/src/MessageModel.cpp
*/
-/*
- * @Author: Caisking 3449750381@qq.com
- * @Date: 2024-01-29 19:58:37
- * @LastEditors: Caisking 3449750381@qq.com
- * @LastEditTime: 2024-06-14 19:15:15
- * @FilePath: /XTS_FD230/FD230/code/sdk/application/wearable/fd230app/nativeui/message/src/MessageModel.cpp
- * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
- */
#include "message/include/MessageModel.h"
#include "common/image_cache_manager.h"
#include <dirent.h>
#include <map>
#include <filesystem>
+#include <service/graphic_service.h>
+#include "thread_adapter.h"
+#include "broadcast_feature.h"
+#include "broadcast_service.h"
#define MESSAGE_JSON_FILE "user/userdata/message/ancs.json"
#define MESSAGE_JSON_DIR "user/userdata/message/"
#define PRINTFDATA 0
extern "C" errcode_t uapi_get_TIME_STR(uint32_t time, char *p_time, char date_separator, char time_separator, date_format_t format);
+
+#ifdef __cplusplus
+extern "C"
+{
+#endif
+ static void Notify(void *args, const Topic topic, Request *request);
+
+ using namespace OHOS;
+
+ Subscriber MessageSubscirber = {
+ .identity = NULL,
+ .args = NULL,
+ .Notify = Notify,
+ };
+
+ void MessageSubscribeTopic(Topic topic)
+ {
+ WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "stepsubscribeTopic, topic=%u", topic);
+ BroadcastApi *broadcastApi = NULL;
+ IUnknown *api = SAMGR_GetInstance()->GetFeatureApi(BROADCAST_SERVICE_NAME, BROADCAST_FEATURE_NAME);
+ if (api == NULL)
+ {
+ WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "stepsubscribeTopic, GetFeatureApi failed");
+ return;
+ }
+ int result = api->QueryInterface(api, DEFAULT_VERSION, (void **)&broadcastApi);
+ if (result != 0 || broadcastApi == NULL)
+ {
+ WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "stepsubscribeTopic, QueryInterface failed");
+ return;
+ }
+ broadcastApi->SubscribeTopic((IUnknown *)broadcastApi, topic, &MessageSubscirber);
+ broadcastApi->Release((IUnknown *)broadcastApi);
+ }
+
+ void MessageUnsubscribeTopic(Topic topic)
+ {
+ WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "MessageUnsubscribeTopic, topic=%u", topic);
+ BroadcastApi *broadcastApi = NULL;
+ IUnknown *api = SAMGR_GetInstance()->GetFeatureApi(BROADCAST_SERVICE_NAME, BROADCAST_FEATURE_NAME);
+ if (api == NULL)
+ {
+ WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "MessageUnsubscribeTopic, GetFeatureApi failed");
+ return;
+ }
+ int result = api->QueryInterface(api, DEFAULT_VERSION, (void **)&broadcastApi);
+ if (result != 0 || broadcastApi == NULL)
+ {
+ WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "MessageUnsubscribeTopic, QueryInterface failed");
+ return;
+ }
+ broadcastApi->UnsubscribeTopic((IUnknown *)broadcastApi, topic, &MessageSubscirber);
+ broadcastApi->Release((IUnknown *)broadcastApi);
+ }
+
+ static void Notify(void *args, const Topic topic, Request *request)
+ {
+ UNUSED(args);
+ UNUSED(topic);
+ if (request == NULL)
+ {
+ return;
+ }
+ WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "request %d\r\n", request->msgId);
+ if (request->msgId == TOPIC_EVENT_MESSAGE_UPDATA)
+ {
+ ancs_data_info_t *messageInfo = new ancs_data_info_t();
+ int rets = memcpy_s(messageInfo, sizeof(ancs_data_info_t), (ancs_data_info_t *)request->data, request->len);
+ if (rets != EOK)
+ {
+ delete messageInfo; // 处理内存分配失败的情况
+ messageInfo = NULL;
+ WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "memcpy_s ancs_data_info_t fail ret = %d", rets);
+ return;
+ }
+ MessageModel::GetInstance().AddMessageData(messageInfo);
+ delete messageInfo; // 释放内存
+
+ // 发送信息进行弹窗
+ CustomEventPublish(TOPIC_EVENT_MESSAGENOTE, NULL, 0);
+ }
+ }
+#ifdef __cplusplus
+}
+#endif
+
namespace OHOS
{
- MessageData message_;
MessageModel::MessageModel()
{
WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "MessageModel");
- HasDataJson(); // 更新json状态
}
-
- MessageModel::~MessageModel() {}
-
+ MessageModel::~MessageModel()
+ {
+ WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "MessageModel::~MessageModel()");
+ MessageUnsubscribeTopic(TOPIC_EVENT_MESSAGE_UPDATA); // 取消订阅
+ }
MessageModel &MessageModel::GetInstance()
{
static MessageModel instance;
return instance;
}
+ void MessageModel ::Init()
+ {
+ MessageSubscribeTopic(TOPIC_EVENT_MESSAGE_UPDATA); // 进行消息订阅
+ }
+ // 获取消息列表项
+ const MessageListItem *MessageModel::GetMessageListItems() const
+ {
+ return Messages;
+ }
+ // 获取消息数量
uint8_t MessageModel::GetMessageCount() const
+ {
+ return numberOfItems;
+ }
+
+ // 加载Message文件
+ bool MessageModel::LoadMessagesFromStorage()
+ {
+ std::ifstream file(MESSAGE_JSON_FILE);
+ if (!file.is_open())
+ {
+ WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "Failed to open file for reading");
+ return false;
+ }
+
+ std::string fileContent((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>());
+ file.close();
+
+ cJSON *root = cJSON_Parse(fileContent.c_str());
+ if (!root)
+ {
+ WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "Error parsing JSON file");
+ return false;
+ }
+
+ cJSON *messageArray = cJSON_GetObjectItem(root, "messages");
+ if (!messageArray || !cJSON_IsArray(messageArray))
+ {
+ WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "Invalid data format in JSON file");
+ cJSON_Delete(root);
+ return false;
+ }
+
+ // 清空现有数据
+ MessageVector.clear();
+
+ // 将JSON数据转换为MessageData并添加到消息数组中
+ int dataCount = cJSON_GetArraySize(messageArray);
+ WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "dataCount is %d", dataCount);
+ for (int i = 0; i < dataCount; i++)
+ {
+ cJSON *item = cJSON_GetArrayItem(messageArray, i);
+
+ cJSON *isRead = cJSON_GetObjectItem(item, "isRead");
+ cJSON *labelDetails = cJSON_GetObjectItem(item, "labelDetails");
+ cJSON *labelTitle = cJSON_GetObjectItem(item, "labelTitle");
+ cJSON *timestamp = cJSON_GetObjectItem(item, "timestamp");
+
+ MessageData data;
+ data.isRead = cJSON_IsTrue(isRead) ? true : false;
+ data.labelDetails = labelDetails->valuestring;
+ data.labelTitle = labelTitle->valuestring;
+ data.timestamp = timestamp->valuedouble;
+
+ MessageVector.push_back(data);
+ }
+
+ cJSON_Delete(root);
+ WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "Messages loaded from storage successfully");
+ return true;
+ }
+
+ // 保存Message文件
+ bool MessageModel::SaveMessagesToStorage()
+ {
+ cJSON *root = cJSON_CreateObject();
+ cJSON *messageArray = cJSON_CreateArray();
+
+ for (const auto &data : MessageVector)
+ {
+ cJSON *item = cJSON_CreateObject();
+ cJSON_AddBoolToObject(item, "isRead", data.isRead);
+ cJSON_AddStringToObject(item, "labelDetails", data.labelDetails.c_str());
+ cJSON_AddStringToObject(item, "labelTitle", data.labelTitle.c_str());
+ cJSON_AddNumberToObject(item, "timestamp", data.timestamp);
+
+ cJSON_AddItemToArray(messageArray, item);
+ }
+
+ cJSON_AddItemToObject(root, "messages", messageArray);
+
+ std::ofstream file(MESSAGE_JSON_FILE);
+ if (!file.is_open())
+ {
+ WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "Failed to open file for writing");
+ cJSON_Delete(root);
+ return false;
+ }
+
+ char *jsonData = cJSON_Print(root);
+ file << jsonData << std::endl;
+ file.close();
+ cJSON_free(jsonData);
+ cJSON_Delete(root);
+
+ WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "Messages saved to storage successfully");
+ return true;
+ }
+ // 初始化列表
+ void MessageModel::InitMessageListItems()
+ {
+ // 读取数组数据
+ if (!MessageVector.empty())
+ {
+ numberOfItems = MessageVector.size();
+ WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "the numberOfItems is %d", numberOfItems);
+ for (int i = numberOfItems - 1, count = 0; i >= 0 && count < MaxMessageListCount; i--, count++)
+ {
+ MessageData messageData;
+ messageData.isRead = MessageVector[i].isRead;
+ messageData.labelTitle = MessageVector[i].labelTitle;
+ messageData.labelDetails = MessageVector[i].labelDetails;
+ messageData.labelTime = MessageVector[i].labelTime;
+ // 获取是否读取 来信 信息内容 时间
+ Messages[count] = {count, messageData.isRead, messageData.labelTitle, messageData.labelDetails, messageData.labelTime};
+ }
+ }
+ }
+ // 设置当前项[数组下标]
+ void MessageModel::SetCurrentIndex(uint8_t index)
+ {
+ numberOfItems = MessageVector.size();
+ currentIndex_ = numberOfItems - index - 1;
+ }
+
+ // 获取当前项[数组下标]
+ uint8_t MessageModel::GetCurrentIndex()
+ {
+ return currentIndex_;
+ }
+
+ // 增加消息项[添加数组项]
+ void MessageModel::AddMessageData(ancs_data_info_t *messageInfo)
+ {
+ if (messageInfo == nullptr)
+ {
+ WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "messageInfo is null");
+ return;
+ }
+
+ MessageData data;
+ data.isRead = messageInfo->read;
+ data.labelDetails = messageInfo->msg;
+ data.labelTitle = messageInfo->title;
+ data.timestamp = messageInfo->time;
+
+ 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);
+
+ // 删除多余的数据
+ while (MessageVector.size() > MaxMessageListCount)
+ {
+ MessageVector.erase(MessageVector.begin());
+ }
+
+ WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "messageInfo add is ok");
+ }
+
+ // 获取目前的项
+ MessageData *MessageModel::GetCurrentMessageData()
+ {
+ if (currentIndex_ < MessageVector.size())
+ {
+ return &MessageVector[currentIndex_];
+ }
+ return nullptr;
+ }
+
+ // 设置当前项已读
+ bool MessageModel::SetMessageAsRead()
+ {
+ if (currentIndex_ < MessageVector.size())
+ {
+ MessageVector[currentIndex_].isRead = true;
+ return true;
+ }
+ return false;
+ }
+
+ // 删除当前项[删除数组的当前项]
+ bool MessageModel::DeleteCurrentMessage()
+ {
+ if (currentIndex_ < MessageVector.size())
+ {
+ MessageVector.erase(MessageVector.begin() + currentIndex_);
+ return true;
+ }
+ return false;
+ }
+
+ // 删除所有数据[清空数组]
+ bool MessageModel::ClearAllMessages()
+ {
+ MessageVector.clear();
+ return true;
+ }
+
+ // 获取最新信息[数组的最后一个]
+ MessageData *MessageModel::GetNewMessage()
+ {
+ if (!MessageVector.empty())
+ {
+ return &MessageVector.back();
+ }
+ return nullptr;
+ }
+ // 判断数组是否存在数据
+ bool MessageModel::HasDataVector()
+ {
+ return !MessageVector.empty();
+ }
+
+ // 设置弹窗状态
+ void MessageModel::SetPopupViewState(bool state)
+ {
+ popupViewState_ = state;
+ }
+
+ // 获取弹窗状态
+ bool MessageModel::GetPopupViewState()
+ {
+ return popupViewState_;
+ }
+
+}
+
+#if 0
+ uint8_t MessageModel::GetMessageCount() const
{
return (numberOfItems != 0 ? numberOfItems : MaxMessageListCount);
}
// 2.遍历文件夹里面的文件名生成文件名数组
if (ent->d_type == DT_REG || ent->d_type == DT_UNKNOWN) // 普通文件
{
+ fileName = directory + fileName;
filenames.push_back(fileName);
// printf("GetFileNamesInDirectory:%s\r\n", fileName.c_str());
}
}
// 4.文件名排序
- std::sort(filenames.begin(), filenames.end());
+ // std::sort(filenames.begin(), filenames.end());
// 5.文件数量超出限制删除多余文件
DeleteExcessFiles(filenames); // 删除多余文件
// 6.解析json
for (int i = 0; i < filenames.size(); i++)
{
- std::ifstream file((MESSAGE_JSON_DIR + filenames[i]));
+ std::ifstream file((filenames[i]));
if (!file.is_open())
{
WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "Unable to open file %s ", filenames[i].c_str());
}
}
}
+
const MessageListItem *MessageModel::GetMessageListItems() const
{
return Messages;
{
popupViewState = state;
}
+
// 获取当前界面 0:没有记录 | 1:有记录
bool MessageModel::GetPopupViewState()
{
return popupViewState;
}
+
/*保存json数据*/
void MessageModel::SaveMessageData(const MessageData data, const int index)
{
WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "Failed to open file for writing");
}
}
+
// 获取目前的message
MessageData *MessageModel::GetCurrentMessageData()
{
PrintData(message_);
return &message_;
}
+
// 设置目前的message
void MessageModel::SetCurrentMessageData(MessageData *data)
{
WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "SetMessageData is ok");
}
}
+
// 清除所有数据[删除文件夹内所有文件]
bool MessageModel::ClearAllData()
{
+ // 遍历fileIndexMap并删除对应文件
+ for (auto it = fileIndexMap.begin(); it != fileIndexMap.end(); ++it)
+ {
+ std::string filePath = it->second;
+ uapi_delete_files(filePath.c_str());
+ }
- WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "ClearAllData is ok");
- return true;
+ // 清空fileIndexMap
+ fileIndexMap.clear();
- WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "ClearAllData is fail");
- return false;
+ return true; // 表示清除操作成功
}
+
// 删除单个项 [通过 map 删除]
bool MessageModel::DeleteSingleItem(int currentIndex)
{
- if (currentIndex < 0 || currentIndex >= fileIndexMap.size())
+ if (currentIndex < 0)
{
- WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "Invalid index for deletion");
+ WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "Invalid index for deletion: Index is less than 0");
return false;
}
- uapi_delete_files(fileIndexMap[currentIndex].c_str());
+ auto it = std::next(fileIndexMap.begin(), currentIndex);
+ if (it == fileIndexMap.end())
+ {
+ WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "Item at index not found");
+ return false;
+ }
+
+ // 删除文件
+ uapi_delete_files(it->second.c_str());
+
+ // 删除对应项
+ fileIndexMap.erase(it);
- fileIndexMap.erase(currentIndex);
return true;
}
// 设置当前项已读
bool MessageModel::SetMessageIsread()
{
- // 保存更新后的消息数据
- std::vector<MessageData> messageVector = LoadMessageData();
- if (!messageVector.empty())
+ // 1.找到对应的项文件
+ if (fileIndexMap.find(indexCurrent) == fileIndexMap.end())
+ {
+ // 检查索引是否存在于map中
+ WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "Error: Index not found in fileIndexMap");
+ return false;
+ }
+ std::ofstream file(fileIndexMap[indexCurrent]);
+ if (!file.is_open())
+ {
+ WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "filename is error");
+ return false;
+ }
+
+ // 2.获取文件内容
+ std::string jsonData((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>());
+ file.close();
+ if (jsonData.empty())
{
- messageVector[GetCurrentIndex()].isRead = true;
+ WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "jsonData is null");
+ return false;
+ }
- // SaveMessageData(messageVector);
- WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "Message marked as read and data saved");
- return true;
+ // 3.设置属性已读
+ cJSON *root = cJSON_Parse(jsonData.c_str());
+ if (root == nullptr)
+ {
+ WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "Error: Failed to parse JSON in file");
+ return false;
+ }
+
+ // 4.更新数据并保存文件
+ cJSON_ReplaceItemInObject(root, "isRead", cJSON_CreateNumber(1)); // 将"isRead"属性值设为1,表示已读
+
+ // 5.重新生成 JSON 字符串
+ char *updatedJsonData = cJSON_Print(root);
+
+ // 6.打开文件以写入更新后的数据
+ outFile.open(fileIndexMap[indexCurrent]);
+ if (outFile.is_open())
+ {
+ outFile << updatedJsonData; // 将更新后的 JSON 数据写入文件
+ outFile.close();
+ cJSON_free(updatedJsonData);
}
else
{
- WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "Failed to mark message as read and save data");
+ WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "Error: Failed to open file for writing");
+ cJSON_free(updatedJsonData);
+ cJSON_Delete(root);
return false;
}
+ // 释放 JSON 对象内存
+ cJSON_Delete(root);
+ return true;
}
// 获取最新消息项
- MessageData *MessageModel::GetNewestData()
+ MessageData *MessageModel::GetNewestData(int index)
{
- std::vector<MessageData> messageVector = LoadMessageData();
- numberOfItems = messageVector.size();
- WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "the numberOfItems is %d", numberOfItems);
- if (numberOfItems > 0)
+ // 打开文件
+ std::string filename = MESSAGE_JSON_DIR + std::to_string(index) + ".json";
+ std::ifstream file(filename);
+ if (!file.is_open())
{
- return &messageVector[numberOfItems - 1];
+ WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "Error: Failed to open file");
+ return nullptr;
}
- return nullptr;
+
+ // 获取文件内容
+ std::string jsonData((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>());
+ file.close();
+
+ if (jsonData.empty())
+ {
+ WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "Error: File is empty");
+ return nullptr;
+ }
+
+ // 解析JSON数据
+ cJSON *root = cJSON_Parse(jsonData.c_str());
+ if (root == nullptr)
+ {
+ WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "Error: Failed to parse JSON in file");
+ return nullptr;
+ }
+
+ // 创建MessageData对象并设置属性
+ MessageData *data = new MessageData();
+ data->labelTitle = cJSON_GetObjectItem(root, "title")->valuestring;
+ data->labelDetails = cJSON_GetObjectItem(root, "msg")->valuestring;
+ data->isRead = cJSON_GetObjectItem(root, "read")->valueint;
+ data->timestamp = cJSON_GetObjectItem(root, "time")->valueint;
+
+ // 释放资源并返回数据
+ cJSON_Delete(root);
+ return data;
}
// 设置当前项
void MessageModel::SetCurrentIndex(uint8_t index)
{
return indexCurrent;
}
-}
+#endif
\ No newline at end of file