mcu_hi3321_watch/tjd/ui/app/main/TjdUiAppMainPageWeather.cpp
2025-05-26 20:15:20 +08:00

471 lines
19 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/*----------------------------------------------------------------------------
* Copyright (c) TJD Technologies Co., Ltd. 2024. All rights reserved.
*
* Description: TjdUiAppMainPageWeather.cpp
*
* Author: luziquan@ss-tjd.com
*
* Create: 2024-12-27
*--------------------------------------------------------------------------*/
#include "TjdUiAppMainPageWeather.h"
#include "TjdUiAppWeatherModel.h"
#include "TjdUiImageIds.h"
#include "TjdUiMultiLanguageExt.h"
#include "common/image_cache_manager.h"
#include "common/screen.h"
#include "graphic_service.h"
#include "hal_tick.h"
#include "sys_config.h"
extern "C"
{
#include "task_ancillary.h"
}
#define ENABLE_PRINT_INFO 1
#if ENABLE_PRINT_INFO
#define static_print_info(...) sys_ui_log_i(__VA_ARGS__) // 一般信息打印宏控制
#define static_print_warn(...) sys_ui_log_w(__VA_ARGS__) // 警告信息打印一般常开
#define static_print_error(...) sys_ui_log_e(__VA_ARGS__) // 错误信息打印一般常开
#else
#define static_print_info(...)
#define static_print_warn(...)
#define static_print_error(...)
#endif
namespace TJD {
#define MAIN_WEATHER_COMMON_BIN_PATH TJD_IMAGE_PATH "img_weather_common.bin"
#define MAIN_WEATHER_BJ_CLOUDY_BIN_PATH TJD_IMAGE_PATH "img_weather_bj_cloudy.bin"
#define MAIN_WEATHER_BJ_FOG_BIN_PATH TJD_IMAGE_PATH "img_weather_bj_fog.bin"
#define MAIN_WEATHER_BJ_RAIN_BIN_PATH TJD_IMAGE_PATH "img_weather_bj_rain.bin"
#define MAIN_WEATHER_BJ_SANDSTORM_BIN_PATH TJD_IMAGE_PATH "img_weather_bj_sandstorm.bin"
#define MAIN_WEATHER_BJ_SNOW_BIN_PATH TJD_IMAGE_PATH "img_weather_bj_snow.bin"
#define MAIN_WEATHER_BJ_SUNNY_BIN_PATH TJD_IMAGE_PATH "img_weather_bj_sunny.bin"
#define MAIN_WEATHER_BJ_THUNDERSTORM_BIN_PATH TJD_IMAGE_PATH "img_weather_bj_thunderstorm.bin"
#define MAIN_WEATHER_BJ_TYPHOON_BIN_PATH TJD_IMAGE_PATH "img_weather_bj_typhoon.bin"
static std::string g_loadAllPath = "";
struct EventInfo
{
signed int (*func_event_handler)(void *param); // 事件处理函数
void *param; // 事件处理函数入参
void (*func_event_debug)(char *str, ...); // 打印
void *payload;
};
static std::function<void(void)> g_postEventCallback = nullptr;
static signed int EventHandler(void *param)
{
if (g_postEventCallback != nullptr) {
g_postEventCallback();
}
return 0;
}
static void PostEventHandle(std::function<void(void)> event)
{
EventInfo g_msg_data = {
.func_event_handler = EventHandler,
.param = NULL,
.func_event_debug = NULL,
.payload = NULL,
};
g_postEventCallback = event;
static unsigned int g_msg_size = sizeof(EventInfo);
osal_msg_queue_write_copy(tjd_task_ancillary_get_queue_id(), &g_msg_data, g_msg_size, 0);
}
TjdUiAppMainPageWeather::TjdUiAppMainPageWeather()
{
SetPosition(0, 0, OHOS::Screen::GetInstance().GetWidth(), OHOS::Screen::GetInstance().GetHeight());
weartherTypeRes_.reset(new int[WeatherInfo::SQL_WEATHER_TYPE_MAX]);
weartherTypeRes_[WeatherInfo::SQL_SUNNY] = IMG_WEATHER_COMMON_W_ICON_SUNNY;
weartherTypeRes_[WeatherInfo::SQL_PARTLY_CLOUDY] = IMG_WEATHER_COMMON_W_ICON_PARTLY_CLOUDY;
weartherTypeRes_[WeatherInfo::SQL_CLOUDY] = IMG_WEATHER_COMMON_W_ICON_CLOUDY;
weartherTypeRes_[WeatherInfo::SQL_LIGHT_RAIN] = IMG_WEATHER_COMMON_W_ICON_LIGHT_RAIN;
weartherTypeRes_[WeatherInfo::SQL_MODERATE_RAIN] = IMG_WEATHER_COMMON_W_ICON_MODERATE_RAIN;
weartherTypeRes_[WeatherInfo::SQL_HEAVY_RAIN] = IMG_WEATHER_COMMON_W_ICON_HEAVY_RAIN;
weartherTypeRes_[WeatherInfo::SQL_SHOWER] = IMG_WEATHER_COMMON_W_ICON_SHOWER;
weartherTypeRes_[WeatherInfo::SQL_THUNDERSTORM] = IMG_WEATHER_COMMON_W_ICON_THUNDERSTORM;
weartherTypeRes_[WeatherInfo::SQL_SNOWY] = IMG_WEATHER_COMMON_W_ICON_SNOWY;
weartherTypeRes_[WeatherInfo::SQL_FOGGY] = IMG_WEATHER_COMMON_W_ICON_FOGGY;
weartherTypeRes_[WeatherInfo::SQL_DUSTSTORM] = IMG_WEATHER_COMMON_W_ICON_DUST_STORM;
weartherTypeRes_[WeatherInfo::SQL_TYPHOON] = IMG_WEATHER_COMMON_W_ICON_TYPHOON;
}
TjdUiAppMainPageWeather::~TjdUiAppMainPageWeather()
{
if (viewiInitStatus) {
OHOS::ImageCacheManager::GetInstance().UnloadAllInMultiRes(MAIN_WEATHER_COMMON_BIN_PATH);
RemoveAll();
if (!g_loadAllPath.empty()) {
OHOS::ImageCacheManager::GetInstance().UnloadAllInMultiRes(g_loadAllPath.c_str());
g_loadAllPath.clear();
}
if (container_ != nullptr) {
container_->RemoveAll();
delete container_;
container_ = nullptr;
}
if (bgStatic_ != nullptr) {
delete bgStatic_;
bgStatic_ = nullptr;
}
if (bgAnimator_ != nullptr) {
delete bgAnimator_;
bgAnimator_ = nullptr;
}
if (localPosIcon_ != nullptr) {
delete localPosIcon_;
localPosIcon_ = nullptr;
}
if (localPos_ != nullptr) {
delete localPos_;
localPos_ = nullptr;
}
if (currentTemp_ != nullptr) {
delete currentTemp_;
currentTemp_ = nullptr;
}
if (minTempIcon_ != nullptr) {
delete minTempIcon_;
minTempIcon_ = nullptr;
}
if (minTemp_ != nullptr) {
delete minTemp_;
minTemp_ = nullptr;
}
if (maxTempIcon_ != nullptr) {
delete maxTempIcon_;
maxTempIcon_ = nullptr;
}
if (maxTemp_ != nullptr) {
delete maxTemp_;
maxTemp_ = nullptr;
}
if (windIcon_ != nullptr) {
delete windIcon_;
windIcon_ = nullptr;
}
if (wind_ != nullptr) {
delete wind_;
wind_ = nullptr;
}
if (leftLine_ != nullptr) {
delete leftLine_;
leftLine_ = nullptr;
}
if (weatherIcon_ != nullptr) {
delete weatherIcon_;
weatherIcon_ = nullptr;
}
if (weather_ != nullptr) {
delete weather_;
weather_ = nullptr;
}
if (rightLine_ != nullptr) {
delete rightLine_;
rightLine_ = nullptr;
}
if (humidityIcon_ != nullptr) {
delete humidityIcon_;
humidityIcon_ = nullptr;
}
if (humidity_ != nullptr) {
delete humidity_;
humidity_ = nullptr;
}
viewiInitStatus = false;
}
}
void TjdUiAppMainPageWeather::PreLoad(void)
{
static_print_info("TjdUiAppMainPageWeather::PreLoad");
OHOS::ImageCacheManager::GetInstance().LoadAllInMultiRes(MAIN_WEATHER_COMMON_BIN_PATH);
if (!viewiInitStatus) {
InitView();
UpdateValue();
viewiInitStatus = true;
}
}
void TjdUiAppMainPageWeather::UnLoad(void) { static_print_info("TjdUiAppMainPageWeather::UnLoad"); }
void TjdUiAppMainPageWeather::ScrollEnd(bool isActive)
{
if (bgAnimator_ == nullptr || bgAnimator_->GetImageAnimatorSrc() == nullptr) {
return;
}
}
void TjdUiAppMainPageWeather::InitView(void)
{
auto &image = OHOS::ImageCacheManager::GetInstance();
OHOS::ImageInfo *imageInfo = nullptr;
container_ = new OHOS::UIViewGroup();
container_->SetPosition(0, 0, OHOS::Screen::GetInstance().GetWidth(), OHOS::Screen::GetInstance().GetHeight());
bgStatic_ = new OHOS::UIImageView();
bgStatic_->SetPosition(0, 0, OHOS::Screen::GetInstance().GetWidth(), OHOS::Screen::GetInstance().GetHeight());
container_->Add(bgStatic_);
bgAnimator_ = new OHOS::UIImageAnimatorView();
bgAnimator_->SetPosition(0, 0, OHOS::Screen::GetInstance().GetWidth(), OHOS::Screen::GetInstance().GetHeight());
container_->Add(bgAnimator_);
bgAnimator_->SetVisible(false);
localPosIcon_ = new OHOS::UIImageView();
localPosIcon_->SetPosition(140, 150, 24, 28);
imageInfo = image.LoadOneInMultiRes(IMG_WEATHER_COMMON_DINGWEI_ICON, MAIN_WEATHER_COMMON_BIN_PATH);
localPosIcon_->SetSrc(imageInfo);
container_->Add(localPosIcon_);
localPos_ = new OHOS::UILabel();
localPos_->SetFont(TJD_VECTOR_FONT_FILENAME, 24);
localPos_->SetText("深圳市 宝安区");
localPos_->SetLineBreakMode(OHOS::UILabel::LINE_BREAK_WRAP);
localPos_->SetAlign(OHOS::TEXT_ALIGNMENT_LEFT, OHOS::TEXT_ALIGNMENT_CENTER);
localPos_->SetPosition(180, 148, 230, 24);
container_->Add(localPos_);
currentTemp_ = new OHOS::UILabel();
currentTemp_->SetFont(TJD_D_DIN_PRO_REGULAR_FONT_FILENAME, 82);
currentTemp_->SetText("24°");
currentTemp_->SetLineBreakMode(OHOS::UILabel::LINE_BREAK_WRAP);
currentTemp_->SetAlign(OHOS::TEXT_ALIGNMENT_RIGHT, OHOS::TEXT_ALIGNMENT_CENTER);
currentTemp_->SetPosition(55, 200, 190, 58);
container_->Add(currentTemp_);
minTempIcon_ = new OHOS::UIImageView();
minTempIcon_->SetPosition(258, 209, 15, 22);
imageInfo = image.LoadOneInMultiRes(IMG_WEATHER_COMMON_LOW, MAIN_WEATHER_COMMON_BIN_PATH);
minTempIcon_->SetSrc(imageInfo);
container_->Add(minTempIcon_);
minTemp_ = new OHOS::UILabel();
minTemp_->SetFont(TJD_D_DIN_PRO_REGULAR_FONT_FILENAME, 30);
minTemp_->SetText("10°C");
minTemp_->SetLineBreakMode(OHOS::UILabel::LINE_BREAK_WRAP);
minTemp_->SetAlign(OHOS::TEXT_ALIGNMENT_LEFT, OHOS::TEXT_ALIGNMENT_CENTER);
minTemp_->SetPosition(286, 206, 80, 22);
minTemp_->SetStyle(OHOS::STYLE_TEXT_OPA, OHOS::OPA_OPAQUE * 0.6);
container_->Add(minTemp_);
maxTempIcon_ = new OHOS::UIImageView();
maxTempIcon_->SetPosition(258, 246, 15, 22);
imageInfo = image.LoadOneInMultiRes(IMG_WEATHER_COMMON_HIGH, MAIN_WEATHER_COMMON_BIN_PATH);
maxTempIcon_->SetSrc(imageInfo);
container_->Add(maxTempIcon_);
maxTemp_ = new OHOS::UILabel();
maxTemp_->SetFont(TJD_D_DIN_PRO_REGULAR_FONT_FILENAME, 30);
maxTemp_->SetText("26°C");
maxTemp_->SetLineBreakMode(OHOS::UILabel::LINE_BREAK_WRAP);
maxTemp_->SetAlign(OHOS::TEXT_ALIGNMENT_LEFT, OHOS::TEXT_ALIGNMENT_CENTER);
maxTemp_->SetPosition(286, 243, 80, 22);
maxTemp_->SetStyle(OHOS::STYLE_TEXT_OPA, OHOS::OPA_OPAQUE * 0.6);
container_->Add(maxTemp_);
windIcon_ = new OHOS::UIImageView();
windIcon_->SetPosition(130, 338, 32, 34);
imageInfo = image.LoadOneInMultiRes(IMG_WEATHER_COMMON_WIND_ICON, MAIN_WEATHER_COMMON_BIN_PATH);
windIcon_->SetSrc(imageInfo);
container_->Add(windIcon_);
wind_ = new OHOS::UILabel();
wind_->SetFont(TJD_D_DIN_PRO_REGULAR_FONT_FILENAME, 24);
wind_->SetText("03.3");
wind_->SetLineBreakMode(OHOS::UILabel::LINE_BREAK_WRAP);
wind_->SetAlign(OHOS::TEXT_ALIGNMENT_CENTER, OHOS::TEXT_ALIGNMENT_CENTER);
wind_->SetPosition(107, 385, 74, 18);
container_->Add(wind_);
leftLine_ = new OHOS::UIView();
leftLine_->SetPosition(188, 327, 1, 85);
leftLine_->SetStyle(OHOS::STYLE_BACKGROUND_COLOR, 0xFFF0F1F3);
leftLine_->SetStyle(OHOS::STYLE_BACKGROUND_OPA, OHOS::OPA_OPAQUE * 0.3);
container_->Add(leftLine_);
weatherIcon_ = new OHOS::UIImageView();
weatherIcon_->SetPosition(210, 330, 47, 47);
imageInfo = image.LoadOneInMultiRes(IMG_WEATHER_COMMON_W_ICON_SUNNY, MAIN_WEATHER_COMMON_BIN_PATH);
weatherIcon_->SetSrc(imageInfo);
OHOS::Vector2<float> scale = {47.0 / imageInfo->header.width, 47.0 / imageInfo->header.height};
weatherIcon_->Scale(scale, {0, 0});
container_->Add(weatherIcon_);
weather_ = new OHOS::UILabelExt();
weather_->SetFont(TJD_VECTOR_FONT_FILENAME, 20);
// weather_->SetText("晴天");
weather_->SetLineBreakMode(OHOS::UILabel::LINE_BREAK_WRAP);
weather_->SetAlign(OHOS::TEXT_ALIGNMENT_CENTER, OHOS::TEXT_ALIGNMENT_CENTER);
weather_->SetPosition(190, 383, 85, 19);
container_->Add(weather_);
rightLine_ = new OHOS::UIView();
rightLine_->SetPosition(274, 327, 1, 85);
rightLine_->SetStyle(OHOS::STYLE_BACKGROUND_COLOR, 0xFFF0F1F3);
rightLine_->SetStyle(OHOS::STYLE_BACKGROUND_OPA, OHOS::OPA_OPAQUE * 0.3);
container_->Add(rightLine_);
humidityIcon_ = new OHOS::UIImageView();
humidityIcon_->SetPosition(311, 340, 29, 29);
imageInfo = image.LoadOneInMultiRes(IMG_WEATHER_COMMON_HUMIDITY_ICON, MAIN_WEATHER_COMMON_BIN_PATH);
humidityIcon_->SetSrc(imageInfo);
container_->Add(humidityIcon_);
humidity_ = new OHOS::UILabel();
humidity_->SetFont(TJD_D_DIN_PRO_REGULAR_FONT_FILENAME, 24);
humidity_->SetText("-150%");
humidity_->SetLineBreakMode(OHOS::UILabel::LINE_BREAK_WRAP);
humidity_->SetAlign(OHOS::TEXT_ALIGNMENT_CENTER, OHOS::TEXT_ALIGNMENT_CENTER);
humidity_->SetPosition(290, 385, 70, 18);
container_->Add(humidity_);
Add(container_);
}
static inline int LoadInfo(std::unique_ptr<OHOS::ImageAnimatorInfo[]> &refInfo, int resId, int size,
const std::string &path)
{
auto &image = OHOS::ImageCacheManager::GetInstance();
OHOS::ImageInfo *imageInfo = nullptr;
refInfo.reset(new OHOS::ImageAnimatorInfo[size]);
for (int i = 0; i < size; ++i) {
imageInfo = image.LoadOneInMultiRes(resId + i, path);
refInfo[i].imageInfo = imageInfo;
refInfo[i].pos = {0, 0};
refInfo[i].width = imageInfo->header.width;
refInfo[i].height = imageInfo->header.height;
refInfo[i].imageType = OHOS::IMG_SRC_IMAGE_INFO;
}
return size;
}
static WeatherInfoRes GetWeatherInfoRes(const WeatherInfo &type)
{
int size = 0;
int startRes = 0;
std::string path = "";
// clang-format off
switch (type) {
case WeatherInfo::SQL_SUNNY: size = 20; startRes = IMG_WEATHER_BJ_SUNNY_CLEAR_01; path = MAIN_WEATHER_BJ_SUNNY_BIN_PATH; break;
case WeatherInfo::SQL_PARTLY_CLOUDY:
case WeatherInfo::SQL_CLOUDY: size = 20; startRes = IMG_WEATHER_BJ_CLOUDY_CLOUDY_DT_01; path = MAIN_WEATHER_BJ_CLOUDY_BIN_PATH; break;
case WeatherInfo::SQL_SHOWER:
case WeatherInfo::SQL_LIGHT_RAIN:
case WeatherInfo::SQL_MODERATE_RAIN:
case WeatherInfo::SQL_HEAVY_RAIN: size = 4; startRes = IMG_WEATHER_BJ_RAIN_RAIN_DT_01; path = MAIN_WEATHER_BJ_RAIN_BIN_PATH; break;
case WeatherInfo::SQL_THUNDERSTORM: size = 8; startRes = IMG_WEATHER_BJ_THUNDERSTORM_THUNDERSTORM_DT_01; path = MAIN_WEATHER_BJ_THUNDERSTORM_BIN_PATH; break;
case WeatherInfo::SQL_SNOWY: size = 10; startRes = IMG_WEATHER_BJ_SNOW_SNOW_DT_01; path = MAIN_WEATHER_BJ_SNOW_BIN_PATH; break;
case WeatherInfo::SQL_FOGGY: size = 20; startRes = IMG_WEATHER_BJ_FOG_FOG_DT_01; path = MAIN_WEATHER_BJ_FOG_BIN_PATH; break;
case WeatherInfo::SQL_DUSTSTORM: size = 6; startRes = IMG_WEATHER_BJ_SANDSTORM_SANDSTORM_DT_01; path = MAIN_WEATHER_BJ_SANDSTORM_BIN_PATH; break;
case WeatherInfo::SQL_TYPHOON: size = 5; startRes = IMG_WEATHER_BJ_TYPHOON_TYPHOON_DT_01; path = MAIN_WEATHER_BJ_TYPHOON_BIN_PATH; break;
default: break;
}
// clang-format on
return {startRes, size, path};
}
void TjdUiAppMainPageWeather::AnimatorResLoadSuccess(void)
{
LoadInfo(animatorInfoArray_, curInfo_.startResId, curInfo_.size, curInfo_.path);
if (bgAnimator_) {
bgAnimator_->SetImageAnimatorSrc(animatorInfoArray_.get(), curInfo_.size, 75);
if (bgAnimator_->GetState() == OHOS::Animator::STOP) {
bgStatic_->SetVisible(false);
bgAnimator_->SetVisible(true);
bgAnimator_->Start();
}
}
}
void TjdUiAppMainPageWeather::UpdateValue(void)
{
auto &image = OHOS::ImageCacheManager::GetInstance();
TjdUiAppWeatherModel &model = TjdUiAppWeatherModel::GetInstance();
model.LoadCurWeatherData();
auto type = static_cast<WeatherInfo>(sql_weather_get_forecast_protocol_value(0));
// clang-format off
switch (type) {
case WeatherInfo::SQL_SUNNY: weartherType_ = STR_ID_633;break;
case WeatherInfo::SQL_PARTLY_CLOUDY: weartherType_ = STR_ID_634;break;
case WeatherInfo::SQL_CLOUDY: weartherType_ = STR_ID_635;break;
case WeatherInfo::SQL_LIGHT_RAIN: weartherType_ = STR_ID_636;break;
case WeatherInfo::SQL_MODERATE_RAIN: weartherType_ = STR_ID_637;break;
case WeatherInfo::SQL_HEAVY_RAIN: weartherType_ = STR_ID_638;break;
case WeatherInfo::SQL_SHOWER: weartherType_ = STR_ID_639;break;
case WeatherInfo::SQL_THUNDERSTORM: weartherType_ = STR_ID_640;break;
case WeatherInfo::SQL_SNOWY: weartherType_ = STR_ID_641;break;
case WeatherInfo::SQL_FOGGY: weartherType_ = STR_ID_642;break;
case WeatherInfo::SQL_DUSTSTORM: weartherType_ = STR_ID_643;break;
case WeatherInfo::SQL_TYPHOON: weartherType_ = STR_ID_644;break;
default: weartherType_ = STR_ID_645;break;
}
// clang-format on
if (bgStatic_) {
curInfo_ = GetWeatherInfoRes(type);
if (curInfo_.size != 0) {
// 先加载静态图片然后在ScrollEnd=1的时候加载动画
auto imageRes = image.LoadOneInMultiRes(curInfo_.startResId, curInfo_.path);
bgStatic_->SetSrc(imageRes);
}
}
if (bgAnimator_) {
PostEventHandle([this]() {
uint32_t startTime = OHOS::HALTick::GetInstance().GetTime();
OHOS::ImageCacheManager::GetInstance().LoadAllInMultiRes(curInfo_.path);
g_loadAllPath = curInfo_.path;
uint32_t endTime = OHOS::HALTick::GetInstance().GetElapseTime(startTime);
GraphicService::GetInstance()->PostGraphicEvent([this]() { AnimatorResLoadSuccess(); });
static_print_info("LoadAllInMultiRes time:%d", endTime);
});
}
if (localPos_) {
localPos_->SetText(model.GetLocation());
}
if (currentTemp_) {
currentTemp_->SetText(std::to_string(model.GetCurTemperature()).c_str());
}
if (maxTemp_) {
maxTemp_->SetText(std::to_string(model.GetTemperatureMax()).c_str());
}
if (minTemp_) {
minTemp_->SetText(std::to_string(model.GetTemperatureMin()).c_str());
}
if (wind_) {
wind_->SetText(std::to_string(model.GetWindSpeed()).c_str());
}
if (humidity_) {
humidity_->SetText(std::to_string(model.GetHumidity()).c_str());
}
if (weatherIcon_ && type < WeatherInfo::SQL_WEATHER_TYPE_MAX) {
weatherIcon_->SetSrc(image.LoadOneInMultiRes(weartherTypeRes_[type], MAIN_WEATHER_COMMON_BIN_PATH));
} else {
weatherIcon_->SetSrc(image.LoadOneInMultiRes(IMG_WEATHER_COMMON_W_ICON_UNKNOW, MAIN_WEATHER_COMMON_BIN_PATH));
}
if (weather_) {
weather_->SetTextId(weartherType_);
}
}
} // namespace TJD