* @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
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; // 天气描述
+/*
+ * @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"
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;
}
WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "WeatherView::InitPage fail");
return false;
}
- UpdataTime();
+ UpdateWeatherViewData();
SetDraggable(true);
SetTouchable(true);
return true;
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];
}
}
+ // 设置文本标签的通用函数
+ 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