天气视图第一个View刷新函数接口添加 291/1 291/head 292/target/1
authorluozheng <1732036298@qq.com>
Sat, 15 Jun 2024 04:01:17 +0000 (12:01 +0800)
committerluozheng <1732036298@qq.com>
Sat, 15 Jun 2024 04:01:17 +0000 (12:01 +0800)
code/sdk/application/wearable/fd230app/nativeui/weather/include/WeatherView.h
code/sdk/application/wearable/fd230app/nativeui/weather/src/WeatherModel.cpp
code/sdk/application/wearable/fd230app/nativeui/weather/src/WeatherView.cpp

index c86b933..f1189dd 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-04-13 18:34:25
+ * @LastEditTime: 2024-06-15 11:59:38
  * @FilePath: /FD230/code/sdk/application/wearable/fd230app/nativeui/weather/include/WeatherView.h
  */
 #ifndef WEATHER_VIEW_H
@@ -34,16 +34,26 @@ namespace OHOS
         bool InitPage(void);
         virtual bool InitView(void *caller) override;
         void ClearAll();
-        bool GetInitViewState(void);
-        void UpdataTime();
+        bool UpdateWeatherViewData(void);
 
     private:
         UILabelExt *InitLabelExt(int x, int y, int w, int h, const char *text, int textfont, int lineheight);
-
-    private:
         const char **CreateImagePathsArray(const char *basePath, const char *fileExtension, int numImages);
         void FreeImagePathsArray(const char **imagePaths, const int numImages);
+
+    private:
         void DumpWeatherInfo();
+        bool SetImageSource(UIImageView *image, const std::string &source);
+        bool SetTextLabel(UILabel *label, const std::string &text);
+        bool GetInitViewState(void);
+        bool UpdateWeatherTime(void);
+        bool UpdateWeatherImag(void);
+        bool UpdateWeatherTemperature(void);
+        bool UpdateWeatherWeatherDescription(void);
+        bool UpdateWeatherWeatherArea(void);
+        bool UpdateWeatherMaxandMinTemperature(void);
+
+        std::string WeatherTime;          // 天气时间
         std::string WeatherImag;          // 天气图片
         std::string Temperature;          // 温度
         std::string WeatherDescription;   // 天气描述
index 72848d6..83efef5 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-05-10 11:27:16
+ * @LastEditTime: 2024-06-15 11:54:18
  * @FilePath: /FD230/code/sdk/application/wearable/fd230app/nativeui/weather/src/WeatherModel.cpp
  */
 #include "weather/include/WeatherModel.h"
@@ -115,7 +115,7 @@ namespace OHOS
             WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "MainWeatherView::GetInstance fail!!");
             return;
         }
-        for (uint8_t i = 0; i < DAYS_OF_THE_WEEK; i++)
+        for (uint8_t i = 0; i < Weather_data_seven_count; i++)
         {
             FutureWeatherItem[i].iconDay = GetWeather_data_seven_data_iconDay(i);
             FutureWeatherItem[i].fxDate = GetWeather_data_seven_data_fxDate(i);
index b3c01d0..be8f5fb 100644 (file)
@@ -1,3 +1,10 @@
+/*
+ * @Author: luozheng 1732036298@qq.com
+ * @Date: 2024-06-12 11:44:04
+ * @LastEditors: luozheng 1732036298@qq.com
+ * @LastEditTime: 2024-06-15 11:57:39
+ * @FilePath: /FD230/code/sdk/application/wearable/fd230app/nativeui/weather/src/WeatherView.cpp
+ */
 #include "weather/include/WeatherView.h"
 #include "ConfigModel.h"
 #include "weather/include/WeatherModel.h"
@@ -75,23 +82,15 @@ namespace OHOS
     static WeatherView *g_pWeatherView{nullptr};
     WeatherView::WeatherView()
     {
-        WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "WeatherView::WeatherView");
+        WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "WeatherView::WeatherView");
         SetPosition(0, 0, HORIZONTAL_RESOLUTION, VERTICAL_RESOLUTION);
         SetViewIndex(0); // 视图索引
-
-        WeatherModel *p_WeatherModel = &WeatherModel::GetInstance();
-        WeatherImag = p_WeatherModel->GetWeatherImage();
-        Temperature = p_WeatherModel->GetWeatherTemperature();
-        WeatherDescription = p_WeatherModel->GetWeatherDescription();
-        WeatherArea = p_WeatherModel->GetWeatherArea();
-        MaxandMinTemperature = p_WeatherModel->GetWeatherMaxAndMinTemperature();
-
         g_pWeatherView = this;
     }
 
     WeatherView::~WeatherView()
     {
-        WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, " WeatherView::~WeatherView");
+        WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, " WeatherView::~WeatherView");
         ClearAll();
         g_pWeatherView = nullptr;
     }
@@ -283,7 +282,7 @@ namespace OHOS
             WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "WeatherView::InitPage fail");
             return false;
         }
-        UpdataTime();
+        UpdateWeatherViewData();
         SetDraggable(true);
         SetTouchable(true);
         return true;
