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

362 lines
13 KiB
C++

#include "TjdUiAppAlbumAdapter.h"
#include "TjdUiAppAlbumPresenter.h"
#include "TjdUiAppAlbumView.h"
#include "TjdUiAppSettingPage.h"
#include "sys_config.h"
#include "ui_list.h"
#define ENABLE_PRINT_INFO 1
#define ENABLE_DEBUG 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__) // 错误信息打印一般常开
#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(...)
#define static_print_debug(...)
#endif
using namespace OHOS;
namespace TJD {
static constexpr int16_t ICON_INTERVAL = DELECT_ICON_INTERVAL;
static constexpr int16_t ALBUM_ITEM_WIDTH = ALBUM_ITEM_W;
static constexpr int16_t ALBUM_ITEM_HEIGHT = ALBUM_ITEM_H;
static constexpr int16_t ICON_DELETE_WIDTH = DELECT_ICON_W;
static constexpr int16_t ICON_DELETE_HEIGHT = DELECT_ICON_H;
bool AlbumItemScrollView::OnDragStartEvent(const DragEvent &event)
{
static_print_info("AlbumItemScrollView::%s directon:%d", __func__, event.GetDragDirection());
uint8_t direction = event.GetDragDirection();
if (direction == DragEvent::DIRECTION_TOP_TO_BOTTOM || direction == DragEvent::DIRECTION_BOTTOM_TO_TOP) {
scrollEnable = false;
return false;
}
UIScrollView::OnDragStartEvent(event);
return true;
}
bool AlbumItemScrollView::OnDragEvent(const DragEvent &event)
{
// static_print_info("AlbumItemScrollView::%s directon:%d", __func__, event.GetDragDirection());
if (icon_ == nullptr || photo_ == nullptr)
static_print_error("icon_ or photo_ is nullptr");
if (!scrollEnable)
return false;
UIScrollView::OnDragEvent(event);
return true;
}
bool AlbumItemScrollView::OnDragEndEvent(const DragEvent &event)
{
static_print_info("AlbumItemScrollView::%s directon:%d", __func__, event.GetDragDirection());
if (!scrollEnable) {
scrollEnable = true;
return false;
}
UIScrollView::OnDragEndEvent(event);
return true;
}
/*----------------------------------------------------------------------------------------*/
/*----------------------------------------------------------------------------------------*/
/*---------------------------------- Class ItemListener ----------------------------------*/
/*----------------------------------------------------------------------------------------*/
/*----------------------------------------------------------------------------------------*/
ItemListener::ItemListener(void)
{
view_ = TjdUiAppAlbumView::GetInstance();
present_ = TjdUiAppAlbumPresenter::GetInstance();
}
ItemListener::~ItemListener(void)
{
view_ = nullptr;
present_ = nullptr;
}
bool ItemListener::OnDragStart(UIView &view, const DragEvent &event)
{
uint8_t direction = event.GetDragDirection();
static_print_info("ItemListener::%s direction:%d", __func__, direction);
if (direction == DragEvent::DIRECTION_LEFT_TO_RIGHT || direction == DragEvent::DIRECTION_RIGHT_TO_LEFT) {
if (direction == DragEvent::DIRECTION_LEFT_TO_RIGHT) {
dragStartX_ = image_->GetX();
static_print_info("ItemListener::%s direction:%d X:%d", __func__, direction, dragStartX_);
if (dragStartX_ ==
(HORIZONTAL_RESOLUTION - ALBUM_ITEM_WIDTH) / 2) { // 如果从起始位置向右滑则表示返回上一层界面
return false;
}
}
return true;
}
return false;
}
bool ItemListener::OnDrag(UIView &view, const DragEvent &event)
{
uint8_t direction = event.GetDragDirection();
// static_print_info("ItemListener::%s direction:%d", __func__, direction);
if (direction == DragEvent::DIRECTION_LEFT_TO_RIGHT || direction == DragEvent::DIRECTION_RIGHT_TO_LEFT) {
if (direction == DragEvent::DIRECTION_LEFT_TO_RIGHT) {
if (dragStartX_ ==
(HORIZONTAL_RESOLUTION - ALBUM_ITEM_WIDTH) / 2) { // 如果从起始位置向右滑则表示返回上一层界面
return false;
}
}
return true;
}
return false;
}
bool ItemListener::OnDragEnd(UIView &view, const DragEvent &event)
{
uint8_t direction = event.GetDragDirection();
static_print_info("ItemListener::%s direction:%d", __func__, direction);
if (direction == DragEvent::DIRECTION_LEFT_TO_RIGHT || direction == DragEvent::DIRECTION_RIGHT_TO_LEFT) {
if (direction == DragEvent::DIRECTION_LEFT_TO_RIGHT) {
if (dragStartX_ ==
(HORIZONTAL_RESOLUTION - ALBUM_ITEM_WIDTH) / 2) { // 如果从起始位置向右滑则表示返回上一层界面
return false;
}
}
dragStartX_ = 0;
return true;
}
return false;
}
bool ItemListener::OnClick(UIView &view, const ClickEvent &event)
{
static_print_info("ItemListener::%s ", __func__);
if (view.GetViewType() == UI_IMAGE_VIEW) {
present_->SetSelectedListId(itemView_->GetItemInfo().index);
if (itemView_->GetItemInfo().type == ALBUM_PHOTO) {
view_->ChangeView(ALBUM_PHOTO_VIEW, ALBUM_LIST_VIEW);
} else {
view_->ChangeView(ALBUM_VIDEO_VIEW, ALBUM_LIST_VIEW);
}
} else {
static_print_info("ItemListener::%s view index: %d", __func__, itemView_->GetViewIndex());
DeleteListItem();
}
return true;
}
void ItemListener::SetElement(UIViewType type, UIView *view)
{
switch (type) {
case UI_IMAGE_VIEW:
image_ = dynamic_cast<UIImageView *>(view);
break;
case UI_LABEL_BUTTON:
button_ = dynamic_cast<UILabelButton *>(view);
break;
case COMMON_ALBUM_ITEMVIEW:
itemView_ = dynamic_cast<TjdAlbumItemView *>(view);
break;
default:
break;
}
}
void ItemListener::DeleteListItem() { view_->DeleteAlbumItemFromList(itemView_->GetItemInfo().index); }
/*----------------------------------------------------------------------------------------*/
/*----------------------------------------------------------------------------------------*/
/*-------------------------------- Class TjdAlbumItemView --------------------------------*/
/*----------------------------------------------------------------------------------------*/
/*----------------------------------------------------------------------------------------*/
TjdAlbumItemView::TjdAlbumItemView() : scrollContainer_(&deleteIcon_, &photo_)
{
SetPosition(0, 0, HORIZONTAL_RESOLUTION, ALBUM_ITEM_HEIGHT);
SetStyle(STYLE_MARGIN_BOTTOM, 13);
SetStyle(STYLE_BACKGROUND_OPA, 0);
SetTouchable(false);
scrollContainer_.SetPosition(0, 0, HORIZONTAL_RESOLUTION, ALBUM_ITEM_HEIGHT);
scrollContainer_.SetHorizontalScrollState(true);
scrollContainer_.SetDraggable(false);
scrollContainer_.SetTouchable(false);
scrollContainer_.SetOnDragListener(&itemListener_);
Add(&scrollContainer_);
fillView_.SetPosition(0, 0, (HORIZONTAL_RESOLUTION - ALBUM_ITEM_WIDTH) / 2, ALBUM_ITEM_HEIGHT);
scrollContainer_.Add(&fillView_);
photo_.SetPosition((HORIZONTAL_RESOLUTION - ALBUM_ITEM_WIDTH) / 2, 0, ALBUM_ITEM_WIDTH, ALBUM_ITEM_HEIGHT);
photo_.SetStyle(STYLE_BACKGROUND_COLOR, 0xff262626);
photo_.SetStyle(STYLE_BACKGROUND_OPA, 0xff);
photo_.SetStyle(STYLE_BORDER_RADIUS, 30);
photo_.SetAutoEnable(false);
photo_.SetTouchable(false);
photo_.SetOnClickListener(&itemListener_);
scrollContainer_.Add(&photo_);
icon_.SetPosition((HORIZONTAL_RESOLUTION - ALBUM_ITEM_WIDTH) / 2 + (ALBUM_ITEM_WIDTH - 50) / 2,
(ALBUM_ITEM_HEIGHT - 50) / 2, 50, 50);
ImageInfo *imageinfo = TjdUiAppAlbumPresenter::GetInstance()->GetAlbumIcon(ALBUM_RESUME);
icon_.SetSrc(imageinfo);
icon_.SetVisible(false);
scrollContainer_.Add(&icon_);
int16_t x = (HORIZONTAL_RESOLUTION - ALBUM_ITEM_WIDTH) / 2; // 图片放置屏幕中心的坐标位置
canvas_.SetPosition(x, 0, ALBUM_ITEM_WIDTH, ALBUM_ITEM_HEIGHT);
canvas_.SetStyle(STYLE_BACKGROUND_OPA, OPA_TRANSPARENT);
scrollContainer_.Add(&canvas_);
paint_.SetStyle(Paint::PaintStyle::STROKE_STYLE);
paint_.SetStrokeColor(Color::Black());
paint_.SetStrokeWidth(24);
canvas_.DrawLine({24, 0}, {ALBUM_ITEM_WIDTH - 24, 0}, paint_);
canvas_.DrawLine({0, 24}, {0, ALBUM_ITEM_HEIGHT - 24}, paint_);
canvas_.DrawLine({24, ALBUM_ITEM_HEIGHT}, {ALBUM_ITEM_WIDTH - 24, ALBUM_ITEM_HEIGHT}, paint_);
canvas_.DrawLine({ALBUM_ITEM_WIDTH, 24}, {ALBUM_ITEM_WIDTH, ALBUM_ITEM_HEIGHT - 24}, paint_);
canvas_.DrawArc({24, 24}, 24, 360, 270, paint_);
canvas_.DrawArc({24, ALBUM_ITEM_HEIGHT - 24}, 24, 270, 180, paint_);
canvas_.DrawArc({ALBUM_ITEM_WIDTH - 24, ALBUM_ITEM_HEIGHT - 24}, 24, 180, 90, paint_);
canvas_.DrawArc({ALBUM_ITEM_WIDTH - 24, 24}, 24, 90, 0, paint_);
deleteIcon_.SetPosition(HORIZONTAL_RESOLUTION + ICON_INTERVAL, (ALBUM_ITEM_HEIGHT - ICON_DELETE_HEIGHT) / 2);
deleteIcon_.Resize(ICON_DELETE_WIDTH, ICON_DELETE_HEIGHT);
imageinfo = TjdUiAppAlbumPresenter::GetInstance()->GetAlbumIcon(ALBUM_DELETE);
deleteIcon_.SetImageSrc(imageinfo, imageinfo);
deleteIcon_.SetStyle(STYLE_BACKGROUND_OPA, 0);
deleteIcon_.SetStyleForState(STYLE_BACKGROUND_OPA, 0, UIButton::ButtonState::INACTIVE);
deleteIcon_.SetStyleForState(STYLE_BACKGROUND_OPA, 0, UIButton::ButtonState::RELEASED);
deleteIcon_.SetStyleForState(STYLE_BACKGROUND_OPA, 0, UIButton::ButtonState::PRESSED);
deleteIcon_.SetOnClickListener(&itemListener_);
deleteIcon_.Disable();
scrollContainer_.Add(&deleteIcon_);
itemListener_.SetElement(UI_IMAGE_VIEW, &photo_);
itemListener_.SetElement(UI_LABEL_BUTTON, &deleteIcon_);
itemListener_.SetElement(static_cast<UIViewType>(COMMON_ALBUM_ITEMVIEW), this);
}
TjdAlbumItemView::~TjdAlbumItemView()
{
static_print_info("TjdAlbumItemView::%s index:%d", __func__, GetViewIndex());
scrollContainer_.Remove(&fillView_);
scrollContainer_.Remove(&photo_);
scrollContainer_.Remove(&icon_);
scrollContainer_.Remove(&canvas_);
scrollContainer_.Remove(&deleteIcon_);
RemoveAll();
}
void TjdAlbumItemView::SetItemInfo(const AlbumItem &itemInfo)
{
itemInfo_ = itemInfo;
icon_.SetVisible(false);
static_print_debug("TjdAlbumItemView::%s %s\n", __func__, itemInfo.path);
SetViewIndex(itemInfo.index);
if (itemInfo.path == nullptr) {
static_print_error("TjdAlbumItemView::%s: itemInfo.path nullptr", __func__);
} else {
photo_.SetResizeMode(UIImageView::CENTER);
photo_.SetSrc(itemInfo.path);
if (itemInfo.type == ALBUM_VIDEO) {
icon_.SetVisible(true);
static_print_debug("icon_.SetVisible(true);\n");
}
}
}
void TjdAlbumItemView::SelectedChange()
{
photo_.SetTouchable(true);
deleteIcon_.Enable();
scrollContainer_.SetDraggable(true);
}
void TjdAlbumItemView::Reset()
{
photo_.SetTouchable(false);
deleteIcon_.Disable();
scrollContainer_.ScrollBy(DELECT_ICON_INTERVAL + DELECT_ICON_W, 0);
scrollContainer_.SetDraggable(false);
}
/*----------------------------------------------------------------------------------------*/
/*----------------------------------------------------------------------------------------*/
/*-------------------------------- Class TjdAlbumAdapter ---------------------------------*/
/*----------------------------------------------------------------------------------------*/
/*----------------------------------------------------------------------------------------*/
TjdAlbumAdapter::TjdAlbumAdapter() {}
TjdAlbumAdapter::~TjdAlbumAdapter() { listData_.clear(); }
uint16_t TjdAlbumAdapter::GetCount(void) { return listData_.size(); }
void TjdAlbumAdapter::ClearItem(void) { listData_.clear(); }
void TjdAlbumAdapter::DeleteListItem(uint8_t index)
{
auto it = std::find_if(listData_.begin(), listData_.end(),
[index](const AlbumItem &item) { return item.index == index; });
if (it != listData_.end()) {
listData_.erase(it);
}
}
UIView *TjdAlbumAdapter::GetView(UIView *inView, int16_t index)
{
if (index >= (int16_t)listData_.size() || index < 0) {
return nullptr;
}
std::list<AlbumItem>::iterator it = listData_.begin();
for (int i = 0; i < index; i++) {
it++;
}
AlbumItem itemData = *it;
TjdAlbumItemView *item = nullptr;
if (inView == nullptr) {
item = new TjdAlbumItemView();
} else {
item = static_cast<TjdAlbumItemView *>(inView);
}
if (itemData.path == nullptr)
static_print_error("TjdAlbumAdapter::%s: itemData.path is nullptr", __func__);
item->SetItemInfo(itemData);
return item;
}
AlbumItem *TjdAlbumAdapter::GetListItem(uint8_t index)
{
for (auto it = listData_.begin(); it != listData_.end(); ++it) {
if ((*it).index == index) {
return &*it;
}
}
return nullptr;
}
} // namespace TJD