优化星历下载交互UI 2453/1 2453/head 2454/target/1
authorCaisking <Casiking@zhihuishixun.com>
Tue, 30 Sep 2025 08:09:29 +0000 (16:09 +0800)
committerCaisking <Casiking@zhihuishixun.com>
Tue, 30 Sep 2025 08:09:29 +0000 (16:09 +0800)
code/sdk/application/wearable/n9/nativeapp/nativeui/ephemeris_download/include/EphemerisDownloadModel.h
code/sdk/application/wearable/n9/nativeapp/nativeui/ephemeris_download/include/EphemerisDownloadPresenter.h
code/sdk/application/wearable/n9/nativeapp/nativeui/ephemeris_download/src/EphemerisDownloadModel.cpp
code/sdk/application/wearable/n9/nativeapp/nativeui/ephemeris_download/src/EphemerisDownloadPresenter.cpp
code/sdk/application/wearable/n9/nativeapp/nativeui/ephemeris_download/src/EphemerisDownloadView.cpp

index 702f61e..5e10012 100644 (file)
@@ -39,6 +39,9 @@ namespace OHOS
         //获取有效期
         bool GetValidityPeriod();
 
+        // 定时器相关方法
+        void ephemeris_data_timer(uint32_t interval);
+        void ephemeris_stop_data_timer(void);
     private:
         
         // 星历相关状态
index 5e08d45..f498e7d 100644 (file)
@@ -1,21 +1,26 @@
 #ifndef EPHEMERIS_DOWNLOAD_PRESENTER_H
 #define EPHEMERIS_DOWNLOAD_PRESENTER_H
 
-#include "components/ui_view.h"
-#include "key/include/KeyInputListener.h"
 #include "Presenter.h"
+#include "View.h"
+#include "key/include/KeyInputListener.h"
 #include "ephemeris_download/include/EphemerisDownloadModel.h"
 #include "ephemeris_download/include/EphemerisDownloadView.h"
-#include "ModelListenerSample.h"
 
 namespace OHOS
 {
     class EphemerisDownloadModel;
     class EphemerisDownloadView;
+    
+    // Forward declarations for UI types
+    class UIView;
+    class RootView;
+    class ClickEvent;
+    class DragEvent;
+    class KeyEvent;
     class EphemerisDownloadPresenter : public Presenter<EphemerisDownloadView, EphemerisDownloadModel>,
                                       public UIView::OnClickListener,
                                       public RootView::OnKeyActListener,
-                                      public ModelListenerSample, 
                                       public UIView::OnDragListener
     {
     public:
@@ -24,10 +29,16 @@ namespace OHOS
         EphemerisDownloadPresenter(const EphemerisDownloadPresenter &) = delete;
         EphemerisDownloadPresenter &operator=(const EphemerisDownloadPresenter &) = delete;
         void OnResume() override;
-        void Notify() override;
+        void OnStop() override;
         bool OnClick(UIView &view, const ClickEvent &event) override;
         bool OnDrag(UIView &view, const DragEvent &event) override;
         bool OnKeyAct(UIView &view, const KeyEvent &event) override;
+        
+        // 静态实例获取方法
+        static EphemerisDownloadPresenter* GetInstance();
+        
+        // 供Model调用的回调函数
+        void UpdateDownloadStatus();
     };
 
 } // OHOS
index 68ef854..e67a43b 100644 (file)
@@ -1,9 +1,12 @@
 #include "ephemeris_download/include/EphemerisDownloadModel.h"
+#include "ephemeris_download/include/EphemerisDownloadPresenter.h"
 
 #include "custom_runtime.h"
 #include "custom_utils.h"
 
 #include "wearable_log.h"
+#include "cmsis_os.h"
+#include "service/graphic_service.h"
 
 #if defined(SUPPORT_GNSS)
 #include "gnss_http_server.h"
@@ -106,4 +109,64 @@ std::string EphemerisDownloadModel::GetEphemerisValidityPeriod() {
   return ephemerisValidityPeriod;
 }
 
