2 * @Date: 2024-07-06 16:27:31
3 * @LastEditors: Caisking 3449750381@qq.com
4 * @LastEditTime: 2024-08-02 10:21:14
6 * /FD230/code/sdk/application/wearable/fd230app/nativeui/traing/training_card/src/TraingCardPresenter.cpp
9 * @Date: 2024-04-26 09:23:18
10 * @LastEditors: Caisking 3449750381@qq.com
11 * @LastEditTime: 2024-04-26 10:25:05
13 * /XTS_FD230/FD230/code/sdk/application/wearable/fd230app/nativeui/traing/training_card/src/TraingCardPresenter.cpp
16 #include "AppViewIDs.h"
17 #include "NativeAbility.h"
18 #include "AbilityNavigationApi.h"
19 #include "components/ui_label_ext.h"
20 #include "ui_resource_string.h"
22 #include "ConfigModel.h"
23 #include "custom_utils.h"
24 #include "graphic_service.h"
25 #include "traing/include/TraingModel.h"
26 #include "traing/training_card/include/TraingCardPresenter.h"
27 #include "traing/training_card/include/TrainingControlView.h"
28 #include "uicomponents/UIComponents.h"
29 #include "vibrator_service.h"
37 #include <unordered_map>
38 #include "custom_runtime.h"
40 extern "C" void do_cywee_service_check_msg(int cmd, int data[]);
42 #define MAXSPORTID 176
44 #define DEFAULT_DASH "--"
49 void StopAndResetTimer();
53 #include "NativeRegisterManager.h"
55 #ifdef SUPPORT_CYWEE_SERVICE
56 #include "cywee_service.h"
60 REGIST_SLICE(VIEW_TRAININGCRAD, TraingCardView, TraingCardPresenter);
62 // 当前Presenter属性实例化的内存指针
63 static TraingCardPresenter *g_pTraingCardPresenter = nullptr;
65 TraingCardPresenter::TraingCardPresenter() { g_pTraingCardPresenter = this; }
67 TraingCardPresenter::~TraingCardPresenter() {
69 g_pTraingCardPresenter = nullptr;
72 TraingCardPresenter *TraingCardPresenter::GetInstance() {
73 static TraingCardPresenter instance;
77 void TraingCardPresenter::OnStart() {
78 Presenter<TraingCardView>::OnStart();
79 // ScreenAlwaysOnCtr(OPEN_SCREEN_ALWAYS_ON); // 长亮屏
82 void TraingCardPresenter::OnPause(void) {
84 Presenter<TraingCardView>::OnPause();
87 void TraingCardPresenter::OnResume(void) {
89 Presenter<TraingCardView>::OnResume();
90 RootView::GetInstance()->SetOnKeyActListener(this);
93 void TraingCardPresenter::OnStop(void) {
95 Presenter<TraingCardView>::OnStop();
96 // ScreenAlwaysOnCtr(CANCEL_SCREEN_ALWAYS_ON); // 长亮屏
99 bool TraingCardPresenter::OnDragStart(UIView &view, const DragEvent &event) {
107 bool TraingCardPresenter::OnDrag(UIView &view, const DragEvent &event) {
109 // 拖动过程中判断是否是垂直滑动:垂直距离大于阈值,且垂直距离大于水平距离的2倍
110 if ((event.GetDeltaY() > X_DRAG_OFFSET || event.GetDeltaY() < -X_DRAG_OFFSET) &&
111 (MATH_ABS(event.GetDeltaY()) > (DOUBLES * MATH_ABS(event.GetDeltaX())))) {
112 Y_DRAG_FLAG = true; // 标记为垂直滑动
113 WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "Vertical drag detected (deltaY=%d, deltaX=%d), blocking horizontal swipe",
114 event.GetDeltaY(), event.GetDeltaX());
119 bool TraingCardPresenter::OnDragEnd(UIView &view, const DragEvent &event) {
123 WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "Vertical drag ended, blocking horizontal swipe");
133 void TraingCardPresenter::ModifiesTime() {
134 WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "ModifiesTime begin");
135 std ::string TimeStr = TraingModel::GetInstance()->DisplayMotionDuration();
138 if (uapi_get_screen_state() == 1) {
139 WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "uapi_get_screen_state is off");
143 if (TimeStr.empty()) {
145 WEARABLE_LOG_MODULE_APP,
146 "TraingModel::GetInstance()->DisplayMotionDuration() nullptr");
149 if (TrainingControlView::GetInstance() != nullptr &&
150 TrainingDataView::GetInstance() != nullptr) {
151 if (TrainingControlView::GetInstance()->GetTimeLabel() != nullptr) {
152 TrainingControlView::GetInstance()->GetTimeLabel()->SetText(
155 if (TrainingDataView::GetInstance()->GetTimeLabel() != nullptr) {
156 TrainingDataView::GetInstance()->GetTimeLabel()->SetText(TimeStr.c_str());
158 WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "ModifiesTime end");
163 void TraingCardPresenter::UpdateTimeOfControl() {
164 std ::string TimeStr = TraingModel::GetInstance()->DisplayMotionDuration();
165 if (TimeStr.empty()) {
167 WEARABLE_LOG_MODULE_APP,
168 "TraingModel::GetInstance()->DisplayMotionDuration() nullptr");
171 if (TrainingControlView::GetInstance()->GetTimeLabel() != nullptr) {
172 TrainingControlView::GetInstance()->GetTimeLabel()->SetText(
176 bool isNeedGPS = TraingModel::GetInstance()->IsNeedGPS(TraingModel::GetInstance()->GetSportEvent());
177 if(isNeedGPS == false)
179 WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "该运动项不需要GPS");
183 if (TrainingControlView::GetInstance()->GetGpsStatusLabel() != nullptr) {
185 bool gpsStatus = TraingModel::GetInstance()->GetGPSConnectState();
186 std::string gpsStatusStr;
188 if(ConfigModel::GetInstance()->GetLanguage() == 0)
189 gpsStatusStr = "GPS定位成功";
191 gpsStatusStr = "GPS Success";
192 TrainingControlView::GetInstance()->GetGpsStatusLabel()->SetTextColor(
195 if(ConfigModel::GetInstance()->GetLanguage() == 0)
196 gpsStatusStr = "GPS未定位成功";
198 gpsStatusStr = "GPS not found";
199 TrainingControlView::GetInstance()->GetGpsStatusLabel()->SetTextColor(
202 TrainingControlView::GetInstance()->GetGpsStatusLabel()->SetText(
203 gpsStatusStr.c_str());
206 void TraingCardPresenter::UpdateTimeOfData() {
207 std ::string TimeStr = TraingModel::GetInstance()->DisplayMotionDuration();
208 if (TimeStr.empty()) {
210 WEARABLE_LOG_MODULE_APP,
211 "TraingModel::GetInstance()->DisplayMotionDuration() nullptr");
214 if (TrainingDataView::GetInstance()->GetTimeLabel() != nullptr) {
215 TrainingDataView::GetInstance()->GetTimeLabel()->SetText(TimeStr.c_str());
219 /* --------------------------------- cywee库操作
220 * -------------------------------- */
222 bool TraingCardPresenter::OpenSensor(ExerciseClass type) {
224 if (opensensorstuats) {
225 WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "OpenSensor is opening!!");
229 WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "TraingCardPresenter::OpenSensor!!");
230 #ifdef SUPPORT_CYWEE_SERVICE
232 case ExerciseClass::NORMAL:
233 cywee_sport_mode_change(CYWEE_SERVICE_CMD_NORMAL_MODE);
235 case ExerciseClass::TREADMILL:
236 cywee_sport_mode_change(CYWEE_SERVICE_CMD_TREADMILL_MODE);
238 case ExerciseClass::OUTDOOR:
239 cywee_sport_mode_change(CYWEE_SERVICE_CMD_OUTDOOR_RUNNING_MODE);
241 case ExerciseClass::INDOOR:
242 cywee_sport_mode_change(CYWEE_SERVICE_CMD_INDOOR_RUNNING_MODE);
244 case ExerciseClass::FITNESS_PEDOMETER:
245 cywee_sport_mode_change(CYWEE_SERVICE_CMD_FITNESS_PEDOMETER_MODE);
247 case ExerciseClass::FREE_TRAINING:
248 cywee_sport_mode_change(CYWEE_SERVICE_CMD_FREE_TRAINING_MODE);
254 opensensorstuats = true;
255 WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "OpenSensor--%d!!", type);
260 bool TraingCardPresenter::ControlSensor(ExerciseState state) {
261 #ifdef SUPPORT_PPG_SERVICE
263 case ExerciseState::End:
265 do_cywee_service_check_msg(CYWEE_SERVICE_CMD_NORMAL_MODE, NULL);
266 opensensorstuats = false; // 恢复标志位
267 StopAndResetTimer(); // 关闭计时器
269 case ExerciseState::Pause:
270 do_cywee_service_check_msg(CYWEE_SERVICE_CMD_PAUSE, 0);
272 case ExerciseState::Continue:
273 do_cywee_service_check_msg(CYWEE_SERVICE_CMD_RESUME, 0);
279 WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "ControlSensor[%d]!!", state);
283 void TraingCardPresenter::RefreshData(SportData *sportdata) {
284 if (sportdata == nullptr) {
285 WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "sportdata is null");
289 if (TrainingDetailView::GetInstance() == nullptr ||
290 TrainingDataView::GetInstance() == nullptr)
293 std::string distanceStr;
294 if (sportdata->distance != 0) {
296 sportdata->distance / 1000.0; // 转换为公里并保留两位小数
297 std::stringstream ss;
298 ss << std::fixed << std::setprecision(2) << distance_km;
299 distanceStr = ss.str();
301 distanceStr = DEFAULT_DASH;
304 std::string heartStr =
305 (sportdata->heart != 0) ? std::to_string(sportdata->heart) : DEFAULT_DASH;
306 // 配速 pc 单位为秒/公里,显示为 分钟'秒"(如 5'30");慢于 60 分钟/公里时显示 --,避免异常递增
307 const int PACE_MAX_SEC_PER_KM = 60 * 60; // 60 分钟/公里 = 3600 秒/公里
309 if (sportdata->pc > 0 && sportdata->pc <= PACE_MAX_SEC_PER_KM) {
310 int min = sportdata->pc / 60;
311 int sec = sportdata->pc % 60;
312 std::stringstream ss;
313 ss << min << "'" << std::setfill('0') << std::setw(2) << sec << "\"";
316 paceStr = DEFAULT_DASH;
318 std::string calorieStr = (sportdata->calories != 0)
319 ? std::to_string(sportdata->calories)
322 UIResourceTextId intensityid = TraingModel::GetInstance()->GetIntensity();
323 TrainingDetailView::GetInstance()->Get_Intensity_Button()->SetTextId(
326 TrainingDataView::GetInstance()->GetProgress()->SetValue(
327 sportdata->goaldegree); // 目标完成度
328 TrainingDataView::GetInstance()->GetHeartLabel()->SetText(
329 heartStr.c_str()); // 心率
330 TrainingDetailView::GetInstance()->GetCalorieLabel()->SetText(
331 calorieStr.c_str()); // 卡路里
332 TrainingDetailView::GetInstance()->GetMileLabel()->SetText(
333 distanceStr.c_str()); // 距离
334 TrainingDetailView::GetInstance()->GetStepLabel()->SetText(
335 paceStr.c_str()); // 配速(原步数位置)
337 WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP,
338 "[Calories: %s, Distance: %s, Pace: %s Intensityid: %d]",
339 calorieStr.c_str(), distanceStr.c_str(), paceStr.c_str(),
345 void TraingCardPresenter::updataview(void) {
347 if (TraingModel::GetInstance()->GetSportState() == ExerciseState::General) {
348 WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP,
349 "TraingModel::GetInstance()->GetSportState() == "
350 "ExerciseState::General ");
354 if (TraingCardView::GetInstance() == nullptr) {
355 WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "view_ == nullptr");
359 Control_Card cardId = static_cast<Control_Card>(
360 TraingCardView::GetInstance()->GetCurrentPage());
361 WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "cardId %d", (int)cardId);
364 case Control_Card::CONTROL: {
365 UpdateTimeOfControl(); // 更新控制界面的时间
368 case Control_Card::DATA: {
369 UpdateDataView(); // 更新训练数据视图
377 void TraingCardPresenter::Notify(void) {
379 if (TraingModel::GetInstance()->GetSportState() == ExerciseState::General ||
384 Control_Card cardId = static_cast<Control_Card>(view_->GetCurrentPage());
387 case Control_Card::CONTROL: {
388 UpdateTimeOfControl(); // 更新控制界面的时间
391 case Control_Card::DATA: {
392 UpdateDataView(); // 更新训练数据视图
400 void TraingCardPresenter::UpdateDataView() {
405 GraphicService::GetInstance()->PostGraphicEvent(std::bind(
406 &TrainingDataView::UpdataTime, TrainingDataView::GetInstance()));
408 TraingModel::GetInstance()->updata_goal();
410 // 刷新训练数据:心率、卡路里、距离、配速、训练强度、目标完成度
411 SportData *sportdata = TraingModel::GetInstance()->GetSportdata();
412 RefreshData(sportdata);
414 /* --------------------------------- 点击事件处理
415 * --------------------------------- */
416 bool TraingCardPresenter::OnClick(UIView &view, const ClickEvent &event) {
417 const std::string viewId = view.GetViewId();
420 static const std::unordered_map<std::string, std::function<void()>>
425 auto currentState = TraingModel::GetInstance()->GetSportState();
426 if (currentState == ExerciseState::Continue ||
427 currentState == ExerciseState::Start) {
429 } else if (currentState == ExerciseState::Pause) {
430 handleContinueState();
436 // ControlSensor(ExerciseState::Pause);
437 TraingCardView::GetInstance()->SetPopupViewStatus(EndPopup, true);
438 TraingCardView::GetInstance()->RefreshEndView();
441 {BUTTONEXIT, [&]() { handleEndState(); }},
445 // ControlSensor(ExerciseState::Continue);
446 TraingCardView::GetInstance()->SetPopupViewStatus(EndPopup, false);
451 TraingCardView::GetInstance()->GetAnimator()->Stop();
452 TraingCardView::GetInstance()->SetPopupViewStatus(GetTagPopup,
458 TraingCardView::GetInstance()->SetPopupViewStatus(HeartPopup,
464 auto it = actionMap.find(viewId);
465 if (it != actionMap.end()) {
466 it->second(); // 执行操作
470 return false; // 没有匹配的操作
472 void TraingCardPresenter::OnSwipe(UISwipeView &view) {
473 WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "TempPresenter::OnSwipe ");
477 bool TraingCardPresenter::OnKeyAct(UIView &view, const KeyEvent &event) {
481 /* -------------------------------- 协议发起运动处理
482 * -------------------------------- */
483 #ifdef SUPPORT_BLE_SERVICE
484 bool TraingCardPresenter::send_sport_cmd(training_ble *cmd) {
485 if (cmd == nullptr) {
486 WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "无效的运动命令 cmd 为空!");
491 ExerciseState currentState = TraingModel::GetInstance()->GetSportState();
492 ExerciseState cmdstate = static_cast<ExerciseState>(cmd->controlmd);
493 WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "cmdstate:%d currentState:%d",
494 cmdstate, currentState);
496 switch (cmd->controlmd) {
499 if (currentState == ExerciseState::General) {
500 if (cmd->sportTagType == 0) {
502 m_sportTag = cmd->sportTag / 1000;
504 m_sportTag = cmd->sportTag;
506 TraingModel::GetInstance()->SetSportTag(
507 static_cast<ExerciseTag>(cmd->sportTagType), m_sportTag);
508 WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "目标设置成功!\r\n");
515 TraingModel::GetInstance()->SendLiveData();
516 WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "实时数据发送成功!\r\n");
524 if (cmdstate == ExerciseState::Start) {
525 if ((currentState == ExerciseState::General)) {
526 if (TraingCardView::GetInstance() == nullptr) {
527 WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "ble 开启运动");
529 switch (cmd->sportType) {
531 TraingModel::GetInstance()->SetSportEvent(STR_EXERCISE_OUTDOORWALKING);
532 OpenSensor(ExerciseClass::OUTDOOR);
535 TraingModel::GetInstance()->SetSportEvent(STR_EXERCISE_OUTDOORRUNNING);
536 OpenSensor(ExerciseClass::OUTDOOR);
539 TraingModel::GetInstance()->SetSportEvent(STR_EXERCISE_OUTDOORBIKING);
540 OpenSensor(ExerciseClass::FREE_TRAINING);
546 reply_APP_OpenSportModeCmd(true);
548 WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP,"*************app开启运动******************");
549 WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP,"sportType:%d",cmd->sportType);
550 WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP,"sportTagType:%d",cmd->sportTagType);
551 WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP,"sportTag:%d",cmd->sportTag);
552 WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP,"*******************************");
554 uapi_set_screen_on_api(); // 亮屏
555 NativeAbility::GetInstance().ChangeSlice(VIEW_TRAININGCRAD);
559 reply_APP_OpenSportModeCmd(false);
564 if (cmdstate == ExerciseState::Pause) {
565 if (currentState == ExerciseState::Start ||
566 currentState == ExerciseState::Continue) {
567 WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "ble 暂停运动");
568 reply_APP_SportPasueResetCmd(TraingModel::GetInstance()->GetSportTime());
575 if (cmdstate == ExerciseState::Continue &&
576 currentState == ExerciseState::Pause) {
577 WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "ble 恢复运动");
578 reply_APP_SportPasueResetCmd(TraingModel::GetInstance()->GetSportTime());
579 handleContinueState();
583 // 仅在运动状态为开始、继续或暂停时允许结束运动
584 if (cmdstate == ExerciseState::End &&
585 (currentState == ExerciseState::Start ||
586 currentState == ExerciseState::Continue ||
587 currentState == ExerciseState::Pause)) {
588 WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "ble 结束运动");
589 uint32_t sport_time = TraingModel::GetInstance()->GetSportTime();
590 reply_APP_SportStopModeCmd(sport_time);
596 WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP,
597 "cmd->sportType[%d],cmd->controlmd[%d],cmd->sportTagType[%d],"
598 "cmd->sportTag[%d]\r\n",
599 cmd->sportType, cmd->controlmd, cmd->sportTagType,
602 WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP,
603 "由于状态限制,命令未执行 当前状态 %d。\r\n", currentState);
604 return false; // 没有执行任何命令
608 void TraingCardPresenter::handleEndState() {
609 TraingModel::GetInstance()->SetSportState(ExerciseState::End);
610 ControlSensor(ExerciseState::End);
611 uapi_set_screen_on_api(); // 亮屏
613 // NativeAbility::GetInstance().ChangeSlice(VIEW_TRAININGENDDATA);
615 NativeAbility::GetInstance().ChangeSlice(VIEW_TRAININGNOMAL);
619 if (TraingModel::GetInstance()->GetMomentum()) {
621 NativeAbility::GetInstance().ChangeSlice(VIEW_TRAININGENDDATA);
622 OHOS::AbilityNavigation::ClearSliceIdStack(); // 清空所有SLiceId栈元素
623 uapi_dev_runtime_set_train_status_is_end_flag(1);
626 // NativeAbility::GetInstance().ChangeSlice(VIEW_TRAINING_RECORD);
627 if (TraingModel::GetInstance()->GetEntryType() == APPLIST_ENTER)
628 NativeAbility::GetInstance().ChangeSlice(
629 VIEW_TRAINING, TransitionType::TRANSITION_BACK_TO_HEXAGONS);
631 NativeAbility::GetInstance().ChangeSlice(
632 VIEW_MAIN, TransitionType::TRANSITION_BACK_TO_HEXAGONS);
633 OHOS::AbilityNavigation::ClearSliceIdStack(); // 清空所有SLiceId栈元素
635 OHOS::AbilityNavigation::ClearSliceIdStack(); // 清空所有SLiceId栈元素
640 void TraingCardPresenter::handlePauseState() {
641 uapi_set_screen_on_api(); // 亮屏
642 TrainingControlView::GetInstance()->GetLabel()->SetTextId(STR_RESUME);
643 TrainingControlView::GetInstance()->GetButton()->SetImageSrc(
644 SPORT_CONTINUE_IMAGE, SPORT_CONTINUE_IMAGE);
645 TraingModel::GetInstance()->SetSportState(ExerciseState::Pause);
647 ControlSensor(ExerciseState::Pause);
651 void TraingCardPresenter::handleContinueState() {
652 uapi_set_screen_on_api(); // 亮屏
653 TrainingControlView::GetInstance()->GetLabel()->SetTextId(STR_PAUSE);
654 TrainingControlView::GetInstance()->GetButton()->SetImageSrc(
655 SPORT_PAUSE_IMAGE, SPORT_PAUSE_IMAGE);
656 TraingModel::GetInstance()->SetSportState(ExerciseState::Continue);
658 ControlSensor(ExerciseState::Continue);
662 void TraingCardPresenter::handleStartState() {
663 ControlSensor(ExerciseState::Start);
664 TraingModel::GetInstance()->SetSportOpenWay(true);
666 TraingModel::GetInstance()->start_training_heart();
667 #if defined(SUPPORT_GNSS)
668 WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "开启gps定时器,频率:2秒/点");
669 TraingModel::GetInstance()->gnss_start_timer(2 * 1000); // 2秒一个GPS点
672 TraingModel::GetInstance()->SetSportType(Exercisetype::FITNESS);