修复训练记录显示问题
authorCaisking <3449750381@qq.com>
Tue, 11 Jun 2024 09:42:57 +0000 (17:42 +0800)
committerCaisking <3449750381@qq.com>
Tue, 11 Jun 2024 09:42:57 +0000 (17:42 +0800)
code/sdk/application/wearable/fd230app/nativeui/traing/include/TraingModel.h
code/sdk/application/wearable/fd230app/nativeui/traing/src/TraingEnddataPresenter.cpp
code/sdk/application/wearable/fd230app/nativeui/traing/src/TraingEnddataView.cpp
code/sdk/application/wearable/fd230app/nativeui/traing/src/TraingModel.cpp
code/sdk/application/wearable/fd230app/nativeui/traing/src/TraingView.cpp
code/sdk/application/wearable/fd230app/nativeui/traingrecord/include/TraingRecordModel.h
code/sdk/application/wearable/fd230app/nativeui/traingrecord/include/TraingRecordView.h
code/sdk/application/wearable/fd230app/nativeui/traingrecord/src/TraingRecordModel.cpp
code/sdk/application/wearable/fd230app/nativeui/traingrecord/src/TraingRecordPresenter.cpp
code/sdk/application/wearable/fd230app/nativeui/traingrecord/src/TraingRecordView.cpp

index 652cef9..58ac873 100644 (file)
@@ -290,6 +290,9 @@ namespace OHOS
         bool SaveDataToJson(SportData *data);
         std::vector<SportData> LoadDataFormJson();
 
+        /*打印函数*/
+        void printf_data(SportData *data);
+
     private:
         TraingModel(const TraingModel &) = delete;
         TraingModel &operator=(const TraingModel &) = delete;
index 0d92edb..aae8b1b 100644 (file)
@@ -1,3 +1,9 @@
+/*** 
+ * @Date: 2024-04-15 14:19:47
+ * @LastEditors: Caisking 3449750381@qq.com
+ * @LastEditTime: 2024-06-11 15:34:57
+ * @FilePath: /XTS_FD230/FD230/code/sdk/application/wearable/fd230app/nativeui/traing/src/TraingEnddataPresenter.cpp
+ */
 /***
  * @Date: 2024-04-15 14:19:47
  * @LastEditors: Caisking 3449750381@qq.com
@@ -67,7 +73,7 @@ namespace OHOS
     {
         if (event.GetDragDirection() == DragEvent::DIRECTION_LEFT_TO_RIGHT)
         {
-            NativeAbility::GetInstance().ChangeSlice(VIEW_TRAININGMORE);
+            NativeAbility::GetInstance().ChangeSlice(VIEW_APPLIST);
         }
         UNUSED(view);
         return true;
@@ -78,7 +84,7 @@ namespace OHOS
         // 根据控件id查找是哪个控件触发了点击回调
         if (strcmp(view.GetViewId(), BUTTONBACK) == 0)
         {
-            NativeAbility::GetInstance().ChangeSlice(VIEW_TRAININGMORE);
+            NativeAbility::GetInstance().ChangeSlice(VIEW_APPLIST);
         }
         return true;
     }
index ae15423..f77a5a4 100644 (file)
@@ -14,7 +14,9 @@
 #include <cmath>  // 包含数学函数库,用于四舍五入
 #include <string> // 包含字符串处理库,用于使用 std::to_string
 
+/*测试打印函数*/
 #define PRINT_LINE_INFO() printf("File: %s, Line: %d\r\n", __FILE__, __LINE__)
