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

296 lines
11 KiB
C++

#include "TjdUiAppSportAdapter.h"
#include "NativeAbility.h"
#include "TjdUiAppSportDrawGuiAPI.h"
#include "TjdUiAppSportPage.h"
#include "TjdUiAppSportView.h"
#include "TjdUiImageIds.h"
#include "TjdUiMultiLanguageExt.h"
#include "common/image_cache_manager.h"
#include "style.h"
#include "ui_checkbox.h"
#include "ui_view.h"
#include <stdio.h>
using namespace OHOS;
#define ENABLE_PRINT_INFO 0
#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__) // 错误信息打印一般常开
#define static_print_debug(...) sys_ui_log_d(__VA_ARGS__) // 调试信息打印
#else
#define static_print_info(...)
#define static_print_warn(...)
#define static_print_error(...)
#define static_print_debug(...)
#endif
namespace TJD {
#define SPORT_IMAGE_BIN_PATH TJD_IMAGE_PATH "img_sport.bin"
#define HIEGHT 10
#define SCALE_FACTOR 310
SportCaseItemView::SportCaseItemView(const SportCaseInfo &info)
{
SetPosition(0, 0, 450, 140);
SetStyle(OHOS::STYLE_MARGIN_TOP, 4);
SetStyle(OHOS::STYLE_MARGIN_BOTTOM, 4);
SetStyle(STYLE_BORDER_RADIUS, 120);
SetTouchable(true);
SetStyle(OHOS::STYLE_BACKGROUND_COLOR, 0xff202020);
text_.SetFont(TJD_VECTOR_FONT_FILENAME, 42);
text_.SetTextId(info.sportTypeId);
text_.SetLineBreakMode(OHOS::UILabel::LINE_BREAK_MARQUEE);
text_.SetAlign(OHOS::TEXT_ALIGNMENT_CENTER, OHOS::TEXT_ALIGNMENT_CENTER);
text_.SetPosition(141, 22, 168, 56); // 168 56
goalContent_.SetFont(TJD_VECTOR_FONT_FILENAME, 28);
// goalContent_.SetTextId(STR_ID_573);
// goalContent_.SetLineBreakMode(OHOS::UILabel::LINE_BREAK_ADAPT);
goalContent_.SetAlign(OHOS::TEXT_ALIGNMENT_CENTER, OHOS::TEXT_ALIGNMENT_CENTER);
goalContent_.SetPosition(0, 81, 450, 37);
goalContent_.SetStyle(STYLE_TEXT_OPA, 0xff * 0.2);
sportIcon_.SetPosition(20, 10 + HIEGHT, 100, 100);
if (info.imgInfo != nullptr) {
sportIcon_.SetSrc(info.imgInfo);
}
auto imgInfo = ImageCacheManager::GetInstance().LoadOneInMultiRes(IMG_SPORT_MORE, SPORT_IMAGE_BIN_PATH);
moreIcon_.SetPosition(374, 35 + HIEGHT, 50, 50);
if (imgInfo != nullptr) {
moreIcon_.SetSrc(imgInfo);
}
moreIcon_.SetTouchable(true);
Add(&text_);
Add(&sportIcon_);
Add(&goalContent_);
Add(&moreIcon_);
}
SportCaseItemView::~SportCaseItemView()
{
static_print_info("==line[%d]==func[%s]\n", __LINE__, __func__);
RemoveAll();
}
void SportCaseItemView::RefreshItem(const SportCaseInfo &itemInfo, int16_t index)
{
SetVisible(true);
SetViewIndex(index);
SetCurSportIndex(itemInfo.index);
text_.SetTextId(itemInfo.sportTypeId);
if (itemInfo.sportTypeId == 137)
printf("sportTypeId = %d,height = %d,width = %d\n", itemInfo.sportTypeId, text_.GetHeight(), text_.GetWidth());
goalContent_.SetText(g_pv_SportGoal[itemInfo.index].sportGoal.c_str());
sportIcon_.SetSrc(itemInfo.imgInfo);
// moreIcon_.LayoutRightOfParent(30);
}
bool SportCaseItemView::OnClickEvent(const OHOS::ClickEvent &event)
{
// static_print_info(" SportCaseItemView::OnClickEvent========index=%d==%s===%d==\n", GetViewIndex(), GetViewId(),
// GetCurSportIndex());
auto touchX = event.GetCurrentPos().x; // 获取点击的x坐标
auto x = GetX(); // 获取当前控件的x坐标
auto width = GetWidth(); // 获取当前控件的宽度
// 计算左边3/4和右边1/4的边界
auto leftThird = x + 3 * width / 4;
auto rightThird = x + 3 * width / 4;
TjdUiAppSportView::currentIconIndex_ = static_cast<SportTypeIndex>(GetCurSportIndex());
if (touchX < leftThird) {
// static_print_info("leftThird = %d, touchX = %d\n", leftThird, touchX);
auto sportModel = TjdUiAppSportModel::GetInstance();
TjdUiAppSportView &sportView = *TjdUiAppSportView::GetInstance();
sportView.ChooseSportItem(static_cast<SportTypeIndex>(GetCurSportIndex()));
if (g_pv_SportGoal[TjdUiAppSportView::currentIconIndex_].sportGoal != "无目标") {
printf("comeing sportGoal = %s ====CurSelectGoalView = %d\n",
g_pv_SportGoal[TjdUiAppSportView::currentIconIndex_].sportGoal.c_str(),
CommonViewGoalPage::CurSelectGoalView);
switch (CommonViewGoalPage::CurSelectGoalView) {
case DISTANCE_SELECT_VIEW:
sportModel->SetDistancePopUpRemind(g_pv_SportGoal[TjdUiAppSportView::currentIconIndex_].sportGoal);
break;
case TIME_SELECT_VIEW:
sportModel->SetTimePopUpRemind(g_pv_SportGoal[TjdUiAppSportView::currentIconIndex_].sportGoal);
break;
case CALORIES_SELECT_VIEW:
sportModel->SetCaloriesPopUpRemind(g_pv_SportGoal[TjdUiAppSportView::currentIconIndex_].sportGoal);
break;
default:
break;
}
}
} else if (touchX > rightThird) {
// static_print_info("rightThird = %d, touchX = %d\n", rightThird, touchX);
TjdUiAppSportView::GetInstance()->ShowTargetView(SportViewManager::SPORT_GOAL_SELECT_VIEW);
}
return UIView::OnClickEvent(event);
}
#pragma region 编辑列表
bool EditItemListListener::OnClick(OHOS::UIView &view, const OHOS::ClickEvent &event)
{
// static_print_info("EditItemListListener::OnClick====%d\n", static_cast<EditCaseItemView
// *>(view.GetParent())->GetSportIndex());
auto sportItemList = TjdUiAppSportView::GetInstance()->GetSportItemList();
auto sportModel = TjdUiAppSportModel::GetInstance();
std::list<SportCaseInfo>::iterator node = sportItemList->begin();
if (view.GetViewId() == "editBox_") {
EditCaseItemView *parent = static_cast<EditCaseItemView *>(view.GetParent());
if (parent == nullptr)
return true;
if (parent->GetEditState()) {
static_print_info("selected\n");
for (auto it = sportItemList->begin(); it != sportItemList->end();) {
if (parent->GetSportIndex() == it->index) {
it = sportItemList->erase(it);
parent->ChangeEditBoxImage(false);
g_pv_EditItemInfo[hashMap[parent->GetSportIndex()]].state = false;
sportModel->ReadAndChangeSportState(parent->GetSportIndex(), false);
} else
++it;
}
} else {
static_print_info("unselected\n");
TJD::SportCaseInfo sportCaseInfo;
auto it = sportItemList->end();
std::advance(it, -1);
for (uint8_t i = 0; i < MAX_SPORT_TYPE; i++) {
if (g_pv_Sport[i].index == parent->GetSportIndex()) {
parent->ChangeEditBoxImage(true);
g_pv_EditItemInfo[hashMap[parent->GetSportIndex()]].state = true;
auto imgInfo = ImageCacheManager::GetInstance().LoadOneInMultiRes(g_pv_Sport[i].imgIconResId,
SPORT_IMAGE_BIN_PATH);
sportCaseInfo.index = g_pv_Sport[i].index;
sportCaseInfo.sportTypeId = g_pv_Sport[i].sportTypeId;
sportCaseInfo.imgInfo = imgInfo;
sportCaseInfo.goalId = g_pv_Sport[i].goalId;
break;
}
}
sportItemList->push_back(sportCaseInfo);
// static_print_info("=====ReadAndChangeSportState======%d==\n",
// static_cast<EditCaseItemView *>(view.GetParent())->GetSportIndex());
sportModel->ReadAndChangeSportState(parent->GetSportIndex(), true);
}
SportList::GetInstance()->GetSportListView()->RefreshList();
TjdUiAppSportView::GetInstance()->ShowTargetView(SportViewManager::SPORT_LIST_VIEW);
}
return true;
}
EditCaseItemView::EditCaseItemView(const EditItemInfo &info)
{
auto &image = OHOS::ImageCacheManager::GetInstance();
SetPosition(0, 0, 450, 140);
SetStyle(STYLE_BORDER_RADIUS, 70);
SetStyle(OHOS::STYLE_MARGIN_TOP, 4);
SetStyle(OHOS::STYLE_MARGIN_BOTTOM, 4);
SetStyle(OHOS::STYLE_BACKGROUND_OPA, 0xff);
SetStyle(OHOS::STYLE_BACKGROUND_COLOR, 0xff262626);
SetTouchable(true);
// InitLabelHorCenter(text_, 32, 48, 450, "户外步行");
text_.SetPosition(141, 42, 168, 56); // 168 56
text_.SetFont(TJD_VECTOR_FONT_FILENAME, 42);
text_.SetText("户外步行");
text_.SetLineBreakMode(OHOS::UILabel::LINE_BREAK_MARQUEE);
text_.SetAlign(OHOS::TEXT_ALIGNMENT_CENTER, OHOS::TEXT_ALIGNMENT_CENTER);
printf("text_.getheight() = %d\n", text_.GetHeight());
OHOS::ImageInfo *imgInfo = image.LoadOneInMultiRes(IMG_SPORT_01_OUTDOOR_RUNNING, SPORT_IMAGE_BIN_PATH);
sportIcon_.SetPosition(20, 20, 100, 100);
if (imgInfo != nullptr) {
sportIcon_.SetSrc(imgInfo);
}
addImg = ImageCacheManager::GetInstance().LoadOneInMultiRes(IMG_SPORT_ADD, SPORT_IMAGE_BIN_PATH);
educeImg = ImageCacheManager::GetInstance().LoadOneInMultiRes(IMG_SPORT_EDUCE, SPORT_IMAGE_BIN_PATH);
editBox_.SetPosition(380, 46, 48, 48);
// editBox_.SetState(UICheckBox::SELECTED);
// editBox_.SetImages(selectedImg, unselectedImg);
editBox_.SetSrc(educeImg);
editBox_.SetViewId("editBox_");
editBox_.SetOnClickListener(&boxOnClickListener_);
Add(&text_);
Add(&sportIcon_);
Add(&editBox_);
}
void EditCaseItemView::ChangeEditBoxImage(bool state)
{
if (state) {
state_ = true;
editBox_.SetSrc(educeImg);
} else {
state_ = false;
editBox_.SetSrc(addImg);
}
}
void EditCaseItemView::RefreshItem(const EditItemInfo &itemInfo, int16_t index)
{
itemInfo_ = itemInfo;
SetVisible(true);
SetViewIndex(index);
sportIcon_.SetSrc(itemInfo.imgInfo);
editBox_.SetViewIndex(index);
text_.SetTextId(itemInfo.sportTypeId);
text_.SetX((450 - text_.GetWidth()) / 2);
SetSportIndex(itemInfo.index);
// static_print_info("===EditCaseItemView::UpdateView==state=%d==info.index)=%d==\n", info.state, info.index);
// SportInfo* jsonSportInfo = TjdUiAppSportModel::GetInstance()->ReadFileAndTransformToJson();
if (g_pv_EditItemInfo[hashMap[itemInfo_.index]].state) {
state_ = true;
editBox_.SetSrc(educeImg);
} else {
state_ = false;
editBox_.SetSrc(addImg);
}
editBox_.Invalidate();
}
bool EditCaseItemView::OnClickEvent(const OHOS::ClickEvent &event)
{
auto touchX = event.GetCurrentPos().x; // 获取点击的x坐标
auto x = GetX(); // 获取当前控件的x坐标
auto width = GetWidth(); // 获取当前控件的宽度
// 计算左边3/4和右边1/4的边界
auto leftThird = x + 3 * width / 4;
auto rightThird = x + 3 * width / 4;
if (touchX > rightThird) {
return editBox_.OnClickEvent(event);
}
return false;
}
EditCaseItemView::~EditCaseItemView()
{
static_print_info("EditCaseItemView==line[%d]==func[%s]\n", __LINE__, __func__);
RemoveAll();
}
#pragma endregion
} // namespace TJD