From 42c9af14435eb53783c9eb8eeb057967d2738add Mon Sep 17 00:00:00 2001 From: luozheng <1732036298@qq.com> Date: Sat, 15 Jun 2024 12:01:17 +0800 Subject: [PATCH] =?utf8?q?=E5=A4=A9=E6=B0=94=E8=A7=86=E5=9B=BE=E7=AC=AC?= =?utf8?q?=E4=B8=80=E4=B8=AAView=E5=88=B7=E6=96=B0=E5=87=BD=E6=95=B0?= =?utf8?q?=E6=8E=A5=E5=8F=A3=E6=B7=BB=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .../nativeui/weather/include/WeatherView.h | 20 ++- .../nativeui/weather/src/WeatherModel.cpp | 4 +- .../nativeui/weather/src/WeatherView.cpp | 160 ++++++++++++++---- 3 files changed, 144 insertions(+), 40 deletions(-) diff --git a/code/sdk/application/wearable/fd230app/nativeui/weather/include/WeatherView.h b/code/sdk/application/wearable/fd230app/nativeui/weather/include/WeatherView.h index c86b93323..f1189ddeb 100644 --- a/code/sdk/application/wearable/fd230app/nativeui/weather/include/WeatherView.h +++ b/code/sdk/application/wearable/fd230app/nativeui/weather/include/WeatherView.h @@ -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; // 天气描述 diff --git a/code/sdk/application/wearable/fd230app/nativeui/weather/src/WeatherModel.cpp b/code/sdk/application/wearable/fd230app/nativeui/weather/src/WeatherModel.cpp index 72848d63d..83efef535 100644 --- a/code/sdk/application/wearable/fd230app/nativeui/weather/src/WeatherModel.cpp +++ b/code/sdk/application/wearable/fd230app/nativeui/weather/src/WeatherModel.cpp @@ -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); diff --git a/code/sdk/application/wearable/fd230app/nativeui/weather/src/WeatherView.cpp b/code/sdk/application/wearable/fd230app/nativeui/weather/src/WeatherView.cpp index b3c01d02d..be8f5fbcd 100644 --- a/code/sdk/application/wearable/fd230app/nativeui/weather/src/WeatherView.cpp +++ b/code/sdk/application/wearable/fd230app/nativeui/weather/src/WeatherView.cpp @@ -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 -- 2.17.1