+#define PRINT_FUNCTION_LINE() printf("Function: %s, Line: %d\r\n", __FUNCTION__, __LINE__)
 
 namespace OHOS
 {
@@ -285,37 +287,33 @@ namespace OHOS
     void TraingEnddataView::InitData()
     {
         SportData *enddata = TraingModel::GetInstance()->GetSportdata();
+
         if (enddata == nullptr)
         {
             printf("Enddata is nullptr. Exiting InitData()...\r\n");
             return;
         }
-
         printf("Initializing TraingEnddataView data...\r\n");
-
         // 训练图标Icon
         std::string imageStr = TraingModel::GetInstance()->GetSportType(enddata->sportType);
         if (!imageStr.empty())
         {
+            printf("imageIcon %s\r\n", imageStr.c_str());
             imageIcon->SetSrc(imageStr.c_str());
         }
         else
         {
             printf("Failed to get image string. Skipping imageIcon update.\r\n");
         }
-       
         // 运动项目
         labelsport->SetTextId(enddata->sportId);
-       
+
         // 运动开始结束时间
         std::string startTimeStr = enddata->start_time;
-       
         std::string endTimeStr = enddata->end_time;
-       
         std::string durationStr = startTimeStr + "-" + endTimeStr;
-       
+
         labelduration->SetText(durationStr.c_str());
-       
 
         // 运动目标
         if (enddata->tag)
@@ -327,29 +325,29 @@ namespace OHOS
         {
             printf("Enddata tag is null. Skipping labeltag update.\r\n");
         }
-       
+
         // 运动时长
         printf("Duration of the sport: %d\r\n", enddata->duration);
         std::string timeStr = TraingModel::GetInstance()->GetTrainingTime();
-       
+
         labeltime->SetText(timeStr.c_str());
-       
+
         // 运动距离
         std::string mileStr = std::to_string(enddata->distance);
         labelmile->SetText(mileStr.c_str());
-       
+
         // 平均心率
         std::string heartStr = std::to_string(enddata->avgheart);
         labelheart->SetText(heartStr.c_str());
-       
+
         // 步数
         std::string stepStr = std::to_string(enddata->steps);
         labelsteps->SetText(stepStr.c_str());
-       
+
         // 训练强度
         UIResourceTextId intensityid = TraingModel::GetInstance()->GetIntensity();
         labelstrong->SetTextId(intensityid);
-       
+
         printf("TraingEnddataView data initialization complete.\r\n");
     }
 
index 3241b58..fd9b46c 100644 (file)
@@ -354,15 +354,49 @@ namespace OHOS
             }
         }
     }
+    /*打印函数*/
+
+    void TraingModel::printf_data(SportData *data)
+    {
+        /*打印测试一下数据*/
+        printf("\r\n****************************************\r\n");
+        printf("Sport type: %d\n", data->sportType);
+        printf("Sport ID: %d\n", data->sportId);
+        printf("Start date: %s\n", data->start_date.c_str());
+        printf("Start time: %s\n", data->start_time.c_str());
+        printf("End time: %s\n", data->end_time.c_str());
+        printf("Goal degree: %d\n", data->goaldegree);
+        printf("Duration: %d\n", data->duration);
+        printf("Distance: %d\n", data->distance);
+        printf("Average heart rate: %d\n", data->avgheart);
+        printf("Steps: %d\n", data->steps);
+        printf("Intensity: %d\n", data->intensity);
+        printf("\r\n****************************************\r\n");
+    }
     /*获取数据*/
     SportData *TraingModel::GetSportdata()
     {
+        // printf_data(sportdata);
         return sportdata;
     }
-    /*设置训练数据[为训练记录准备]*/
+    /* 设置训练数据[为训练记录准备] */
     void TraingModel::SetSportdata(SportData *data)
     {
-        sportdata = data;
+        if (data != nullptr && sportdata != nullptr)
+        {
+            sportdata->sportTagType = data->sportTagType;
+            sportdata->sportId = data->sportId;
+            sportdata->start_date = data->start_date;
+            sportdata->start_time = data->start_time;
+            sportdata->end_time = data->end_time;
+            sportdata->goaldegree = data->goaldegree;
+            sportdata->duration = data->duration;
+            sportdata->distance = data->distance;
+            sportdata->avgheart = data->avgheart;
+            sportdata->steps = data->steps;
+            sportdata->intensity = data->intensity;
+        }
+        printf_data(data);
     }
     /*获取训练卡路里*/
     int TraingModel::GetCal()
@@ -1117,14 +1151,10 @@ namespace OHOS
         return true;
     }
 
-    /* 保存训练数据到 json 文件 */
     bool TraingModel::SaveDataToJson(SportData *data)
     {
-        cJSON *root = cJSON_CreateObject();
-        cJSON *dataArray = cJSON_CreateArray();
-
-        // 1.获取record.json文件,不存在则创建
-        FILE *fp = fopen(TRAINING_RECORD_FILE, "r+");
+        // 获取 JSON 文件,不存在则创建
+        FILE *fp = fopen(TRAINING_RECORD_FILE, "r");
         if (fp == NULL)
         {
             fp = fopen(TRAINING_RECORD_FILE, "w+");
@@ -1134,30 +1164,33 @@ namespace OHOS
                 return false;
             }
         }
