mcu_hi3321_watch/tjd/ui/app/message/particular/TjdUiMessageParticularPage.cpp
2025-05-31 10:45:15 +08:00

334 lines
13 KiB
C++

/*----------------------------------------------------------------------------
* Copyright (c) TJD Technologies Co., Ltd. 2024. All rights reserved.
*
* Description:
*
* Author: huangshuyi
*
* Create: 2024-7
*--------------------------------------------------------------------------*/
#include "TjdUiMessageParticularPage.h"
#include "components/ui_label.h"
#include "wearable_log.h"
#include "common/image_cache_manager.h"
#include <time.h>
#include <unordered_map>
#include "gfx_utils/image_info.h"
#include "gfx_utils/heap_base.h"
#include "TjdUiImageIds.h"
#include "ui_label.h"
#include "ui_label_ext.h"
#include "components/ui_label_ext.h"
#include "TjdUiMultiLanguageExt.h"
#include "sys_config.h"
#include "components/ui_view.h"
#include "TjdUiMessageCommon.h"
#include "TjdUiMessageTotalView.h"
#include "TjdUiMemManage.h"
#include <ctime>
#include "sql_message.h"
using namespace OHOS;
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
#define IMAGE_BIN_PATH TJD_IMAGE_PATH"img_main_message.bin"
static TjdUiMessageParticularPage *g_pMessageParticularPage = nullptr;
uint8_t TjdUiMessageParticularPage::msgIndex;
TjdUiMessageParticularPage::TjdUiMessageParticularPage()
{
//SetPosition(0, 0, HORIZONTAL_RESOLUTION, VERTICAL_RESOLUTION);
g_pMessageParticularPage = this;
}
TjdUiMessageParticularPage::~TjdUiMessageParticularPage()
{
static_print_debug("delete TjdUiMessageParticularPage......\n");
if (itemClickListener) {
delete itemClickListener;
itemClickListener = nullptr;
}
if (delClickListener) {
delete delClickListener;
delClickListener = nullptr;
}
container_->Remove(pageGroup);
scrollCont->ClearFocus();
TjdUiMemManage::DeleteChildren(pageGroup);
// if (screenDragListener) {
// delete screenDragListener;
// screenDragListener = nullptr;
// }
g_pMessageParticularPage = nullptr;
}
TjdUiMessageParticularPage *TjdUiMessageParticularPage::GetInstance(void)
{
return g_pMessageParticularPage;
}
bool TjdUiMessageParticularPage::OnClick(UIView &view, const ClickEvent &event)
{
return true;
}
void TjdUiMessageParticularPage::show(void)
{
static_print_debug("TjdUiMessageParticularPage::show\n");
if (scrollCont) {
scrollCont->SetVisible(true);
}
}
void TjdUiMessageParticularPage::hide(void)
{
static_print_debug("TjdUiMessageParticularPage::hide\n");
if (scrollCont) {
scrollCont->SetVisible(false);
}
}
void TjdUiMessageParticularPage::DeleteMessage(uint8_t index)
{
static_print_debug("TjdUiMessageParticularPage::DeleteMessage\n");
sql_message_delete_message_by_type(TjdUiMessageTotalPage::messageType);
NativeAbility::GetInstance().ChangeSlice(TJD_APP_VIEW_MESSAGE_TOTAL);
}
int16_t TjdUiMessageParticularPage::CreateMessageItem(UIViewGroup*& cont, const uint8_t& index, const int16_t& x, const int16_t& y, UIView::OnClickListener*itemClickListener)
{
static_print_debug("CreateMessageItem\n");
message_enum messageType = (message_enum)sql_message_get_type(index);//找到类型
UIViewGroup*textGroup = new UIViewGroup();
textGroup->SetTouchable(true);
textGroup->SetOnClickListener(itemClickListener);
textGroup->SetViewIndex((uint8_t)index);
cont->Add(textGroup);
textGroup->SetStyle(OHOS::STYLE_BORDER_RADIUS, 25);
textGroup->SetStyle(STYLE_BACKGROUND_COLOR, 0xff262626);
textGroup->SetAutoSize(false);
textGroup->SetPosition(x, y);
textGroup->SetWidth(Screen::GetInstance().GetWidth() - 120);
//textGroup->SetPosition(60, 80,Screen::GetInstance().GetWidth() - 120, 200);
UIViewGroup*line1Group = new UIViewGroup();
line1Group->SetViewId("line1Group");
textGroup->Add(line1Group);
line1Group->LayoutTopOfParent(11);
line1Group->SetStyle(STYLE_BACKGROUND_COLOR, 0xff262626);
line1Group->SetAutoSize(true);
UIImageView* imageType = new UIImageView();
line1Group->Add(imageType);
TjdUiMessage* messageRes = TjdUiMessageResGet(messageType);
if (messageRes == nullptr) {
static_print_error("messageRes is null......\n");
return 0;
}
ImageInfo* imgInfo = ImageCacheManager::GetInstance().LoadOneInMultiRes(messageRes->imageId, IMAGE_BIN_PATH);
imageType->SetSrc(imgInfo);
Vector2<float> itemScaleCenter = {0, 0};
float imgFac = (float)30/imgInfo->header.width ;
Vector2<float> itemScaleFactor = {imgFac, imgFac};
//imageType->Scale(itemScaleFactor, itemScaleCenter);
imageType->SetAutoEnable(false);
imageType->Resize(40, 40);
imageType->SetResizeMode(UIImageView::ImageResizeMode::FILL);
imageType->SetViewId("messageIcon");
imageType->SetPosition(0, 0);
line1Group->Remove(imageType);
line1Group->Add(imageType);
UILabelExt *labelType = new UILabelExt();
line1Group->Add(labelType);
labelType->SetTextId(messageRes->nameId);//需要换成多语言
labelType->SetFont(TJD_VECTOR_FONT_FILENAME, TJD_FONT_SIZE_28);
labelType->SetLineBreakMode(UILabel::LINE_BREAK_ADAPT);
labelType->SetAlign(TEXT_ALIGNMENT_CENTER, TEXT_ALIGNMENT_CENTER);
labelType->AlignVerCenterToSibling(imageType->GetViewId(), 0);
labelType->LayoutRightToSibling(imageType->GetViewId(), 8);
labelType->SetViewId("labelType");
line1Group->Remove(labelType);
line1Group->Add(labelType);
if (sql_message_get_read_flag(index)) {
UIImageView* imageUnread = new UIImageView();
line1Group->Add(imageUnread);
imgInfo = ImageCacheManager::GetInstance().LoadOneInMultiRes(IMG_MAIN_MESSAGE_UNREAD_DOT, IMAGE_BIN_PATH);
imageUnread->SetSrc(imgInfo);
imageUnread->AlignVerCenterToSibling(labelType->GetViewId(), 0);
imageUnread->LayoutRightToSibling(labelType->GetViewId(), 8);
line1Group->Remove(imageUnread);
line1Group->Add(imageUnread);
}
line1Group->LayoutLeftOfParent((textGroup->GetWidth()-line1Group->GetWidth()) / 2);
time_t timesample = sql_message_get_timestamp(index);
struct tm *timeinfo = std::localtime(&timesample);
uint16_t year = timeinfo->tm_year + 1900;
uint8_t month = timeinfo->tm_mon + 1;
uint8_t day = timeinfo->tm_mday;
uint8_t hour = timeinfo->tm_hour;
uint8_t minute = timeinfo->tm_min;
printf("get timesample:%d, year:%d, month:%d, day:%d, hour:%d, minute:%d\n", timesample, year, month, day, hour, minute);
char strTime[30];
snprintf(strTime, sizeof(strTime), "%d-%d-%d %d:%d", year, month, day, hour, minute);
//std::string strTime = "2024-5-21";
UILabel *labelTime = new UILabel();
textGroup->Add(labelTime);
//labelTime->SetText(strTime.c_str());
labelTime->SetText(strTime);
labelTime->SetFont(TJD_VECTOR_FONT_FILENAME, TJD_FONT_SIZE_28);
labelTime->SetLineBreakMode(UILabel::LINE_BREAK_ADAPT);
labelTime->SetAlign(TEXT_ALIGNMENT_CENTER, TEXT_ALIGNMENT_CENTER);
labelTime->AlignHorCenterToSibling(line1Group->GetViewId(), 0);
labelTime->LayoutBottomToSibling(line1Group->GetViewId(), 3);
labelTime->SetViewId("labelTime");
//std::string strText = "nnoodsfofdjojglfdjlgjfdslkjlkgjfdlkjnnodsfljglkjelkrjglkkndsnvsalkfkjaljlgkjrkejlkglkalkgj";
char* strText;
sql_message_get_detail_info(index, &strText);
UILabel *labelText = new UILabel();
textGroup->Add(labelText);
//labelText->SetText(strText.c_str());
labelText->SetText(strText);
labelText->SetFont(TJD_VECTOR_FONT_FILENAME, TJD_FONT_SIZE_28);
labelText->SetLineBreakMode(UILabel::LINE_BREAK_WRAP);
labelText->SetWidth(textGroup->GetWidth() - 40);
labelText->SetAlign(TEXT_ALIGNMENT_CENTER, TEXT_ALIGNMENT_CENTER);
labelText->AlignHorCenterToSibling(labelTime->GetViewId(), 0);
labelText->LayoutBottomToSibling(labelTime->GetViewId(), 3);
int16_t labelTextH = labelText->GetHeight();
int16_t labelTextY = labelText->GetY();
textGroup->SetHeight(labelTextY + labelTextH + 11);
cont->Remove(textGroup);
cont->Add(textGroup);
return textGroup->GetRelativeRect().GetBottom();
}
void TjdUiMessageParticularPage::CreateMessagePageGroup(void)
{
message_enum messageType = TjdUiMessageTotalPage::messageType;
messagePageGroup = new UIViewGroup();
scrollCont->Add(messagePageGroup);
messagePageGroup->SetAutoSize(true);
messagePageGroup->SetPosition(0, 0);
//pageGroup->SetWidth(Screen::GetInstance().GetWidth());
//pageGroup->SetHeight(Screen::GetInstance().GetHeight());
UIImageView* imageType = new UIImageView();
messagePageGroup->Add(imageType);
int16_t scrollW = scrollCont->GetWidth();
TjdUiMessage* messageRes = TjdUiMessageResGet(messageType);
ImageInfo* imgInfo = ImageCacheManager::GetInstance().LoadOneInMultiRes(messageRes->imageId, IMAGE_BIN_PATH);
imageType->SetSrc(imgInfo);
imageType->SetAutoEnable(false);
imageType->Resize(60, 60);
imageType->SetResizeMode(UIImageView::ImageResizeMode::FILL);
int16_t imageWidth = imageType->GetRect().GetWidth();
imageType->LayoutLeftOfParent(scrollW/2 - imageWidth/2);
imageType->LayoutTopOfParent(20);
imageType->SetViewId("imageType");
UILabelExt *labelName = new UILabelExt();
messagePageGroup->Add(labelName);
labelName->SetTextId(messageRes->nameId);//需要换成多语言
labelName->SetFont(TJD_VECTOR_FONT_FILENAME, TJD_FONT_SIZE_30);
labelName->SetLineBreakMode(UILabel::LINE_BREAK_ADAPT);
labelName->SetAlign(TEXT_ALIGNMENT_CENTER, TEXT_ALIGNMENT_CENTER);
labelName->LayoutTopOfParent(20);
scrollW = scrollCont->GetWidth();
int16_t labelNameW = labelName->GetWidth();
labelName->LayoutLeftOfParent((scrollW - labelNameW) / 2);
labelName->SetStyle(STYLE_BACKGROUND_COLOR, Color::Gray().full);
labelName->LayoutBottomToSibling(imageType->GetViewId(), 4);
itemClickListener = new ItemClickListener();
//sql_message_test();
uint8_t* index_list = new uint8_t[sql_message_get_max_number()];
uint8_t count = sql_message_get_message_index_list_by_type(index_list, messageType);
#define ITEM_GAP_DISTANCE 20
int16_t y = labelName->GetY() + labelName->GetHeight() + 20;
for (uint8_t i = 0; i < count; i++) {
y = CreateMessageItem(messagePageGroup, index_list[i], 60, y, itemClickListener);
y += ITEM_GAP_DISTANCE;
}
delete []index_list;
UIImageView* imageDel = new UIImageView();
messagePageGroup->Add(imageDel);
imgInfo = ImageCacheManager::GetInstance().LoadOneInMultiRes(IMG_MAIN_MESSAGE_DELETE, IMAGE_BIN_PATH);
if (y + imgInfo->header.height < Screen::GetInstance().GetHeight()) {
y = Screen::GetInstance().GetHeight() - imgInfo->header.height;
}
imageDel->SetPosition(186, y);
imageDel->SetSrc(imgInfo);
imageDel->SetTouchable(true);
delClickListener = new DelClickListener();
imageDel->SetOnClickListener(delClickListener);
messagePageGroup->Remove(imageDel);
messagePageGroup->Add(imageDel);
}
UIViewGroup *TjdUiMessageParticularPage::PageInit(UIViewGroup *cont)
{
static_print_debug("TjdUiMessageParticularPage::PageInit:%p\n", cont);
container_ = cont;
pageGroup = new UIViewGroup();
container_->Add(pageGroup);
pageGroup->SetPosition(0, 0);
pageGroup->SetWidth(Screen::GetInstance().GetWidth());
pageGroup->SetHeight(Screen::GetInstance().GetHeight());
pageGroup->SetOnDragListener(this);
pageGroup->SetTouchable(true);
pageGroup->SetDraggable(true);
if (scrollCont != nullptr) {
return scrollCont;
}
//UIViewGroup *container = (UIViewGroup *)TjdUiMessageTotalView::GetInstance()->GetContainer();
scrollCont = new UIScrollView();
pageGroup->Add(scrollCont);
// container->Add(scrollCont);
scrollCont->SetScrollBlankSize(20);
scrollCont->SetHorizontalScrollState(false);
scrollCont->SetThrowDrag(true);//设置结束拖动时,是否产生惯性滑动动效
scrollCont->SetMaxScrollDistance(1000);
scrollCont->SetSwipeACCLevel(5); // 越大距离越长
scrollCont->SetDragACCLevel(10); //越大时间越短
scrollCont->SetReboundSize(Screen::GetInstance().GetHeight() >> 1);
scrollCont->SetElastic(true);
scrollCont->SetPosition(0, 0);
scrollCont->RequestFocus();
scrollCont->SetWidth(Screen::GetInstance().GetWidth());
scrollCont->SetHeight(Screen::GetInstance().GetHeight());
CreateMessagePageGroup();
static_print_debug("TjdUiMessageParticularPage::PageInit done\n");
return pageGroup;
}
}