解决天气界面没有时间显示的bug,并优化时间刷新
authorluozheng <1732036298@qq.com>
Tue, 23 Jul 2024 07:13:08 +0000 (15:13 +0800)
committerluozheng <1732036298@qq.com>
Tue, 23 Jul 2024 07:13:08 +0000 (15:13 +0800)
1.目前主界面天气刷新:如果在天气卡片,天气数据有效,就刷新时间
2.如果是Slice天气:滑到第一个界面,时间就会更新

code/sdk/application/wearable/fd230app/nativeui/main/src/MainPresenterSample.cpp
code/sdk/application/wearable/fd230app/nativeui/weather/include/WeatherModel.h
code/sdk/application/wearable/fd230app/nativeui/weather/include/WeatherView.h
code/sdk/application/wearable/fd230app/nativeui/weather/src/MainWeatherPresenter.cpp
code/sdk/application/wearable/fd230app/nativeui/weather/src/WeatherModel.cpp
code/sdk/application/wearable/fd230app/nativeui/weather/src/WeatherView.cpp

index 8cb7d95..6698e36 100644 (file)
@@ -29,6 +29,7 @@
 #include "custom_utils.h"
 #include "traing/training_card/include/Training_CardView.h"
 #include "dnotdisturb/include/DnotDisturbModel.h"
+#include "weather/include/WeatherModel.h"
 #if USE_OFFLINE_DIAL
 #include "DialViewGroup.h"
 #endif
@@ -392,6 +393,11 @@ namespace OHOS
             }
         }
         break;
+        case WEATHER:
+            if(WeatherModel::GetInstance().GetWeatherDataIsValid()) {
+                WeatherModel::GetInstance().UpdateWeatherCurTime();
+            }
+            break;
         }
     }
 
index 669210b..2d92052 100644 (file)
@@ -2,7 +2,7 @@
  * @Author: luozheng 1732036298@qq.com
  * @Date: 2024-04-08 18:40:20
  * @LastEditors: luozheng 1732036298@qq.com
- * @LastEditTime: 2024-06-19 10:24:03
+ * @LastEditTime: 2024-07-23 14:41:27
  * @FilePath: /FD230/code/sdk/application/wearable/fd230app/nativeui/weather/include/WeatherModel.h
  */
 #ifndef WEATHER_MODEL_H
@@ -88,6 +88,7 @@ namespace OHOS
         static WeatherModel &GetInstance(void);
         void InitFutureWeatherItem();
         void TimestampToMonthDayString(time_t timestamp, char *buffer);
+        std::string GetWeatherCurTime();
         std::string GetWeatherImage();
         std::string GetWeatherTemperature();
         std::string GetWeatherDescription();
@@ -109,6 +110,7 @@ namespace OHOS
         void SetWeatherDataIsValid(bool value);
         /* -------------------------------------------------------------------------- */
         void UpdateMainWeatherData(void);
+        void UpdateWeatherCurTime(void);
     private:
         WeatherModel();
         WeatherModel(const WeatherModel &) = delete;
index 3c93ddf..618c915 100644 (file)
@@ -2,7 +2,7 @@
  * @Author: luozheng 1732036298@qq.com
  * @Date: 2024-03-30 09:14:38
  * @LastEditors: luozheng 1732036298@qq.com
- * @LastEditTime: 2024-06-15 16:31:50
+ * @LastEditTime: 2024-07-23 14:51:03
  * @FilePath: /FD230/code/sdk/application/wearable/fd230app/nativeui/weather/include/WeatherView.h
  */
 #ifndef WEATHER_VIEW_H
@@ -35,6 +35,7 @@ namespace OHOS
         virtual bool InitView(void *caller) override;
         void ClearAll();
         bool UpdateWeatherViewData(void);
+        bool UpdateWeatherTime(void);
 
     private:
         UILabelExt *InitLabelExt(int x, int y, int w, int h, const char *text, int textfont, int lineheight);
@@ -45,7 +46,6 @@ namespace OHOS
         void DumpWeatherInfo();
         bool SetImageSource(UIImageView *image, const std::string &source);
         bool SetTextLabel(UILabel *label, const std::string &text);
-        bool UpdateWeatherTime(void);
         bool UpdateWeatherImag(void);
         bool UpdateWeatherTemperature(void);
         bool UpdateWeatherWeatherDescription(void);
index daaba8b..edeae73 100644 (file)
@@ -101,9 +101,9 @@ namespace OHOS
     {\r
         WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "MainWeatherPresenter::OnSwipe,current tab index = %d", view.GetCurrentPage());\r
         // WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "the view index = %d,dragging direction = %d,current tab index = %d", view.GetViewIndex(), view.GetDirection(), view.GetCurrentPage());\r
-        if (view.GetCurrentPage() == WEATHERREPORTVIEW)\r
+        if (view.GetCurrentPage() == MAINWEATHERVIEW)\r
         {\r
-            //MainWeatherView::GetInstance()->GetFutureWeatherView()->GetFutureWeatherList()->RefreshList();\r
+            WeatherModel::GetInstance().UpdateWeatherCurTime();\r
         }\r
     }\r
 \r