+
+        // 读取并解析现有的 JSON 数据
         fseek(fp, 0, SEEK_END);
         long fileSize = ftell(fp);
         fseek(fp, 0, SEEK_SET);
-        
+
         char *jsonStr = (char *)malloc(fileSize + 1);
+        if (jsonStr == NULL)
+        {
+            WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "Failed to allocate memory for JSON data");
+            fclose(fp);
+            return false;
+        }
+
         fread(jsonStr, 1, fileSize, fp);
-        jsonStr[fileSize] = '\0';
+        fclose(fp);
 
-        // 2.读取已有数据
-        cJSON *existingData = cJSON_Parse(jsonStr);
-        cJSON *existingArray = cJSON_GetObjectItem(existingData, "data");
-        if (existingArray != NULL && cJSON_GetArraySize(existingArray) > 0)
+        cJSON *root = cJSON_Parse(jsonStr);
+        if (root == NULL)
         {
-            int existingCount = cJSON_GetArraySize(existingArray);
-            for (int i = 0; i < existingCount; i++)
-            {
-                cJSON *item = cJSON_GetArrayItem(existingArray, i);
-                cJSON_AddItemToArray(dataArray, cJSON_Duplicate(item, 1));
-            }
+            WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "Failed to parse JSON data");
+            free(jsonStr);
+            return false;
         }
-        cJSON_Delete(existingData);
-        free(jsonStr);
 
-        // 3.添加新数据
+        // 创建或更新数据节点
+        cJSON *dataArray = cJSON_GetObjectItem(root, "data");
         cJSON *sportData = cJSON_CreateObject();
         cJSON_AddNumberToObject(sportData, "sportTypeId", int(data->sportType));     // 训练项目类 Id
         cJSON_AddNumberToObject(sportData, "sportId", data->sportId);                // 训练项目 Id
@@ -1172,22 +1205,40 @@ namespace OHOS
         cJSON_AddNumberToObject(sportData, "intensity", data->intensity);            // 训练强度
         cJSON_AddItemToArray(dataArray, sportData);
 
-        // 4.限制存储数量,只保存最新的10条数据
-        int dataCount = cJSON_GetArraySize(dataArray);
-        if (dataCount > MAX_DATA_COUNT)
+        // 限制存储数量并删除多余数据
+        while (cJSON_GetArraySize(dataArray) > MAX_DATA_COUNT)
         {
-            WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "The training data has reached its maximum ");
             cJSON_DeleteItemFromArray(dataArray, 0);
         }
 
-        cJSON_AddItemToObject(root, "data", dataArray);
+        // 将更新后的 JSON 数据写入文件
         char *jsonStrNew = cJSON_Print(root);
-        cJSON_Delete(root);
+        if (jsonStrNew == NULL)
+        {
+            WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "Failed to serialize JSON data");
+            cJSON_Delete(root);
+            free(jsonStr);
+            return false;
+        }
 
-        fseek(fp, 0, SEEK_SET);
-        fprintf(fp, "%s\n", jsonStrNew);
+        fp = fopen(TRAINING_RECORD_FILE, "w");
+        if (fp == NULL)
+        {
+            WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "Failed to open file for writing");
+            cJSON_Delete(root);
+            free(jsonStr);
+            return false;
+        }
+
+        fwrite(jsonStrNew, strlen(jsonStrNew), 1, fp);
         fclose(fp);
-        // WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "SaveDataToJson  is succeed");
+
+        // 释放资源
+        cJSON_Delete(root);
+        free(jsonStr);
+        cJSON_free(jsonStrNew);
+
+        WEARABLE_LOGI(WEARABLE_LOG_MODULE_APP, "SaveDataToJson succeeded");
         return true;
     }
     /* 加载训练数据函数 */
@@ -1207,8 +1258,9 @@ namespace OHOS
         cJSON *dataArray = cJSON_GetObjectItem(root, "data");
 
         int dataCount = cJSON_GetArraySize(dataArray);
-        std::vector<SportData> sportDataVector;
+        WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "dataCount is %d", dataCount);
 
