From 3f681bac088943793b4e1a464c18541346363e7d Mon Sep 17 00:00:00 2001 From: Caisking <3449750381@qq.com> Date: Sat, 17 Aug 2024 11:54:20 +0800 Subject: [PATCH] =?utf8?q?=E7=9C=81=E7=94=B5=E6=A8=A1=E5=BC=8FUI=E6=9B=B4?= =?utf8?q?=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .../nativeui/saveele/include/SaveBatView.h | 29 +- .../nativeui/saveele/src/SaveBatPresenter.cpp | 7 + .../nativeui/saveele/src/SaveBatView.cpp | 317 +++++++----------- 3 files changed, 141 insertions(+), 212 deletions(-) diff --git a/code/sdk/application/wearable/fd230app/nativeui/saveele/include/SaveBatView.h b/code/sdk/application/wearable/fd230app/nativeui/saveele/include/SaveBatView.h index bbaf4fb1f..5f563e6ab 100644 --- a/code/sdk/application/wearable/fd230app/nativeui/saveele/include/SaveBatView.h +++ b/code/sdk/application/wearable/fd230app/nativeui/saveele/include/SaveBatView.h @@ -1,3 +1,9 @@ +/*** + * @Date: 2024-04-15 10:55:58 + * @LastEditors: Caisking 3449750381@qq.com + * @LastEditTime: 2024-08-17 10:30:26 + * @FilePath: /FD230/code/sdk/application/wearable/fd230app/nativeui/saveele/include/SaveBatView.h + */ #ifndef _SAVEBAT_VIEW_H #define _SAVEBAT_VIEW_H @@ -34,22 +40,27 @@ namespace OHOS void Init(); void DeInit(); UIView *GetView(); + void RefreshView(); + void InitBatteryView(); + void InitTimeView(); + void InitButtonView(); private: UIView::OnDragListener *draglistener{nullptr}; SaveBatView(SaveBatView &&) = delete; SaveBatView &operator=(SaveBatView &&) = delete; - // UIViewGroup *container_ = nullptr; - UIDigitalClock *labelTime{nullptr}; - UILabel *timelabel = nullptr; // 时间 - UILabel *datelabel = nullptr; // 日期标签 - UILabel *weeklabel = nullptr; // 星期标签 - UILabel *elelabel = nullptr; // 电量标签 - UIBoxProgress *progress{nullptr}; // 电池进度条 - UIImageView *eleicon = nullptr; // 电池框图标 - UIButton *button_exit = nullptr; // 退出按键 + + UILabel *timelabel = nullptr; // 时间 + UILabel *datelabel = nullptr; // 日期标签 + UILabel *weeklabel = nullptr; // 星期标签 + + UILabel *batterylabel = nullptr; // 电量标签 + UIBoxProgress *batteryprogress{nullptr}; // 电池进度条 + UIImageView *batteryiamge = nullptr; // 电池框图标 + + UIButton *button_exit = nullptr; // 退出按键 }; } // namespace OHOS diff --git a/code/sdk/application/wearable/fd230app/nativeui/saveele/src/SaveBatPresenter.cpp b/code/sdk/application/wearable/fd230app/nativeui/saveele/src/SaveBatPresenter.cpp index 13aa264dc..c6bdb4609 100644 --- a/code/sdk/application/wearable/fd230app/nativeui/saveele/src/SaveBatPresenter.cpp +++ b/code/sdk/application/wearable/fd230app/nativeui/saveele/src/SaveBatPresenter.cpp @@ -1,3 +1,9 @@ +/*** + * @Date: 2024-04-15 10:55:58 + * @LastEditors: Caisking 3449750381@qq.com + * @LastEditTime: 2024-08-17 11:49:58 + * @FilePath: /FD230/code/sdk/application/wearable/fd230app/nativeui/saveele/src/SaveBatPresenter.cpp + */ #include "AppViewIDs.h" #include "components/ui_label_ext.h" @@ -63,6 +69,7 @@ namespace OHOS bool SaveBatPresenter::OnClick(UIView &view, const ClickEvent &event) { + return true; } bool SaveBatPresenter::OnLongPress(UIView &view, const LongPressEvent &event) diff --git a/code/sdk/application/wearable/fd230app/nativeui/saveele/src/SaveBatView.cpp b/code/sdk/application/wearable/fd230app/nativeui/saveele/src/SaveBatView.cpp index 229ca204a..bf4bb482a 100644 --- a/code/sdk/application/wearable/fd230app/nativeui/saveele/src/SaveBatView.cpp +++ b/code/sdk/application/wearable/fd230app/nativeui/saveele/src/SaveBatView.cpp @@ -1,17 +1,4 @@ -/* - * Copyright (c) 2023 CompanyNameMagicTag. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ + #include "saveele/include/SaveBatView.h" @@ -25,21 +12,23 @@ #include "battery_hal.h" #include "string" #include "custom_utils.h" - +#include "UiConfig.h" +#include "wearable_log.h" extern "C" { void uapi_exit_power_saving_mode(void); void uapi_enter_power_saving_mode(void); } +std::string Cnweekday[] = {"周天", "周一", "周二", "周三", "周四", "周五", "周六"}; + +static constexpr const char *WHITE_BATTERY = "/user/res/Battery/Battery-E.bin"; +static constexpr const char *SAVE_BUTTON = "/user/res/Save_Ele/finger.bin"; + namespace OHOS { static SaveBatView *g_pSaveBatView{nullptr}; - std::string WeekString(int wday) - { - std::string weekDays[] = {"星期天", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"}; - return weekDays[wday]; - } + SaveBatView::SaveBatView() { /*开启省电模式*/ @@ -51,249 +40,164 @@ namespace OHOS { /*关闭省电模式*/ uapi_exit_power_saving_mode(); - DeInit(); + + UIViewGroup *tmpGroup = dynamic_cast(container_); + UIViewGroup::RemoveAndDeleteAllRecursively(tmpGroup); + g_pSaveBatView = nullptr; } SaveBatView *SaveBatView::GetInstance() { - return g_pSaveBatView; } + + /* 更新主界面数据 */ void SaveBatView::RefreshView() { + /* + 日期样式 04-22 周五 + 时间样式 09:10 + */ + // 获取当前时间 - struct tm pTM; time_t seconds; time(&seconds); + struct tm pTM; localtime_r(&seconds, &pTM); // 解析时间 int hour = pTM.tm_hour; int minute = pTM.tm_min; + int day = pTM.tm_mday; + int month = pTM.tm_mon + 1; // 月份范围 [1, 12] + std::string weekStr = Cnweekday[pTM.tm_wday]; - // 构建日期字符串 - std::string yearStr = std::to_string(pTM.tm_year + 1900); - std::string monthStr = (pTM.tm_mon + 1 < 10) ? "0" + std::to_string(pTM.tm_mon + 1) : std::to_string(pTM.tm_mon + 1); - std::string weekStr = WeekString(pTM.tm_wday); - std::string dayStr = (pTM.tm_mday < 10) ? "0" + std::to_string(pTM.tm_mday) : std::to_string(pTM.tm_mday); - std::string dateStr = monthStr + "月" + dayStr + "日"; + // 构建日期和时间字符串 + std::string dateStr = + (month < 10 ? "0" : "") + std::to_string(month) + "-" + + (day < 10 ? "0" : "") + std::to_string(day)+" "+weekStr; - // 将时间和日期转换为字符串 - std::string stringtime = (hour < 10 ? "0" : "") + std::to_string(hour) + ":" + (minute < 10 ? "0" : "") + std::to_string(minute); + std::string stringtime = + (hour < 10 ? "0" : "") + std::to_string(hour) + ":" + + (minute < 10 ? "0" : "") + std::to_string(minute); // 获取电量值 float voltage = 0.0F; - uint8_t value = 0; - value = battery_get_voltage(&voltage); + uint8_t value = battery_get_voltage(&voltage); // 更新界面组件 timelabel->SetText(stringtime.c_str()); datelabel->SetText(dateStr.c_str()); - weeklabel->SetText(weekStr.c_str()); - elelabel->SetText((std::to_string(value) + "%").c_str()); - progress->SetValue(value); + batterylabel->SetText((std::to_string(value) + "%").c_str()); + batteryprogress->SetValue(value); } - - void SaveBatView::Init() + void SaveBatView::InitBatteryView() { - if (container_ == nullptr) + // 创建电池框图像 + batteryiamge = new UIImageView(); + if (!batteryiamge) { - container_ = new UIViewGroup(); - container_->SetPosition(0, 0, 466, 466); - container_->SetStyle(STYLE_BACKGROUND_OPA, 0); - } - /* --------------------------------- 电池标签初始化 -------------------------------- */ - if (elelabel == nullptr) - { - elelabel = new UILabel(); + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "batteryiamge creation failed"); + return; } - elelabel->SetPosition(239, 52); // widget's left position and top position - elelabel->Resize(200, 60); // widget's width and height - elelabel->SetViewId("label1"); // Set style - elelabel->SetStyle(STYLE_TEXT_FONT, 27); - elelabel->SetText("10%"); - elelabel->SetLineBreakMode(UILabel::LINE_BREAK_ADAPT); - elelabel->SetAlign(TEXT_ALIGNMENT_CENTER, TEXT_ALIGNMENT_CENTER); - elelabel->SetRollSpeed(0); - elelabel->SetFont(DEFAULT_VECTOR_FONT_FILENAME, 27); - container_->Add(elelabel); - elelabel->SetVisible(true); - /* -------------------------------- 电池框图标初始化 -------------------------------- */ - if (eleicon == nullptr) + + // 创建电池进度条 + batteryprogress = new UIBoxProgress(); + if (!batteryprogress) { - eleicon = new UIImageView(); + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "batteryprogress creation failed"); + return; } - eleicon->SetPosition(179, 52); // widget's left position and top position - eleicon->Resize(48, 30); // widget's width and height - eleicon->SetViewId("image7"); // Set style - // Set additional property - char *image7imagesPath = "/user/res/Save_Ele/Battery-E.bin"; - eleicon->SetSrc(image7imagesPath); - // Add to rootView - container_->Add(eleicon); - eleicon->SetVisible(true); - /* -------------------------------- 电池进度条初始化 -------------------------------- */ - // Add BoxProgress to App - if (progress == nullptr) + + // 创建电池标签 + batterylabel = new UILabel(); + if (!batterylabel) { - progress = new UIBoxProgress(); + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "batterylabel creation failed"); + return; } - progress->SetPosition(184, 58); // widget's left position and top position - progress->Resize(31, 19); // widget's width and height - progress->SetViewId("progress1"); // Set style - progress->SetValidHeight(19); - progress->SetValidWidth(31); - progress->SetValue(20); - // Set BackgroundStyle - progress->SetBackgroundStyle(STYLE_BACKGROUND_OPA, 0x00); - progress->SetBackgroundStyle(STYLE_TEXT_COLOR, 0xff000000); - // Set ForegroundStyle - progress->SetForegroundStyle(STYLE_BACKGROUND_COLOR, 0xff2195F6); - progress->SetForegroundStyle(STYLE_TEXT_COLOR, 0xff000000); - progress->SetForegroundStyle(STYLE_LINE_COLOR, 0xff45A5FF); - char *progress1ForeImageSrc = "/user/res/Battery/Battery_Nomoal.bin"; - char *progress1BackImageSrc = nullptr; - progress->SetImage(progress1ForeImageSrc, progress1BackImageSrc); - // Add to rootView - container_->Add(progress); - /* --------------------------------- 时间标签初始化 -------------------------------- */ - // Add Label to App + + // 设置位置 + batteryiamge->SetPosition(173, 52, 100, 100); + batteryprogress->SetPosition(179, 59, 30, 18); + batteryprogress->SetViewId("batteryprogress"); + batteryprogress->SetStyle(STYLE_BACKGROUND_OPA, 0xff); + batterylabel->SetPosition(228, 54, 200, 60); + batterylabel->SetFont(DEFAULT_VECTOR_FONT_FILENAME, 26); + + batteryiamge->SetSrc(WHITE_BATTERY); + batterylabel->SetTextColor(Color::White()); + batteryprogress->SetForegroundStyle(STYLE_BACKGROUND_COLOR, 0xffffc300); + + container_->Add(batteryiamge); + container_->Add(batterylabel); + container_->Add(batteryprogress); + } + void SaveBatView::InitTimeView() + { + /*时间time*/ + timelabel = new UILabel(); if (timelabel == nullptr) { - timelabel = new UILabel(); + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "timelabel creation failed"); + return; } - timelabel->SetPosition(118, 148); // widget's left position and top position - timelabel->Resize(231, 115); // widget's width and height - timelabel->SetViewId("label2"); // Set style + timelabel->SetPosition(0, 148); + timelabel->Resize(466, 100); + timelabel->SetViewId("label1"); timelabel->SetStyle(STYLE_TEXT_FONT, 100); - timelabel->SetStyle(STYLE_LINE_HEIGHT, 115); - // Set additional property - timelabel->SetText("23:59"); - timelabel->SetLineBreakMode(UILabel::LINE_BREAK_ADAPT); - timelabel->SetTextColor(Color::GetColorFromRGBA(0x4f, 0xc3, 0x4c, 0xff)); + timelabel->SetText("11:00"); + timelabel->SetLineBreakMode(UILabel::LINE_BREAK_ELLIPSIS); + timelabel->SetTextColor(Color::GetColorFromRGBA(0x49, 0xef, 0x13, 0xff)); timelabel->SetAlign(TEXT_ALIGNMENT_CENTER, TEXT_ALIGNMENT_CENTER); timelabel->SetRollSpeed(0); timelabel->SetFont(DEFAULT_VECTOR_FONT_FILENAME, 100); - // Add to rootView container_->Add(timelabel); - timelabel->SetVisible(true); - /* --------------------------------- 日期标签初始化 -------------------------------- */ - // Add Label to App + + /*日期*/ + datelabel = new UILabel(); if (datelabel == nullptr) { - datelabel = new UILabel(); + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "datelabel creation failed"); + return; } - datelabel->SetPosition(125, 268); // widget's left position and top position - datelabel->Resize(210, 35); // widget's width and height - datelabel->SetViewId("label3"); // Set style + datelabel->SetPosition(83, 258); + datelabel->Resize(300, 60); + datelabel->SetViewId("label2"); datelabel->SetStyle(STYLE_TEXT_FONT, 30); - datelabel->SetStyle(STYLE_LETTER_SPACE, 2); - datelabel->SetStyle(STYLE_LINE_HEIGHT, 35); - // Set additional property - datelabel->SetText("11月3号"); - datelabel->SetLineBreakMode(UILabel::LINE_BREAK_ADAPT); + + datelabel->SetText("04-22 周五"); + datelabel->SetLineBreakMode(UILabel::LINE_BREAK_ELLIPSIS); datelabel->SetTextColor(Color::GetColorFromRGBA(0x66, 0x66, 0x66, 0xff)); datelabel->SetAlign(TEXT_ALIGNMENT_CENTER, TEXT_ALIGNMENT_CENTER); datelabel->SetRollSpeed(0); datelabel->SetFont(DEFAULT_VECTOR_FONT_FILENAME, 30); - // Add to rootView container_->Add(datelabel); - datelabel->SetVisible(true); + } - /* --------------------------------- 星期标签初始化 -------------------------------- */ - if (weeklabel == nullptr) - { - weeklabel = new UILabel(); - } - weeklabel->SetPosition(280, 268); // widget's left position and top position - weeklabel->Resize(280, 35); // widget's width and height - weeklabel->SetViewId("label3"); // Set style - weeklabel->SetStyle(STYLE_TEXT_FONT, 30); - weeklabel->SetStyle(STYLE_LETTER_SPACE, 2); - weeklabel->SetStyle(STYLE_LINE_HEIGHT, 35); - - // Set additional property - weeklabel->SetText("星期五"); - weeklabel->SetLineBreakMode(UILabel::LINE_BREAK_ADAPT); - weeklabel->SetTextColor(Color::GetColorFromRGBA(0x66, 0x66, 0x66, 0xff)); - weeklabel->SetAlign(TEXT_ALIGNMENT_CENTER, TEXT_ALIGNMENT_CENTER); - weeklabel->SetRollSpeed(0); - weeklabel->SetFont(DEFAULT_VECTOR_FONT_FILENAME, 30); - // Add to rootView - container_->Add(weeklabel); - weeklabel->SetVisible(true); - - /* --------------------------------- 退出按键初始化 -------------------------------- */ + void SaveBatView::InitButtonView() + { + // 初始化按键 + button_exit = new UIButton(); if (button_exit == nullptr) { - button_exit = new UIButton(); + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "button_exit creation failed"); + return; } - button_exit->SetPosition(204, 350); // widget's left position and top position - button_exit->Resize(100, 100); // widget's width and height - button_exit->SetViewId("button_exit"); // Set style + button_exit->SetPosition(204, 350); + button_exit->Resize(100, 100); + button_exit->SetViewId("button_exit"); button_exit->SetStyle(STYLE_BACKGROUND_COLOR, 0xff000000); button_exit->SetStyle(STYLE_BACKGROUND_OPA, 0x00); - // Set additional property - char *button1defaultImgSrc = "/user/res/Save_Ele/finger.bin"; - char *button1triggeredImgSrc = nullptr; - button_exit->SetImageSrc(button1defaultImgSrc, button1defaultImgSrc); + button_exit->SetImageSrc(SAVE_BUTTON, SAVE_BUTTON); button_exit->SetStyleForState(STYLE_BACKGROUND_COLOR, 0xff000000, UIButton::PRESSED); - // Add the CallBack about events - UIView::OnLongPressListener *button1LongPress = static_cast(SaveBatPresenter::GetInstance()); - button_exit->SetOnLongPressListener(button1LongPress); - // Add to rootView + button_exit->SetOnLongPressListener(presenter_); container_->Add(button_exit); - button_exit->SetVisible(true); - } - - void SaveBatView::DeInit() - { - if (container_ != nullptr) - { - container_->RemoveAll(); - delete container_; - container_ = nullptr; - } - if (elelabel != nullptr) - { - delete elelabel; - elelabel = nullptr; - } - if (eleicon != nullptr) - { - delete eleicon; - eleicon = nullptr; - } - if (weeklabel != nullptr) - { - delete weeklabel; - weeklabel = nullptr; - } - if (timelabel != nullptr) - { - delete timelabel; - timelabel = nullptr; - } - if (datelabel != nullptr) - { - delete datelabel; - datelabel = nullptr; - } - if (button_exit != nullptr) - { - delete button_exit; - button_exit = nullptr; - } } - UIView *SaveBatView::GetView() - { - Init(); - return container_; - } void SaveBatView::OnStart() { container_ = new UIViewGroup(); @@ -307,7 +211,10 @@ namespace OHOS container_->SetTouchable(true); container_->SetDraggable(true); - GetView(); + InitBatteryView(); + InitTimeView(); + InitButtonView(); + RootView::GetInstance()->Add(container_); } @@ -328,8 +235,12 @@ namespace OHOS void SaveBatView::OnStop() { View::OnStop(); - RootView::GetInstance()->RemoveAll(); - + if (container_ != nullptr) + { + container_->RemoveAll(); + delete container_; + container_ = nullptr; + } return; } } \ No newline at end of file -- 2.17.1