+//////////////////////////////////////////////////////////////////////////////////
+#define EPHEMERIS_TIMER_DATA 1
+
+#if EPHEMERIS_TIMER_DATA
+// 声明定时器变量
+static osTimerId_t g_data_timer = nullptr;
+
+static void ephemeris_data_callbackProc(int data) {
+  //更新下载进度
+  if(OHOS::EphemerisDownloadPresenter::GetInstance()) {
+    OHOS::EphemerisDownloadPresenter::GetInstance()->UpdateDownloadStatus();
+  }
+}
+static void ephemeris_data_timer_callback(int data) {
+  GraphicService::GetInstance()->PostGraphicEvent(
+      std::bind(ephemeris_data_callbackProc, data));
+}
+void EphemerisDownloadModel::ephemeris_data_timer(uint32_t interval) {
+  WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP,
+                "EphemerisDownloadModel::ephemeris_data_timer");
+  if (g_data_timer != nullptr) {
+    WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "Timer already exists!!");
+    return;
+  }
+  g_data_timer = osTimerNew((osTimerFunc_t)ephemeris_data_timer_callback,
+                            osTimerPeriodic, nullptr, nullptr);
+  if (g_data_timer == nullptr) {
+    WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "Timer create failed!!");
+    return;
+  }
+  int tick = osMs2Tick(interval);
+  osStatus_t ret = osTimerStart(g_data_timer, tick);
+  if (ret != osOK) {
+    WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "osTimerDelete failed, ret = %lx!!",
+                  ret);
+    if (g_data_timer != nullptr) {
+      ret = osTimerDelete(g_data_timer);
+    }
+    g_data_timer = nullptr;
+    return;
+  }
+}
+void EphemerisDownloadModel::ephemeris_stop_data_timer(void) {
+  // 检查定时器是否存在
+  if (g_data_timer != nullptr) {
+    WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP,
+                  "EphemerisDownloadModel::ephemeris_stop_data_timer");
+    osTimerStop(g_data_timer);
+    // 删除定时器并置为 nullptr
+    osStatus_t ret = osTimerDelete(g_data_timer);
+    if (ret != osOK) {
+      WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP,
+                    "osTimerDelete failed, ret = %lx!!", ret);
+    }
+    g_data_timer = nullptr;
+  }
+}
+#endif
+
+
 } // namespace OHOS
index 7862d62..88ef07c 100644 (file)
 #include "NativeAbility.h"
 
 #include "NativeRegisterManager.h"