+        std::vector<SportData> sportDataVector;
         for (int i = 0; i < dataCount; i++)
         {
             cJSON *item = cJSON_GetArrayItem(dataArray, i);
@@ -1229,6 +1281,7 @@ namespace OHOS
         }
 
         cJSON_Delete(root);
+        WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "LoadDataFormJson is ok");
         return sportDataVector;
     }
 }
\ No newline at end of file
index 7f93aa5..00f35bf 100644 (file)
@@ -224,7 +224,6 @@ namespace OHOS
         // 删除容器\r
         if (container_ != nullptr)\r
         {\r
-            container_->RemoveAll(); // 移除容器中的所有组件\r
             delete container_;\r
             container_ = nullptr;\r
         }\r
index 617789b..cfbc40d 100644 (file)
@@ -1,3 +1,9 @@
+/***
+ * @Date: 2024-05-20 19:40:41
+ * @LastEditors: Caisking 3449750381@qq.com
+ * @LastEditTime: 2024-06-11 09:42:02
+ * @FilePath: /XTS_FD230/FD230/code/sdk/application/wearable/fd230app/nativeui/traingrecord/include/TraingRecordModel.h
+ */
 /***
  * @Date: 2024-05-20 19:40:41
  * @LastEditors: Caisking 3449750381@qq.com
@@ -30,6 +36,10 @@ namespace OHOS
         uint8_t GetTraingRecordListNum();
         // 设置标志位
         bool GetJsonStatus();
+        // 设置当前界面
+        void SetPopupView(bool state);
+        // 获取当前界面
+        bool GetPopupView();
 
     private:
         TraingRecordModel();
@@ -41,6 +51,7 @@ namespace OHOS
         TraingRecordItem TraingRecords[TraingRecordListMaxNum] = {0, nullptr, nullptr, nullptr};
         // std::map<int, AppViewId> TraingRecordIdToViewId;
         bool jsonStatus{false}; // json文件存在标志位
+        bool popupStatus{false};
     };
 }
 
index b5e592d..3daf075 100644 (file)
@@ -1,3 +1,9 @@
+/*** 
+ * @Date: 2024-05-20 19:40:41
+ * @LastEditors: Caisking 3449750381@qq.com
+ * @LastEditTime: 2024-06-11 09:40:10
+ * @FilePath: /XTS_FD230/FD230/code/sdk/application/wearable/fd230app/nativeui/traingrecord/include/TraingRecordView.h
+ */
 #ifndef _TraingRecord_VIEW_H
 #define _TraingRecord_VIEW_H
 
@@ -53,6 +59,7 @@ namespace OHOS
         void ShowLackView();
         void ShowRecordView();
         void SetPopupViewStatus(PopupRecord view, bool status);
+        int GetPopupRecordView();
         void AddToTraingRecordList(const TraingRecordItem &item);
 
     private:
