423 lines
16 KiB
C++
423 lines
16 KiB
C++
/*----------------------------------------------------------------------------
|
||
* Copyright (c) TJD Technologies Co., Ltd. 2024. All rights reserved.
|
||
*
|
||
* Description:
|
||
*
|
||
* Author: huangshuyi
|
||
*
|
||
* Create: 2024-8
|
||
*--------------------------------------------------------------------------*/
|
||
|
||
#include "TjdUiFemaleCalenderPage.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 "components/ui_view.h"
|
||
#include "TjdUiMultiLanguageExt.h"
|
||
#include "sys_config.h"
|
||
#include "TjdUiMemManage.h"
|
||
#include <ctime>
|
||
#include "sql_message.h"
|
||
#include "rtc_api.h"
|
||
#include "TjdUiFemaleHealthView.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 FEMALE_IMAGE_BIN_PATH TJD_IMAGE_PATH"img_female.bin"
|
||
#define CALENDAR_IMAGE_BIN_PATH TJD_IMAGE_PATH "img_calendar.bin"
|
||
|
||
|
||
|
||
static TjdUiFemaleCalenderPage *g_pFemaleCalenderData = nullptr;
|
||
static inline int16_t HorizontalCenter(int16_t width, int16_t parentWidth) { return (parentWidth - width) / 2; }
|
||
static const char* monthCh[] = {"一月", "二月", "三月", "四月", "五月", "六月",
|
||
"七月", "八月", "九月", "十月", "十一月", "十二月"};
|
||
static const char* monthEn[] = {"January", "February", "March", "April", "May", "June",
|
||
"July", "August", "September", "October", "November", "December"};
|
||
static inline int16_t VerticalCenter(int16_t height, int16_t parentHeight) { return (parentHeight - height) / 2; }
|
||
|
||
static inline void InitLabelHorCenter(OHOS::UILabel*& label, uint8_t size, int16_t y, int16_t target, const char *text)
|
||
{
|
||
label->SetFont(TJD_VECTOR_FONT_FILENAME, size);
|
||
label->SetText(text);
|
||
label->SetLineBreakMode(OHOS::UILabel::LINE_BREAK_ADAPT);
|
||
label->SetAlign(OHOS::TEXT_ALIGNMENT_CENTER, OHOS::TEXT_ALIGNMENT_CENTER);
|
||
label->SetPosition(HorizontalCenter(label->GetWidth(), target), y);
|
||
}
|
||
|
||
TjdUiFemaleCalenderPage::TjdUiFemaleCalenderPage()
|
||
{
|
||
//SetPosition(0, 0, HORIZONTAL_RESOLUTION, VERTICAL_RESOLUTION);
|
||
g_pFemaleCalenderData = this;
|
||
}
|
||
|
||
TjdUiFemaleCalenderPage::~TjdUiFemaleCalenderPage()
|
||
{
|
||
g_pFemaleCalenderData = nullptr;
|
||
}
|
||
|
||
TjdUiFemaleCalenderPage *TjdUiFemaleCalenderPage::GetInstance(void)
|
||
{
|
||
return g_pFemaleCalenderData;
|
||
}
|
||
|
||
void TjdUiFemaleCalenderPage::HideView(void)
|
||
{
|
||
static_print_debug("TjdUiFemaleCalenderPage::HideView\n");
|
||
pageGroup_->SetVisible(false);
|
||
}
|
||
|
||
void TjdUiFemaleCalenderPage::ShowView(void)
|
||
{
|
||
static_print_debug("TjdUiFemaleCalenderPage::ShowView\n");
|
||
pageGroup_->SetVisible(true);
|
||
}
|
||
|
||
bool TjdUiFemaleCalenderPage::OnClick(UIView &view, const ClickEvent &event)
|
||
{
|
||
static_print_debug("TjdUiFemaleCalenderPage::retry to send ble request\n");
|
||
|
||
if (&view == iconLeft_) {
|
||
static_print_debug("iconLeft_ clicked\n");
|
||
lastMonth_--;
|
||
if (lastMonth_ == 0) {
|
||
lastMonth_ = 12;
|
||
lastYear_--;
|
||
}
|
||
UpdateInfo(curDateInfo, lastMonth_, lastYear_);
|
||
} else if (&view == iconRight_) {
|
||
static_print_debug("iconRight_ clicked\n");
|
||
lastMonth_++;
|
||
if (lastMonth_ == 13) {
|
||
lastMonth_ = 1;
|
||
lastYear_++;
|
||
}
|
||
UpdateInfo(curDateInfo, lastMonth_, lastYear_);
|
||
}
|
||
return true;
|
||
}
|
||
|
||
uint8_t TjdUiFemaleCalenderPage::GetDaysOfCurrentMonth(uint8_t month, uint16_t year)
|
||
{
|
||
uint8_t daysInMonth = 0;
|
||
switch (month) {
|
||
case 1:
|
||
case 3:
|
||
case 5:
|
||
case 7:
|
||
case 8:
|
||
case 10:
|
||
case 12:
|
||
daysInMonth = 31;
|
||
break;
|
||
case 4:
|
||
case 6:
|
||
case 9:
|
||
case 11:
|
||
daysInMonth = 30;
|
||
break;
|
||
case 2:
|
||
// 判断是否为闰年
|
||
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
|
||
daysInMonth = 29;
|
||
else
|
||
daysInMonth = 28;
|
||
break;
|
||
default:
|
||
daysInMonth = -1; // 处理无效月份
|
||
break;
|
||
}
|
||
|
||
return daysInMonth;
|
||
}
|
||
|
||
int TjdUiFemaleCalenderPage::GetDaysOfCurrentWeek(int year, int month, int day)
|
||
{
|
||
struct tm time_in = {0};
|
||
time_in.tm_year = year - 1900; // 年份需要减去1900
|
||
time_in.tm_mon = month - 1; // 月份从0开始,所以需要减去1
|
||
time_in.tm_mday = day; // 设置为每月的第一天
|
||
time_t time_sec = mktime(&time_in); // 将时间转换为日历时间
|
||
if (time_sec == -1) {
|
||
static_print_debug("Error: unable to make time using mktime");
|
||
}
|
||
struct tm *time_out = localtime(&time_sec);
|
||
return time_out->tm_wday; // 返回星期几(0-6,分别代表周日到周六)
|
||
}
|
||
|
||
void TjdUiFemaleCalenderPage::UpdateInfo(CurrentDateInfo curDateInfo, uint8_t month, uint16_t year)
|
||
{
|
||
static_print_debug("CalendarView::UpdateInfo");
|
||
uint8_t column = 0;
|
||
daysInMonth = GetDaysOfCurrentMonth(month, year);
|
||
weekDaysOfMonth = GetDaysOfCurrentWeek(year, month, 1);
|
||
//printf("daysInMonth = %d, weekDaysOfMonth = %d,month:%d,year:%d\n", daysInMonth, weekDaysOfMonth, month, year);
|
||
if (curDateInfo.month == month && curDateInfo.year == year) {
|
||
curWeekday_->SetVisible(true);
|
||
} else {
|
||
curWeekday_->SetVisible(false);
|
||
}
|
||
ImageInfo *imgInfo;
|
||
int16_t itemX, itemY;
|
||
char tempstr[10] = {0};
|
||
uint8_t data = 0;//当天的生理数据,预测月经日 月经期 排卵期
|
||
for (uint8_t i = 0; i < MAX_DAYS_OF_MONTH; i++) {
|
||
if (weekDaysOfMonth > 6) {
|
||
weekDaysOfMonth = 0;
|
||
column++;
|
||
}
|
||
|
||
itemX = 55 + (40 + 13)*weekDaysOfMonth;
|
||
itemY = 126 + (40 + 8)*column;
|
||
|
||
CalendarLabel[i]->SetTextColor(Color::GetColorFromRGBA(0xff, 0xff, 0xff, 0x50));
|
||
memset(tempstr, 0, strlen(tempstr));
|
||
sprintf(tempstr, "%d", i + 1);
|
||
CalendarLabel[i]->SetText(tempstr);
|
||
|
||
// if (weekDaysOfMonth < 1 || weekDaysOfMonth > 5) {
|
||
// CalendarLabel[i]->SetTextColor(Color::GetColorFromRGBA(0xff, 0xff, 0xff, 0x50));
|
||
// CalendarLabel[i]->SetStyle(STYLE_BACKGROUND_OPA, 0xff);
|
||
// }
|
||
if (data == 0) {
|
||
//预测月经日
|
||
//CalendarImage[i]->Resize(40, 40);
|
||
CalendarImage[i]->SetVisible(true);
|
||
imgInfo = ImageCacheManager::GetInstance().LoadOneInMultiRes(IMG_FEMALE_FORECAST, FEMALE_IMAGE_BIN_PATH);
|
||
if (imgInfo == nullptr) {
|
||
static_print_debug("imgWeek_ load image error");
|
||
}
|
||
CalendarImage[i]->SetSrc(imgInfo);
|
||
CalendarLabel[i]->SetTextColor(Color::GetColorFromRGBA(0xff, 0xff, 0xff, 0x50));
|
||
CalendarLabel[i]->SetStyle(STYLE_BORDER_RADIUS, 0);
|
||
CalendarLabel[i]->SetStyle(STYLE_BACKGROUND_COLOR, Color::Black().full);
|
||
} else if (data == 1) {
|
||
//月经期
|
||
CalendarLabel[i]->SetStyle(STYLE_BACKGROUND_COLOR, 0xFF9377FF);
|
||
CalendarLabel[i]->SetStyle(STYLE_BACKGROUND_OPA, 0xff);
|
||
CalendarLabel[i]->SetStyle(STYLE_BORDER_RADIUS, 20);
|
||
CalendarImage[i]->SetVisible(false);
|
||
} else {
|
||
//排卵期
|
||
CalendarLabel[i]->SetStyle(STYLE_BACKGROUND_COLOR, 0xFFFA406F);
|
||
CalendarLabel[i]->SetStyle(STYLE_BACKGROUND_OPA, 0xff);
|
||
CalendarLabel[i]->SetStyle(STYLE_BORDER_RADIUS, 20);
|
||
CalendarImage[i]->SetVisible(false);
|
||
}
|
||
|
||
if (i >= daysInMonth) {
|
||
CalendarGroup[i]->SetVisible(false);
|
||
} else {
|
||
CalendarGroup[i]->SetVisible(true);
|
||
}
|
||
|
||
weekDaysOfMonth++;
|
||
}
|
||
|
||
const char* weekStr = nullptr;
|
||
uint8_t language = 0;//0:中文,1:英文
|
||
if (language == 0) {
|
||
weekStr = monthCh[month - 1];
|
||
} else {
|
||
weekStr = monthEn[month - 1];
|
||
}
|
||
labelMonth_->SetText(weekStr);
|
||
}
|
||
|
||
UIViewGroup *TjdUiFemaleCalenderPage::PageInit(void)
|
||
{
|
||
static_print_debug("TjdUiFemaleCalenderPage::PageInit\n");
|
||
UIViewGroup *container = (UIViewGroup *)TjdUiFemaleHealthView::GetInstance()->GetContainer();
|
||
pageGroup_ = new UIViewGroup();
|
||
pageGroup_->SetTouchable(true);
|
||
pageGroup_->SetTouchable(true);
|
||
pageGroup_->SetOnDragListener(this);
|
||
pageGroup_->SetDraggable(true);
|
||
container->Add(pageGroup_);
|
||
pageGroup_->SetPosition(0, 0, Screen::GetInstance().GetWidth(), Screen::GetInstance().GetHeight());
|
||
ImageInfo *imgInfo;
|
||
|
||
rtc_class_ops *rtc = tjd_driver_rtc_get_ops();
|
||
struct rtc_time localTime;
|
||
rtc->get_rtc_time(&localTime);
|
||
curDateInfo.year = localTime.tm_year;
|
||
curDateInfo.month = localTime.tm_mon;
|
||
curDateInfo.day = localTime.tm_mday;
|
||
lastMonth_ = curDateInfo.month;
|
||
lastYear_ = curDateInfo.year;
|
||
|
||
labelMonth_ = new UILabel();
|
||
pageGroup_->Add(labelMonth_);
|
||
const char* weekStr = nullptr;
|
||
uint8_t language = 0;//0:中文,1:英文
|
||
if (language == 0) {
|
||
weekStr = monthCh[curDateInfo.month - 1];
|
||
} else {
|
||
weekStr = monthEn[curDateInfo.month - 1];
|
||
}
|
||
InitLabelHorCenter(labelMonth_, 28, 17, Screen::GetInstance().GetWidth(), weekStr);
|
||
|
||
iconLeft_ = new UIImageView();
|
||
pageGroup_->Add(iconLeft_);
|
||
imgInfo = ImageCacheManager::GetInstance().LoadOneInMultiRes(IMG_CALENDAR_ICON_LEFT, CALENDAR_IMAGE_BIN_PATH);
|
||
if (imgInfo == nullptr) {
|
||
static_print_debug("iconLeft_ load image error");
|
||
}
|
||
iconLeft_->SetAutoEnable(false);
|
||
iconLeft_->Resize(imgInfo->header.width * 10, imgInfo->header.height * 5);
|
||
iconLeft_->SetSrc(imgInfo);
|
||
iconLeft_->SetOnClickListener(this);
|
||
iconLeft_->SetTouchable(true);
|
||
//iconLeft_->SetStyle(STYLE_BACKGROUND_COLOR, Color::Red().full);
|
||
iconLeft_->SetPosition(130 - (imgInfo->header.width * 10 - imgInfo->header.width) / 2, 31 - (imgInfo->header.height * 5 - imgInfo->header.height) / 2);
|
||
//iconLeft_->SetStyle(STYLE_BACKGROUND_OPA, 0xff);
|
||
iconLeft_->SetResizeMode(UIImageView::ImageResizeMode::CENTER);
|
||
|
||
iconRight_ = new UIImageView();
|
||
pageGroup_->Add(iconRight_);
|
||
imgInfo = ImageCacheManager::GetInstance().LoadOneInMultiRes(IMG_CALENDAR_ICON_RIGHT, CALENDAR_IMAGE_BIN_PATH);
|
||
if (imgInfo == nullptr) {
|
||
static_print_debug("iconLeft_ load image error");
|
||
}
|
||
iconRight_->SetAutoEnable(false);
|
||
iconRight_->Resize(imgInfo->header.width * 10, imgInfo->header.height * 5);
|
||
iconRight_->SetSrc(imgInfo);
|
||
iconRight_->SetOnClickListener(this);
|
||
iconRight_->SetTouchable(true);
|
||
//iconRight_->SetStyle(STYLE_BACKGROUND_COLOR, Color::Red().full);
|
||
iconRight_->SetPosition(324 - (imgInfo->header.width * 10 - imgInfo->header.width) / 2, 31 - (imgInfo->header.height * 5 - imgInfo->header.height) / 2);
|
||
//iconRight_->SetStyle(STYLE_BACKGROUND_OPA, 0xff);
|
||
iconRight_->SetResizeMode(UIImageView::ImageResizeMode::CENTER);
|
||
|
||
uint8_t column = 0;
|
||
char tempstr[10] = {0};
|
||
daysInMonth = GetDaysOfCurrentMonth(curDateInfo.month, curDateInfo.year);
|
||
weekDaysOfMonth = GetDaysOfCurrentWeek(curDateInfo.year, curDateInfo.month, 1); // 每月第一天是星期几(0-6,分别代表周日到周六)
|
||
int16_t itemX, itemY;
|
||
uint8_t data = 0;//当天的生理数据,预测月经日 月经期 排卵期
|
||
for (uint8_t i = 0; i < MAX_DAYS_OF_MONTH; i++) {
|
||
if (weekDaysOfMonth > 6) {
|
||
weekDaysOfMonth = 0;
|
||
column++;
|
||
}
|
||
CalendarGroup[i] = new UIViewGroup();
|
||
pageGroup_->Add(CalendarGroup[i]);
|
||
itemX = 55 + (40 + 13)*weekDaysOfMonth;
|
||
itemY = 126 + (40 + 8)*column;
|
||
CalendarGroup[i]->SetPosition(itemX, itemY);
|
||
CalendarGroup[i]->Resize(40, 40);
|
||
//CalendarGroup[i]->SetStyle(STYLE_BACKGROUND_COLOR, Color::Red().full);
|
||
|
||
|
||
CalendarLabel[i] = new UILabel();
|
||
CalendarGroup[i]->Add(CalendarLabel[i]);
|
||
CalendarLabel[i]->SetPosition(0, 0);
|
||
CalendarLabel[i]->Resize(40, 40);
|
||
CalendarLabel[i]->SetStyle(STYLE_TEXT_FONT, TJD_FONT_SIZE_24);
|
||
CalendarLabel[i]->SetTextColor(Color::GetColorFromRGBA(0xff, 0xff, 0xff, 0x50));
|
||
CalendarLabel[i]->SetStyle(STYLE_BACKGROUND_OPA, 0xff);
|
||
memset(tempstr, 0, strlen(tempstr));
|
||
sprintf(tempstr, "%d", i + 1);
|
||
CalendarLabel[i]->SetText(tempstr);
|
||
|
||
// if (weekDaysOfMonth < 1 || weekDaysOfMonth > 5) {
|
||
// CalendarLabel[i]->SetTextColor(Color::GetColorFromRGBA(0xff, 0xff, 0xff, 0x50));
|
||
// CalendarLabel[i]->SetStyle(STYLE_BACKGROUND_OPA, 0xff);
|
||
// }
|
||
CalendarImage[i] = new UIImageView();
|
||
CalendarGroup[i]->Add(CalendarImage[i]);
|
||
CalendarImage[i]->SetPosition(0, 0);
|
||
if (data == 0) {
|
||
//预测月经日
|
||
//CalendarImage[i]->Resize(40, 40);
|
||
imgInfo = ImageCacheManager::GetInstance().LoadOneInMultiRes(IMG_FEMALE_FORECAST, FEMALE_IMAGE_BIN_PATH);
|
||
if (imgInfo == nullptr) {
|
||
static_print_debug("imgWeek_ load image error");
|
||
}
|
||
CalendarImage[i]->SetSrc(imgInfo);
|
||
} else if (data == 1) {
|
||
//月经期
|
||
CalendarLabel[i]->SetStyle(STYLE_BACKGROUND_COLOR, 0xFF9377FF);
|
||
CalendarLabel[i]->SetStyle(STYLE_BACKGROUND_OPA, 0xff);
|
||
CalendarLabel[i]->SetStyle(STYLE_BORDER_RADIUS, 20);
|
||
CalendarImage[i]->SetVisible(false);
|
||
} else {
|
||
//排卵期
|
||
CalendarLabel[i]->SetStyle(STYLE_BACKGROUND_COLOR, 0xFFFA406F);
|
||
CalendarLabel[i]->SetStyle(STYLE_BACKGROUND_OPA, 0xff);
|
||
CalendarLabel[i]->SetStyle(STYLE_BORDER_RADIUS, 20);
|
||
CalendarImage[i]->SetVisible(false);
|
||
}
|
||
|
||
CalendarLabel[i]->SetLineBreakMode(UILabel::LINE_BREAK_ELLIPSIS);
|
||
CalendarLabel[i]->SetAlign(TEXT_ALIGNMENT_CENTER, TEXT_ALIGNMENT_CENTER);
|
||
CalendarLabel[i]->SetFont(TJD_VECTOR_FONT_FILENAME, TJD_FONT_SIZE_28);
|
||
|
||
if (i >= daysInMonth) {
|
||
CalendarGroup[i]->SetVisible(false);
|
||
} else {
|
||
CalendarGroup[i]->SetVisible(true);
|
||
}
|
||
|
||
weekDaysOfMonth++;
|
||
}
|
||
UIImageView* imgWeek_ = new UIImageView();
|
||
pageGroup_->Add(imgWeek_);
|
||
imgInfo = ImageCacheManager::GetInstance().LoadOneInMultiRes(IMG_CALENDAR_WEEK_CH, CALENDAR_IMAGE_BIN_PATH);
|
||
if (imgInfo == nullptr) {
|
||
static_print_debug("imgWeek_ load image error");
|
||
}
|
||
imgWeek_->SetSrc(imgInfo);
|
||
imgWeek_->SetPosition(65, 73);
|
||
// imgWeek_->SetTouchable(true);
|
||
// imgWeek_->SetOnClickListener(this);
|
||
|
||
UIImageView* bjLine_ = new UIImageView();
|
||
pageGroup_->Add(bjLine_);
|
||
imgInfo = ImageCacheManager::GetInstance().LoadOneInMultiRes(IMG_CALENDAR_BJ_LINE, CALENDAR_IMAGE_BIN_PATH);
|
||
if (imgInfo == nullptr) {
|
||
static_print_debug("bjLine_ load image error");
|
||
}
|
||
bjLine_->SetSrc(imgInfo);
|
||
bjLine_->SetPosition(39, 104);
|
||
|
||
curWeekday_ = new UIImageView();
|
||
pageGroup_->Add(curWeekday_);
|
||
uint8_t Weekday = GetDaysOfCurrentWeek(curDateInfo.year, curDateInfo.month, curDateInfo.day); // 每月第一天是星期几(0-6,分别代表周日到周六)
|
||
curWeekday_->SetPosition(49 + Weekday * 50 + 3 * Weekday, 103);
|
||
imgInfo = ImageCacheManager::GetInstance().LoadOneInMultiRes(IMG_CALENDAR_WEEK_SELECTED, CALENDAR_IMAGE_BIN_PATH);
|
||
if (imgInfo == nullptr) {
|
||
static_print_debug("curWeekday_ load image error");
|
||
}
|
||
curWeekday_->SetSrc(imgInfo);
|
||
|
||
|
||
return pageGroup_;
|
||
}
|
||
|
||
}
|