+#include "wearable_log.h"
 
 namespace OHOS {
 REGIST_SLICE(VIEW_EPHEMERIS_DOWNLOAD, EphemerisDownloadView,
              EphemerisDownloadPresenter);
 
+// 静态实例变量
+static EphemerisDownloadPresenter* g_instance = nullptr;
+
 EphemerisDownloadPresenter::EphemerisDownloadPresenter() {
+    g_instance = this;
 }
 
 EphemerisDownloadPresenter::~EphemerisDownloadPresenter() {}
 
 void EphemerisDownloadPresenter::OnResume() {
   RootView::GetInstance()->SetOnKeyActListener(this);
+  // 启动定时器
+  EphemerisDownloadModel::GetInstance().ephemeris_data_timer(1000); // 1秒间隔
+}
+
+void EphemerisDownloadPresenter::OnStop() {
+  // 停止定时器
+  EphemerisDownloadModel::GetInstance().ephemeris_stop_data_timer();
+}
+
+EphemerisDownloadPresenter* EphemerisDownloadPresenter::GetInstance() {
+  return g_instance;
 }
 
 bool EphemerisDownloadPresenter::OnClick(UIView &view,
@@ -63,7 +79,8 @@ bool EphemerisDownloadPresenter::OnKeyAct(UIView &view, const KeyEvent &event) {
   return true;
 }
 
-void EphemerisDownloadPresenter::Notify() {
+
+void EphemerisDownloadPresenter::UpdateDownloadStatus() {
   //根据下载状态更新视图
   switch (EphemerisDownloadModel::GetInstance().GetEphemerisDownloadStatus()) {
   case EphemerisDownloadStatus::DOWNLOADING:
@@ -115,14 +132,14 @@ void EphemerisDownloadPresenter::Notify() {
       }
     }
     break;
-  case EphemerisDownloadStatus::NOT_DOWNLOADED:
-    if (EphemerisDownloadView::GetInstance()) {
-      if (EphemerisDownloadView::GetInstance()->GetDownloadLabel()) {
-        EphemerisDownloadView::GetInstance()->GetDownloadLabel()->SetText(
-            "未下载");
-      }
-    }
-    break;
+  // case EphemerisDownloadStatus::NOT_DOWNLOADED:
+  //   if (EphemerisDownloadView::GetInstance()) {
+  //     if (EphemerisDownloadView::GetInstance()->GetDownloadLabel()) {
+  //       EphemerisDownloadView::GetInstance()->GetDownloadLabel()->SetText(
+  //           "未下载");
+  //     }
+  //   }
+  //   break;
   }
 }
 
index fb9e6a3..d745b7a 100644 (file)
@@ -13,6 +13,8 @@ static EphemerisDownloadView *g_pEphemerisDownloadView{nullptr};
 
 EphemerisDownloadView::EphemerisDownloadView() {
     g_pEphemerisDownloadView = this;
+    //启动定时器
+    EphemerisDownloadModel::GetInstance().ephemeris_data_timer(1000);
 }
 
 EphemerisDownloadView::~EphemerisDownloadView() {
@@ -47,6 +49,8 @@ EphemerisDownloadView::~EphemerisDownloadView() {
         labelNote_ = nullptr;
     }
     g_pEphemerisDownloadView = nullptr;
+    //停止定时器
+    EphemerisDownloadModel::GetInstance().ephemeris_stop_data_timer();
 }
 
 void EphemerisDownloadView::OnStart() {
@@ -87,7 +91,15 @@ bool EphemerisDownloadView::EphemerisDownloadInit() {
     }
     labelStatus_->SetPosition(90, 120, 300, 40);
     labelStatus_->SetFont(DEFAULT_VECTOR_FONT_FILENAME, 24);
-    labelStatus_->SetText("未下载");
+    //如果有效期大于0,则显示下载成功
+    if(EphemerisDownloadModel::GetInstance().GetValidityPeriod())
+    {
+        labelStatus_->SetText("下载成功");
+    }
+    else
+    {
+        labelStatus_->SetText("未下载");
+    }
     labelStatus_->SetTextColor(Color::Yellow());
     AddViewToRootContainer(labelStatus_);
 
@@ -118,13 +130,13 @@ bool EphemerisDownloadView::EphemerisDownloadInit() {
     //如果有效期大于0,则不能下载 下载按键显示灰色;否则显示绿色
     if(EphemerisDownloadModel::GetInstance().GetValidityPeriod())
     {
-        // btnDownload_->SetTextColor(Color::GetColorFromRGBA(0xff, 0xff, 0xff, 0x5f));
-        // btnDownload_->SetBackgroundColor(Color::GetColorFromRGBA(0xff, 0xff, 0xff, 0x5f));
+        btnDownload_->SetStyle(STYLE_BACKGROUND_COLOR, Color::Gray().full);
+        btnDownload_->SetStyleForState(STYLE_BACKGROUND_COLOR, Color::Black().full, UIButton::PRESSED);
     }
     else
     {
-        // btnDownload_->SetTextColor(Color::GetColorFromRGBA(0xff, 0xff, 0xff, 0xff));
-        // btnDownload_->SetBackgroundColor(Color::GetColorFromRGBA(0xff, 0xff, 0xff, 0xff));
+        btnDownload_->SetStyle(STYLE_BACKGROUND_COLOR, Color::Blue().full);
+        btnDownload_->SetStyleForState(STYLE_BACKGROUND_COLOR, Color::Black().full, UIButton::PRESSED);
     }
     
     AddViewToRootContainer(btnDownload_);