index b496b89..9bfcb29 100644 (file)
@@ -1,3 +1,9 @@
+/***
+ * @Date: 2024-05-20 19:40:41
+ * @LastEditors: Caisking 3449750381@qq.com
+ * @LastEditTime: 2024-06-11 09:41:20
+ * @FilePath: /XTS_FD230/FD230/code/sdk/application/wearable/fd230app/nativeui/traingrecord/src/TraingRecordModel.cpp
+ */
 /***
  * @Date: 2024-05-20 19:40:41
  * @LastEditors: Caisking 3449750381@qq.com
 namespace OHOS
 {
 
-    TraingRecordModel::TraingRecordModel() {}
+    TraingRecordModel::TraingRecordModel()
+    {
+        jsonStatus = TraingModel::GetInstance()->CheckDataJson();
+    }
 
     TraingRecordModel::~TraingRecordModel() {}
 
@@ -34,7 +43,10 @@ namespace OHOS
     // 设置标志位
     bool TraingRecordModel::GetJsonStatus()
     {
-        jsonStatus = TraingModel::GetInstance()->CheckDataJson();
+        if (jsonStatus)
+        {
+            // printf("存在文件!\r\n");
+        }
         return jsonStatus;
     }
 
@@ -45,6 +57,7 @@ namespace OHOS
         if (!sportDataVector.empty())
         {
             ArrayNum = sportDataVector.size();
+            WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "the ArrayNum is %d", ArrayNum);
             for (int i = 0; i < ArrayNum && i < TraingRecordListMaxNum; i++) // 最多只有10条数据
             {
                 SportData sportData = sportDataVector[i];
@@ -54,13 +67,6 @@ namespace OHOS
                 // 4.获取时间
                 std::string Time = sportData.start_time + std::string("-") + sportData.end_time;
                 TraingRecords[i] = {i, imageicon.c_str(), sportData.start_date, Time};
-#if 0
-                // 使用 printf 输出每条训练记录的信息 [测试]
-                printf("Training Record %d:\n", i + 1);
-                printf("Sport Type: %s\n", imageicon.c_str());
-                printf("Start Date: %s\n", sportData.start_date.c_str());
-                printf("Start Time: %s\n", Time.c_str());
-#endif
             }
         }
     }
@@ -73,4 +79,15 @@ namespace OHOS
     {
         return ArrayNum;
     }
+
+    // 设置当前界面
+    void TraingRecordModel::SetPopupView(bool state)
+    {
+        popupStatus = state;
+    }
+    // 获取当前界面 0:没有训练记录 | 1:有训练记录
+    bool TraingRecordModel::GetPopupView()
+    {
+        return popupStatus;
+    }
 }
\ No newline at end of file
index 2cfd3be..952c073 100644 (file)
@@ -89,18 +89,31 @@ namespace OHOS
     {
         // 返回按键
         std::string viewId = view.GetViewId();
-        if (viewId == BUTTONGO)
+        if (TraingRecordModel::GetInstance().GetPopupView() == false)
         {
-            NativeAbility::GetInstance().EnterSlice(VIEW_TRAININGMORE);
-            return true;
+            if (viewId == BUTTONGO)
+            {
+                NativeAbility::GetInstance().EnterSlice(VIEW_TRAININGMORE);
+                WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "go to training");
+                return true;
+            }
         }
-        int index = TraingRecordItemView::GetInstance().GetTraingRecordIndex();
-        // 读取json数据
-        std::vector<SportData> sportDataVector = TraingModel::GetInstance()->LoadDataFormJson();
-        // 选择点击按键的历史数据
-        SportData sportData = sportDataVector[index];
-        TraingModel::GetInstance()->SetSportdata(&sportData);
-        NativeAbility::GetInstance().ChangeSlice(VIEW_TRAININGENDDATA);
-        return true;
+        else
+        {
+            int index = TraingRecordItemView::GetInstance().GetTraingRecordIndex();
+            WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "the index is %d", index);
+            // 读取json数据
+            std::vector<SportData> sportDataVector = TraingModel::GetInstance()->LoadDataFormJson();
+            if (!sportDataVector.empty())
+            {
+                // 选择点击按键的历史数据
+                SportData sportData = sportDataVector[index];
+                NativeAbility::GetInstance().ChangeSlice(VIEW_TRAININGENDDATA);
+                TraingModel::GetInstance()->SetSportdata(&sportData);
+                return true;
+            }
+            WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "the sportDataVector is error");
+        }
+        return false;
     }
 }
index 868b688..33e382d 100644 (file)
@@ -218,7 +218,7 @@ namespace OHOS
         TraingRecordList->SetRotateFactor(2);
         TraingRecordList->RequestFocus();
         TraingRecordList->SetOnClickListener(static_cast<UIView::OnClickListener *>(TraingRecordPresenter::GetInstance()));
-        
+
         UIView::OnDragListener *recorddraglistener = static_cast<UIView::OnDragListener *>(TraingRecordPresenter::GetInstance());
         if (recorddraglistener == nullptr)
         {
@@ -235,27 +235,26 @@ namespace OHOS
         UIView *popupView = nullptr;
         if (view == PopupRecord::LackPopup)
         {
+            TraingRecordModel::GetInstance().SetPopupView(false); // 设置状态为无训练记录
             popupView = lackView;
         }
         else if (view == PopupRecord::RecordPopup)
         {
+            TraingRecordModel::GetInstance().SetPopupView(true); // 设置状态为有训练记录
             popupView = recordView;
         }
-
         if (popupView == nullptr)
         {
             WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "PopupView is NULL");
             return;
         }
-
         if (container_ != nullptr)
         {
             container_->SetVisible(!status);
         }
-
         popupView->SetVisible(status);
     }
-
+    // 获取目前的view
     void TraingRecordView::OnStart()
     {
         container_ = new UIScrollView();