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

265 lines
9.7 KiB
C++

/*----------------------------------------------------------------------------
* Copyright (c) TJD Technologies Co., Ltd. 2025. All rights reserved.
*
* Description: TjdUiAppListPresenter.cpp
*
* Author: luziquan@ss-tjd.com
*
* Create: 2025-02-11
*--------------------------------------------------------------------------*/
#include "TjdUiAppListPresenter.h"
#include "TjdUiAppIds.h"
#include "TjdUiAppListModel.h"
#include "TjdUiAppListView.h"
#include "TjdUiAppMainView.h"
#include "TjdUiRegisterManager.h"
#include "common/key_code.h"
#include "dock/input_device.h"
#include "sql_setting.h"
#include "sys_config.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
TJD_REGIST_SLICE(TJD_APP_VIEW_LIST, TjdUiAppListView, TjdUiAppListPresenter);
static TjdUiAppListPresenter *g_appListPresenter = nullptr;
TjdUiAppListPresenter::TjdUiAppListPresenter()
{
g_appListPresenter = this;
TjdUiAppListModel::GetInstance().Init();
}
TjdUiAppListPresenter::~TjdUiAppListPresenter() { g_appListPresenter = nullptr; }
TjdUiAppListPresenter *TjdUiAppListPresenter::GetInstance() { return g_appListPresenter; }
void TjdUiAppListPresenter::OnStart()
{
TjdUiCommonOnKeyListener::GetInstance()->SetOnKeyActListener(this, KeyModelType::APP_KEY_TYPE);
RefreshAppView();
}
void TjdUiAppListPresenter::OnStop() {}
void TjdUiAppListPresenter::OnPause() { Presenter::OnPause(); }
void TjdUiAppListPresenter::OnResume()
{
Presenter::OnResume();
if (GetCurrentStyle() == E_MenuStyle::MENU_STYLE_WATERFALL) {
view_->ResumeWaterFallDraggable();
}
view_->ResumeFocus();
}
bool TjdUiAppListPresenter::OnRotate(OHOS::UIView &view, const OHOS::RotateEvent &event)
{
OHOS::UICustomHexagonsList *hexagonsView = dynamic_cast<OHOS::UICustomHexagonsList *>(&view);
if (hexagonsView == nullptr) {
return false;
}
int8_t index = hexagonsView->GetFocusedImgIndex();
uint8_t focusIndex_ = 0;
if (index >= 0) {
focusIndex_ = static_cast<uint8_t>(index);
}
HexagonsItemView *itemView = dynamic_cast<HexagonsItemView *>(hexagonsView->GetViewByIndex(focusIndex_));
TjdUiAppViewId viewId = itemView->GetViewId();
if (OHOS::FloatMore(hexagonsView->GetGlobalScale(), 2.0f)) {
hexagonsView->StopRotateAnimator();
OHOS::NativeAbility::GetInstance().ChangeSlice(viewId, OHOS::TransitionType::TRANSITION_HEXAGONS);
hexagonsView->ClearFocus();
}
return true;
}
bool TjdUiAppListPresenter::OnClick(OHOS::UIView &view, const OHOS::ClickEvent &event)
{
if (exitFlag_) {
return true;
}
UNUSED(event);
TjdUiAppViewId viewId = TJD_APP_VIEW_INVALID;
char *uid = nullptr;
OHOS::TransitionType type = OHOS::TransitionType::TRANSITION_INVALID;
E_MenuStyle style = static_cast<E_MenuStyle>(GetCurrentStyle());
if (style == E_MenuStyle::MENU_STYLE_CELLUAR) {
HexagonsItemView *itemView = dynamic_cast<HexagonsItemView *>(&view);
OHOS::UIHexagonsList *hexagonsView = dynamic_cast<OHOS::UIHexagonsList *>(itemView->GetParent());
if (hexagonsView == nullptr || itemView == nullptr) {
WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "hexagonsView or itemView is nullptr");
return false;
}
viewId = itemView->GetViewId();
uid = (char *)itemView->GetAppUid();
hexagonsView->SetDraggable(false);
hexagonsView->Scale(1.0f, itemView->GetViewIndex());
type = OHOS::TransitionType::TRANSITION_HEXAGONS;
} else if (style == E_MenuStyle::MENU_STYLE_WATERFALL) {
WaterfallItemView *itemView = dynamic_cast<WaterfallItemView *>(&view);
OHOS::UIWaterfallList *waterfallView = dynamic_cast<OHOS::UIWaterfallList *>(itemView->GetParent());
if (waterfallView == nullptr || itemView == nullptr) {
WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "waterfallView or itemView is nullptr");
return false;
}
viewId = itemView->GetViewId();
uid = (char *)itemView->GetAppUid();
waterfallView->SetDraggable(false);
waterfallView->Scale(1.0f, itemView->GetViewIndex());
type = OHOS::TransitionType::TRANSITION_WATERFALL;
} else if (style == E_MenuStyle::MENU_STYLE_SPHERE) {
SphereItemView *itemView = dynamic_cast<SphereItemView *>(view.GetParent());
if (itemView == nullptr) {
WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "itemView is nullptr");
return false;
}
viewId = itemView->GetViewId();
uid = (char *)itemView->GetAppUid();
type = OHOS::TransitionType::TRANSITION_INVALID;
} else if (style == E_MenuStyle::MENU_STYLE_GRID) {
GridItemView *itemView = dynamic_cast<GridItemView *>(&view);
if (itemView == nullptr) {
WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "itemView is nullptr");
return false;
}
viewId = itemView->GetViewId();
uid = (char *)itemView->GetAppUid();
type = OHOS::TransitionType::TRANSITION_INVALID;
} else if (style == MENU_STYLE_LIST) {
AppListItemView *itemView = dynamic_cast<AppListItemView *>(&view);
if (itemView == nullptr) {
WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "itemView is nullptr", viewId);
return false;
}
viewId = itemView->GetViewId();
uid = (char *)itemView->GetAppUid();
}
exitFlag_ = true;
if (viewId < TJD_VIEW_MAX_INTER_APP) {
OHOS::NativeAbility::GetInstance().ChangeSlice(viewId, type, OHOS::gSliceDefaultPriority, true);
// OHOS::NativeAbility::GetInstance().ChangeSlice(viewId, type);
return true;
} else if (viewId == TJD_APP_VIEW_EXTERN) {
#ifdef JS_ENABLE
WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "TjdUiAppListPresenter::OnClick bundleName = %s", uid);
Want *want = new Want();
memset_s(want, sizeof(Want), 0, sizeof(Want));
ElementName startElement = {};
startElement.abilityName = (char *)"default";
startElement.bundleName = uid;
startElement.deviceId = nullptr;
SetWantElement(want, startElement);
StartAbilityWithAnimation(want, static_cast<uint8_t>(type), false);
ClearWant(want);
delete want;
return true;
#endif
}
return false;
}
bool TjdUiAppListPresenter::OnKeyAct(OHOS::UIView &view, const OHOS::KeyEvent &event)
{
if (!TjdUiCommonOnKeyListener::CheckIsExitEvent(event)) {
return true;
}
OHOS::NativeAbility::GetInstance().ChangeSlice(TJD_APP_VIEW_MAIN);
return true;
}
void TjdUiAppListPresenter::OnItemSelected(int16_t index, OHOS::UIView *view)
{
UNUSED(index);
if (view != nullptr) {
itemViewSele = static_cast<AppListItemView *>(view);
itemViewSele->SelectedChange();
if (preItemViewSele != nullptr && preItemViewSele != itemViewSele) {
preItemViewSele->Reset();
}
preItemViewSele = itemViewSele;
}
}
bool TjdUiAppListPresenter::StateChange(OHOS::UIView &view, float rowAngle, float colAngle)
{
SphereItemView *item = dynamic_cast<SphereItemView *>(&view);
if (item == nullptr) {
return false;
}
if ((OHOS::FloatMore(rowAngle, 0.0f) && OHOS::FloatLess(rowAngle, 45.0f)) ||
(OHOS::FloatMore(rowAngle, 315.0f) && OHOS::FloatLess(rowAngle, 360.0f))) {
item->SetOnClickListener(this);
} else {
item->SetOnClickListener(nullptr);
}
if (OHOS::FloatMore(rowAngle, 90.0f) && OHOS::FloatLess(rowAngle, 270.0f)) { // 90 270: angle range
view.SetStyle(OHOS::STYLE_IMAGE_OPA, OHOS::OPA_OPAQUE >> 1);
} else {
view.SetStyle(OHOS::STYLE_IMAGE_OPA, OHOS::OPA_OPAQUE);
}
return true;
}
void TjdUiAppListPresenter::RefreshAppView(void)
{
view_->RefreshAppView();
view_->RefreshAppList();
}
void TjdUiAppListPresenter::RefreshAppItemToList(void)
{
auto &model = TjdUiAppListModel::GetInstance();
const TjdAppItem *itemInfo = model.GetApplistItems();
for (uint8 i = 0; i < model.GetAppListNum(); i++) {
if (itemInfo[i].appItem.id == TJD_APP_VIEW_ALIPAY) {
/* 只在中文下显示支付宝 */
if (sql_setting_get_language() != language_enum::LANGUAGE_CHINESE) {
continue;
}
}
view_->AddAppItemToList(itemInfo[i]);
}
}
void TjdUiAppListPresenter::SetStartIndex(uint16_t index) { TjdUiAppListModel::GetInstance().SetStartIndex(index); }
uint16_t TjdUiAppListPresenter::GetStartIndex(void) { return TjdUiAppListModel::GetInstance().GetStartIndex(); }
void TjdUiAppListPresenter::SetHeadItemY(int y) { TjdUiAppListModel::GetInstance().SetHeadItemY(y); }
int TjdUiAppListPresenter::GetHeadItemY(void) { return TjdUiAppListModel::GetInstance().GetHeadItemY(); }
uint8_t TjdUiAppListPresenter::GetListItemNum(void) { return TjdUiAppListModel::GetInstance().GetAppListNum(); }
TjdUiAppListView *TjdUiAppListPresenter::GetAppListView() { return view_; }
void TjdUiAppListPresenter::SetCurrentStyle(uint8_t style)
{
TjdUiAppListModel::GetInstance().SetCurrentStyle(static_cast<E_MenuStyle>(style));
}
uint8_t TjdUiAppListPresenter::GetCurrentStyle(void) { return TjdUiAppListModel::GetInstance().GetCurrentStyle(); }
void TjdUiAppListPresenter::ScreenDragEventCallback(OHOS::UIView &view, const OHOS::DragEvent &event)
{
OHOS::NativeAbility::GetInstance().ChangeSlice(TJD_APP_VIEW_MAIN);
}
} // namespace TJD