mcu_hi3321_watch/tjd/ui/app/main/TjdUiAppMainView.cpp
2025-05-31 10:45:15 +08:00

553 lines
17 KiB
C++

/*----------------------------------------------------------------------------
* Copyright (c) TJD Technologies Co., Ltd. 2024. All rights reserved.
*
* Description:
*
* Author: huangshuyi
*
* Create: 2024-4
*--------------------------------------------------------------------------*/
#include "TjdUiAppMainView.h"
#include "TjdUiAppMainPresenter.h"
#include "TjdUiEditShortcutPresenter.h"
#include "TjdUiMemManage.h"
#include "TjdUiScreenEvent.h"
#include "TjdUiScreenManage.h"
#include "TjdUiWatchFaceCtrl.h"
#include "components/root_view.h"
#include "service_ancillary.h"
#include "sql_bt.h"
#include "sql_setting.h"
#include "sys_config.h"
#include "sys_typedef.h"
#if ENABLE_MEMORY_CHECK
#include "gfx_utils/mem_check.h"
#endif
#include "TjdUiAppMainPageFavorite.h"
#include "TjdUiAppMainPageFourLeafClover.h"
#include "TjdUiAppMainPageHealth.h"
#include "TjdUiAppMainPageMakeFriend.h"
#include "TjdUiAppMainPageMap.h"
#include "TjdUiAppMainPageMessage.h"
#include "TjdUiAppMainPageMusic.h"
#include "TjdUiAppMainPageShortCut.h"
#include "TjdUiAppMainPageTool.h"
#include "TjdUiAppMainPageWeather.h"
namespace TJD {
#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__) //错误信息打印一般常开
#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
static TjdUiAppMainView *g_pMainClockView = nullptr;
uint8_t TjdUiAppMainView::messageReaded = 0;
struct MainViewMapper
{
TjdCardId id;
OHOS::UICardPage *(TjdUiAppMainView::*func)(void);
};
static const MainViewMapper CardMapper[] = {
{TjdCardId::TJD_CARD_FAVORITE, &TjdUiAppMainView::InitFavoritePage},
{TjdCardId::TJD_CARD_WATCH_FACE, &TjdUiAppMainView::InitWatchFacePage},
{TjdCardId::TJD_CARD_HEALTH, &TjdUiAppMainView::InitHealthPage},
{TjdCardId::TJD_CARD_TOOL, &TjdUiAppMainView::InitToolPage},
{TjdCardId::TJD_CARD_WEATHER, &TjdUiAppMainView::InitWeatherPage},
{TjdCardId::TJD_CARD_MUSIC, &TjdUiAppMainView::InitMusicPage},
{TjdCardId::TJD_CARD_MAKE_FRIEND, &TjdUiAppMainView::InitMakeFriendPage},
{TjdCardId::TJD_CARD_MAP, &TjdUiAppMainView::InitMapPage},
{TjdCardId::TJD_CARD_FOUR_LEAF_CLOVER, &TjdUiAppMainView::InitFourLeafCloverPage},
};
void TjdUiAppMainView::CustomCrossView::OnMoveChild(OHOS::UISwipeView &view, int16_t xOffset, int16_t yOffset)
{
TjdUiAppMainView *mainView = TjdUiAppMainView::GetInstance();
if (mainView == nullptr) {
return;
}
if (mainView->GetHorCurrentCard() != TjdCardId::TJD_CARD_WATCH_FACE) {
return;
}
TjdUiAppMainPageShortcut *shortcutPage = TjdUiAppMainPageShortcut::GetInstance();
TjdUiAppMainPageMessage *messagePage = TjdUiAppMainPageMessage::GetInstance();
if (shortcutPage == nullptr || messagePage == nullptr) {
return;
}
int16_t buttom = shortcutPage->GetScrollView()->GetChildrenHead()->GetRect().GetBottom();
if (buttom < 0) {
buttom = 0;
}
int16_t messageTop = messagePage->GetRect().GetTop();
#define SET_BLUE 0
#if SET_BLUE
if (messageTop >= 466) {
int16_t blur = OHOS::EasingEquation::LinearEaseNone(0, 32, buttom, 466);
GetChildrenTail()->SetBackgroundBlur(blur);
} else if (buttom <= 0) {
int16_t blur = OHOS::EasingEquation::LinearEaseNone(32, 0, messageTop, 466);
GetChildrenTail()->SetBackgroundBlur(blur);
}
#else
int16_t opa = 0;
if (messageTop >= 466) {
opa = OHOS::EasingEquation::SineEaseIn(OHOS::OPA_TRANSPARENT, OHOS::OPA_OPAQUE, buttom,
OHOS::VERTICAL_RESOLUTION >> 1);
GetChildrenTail()->SetStyle(OHOS::STYLE_BACKGROUND_OPA, opa);
} else if (buttom <= 0) {
opa = OHOS::EasingEquation::SineEaseIn(OHOS::OPA_TRANSPARENT, OHOS::OPA_OPAQUE,
OHOS::VERTICAL_RESOLUTION - messageTop, OHOS::VERTICAL_RESOLUTION >> 1);
GetChildrenTail()->SetStyle(OHOS::STYLE_BACKGROUND_OPA, opa);
}
#endif
}
void TjdUiAppMainView::CustomCrossView::ResetBackgroundOpa(void)
{
GetChildrenTail()->SetStyle(OHOS::STYLE_BACKGROUND_OPA, OHOS::OPA_TRANSPARENT);
}
bool TjdUiAppMainView::CustomCrossView::OnDragStartEvent(const OHOS::DragEvent &event)
{
curVerId = GetCurrentPageId(OHOS::UISwipeView::VERTICAL);
return OHOS::UICrossView::OnDragStartEvent(event);
}
bool TjdUiAppMainView::CustomCrossView::OnDragEvent(const OHOS::DragEvent &event)
{
if (curVerId != GetCurrentPageId(OHOS::UISwipeView::VERTICAL)) {
return true;
}
return OHOS::UICrossView::OnDragEvent(event);
}
bool TjdUiAppMainView::CustomCrossView::OnDragEndEvent(const OHOS::DragEvent &event)
{
if (curVerId != GetCurrentPageId(OHOS::UISwipeView::VERTICAL)) {
return true;
}
return OHOS::UICrossView::OnDragEndEvent(event);
}
TjdUiAppMainView::TjdUiAppMainView() : wfCommon_(new TjdUiWatchFaceCommonView()) { g_pMainClockView = this; }
TjdUiAppMainView::~TjdUiAppMainView()
{
g_pMainClockView = nullptr;
if (shortcutPage) {
delete shortcutPage;
shortcutPage = nullptr;
}
if (messagePage) {
delete messagePage;
messagePage = nullptr;
}
for (int32_t i = 0; i < TjdCardId::TJD_CARD_MAX_CARD; i++) {
recordCard[i] = nullptr;
}
if (cardSwipe) {
delete cardSwipe;
cardSwipe = nullptr;
}
if (mainSwipeGroup) {
// mainSwipeGroup->RemoveAll();
delete mainSwipeGroup;
mainSwipeGroup = nullptr;
}
if (mainClockView) {
delete mainClockView;
mainClockView = nullptr;
}
if (favoritePage) {
delete favoritePage;
favoritePage = nullptr;
}
if (healthPage) {
delete healthPage;
healthPage = nullptr;
}
if (toolPage) {
delete toolPage;
toolPage = nullptr;
}
if (weatherPage) {
delete weatherPage;
weatherPage = nullptr;
}
if (musicPage) {
delete musicPage;
musicPage = nullptr;
}
if (makeFriendPage) {
delete makeFriendPage;
makeFriendPage = nullptr;
}
if (mapPage) {
delete mapPage;
mapPage = nullptr;
}
if (fourLeafCloverPage) {
delete fourLeafCloverPage;
fourLeafCloverPage = nullptr;
}
}
void TjdUiAppMainView::ResetView(void)
{
HideFloatWindow();
if (GetHorCurrentCard() == TjdCardId::TJD_CARD_WATCH_FACE) {
TjdUiAppMainPageShortcut::GetInstance()->ReturnToTop();
if (TjdUiAppMainPageShortcut::currentViewIndex_ == SHORTCUT_EDIT) {
DeleteShortcutEditView();
}
}
}
TjdUiAppMainView *TjdUiAppMainView::GetInstance(void) { return g_pMainClockView; }
void TjdUiAppMainView::OnStart()
{
static_print_debug("TjdUiAppMainView::OnStart");
mainSwipeGroup = new CustomCrossView();
MainCardEffect effect = sql_setting_get_animation();
// clang-format off
switch (effect) {
case MainCardEffect::TJD_MAIN_PAGE_EFFECT_NONE: break;
case MainCardEffect::TJD_MAIN_PAGE_EFFECT_CURTAIN: cardSwipe = new TjdUiAppCurtainEffect();break;
case MainCardEffect::TJD_MAIN_PAGE_EFFECT_CURTAIN_FADE_IN_OUT: cardSwipe = new TjdUiAppCurtainFadeInFadeOutEffect(); break;
case MainCardEffect::TJD_MAIN_PAGE_EFFECT_CURTAIN_GAOSI: cardSwipe = new TjdUiAppCurtainGaosiEffect(); break;
case MainCardEffect::TJD_MAIN_PAGE_EFFECT_TURN_PAGE: cardSwipe = new OHOS::TurnPageEffect(); break;
case MainCardEffect::TJD_MAIN_PAGE_EFFECT_ROTATE_TURN: cardSwipe = new OHOS::CardFlipEffect(); break;
case MainCardEffect::TJD_MAIN_PAGE_EFFECT_CENTER_ZOOM: cardSwipe = new TjdUiAppCenterZoomEffect(); break;
case MainCardEffect::TJD_MAIN_PAGE_EFFECT_3D: cardSwipe = new TjdUiApp3dEffect(); break;
case MainCardEffect::TJD_MAIN_PAGE_EFFECT_ZOOM: cardSwipe = new OHOS::ZoomEffect(); break;
case MainCardEffect::TJD_MAIN_PAGE_EFFECT_CURTAIN_FADE_IN: cardSwipe = new TjdUiAppCurtainFadeInEffect(); break;
}
// clang-format on
if (cardSwipe != nullptr) {
cardSwipe->SetContainer(mainSwipeGroup);
}
HorizontalViewInit();
HorViewLoad();
VerticalViewInit();
VertViewLoad();
mainSwipeGroup->SetPosition(0, 0, OHOS::Screen::GetInstance().GetWidth(), OHOS::Screen::GetInstance().GetHeight());
mainSwipeGroup->SetAnimatorTime(50); // 50: animator drag time(ms);
mainSwipeGroup->SetLoopState(false);
mainSwipeGroup->EnableScreenCap(true);
mainSwipeGroup->RegisterSwipeCallback(cardSwipe);
if (presenter_->IsFromComponentsView()) {
presenter_->SetFromComponentsView(false);
SwitchToCard(presenter_->GetFromComponentsCardId(), false);
} else {
SwitchToClockPage(false);
}
AddViewToRootContainer(mainSwipeGroup);
if (RET_PROCESS == sql_bt_set_start_complete_flag(true)) {
tjd_service_ancillary_data_save(0xffffffff);
}
}
void TjdUiAppMainView::OnStop() { static_print_debug("TjdUiAppMainView::OnStop"); }
OHOS::UICardPage *TjdUiAppMainView::InitFavoritePage(void)
{
favoritePage = new TjdUiAppMainPageFavorite();
if (favoritePage == nullptr) {
static_print_error("MainClockView new fail");
return nullptr;
}
return favoritePage;
}
OHOS::UICardPage *TjdUiAppMainView::InitWatchFacePage(void)
{
mainClockView = TjdUiWfCtrlInit();
if (mainClockView == nullptr) {
static_print_error("MainClockView new fail");
return nullptr;
}
return mainClockView;
}
OHOS::UICardPage *TjdUiAppMainView::InitHealthPage(void)
{
healthPage = new TjdUiAppMainPageHealth();
if (healthPage == nullptr) {
static_print_error("healthPage new fail");
return nullptr;
}
return healthPage;
}
OHOS::UICardPage *TjdUiAppMainView::InitToolPage(void)
{
toolPage = new TjdUiAppMainPageTool();
if (toolPage == nullptr) {
static_print_error("toolPage new fail");
return nullptr;
}
return toolPage;
}
OHOS::UICardPage *TjdUiAppMainView::InitWeatherPage(void)
{
weatherPage = new TjdUiAppMainPageWeather();
if (weatherPage == nullptr) {
static_print_error("weatherPage new fail");
return nullptr;
}
return weatherPage;
}
OHOS::UICardPage *TjdUiAppMainView::InitMusicPage(void)
{
musicPage = new TjdUiAppMainPageMusic();
if (musicPage == nullptr) {
static_print_error("musicPage new fail");
return nullptr;
}
return musicPage;
}
OHOS::UICardPage *TjdUiAppMainView::InitMakeFriendPage(void)
{
makeFriendPage = new TjdUiAppMainPageMakeFriend();
if (makeFriendPage == nullptr) {
static_print_error("makeFriendPage new fail");
return nullptr;
}
return makeFriendPage;
}
OHOS::UICardPage *TjdUiAppMainView::InitMapPage(void)
{
mapPage = new TjdUiAppMainPageMap();
if (mapPage == nullptr) {
static_print_error("mapPage new fail");
return nullptr;
}
return mapPage;
}
OHOS::UICardPage *TjdUiAppMainView::InitFourLeafCloverPage(void)
{
fourLeafCloverPage = new TjdUiAppMainPageFourLeafClover();
if (fourLeafCloverPage == nullptr) {
static_print_error("fourLeafCloverPage new fail");
return nullptr;
}
return fourLeafCloverPage;
}
bool TjdUiAppMainView::HorizontalViewInit(void)
{
auto cardSettings = presenter_->GetCardSettings();
uint8_t cardMapNumber = sizeof(CardMapper) / sizeof(CardMapper[0]);
for (uint32_t i = 0; i < cardSettings.size(); i++) {
for (uint8_t j = 0; j < cardMapNumber; j++) {
if (cardSettings[i] == CardMapper[j].id) {
recordCard[j] = (this->*(CardMapper[j].func))();
if (recordCard[j] == nullptr) {
static_print_error("TjdUiAppMainView:: recordCard %d error", j);
return false;
}
break;
}
}
}
mainSwipeGroup->SetOnSwipeListener(presenter_);
mainSwipeGroup->SetOnLongPressListener(presenter_);
return true;
}
void TjdUiAppMainView::HorViewLoad(void)
{
auto cardSettings = presenter_->GetCardSettings();
for (uint32_t i = 0; i < cardSettings.size(); i++) {
TjdCardId cardId = cardSettings[i];
if (recordCard[cardId] != nullptr) {
mainSwipeGroup->HorAdd(recordCard[cardId]);
}
}
}
void TjdUiAppMainView::VerticalViewInit(void)
{
shortcutPage = new TjdUiAppMainPageShortcut();
if (shortcutPage == nullptr) {
static_print_error("dropDownView new fail");
return;
}
shortcutPage->PreLoad();
shortcutPage->SetViewId("shortcutPage");
shortcutPage->SetPosition(0, 0, OHOS::Screen::GetInstance().GetWidth(), OHOS::Screen::GetInstance().GetHeight());
shortcutPage->SetStyle(OHOS::STYLE_BACKGROUND_OPA, OHOS::OPA_TRANSPARENT);
messagePage = new TjdUiAppMainPageMessage();
messagePage->PreLoad();
messagePage->SetViewId("messagePage");
messagePage->SetPosition(0, 0, OHOS::Screen::GetInstance().GetWidth(), OHOS::Screen::GetInstance().GetHeight());
messagePage->SetStyle(OHOS::STYLE_BACKGROUND_OPA, OHOS::OPA_TRANSPARENT);
}
void TjdUiAppMainView::VertViewLoad(void)
{
mainSwipeGroup->VerAdd(shortcutPage, OHOS::UICrossView::VPage::UP_PAGE);
mainSwipeGroup->VerAdd(messagePage, OHOS::UICrossView::VPage::DOWN_PAGE);
}
TjdCardId TjdUiAppMainView::GetHorCurrentCard() const
{
auto cardSettings = presenter_->GetCardSettings();
int pageId = mainSwipeGroup->GetCurrentPageId(OHOS::UISwipeView::HORIZONTAL); // 0: horizontal; 1: vertical
/* pageId 与CardId 没有对应关系,需要一次转换 */
for (auto it : cardSettings) {
if (it < TjdCardId::TJD_CARD_MAX_CARD && recordCard[it] != nullptr &&
recordCard[it]->GetViewIndex() == pageId) {
return it;
}
}
return TjdCardId::TJD_CARD_MAX_CARD;
}
TjdVerCardId TjdUiAppMainView::GetVerCurrentCard() const
{
return static_cast<TjdVerCardId>(mainSwipeGroup->GetCurrentPageId(OHOS::UISwipeView::VERTICAL));
}
void TjdUiAppMainView::SwitchToCard(uint8_t cardId, bool loadAdjacent)
{
if ((cardId < TjdCardId::TJD_CARD_MAX_CARD) && (recordCard[cardId] != nullptr)) {
SetPage(recordCard[cardId]->GetViewIndex(), loadAdjacent);
} else {
SwitchToClockPage(loadAdjacent);
}
mainSwipeGroup->ResetBackgroundOpa();
}
bool TjdUiAppMainView::IsMainClockPage(void)
{
if (mainClockView == nullptr || mainSwipeGroup == nullptr) {
return false;
}
if (mainSwipeGroup->GetCurrentPageId(0) == mainClockView->GetViewIndex() &&
mainSwipeGroup->GetCurrentPageId(1) == 1) { // 0: horizontal; 1: vertical
return true;
} else {
return false;
}
}
void TjdUiAppMainView::SetPage(int16_t page, bool loadAdjacent)
{
if (mainSwipeGroup != nullptr) {
mainSwipeGroup->SetHorCurrentPage(page, loadAdjacent);
}
if (presenter_ != nullptr) {
auto cardSettings = presenter_->GetCardSettings();
/* page 与CardId 没有对应关系,需要一次转换 */
TjdCardId id = TjdCardId::TJD_CARD_MAX_CARD;
for (auto it : cardSettings) {
if (it < TjdCardId::TJD_CARD_MAX_CARD && recordCard[it] != nullptr &&
recordCard[it]->GetViewIndex() == page) {
id = it;
}
}
presenter_->SetTaskPeroid(id);
}
}
void TjdUiAppMainView::ReloadWatchFace()
{
if (mainSwipeGroup == nullptr) {
return;
}
mainSwipeGroup->Remove(mainClockView, OHOS::UISwipeView::HORIZONTAL);
delete mainClockView;
mainClockView = nullptr;
recordCard[TjdCardId::TJD_CARD_WATCH_FACE] = InitWatchFacePage();
if (recordCard[TjdCardId::TJD_CARD_WATCH_FACE] == nullptr) {
GRAPHIC_LOGE("failed to reload main clock");
return;
}
mainSwipeGroup->HorInsert(nullptr, mainClockView);
SwitchToCard(TjdCardId::TJD_CARD_WATCH_FACE);
Draw();
}
void TjdUiAppMainView::ReloadVer()
{
if (mainSwipeGroup == nullptr) {
return;
}
mainSwipeGroup->Remove(shortcutPage, OHOS::UISwipeView::VERTICAL);
mainSwipeGroup->Remove(messagePage, OHOS::UISwipeView::VERTICAL);
mainSwipeGroup->VerAdd(shortcutPage, OHOS::UICrossView::VPage::UP_PAGE);
mainSwipeGroup->VerAdd(messagePage, OHOS::UICrossView::VPage::DOWN_PAGE);
Draw();
}
void TjdUiAppMainView::ShowFloatWindow(void)
{
// OHOS::RootView::GetInstance()->ClearBlurView(OHOS::RootView::GetInstance()->GetBlurView());
if (floatWindow_ == nullptr) {
floatWindow_ = new TjdUiFloatWindowBlend();
floatWindow_->SetOnFloatWindowStateListener(presenter_);
mainClockView->Add(floatWindow_);
}
floatWindow_->SetVisible(true);
floatWindow_->RequestFocus();
}
void TjdUiAppMainView::HideFloatWindow(void)
{
if (floatWindow_ != nullptr) {
floatWindow_->SetVisible(false);
floatWindow_->ClearFocus();
}
}
bool TjdUiAppMainView::IsShowFloatWindow(void)
{
if (floatWindow_ == nullptr) {
return false;
}
return floatWindow_->IsVisible();
}
} // namespace TJD