index 7002393..eb23d01 100644 (file)
@@ -2,7 +2,7 @@
  * @Author: luozheng 1732036298@qq.com
  * @Date: 2024-04-08 18:40:09
  * @LastEditors: luozheng 1732036298@qq.com
- * @LastEditTime: 2024-06-19 10:24:12
+ * @LastEditTime: 2024-07-23 14:54:46
  * @FilePath: /FD230/code/sdk/application/wearable/fd230app/nativeui/weather/src/WeatherModel.cpp
  */
 #include "weather/include/WeatherModel.h"
@@ -135,6 +135,22 @@ namespace OHOS
         sprintf(buffer, "%02d.%02d", timeinfo.tm_mon + 1, timeinfo.tm_mday);
     }
 
+    /*获取天气界面的当前时间*/
+    std::string WeatherModel::GetWeatherCurTime()
+    {
+        struct tm pTM;
+        time_t seconds;
+        time(&seconds);
+        localtime_r(&seconds, &pTM);
+
+        int hour = pTM.tm_hour;
+        int minute = pTM.tm_min;
+
+        std::string weatherTime = (hour < 10 ? "0" : "") + std::to_string(hour) + ":" + (minute < 10 ? "0" : "") + std::to_string(minute);
+
+        return weatherTime;
+    }
+
     // 获取天气图片
     std::string WeatherModel::GetWeatherImage()
     {
@@ -267,7 +283,7 @@ namespace OHOS
     /* 更新所有天气数据 */
     void WeatherModel::UpdateMainWeatherData(void)
     {
-        WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "UpdateMainWeatherData");
+        WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "UpdateMainWeatherData");
 
         MainWeatherView *p_MainWeatherView = MainWeatherView::GetInstance();
         if (p_MainWeatherView != nullptr)
@@ -287,4 +303,15 @@ namespace OHOS
             p_WeatherControlView->UpdateWeatherControlViewVisibility();
         }
     }
+
+    /*更新天气界面的时间显示*/
+    void WeatherModel::UpdateWeatherCurTime(void)
+    {
+        WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "UpdateWeatherCurTime");
+        MainWeatherView *p_MainWeatherView = MainWeatherView::GetInstance();
+        if (p_MainWeatherView != nullptr)
+        {
+            p_MainWeatherView->GetWeatherView()->UpdateWeatherTime();
+        }
+    }
 }
\ No newline at end of file
index 7e79b1a..d4c1057 100644 (file)
@@ -2,7 +2,7 @@
  * @Author: luozheng 1732036298@qq.com
  * @Date: 2024-06-12 11:44:04
  * @LastEditors: luozheng 1732036298@qq.com
- * @LastEditTime: 2024-06-19 10:51:02
+ * @LastEditTime: 2024-07-23 14:44:07
  * @FilePath: /FD230/code/sdk/application/wearable/fd230app/nativeui/weather/src/WeatherView.cpp
  */
 #include "weather/include/WeatherView.h"
@@ -87,6 +87,7 @@ namespace OHOS
         SetViewIndex(0); // 视图索引
 
         WeatherModel *p_WeatherModel = &WeatherModel::GetInstance();
+        WeatherTime = p_WeatherModel->GetWeatherCurTime();
         WeatherImag = p_WeatherModel->GetWeatherImage();
         Temperature = p_WeatherModel->GetWeatherTemperature();
         WeatherDescription = p_WeatherModel->GetWeatherDescription();
@@ -200,7 +201,7 @@ namespace OHOS
         showWeatherImage->SetResizeMode(UIImageView::CONTAIN);
         showWeatherImage->SetSrc(WeatherImag.c_str());
         /* -------------------------------------------------------------------------- */
-        showCurrentTimeLabel = InitLabelExt(CURRENT_TIME_LABEL_X, CURRENT_TIME_LABEL_Y, CURRENT_TIME_LABEL_W, CURRENT_TIME_LABEL_H, "",
+        showCurrentTimeLabel = InitLabelExt(CURRENT_TIME_LABEL_X, CURRENT_TIME_LABEL_Y, CURRENT_TIME_LABEL_W, CURRENT_TIME_LABEL_H, WeatherTime.c_str(),
                                             CURRENT_TIME_LABEL_TEXT_FONT, CURRENT_TIME_LABEL_LINE_HEIGHT);
         if (showCurrentTimeLabel == nullptr)
         {
@@ -362,15 +363,7 @@ namespace OHOS
     {
         WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "WeatherView::UpdateWeatherTime");
 
-        struct tm pTM;
-        time_t seconds;
-        time(&seconds);
-        localtime_r(&seconds, &pTM);
-
-        int hour = pTM.tm_hour;
-        int minute = pTM.tm_min;
-
-        std::string weatherTime = (hour < 10 ? "0" : "") + std::to_string(hour) + ":" + (minute < 10 ? "0" : "") + std::to_string(minute);
+        std::string weatherTime = WeatherModel::GetInstance().GetWeatherCurTime();
 
         return SetTextLabel(showCurrentTimeLabel, weatherTime);
     }