优化秒表:修复了秒表定时器已到达最高时间但显示时间的标签未同步更新bug 1473/1
authorluozheng <1732036298@qq.com>
Thu, 28 Nov 2024 02:37:33 +0000 (10:37 +0800)
committerluozheng <1732036298@qq.com>
Thu, 28 Nov 2024 02:37:33 +0000 (10:37 +0800)
code/sdk/application/wearable/fd230app/nativeui/stopwatch/include/StopWatchModel.h
code/sdk/application/wearable/fd230app/nativeui/stopwatch/src/MainStopWatchPresenter.cpp
code/sdk/application/wearable/fd230app/nativeui/stopwatch/src/StopWatchModel.cpp

index 228a8ca..b2d2a77 100644 (file)
@@ -2,7 +2,7 @@
  * @Author: luozheng 1732036298@qq.com\r
  * @Date: 2024-04-04 15:13:51\r
  * @LastEditors: luozheng 1732036298@qq.com\r
- * @LastEditTime: 2024-10-30 11:47:14\r
+ * @LastEditTime: 2024-11-27 17:45:02\r
  * @FilePath: /FD230/code/sdk/application/wearable/fd230app/nativeui/stopwatch/include/StopWatchModel.h\r
  */\r
 #ifndef STOP_WATCH_MODEL_H\r
@@ -20,7 +20,7 @@ namespace OHOS
     public:\r
         static std::shared_ptr<StopWatchModel> GetInstance(); // shared_ptr:当没有引用指向这个实例时会自动释放内存\r
         ~StopWatchModel() {}\r
-        char *DisplayStopWatchDuration();\r
+        std::string DisplayStopWatchDuration();\r
         void StopWatch_StartTimer(uint32_t interval);\r
         void StopWatch_StopTimer(void);\r
         void StopWatch_PauseTimer();\r
index 998137d..229cc51 100644 (file)
@@ -85,10 +85,12 @@ namespace OHOS
     /// @param LineHeight\r
     void MainStopWatchPresenter::SetShowTimeLableStyle(UILabel *label, int x, int y, int TextFont, int LineHeight)\r
     {\r
-        label->SetPosition(x, y);\r
+        label->SetPosition(x, y,466,LineHeight);\r
         label->SetStyle(STYLE_TEXT_FONT, TextFont);\r
         label->SetStyle(STYLE_LINE_HEIGHT, LineHeight);\r
         label->SetFont(DEFAULT_VECTOR_FONT_FILENAME_EN, TextFont);\r
+        label->SetLineBreakMode(UILabel::LINE_BREAK_CLIP);\r
+        label->SetAlign(TEXT_ALIGNMENT_CENTER, TEXT_ALIGNMENT_CENTER);\r
         label->Invalidate();\r
     }\r
 \r
@@ -258,11 +260,16 @@ namespace OHOS
 \r
             if (m_listdata != nullptr)\r
             {\r
-                for (int i = 0; i < m_listdata->Size(); i++)\r
+                // 遍历列表,释放每个元素所占用的内存\r
+                ListNode<const char *> *node = m_listdata->Head();\r
+                int cnt = 0;\r
+                while (node!= m_listdata->End())\r
                 {\r
-                    WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "%d", i);\r
-                    char *p = (char *)(m_listdata->Tail()->data_);\r
-                    free(p);\r
+                    WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "%d",++cnt);\r
+                    const char *p = node->data_;\r
+                    free(const_cast<char *>(p));\r
+\r
+                    node = m_listdata->Remove(node);\r
                 }\r
                 m_listdata->Clear();\r
             }\r
@@ -333,9 +340,9 @@ namespace OHOS
     {\r
         if (view_->GetScrollView(MAIN_STOPWATCH) && view_->GetScrollView(MAIN_STOPWATCH)->IsVisible())\r
         {\r
-            if (view_->GetPlayOrPausecheckBox() && view_->GetPlayOrPausecheckBox() && TimerIsRunning)\r
+            if (TimerIsRunning)\r
             {\r
-                view_->GetShowTimelabel()->SetText(StopWatchModel::GetInstance()->DisplayStopWatchDuration());\r
+                view_->GetShowTimelabel()->SetText(StopWatchModel::GetInstance()->DisplayStopWatchDuration().c_str());\r
                 view_->GetShowTimelabel()->Invalidate();\r
             }\r
         }\r
index fa746c2..ff9d685 100644 (file)
@@ -18,19 +18,19 @@ namespace OHOS
     /* ----------------------------------- 定时器 ---------------------------------- */\r
     // 声明定时器变量\r
     static osTimerId_t g_timer = nullptr;\r
-    static int g_StopWatchTimes = {0};\r
+    static uint32_t g_StopWatchTimes = {0};\r
     static constexpr int32 NUM_TEN = 10; // 主要用于格式化输出,当小时、分钟或秒小于10时,在其前面添加一个’0’以保持常规时间格式\r
     static constexpr int16 ERROR = 0;\r
     static constexpr int16 SUCCESS = 1;\r
     static constexpr int16 NUM_TIME_MIN = 60;\r
     static constexpr int16 NUM_TIME_HOUR = 3600;\r
     static constexpr int32 NUM_TIME_DAY = 86400;\r
-    static constexpr int32 MAX_STOPWATCH_TIME = (99 * 60 * 1000) + (59 * 1000) + 990; // 99分59秒990毫秒\r
+    static constexpr uint32_t MAX_STOPWATCH_TIME = (99 * 60 * 1000) + (59 * 1000) + 990; // 99分59秒990毫秒\r
     static uint64_t g_timerStartTimestamp = 0;                                  // 添加一个全局变量来存储定时器开始的时间戳\r
     /*\r
      * @brief 显示倒计时(分钟、秒、毫秒),毫秒显示为两位数\r
      */\r
-    char *StopWatchModel::DisplayStopWatchDuration()\r
+    std::string StopWatchModel::DisplayStopWatchDuration()\r
     {\r
         stopwatchDuration.erase();\r
         std::string minute;\r
@@ -73,7 +73,7 @@ namespace OHOS
         // 拼接分钟、秒和毫秒\r
         stopwatchDuration = minute + ":" + seconds + "." + milliseconds;\r
 \r
-        return (char *)stopwatchDuration.c_str();\r
+        return stopwatchDuration.c_str();\r
     }\r
 \r
     // 检查是否达到最大记录时间\r
@@ -94,6 +94,9 @@ namespace OHOS
         }\r
         MainStopWatchView::GetInstance()->GetPlayOrPausecheckBox()->SetState(UICheckBox::UNSELECTED);\r
         MainStopWatchView::GetInstance()->ChangeRecordTimeToLimitView();\r
+        WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "已到达记录最高时间:%d",g_StopWatchTimes);\r
+        g_StopWatchTimes = MAX_STOPWATCH_TIME;\r
+        MainStopWatchView::GetInstance()->GetShowTimelabel()->SetText(StopWatchModel::GetInstance()->DisplayStopWatchDuration().c_str());\r
         uapi_set_screen_on_api();\r
     }\r
 \r