mcu_hi3321_watch/tjd/ui/app/e-book/TjdUiAppEbookAdapter.cpp
2025-05-26 20:15:20 +08:00

239 lines
9.3 KiB
C++

#include "TjdUiAppEbookAdapter.h"
#include "sys_config.h"
#include "TjdUiImageIds.h"
#include "TjdUiMultiLanguageExt.h"
#include "TjdUiAppEbookPresenter.h"
#include "common/image_cache_manager.h"
#include "TjdUiAppEbookView.h"
#include "TjdUiAppEbookPresenter.h"
// clang-format off
#define ENABLE_PRINT_INFO 1
#define ENABLE_DEBUG 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__) // 错误信息打印一般常开
#if ENABLE_DEBUG
#define static_print_debug(...) sys_ui_log_d(__VA_ARGS__) // 调试信息打印
#else
#define static_print_debug(...)
#endif
#else
#define static_print_info(...)
#define static_print_warn(...)
#define static_print_error(...)
#endif
// clang-format on
using namespace OHOS;
namespace TJD {
// clang-format off
static constexpr int16_t ICON_INTERVAL = EBOOK_DELECT_ICON_INTERVAL;
static constexpr int16_t EBOOK_ITEM_WIDTH = EBOOK_ITEM_W;
static constexpr int16_t EBOOK_ITEM_HEIGHT = 128;
static constexpr int16_t ICON_DELETE_WIDTH = EBOOK_DELECT_ICON_W;
static constexpr int16_t ICON_DELETE_HEIGHT = EBOOK_DELECT_ICON_H;
static constexpr int16_t CANVAS_ARC_RADIUS = 64;
static constexpr int16_t CANVAS_EAGE_INTERVAL = 8;
// clang-format on
/*----------------------------------------------------------------------------------------*/
/*----------------------------------------------------------------------------------------*/
/*------------------------------- Class EbookItemListener --------------------------------*/
/*----------------------------------------------------------------------------------------*/
/*----------------------------------------------------------------------------------------*/
bool EbookItemListener::OnClick(OHOS::UIView &view, const OHOS::ClickEvent &event)
{
if (&view == item_->GetFillView()) {
static_print_info("EbookItemListener::%s: view is id:%d FillView", __func__, item_->GetItemInfo().id);
TjdUiAppEbookPresenter::GetInstance()->ClickEBookAdapterCallback(&view, item_->GetItemInfo());
return true;
} else if (&view == item_->GetDeleteIcon()) {
static_print_info("EbookItemListener::%s: view is id:%d deleteIcon", __func__, item_->GetItemInfo().id);
TjdUiAppEbookPresenter::GetInstance()->ClickEBookAdapterCallback(&view, item_->GetItemInfo());
return true;
}
static_print_info("EbookItemListener::%s: view is unknown", __func__);
return false;
}
/*----------------------------------------------------------------------------------------*/
/*----------------------------------------------------------------------------------------*/
/*------------------------------ Class EbookItemScrollView ----------- -------------------*/
/*----------------------------------------------------------------------------------------*/
/*----------------------------------------------------------------------------------------*/
bool EbookItemScrollView::OnDragStartEvent(const OHOS::DragEvent &event)
{
uint8_t direction = event.GetDragDirection();
static_print_debug("EbookItemScrollView::%s directon:%d", __func__, event.GetDragDirection());
if (direction != DragEvent::DIRECTION_RIGHT_TO_LEFT && this->GetChildrenHead()->GetX() == 0) {
scrollEnable = false;
return false;
}
UIScrollView::OnDragStartEvent(event);
return true;
}
bool EbookItemScrollView::OnDragEvent(const OHOS::DragEvent &event)
{
// static_print_debug("EbookItemScrollView::%s directon:%d", __func__, event.GetDragDirection());
if (!scrollEnable)
return false;
UIScrollView::OnDragEvent(event);
return true;
}
bool EbookItemScrollView::OnDragEndEvent(const OHOS::DragEvent& event)
{
static_print_debug("EbookItemScrollView::%s directon:%d", __func__, event.GetDragDirection());
if (!scrollEnable) {
scrollEnable = true;
return false;
}
UIScrollView::OnDragEndEvent(event);
return true;
}
/*----------------------------------------------------------------------------------------*/
/*----------------------------------------------------------------------------------------*/
/*------------------------------- Class TjdEbookItemView ---------------------------------*/
/*----------------------------------------------------------------------------------------*/
/*----------------------------------------------------------------------------------------*/
TjdEbookItemView::TjdEbookItemView() : itemListener_(this)
{
SetPosition(0, 0, HORIZONTAL_RESOLUTION, EBOOK_ITEM_HEIGHT);
SetStyle(STYLE_MARGIN_BOTTOM, EBOOK_ITEM_MARGIN_BOTTOM);
SetStyle(STYLE_BACKGROUND_OPA, 0);
SetTouchable(false);
scrollContainer_.SetPosition(0, 0, HORIZONTAL_RESOLUTION, EBOOK_ITEM_HEIGHT);
scrollContainer_.SetHorizontalScrollState(true);
scrollContainer_.SetDraggable(false);
scrollContainer_.SetTouchable(true);
Add(&scrollContainer_);
fillView_.SetPosition(0, 0, HORIZONTAL_RESOLUTION, EBOOK_ITEM_HEIGHT);
fillView_.SetStyle(STYLE_BACKGROUND_OPA, OPA_OPAQUE);
fillView_.SetStyle(STYLE_BACKGROUND_COLOR,0xff202020);
fillView_.SetStyle(STYLE_BORDER_RADIUS,CANVAS_ARC_RADIUS);
fillView_.SetTouchable(true);
fillView_.SetOnClickListener(&itemListener_);
scrollContainer_.Add(&fillView_);
image_.SetPosition(24, (EBOOK_ITEM_HEIGHT-92)/2, 92, 92);
image_.SetStyle(STYLE_BACKGROUND_OPA, OPA_TRANSPARENT);
scrollContainer_.Add(&image_);
bookLabel_.SetPosition(24+92+60, (EBOOK_ITEM_HEIGHT-40)/2, 250, 40);
bookLabel_.SetStyle(STYLE_BACKGROUND_OPA, OPA_TRANSPARENT);
bookLabel_.SetLineBreakMode(UILabel::LINE_BREAK_MARQUEE);
bookLabel_.SetAlign(TEXT_ALIGNMENT_LEFT, TEXT_ALIGNMENT_TOP);
bookLabel_.SetFont(TJD_VECTOR_FONT_FILENAME, TJD_FONT_SIZE_34);
scrollContainer_.Add(&bookLabel_);
deleteIcon_.SetPosition(HORIZONTAL_RESOLUTION + ICON_INTERVAL, (EBOOK_ITEM_HEIGHT - ICON_DELETE_HEIGHT) / 2);
deleteIcon_.Resize(ICON_DELETE_WIDTH, ICON_DELETE_HEIGHT);
ImageInfo *imageinfo = ImageCacheManager::GetInstance().LoadOneInMultiRes(IMG_EBOOK_DELETE, BOOK_BIN_PATH);
deleteIcon_.SetImageSrc(imageinfo, imageinfo);
deleteIcon_.SetStyle(STYLE_BACKGROUND_OPA, 0);
deleteIcon_.SetStyleForState(STYLE_BACKGROUND_OPA, 0, UIButton::INACTIVE);
deleteIcon_.SetOnClickListener(&itemListener_);
deleteIcon_.Disable();
scrollContainer_.Add(&deleteIcon_);
}
TjdEbookItemView::~TjdEbookItemView()
{
static_print_info("TjdEbookItemView::%s id:%d", __func__, GetItemInfo().id);
RemoveAll();
scrollContainer_.RemoveAll();
}
void TjdEbookItemView::SetItemInfo(const EbookItem &itemInfo)
{
itemInfo_ = itemInfo;
ImageInfo *imageinfo = ImageCacheManager::GetInstance().LoadOneInMultiRes(IMG_EBOOK_BOOK_01, BOOK_BIN_PATH);
image_.SetSrc(imageinfo);
bookLabel_.SetText(itemInfo_.name.c_str());
}
void TjdEbookItemView::SelectedChange()
{
static_print_info("TjdEbookItemView::%s ID:%d", __func__, itemInfo_.id);
deleteIcon_.Enable();
scrollContainer_.SetDraggable(true);
}
void TjdEbookItemView::Reset()
{
static_print_info("TjdEbookItemView::%s ID:%d", __func__, itemInfo_.id);
deleteIcon_.Disable();
scrollContainer_.ScrollBy(EBOOK_DELECT_ICON_INTERVAL+EBOOK_DELECT_ICON_W, 0);
scrollContainer_.SetDraggable(false);
}
/*----------------------------------------------------------------------------------------*/
/*----------------------------------------------------------------------------------------*/
/*-------------------------------- Class TjdEbookAdapter ---------------------------------*/
/*----------------------------------------------------------------------------------------*/
/*----------------------------------------------------------------------------------------*/
TjdEbookAdapter::TjdEbookAdapter() {}
TjdEbookAdapter::~TjdEbookAdapter() { listData_.clear(); }
uint16_t TjdEbookAdapter::GetCount(void) { return listData_.size(); }
UIView *TjdEbookAdapter::GetView(UIView *inView, int16_t index)
{
if (index >= (int16_t)listData_.size() || index < 0) {
return nullptr;
}
std::list<EbookItem>::iterator it = listData_.begin();
for (int i = 0; i < index; i++) {
it++;
}
EbookItem itemData = *it;
TjdEbookItemView *item = nullptr;
if (inView == nullptr) {
item = new TjdEbookItemView();
} else {
item = static_cast<TjdEbookItemView *>(inView);
}
item->SetItemInfo(itemData);
return item;
}
void TjdEbookAdapter::DeleteListItem(uint32_t id)
{
auto it = std::find_if(listData_.begin(), listData_.end(),
[id](const EbookItem& item) {
return item.id == id;
}
);
if (it != listData_.end()) {
listData_.erase(it);
}
}
EbookItem *TjdEbookAdapter::GetListItem(uint32_t id)
{
for (auto it = listData_.begin(); it != listData_.end(); ++it) {
if ((*it).id == id) {
return &*it;
}
}
return nullptr;
}
} // namespace TJD