@@ -295,27 +294,6 @@ namespace OHOS
         return viewInitStatus;
     }
 
-    /*更新当前时间*/
-    void WeatherView::UpdataTime()
-    {
-        // 获取当前时间
-        struct tm pTM;
-        time_t seconds;
-        time(&seconds);
-        localtime_r(&seconds, &pTM);
-
-        // 解析时间
-        int hour = pTM.tm_hour;
-        int minute = pTM.tm_min;
-
-        // 将时间和日期转换为字符串
-        std::string stringtime = (hour < 10 ? "0" : "") + std::to_string(hour) + ":" + (minute < 10 ? "0" : "") + std::to_string(minute);
-        if (showCurrentTimeLabel != nullptr)
-        {
-            showCurrentTimeLabel->SetText(stringtime.c_str());
-        }
-    }
-
     const char **WeatherView::CreateImagePathsArray(const char *basePath, const char *fileExtension, int numImages)
     {
         const char **imagePaths = new const char *[numImages];
@@ -343,13 +321,129 @@ namespace OHOS
         }
     }
 
+    // 设置文本标签的通用函数
+    bool WeatherView::SetTextLabel(UILabel *label, const std::string &text)
+    {
+        if (label)
+        {
+            label->SetText(text.c_str());
+            label->Invalidate();
+            return true;
+        }
+        return false;
+    }
+
+    // 设置图片资源的通用函数
+    bool WeatherView::SetImageSource(UIImageView *image, const std::string &source)
+    {
+        if (image)
+        {
+            image->SetSrc(ImageCacheManager::GetInstance().LoadSingleRes(source));
+            image->Invalidate();
+            return true;
+        }
+        return false;
+    }
+
     void WeatherView::DumpWeatherInfo()
     {
-        WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "WeatherInfo:");
+        WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "WeatherView::DumpWeatherInfo");
+        WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "WeatherTime:", WeatherTime.c_str());
         WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "WeatherImag:%s", WeatherImag.c_str());
         WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "Temperature:%s", Temperature.c_str());
         WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "WeatherDescription:%s", WeatherDescription.c_str());
         WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "WeatherArea:%s", WeatherArea.c_str());
         WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "MaxandMinTemperature:%s\n", MaxandMinTemperature.c_str());
     }
+
+    // 更新天气界面的当前时间
+    bool WeatherView::UpdateWeatherTime()
+    {
+        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);
+
+        return SetTextLabel(showCurrentTimeLabel, weatherTime);
+    }
+
+    // 更新天气界面的天气图片
+    bool WeatherView::UpdateWeatherImag()
+    {
+        WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "WeatherView::UpdateWeatherImag");
+
+        std::string weatherImag = WeatherModel::GetInstance().GetWeatherImage();
+
+        return SetImageSource(showWeatherImage, weatherImag);
+    }
+
+    // 更新天气界面的天气温度
+    bool WeatherView::UpdateWeatherTemperature()
+    {
+        WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "WeatherView::UpdateWeatherTemperature");
+
+        std::string temperature = WeatherModel::GetInstance().GetWeatherTemperature();
+
+        return SetTextLabel(showTemperatureLabel, temperature);
+    }
+
+    // 更新天气界面的天气描述
+    bool WeatherView::UpdateWeatherWeatherDescription()
+    {
+        WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "WeatherView::UpdateWeatherWeatherDescription");
+
+        std::string weatherDescription = WeatherModel::GetInstance().GetWeatherDescription();
+
+        return SetTextLabel(showWeatherDescriptionLabel, weatherDescription);
+    }
+
+    // 更新天气界面的天气地区
+    bool WeatherView::UpdateWeatherWeatherArea()
+    {
+        WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "WeatherView::UpdateWeatherWeatherArea");
+
+        std::string weatherArea = WeatherModel::GetInstance().GetWeatherArea();
+
+        return SetTextLabel(showWeatherAreaLabel, weatherArea);
+    }
+
+    // 更新天气界面的最高与最低气温
+    bool WeatherView::UpdateWeatherMaxandMinTemperature()
+    {
+        WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "WeatherView::UpdateWeatherMaxandMinTemperature");
+
+        std::string maxMinTemperature = WeatherModel::GetInstance().GetWeatherMaxAndMinTemperature();
+
+        return SetTextLabel(showMaxandMinTemperatureLabel, maxMinTemperature);
+    }
+
+    // 更新天气视图数据
+    bool WeatherView::UpdateWeatherViewData(void)
+    {
+        WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "WeatherView::UpdateWeatherViewData");
+        bool success = true;
+        success &= UpdateWeatherTime();
+        success &= UpdateWeatherImag();
+        success &= UpdateWeatherTemperature();
+        success &= UpdateWeatherWeatherDescription();
+        success &= UpdateWeatherWeatherArea();
+        success &= UpdateWeatherMaxandMinTemperature();
+
+        if (!success)
+        {
+            WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "WeatherView::UpdateWeatherViewData fail!!");
+        }
+
+        DumpWeatherInfo(); // 输出天气信息到日志
+
+        return success;
+    }
+
 }
\ No newline at end of file