增加timestr类型
authorCaisking <3449750381@qq.com>
Fri, 27 Sep 2024 07:04:49 +0000 (15:04 +0800)
committerCaisking <3449750381@qq.com>
Fri, 27 Sep 2024 07:04:49 +0000 (15:04 +0800)
code/sdk/application/wearable/fd230app/nativeui/main/include/dial/DialDataBase.h
code/sdk/application/wearable/fd230app/nativeui/main/include/dial/DialModelTimeStr.h [new file with mode: 0644]
code/sdk/application/wearable/fd230app/nativeui/main/src/dial/DialModelTimeStr.cpp [new file with mode: 0644]

index 03894a1..bdc2af6 100644 (file)
@@ -44,6 +44,7 @@ enum class DialDataType
     DATA_TYPE_WEEK_STR,           // 星期(string)
     DATA_TYPE_DATE_STR,           // 日期(string)
     DATA_TYPE_WEATHER_STR,        // 天气(string)
+    DATA_TYPE_TIMESTR_STR,        // 时间(string)
 
     DATA_TYPE_MAX = 0x7fff, // 数据类型的最大值
 };
diff --git a/code/sdk/application/wearable/fd230app/nativeui/main/include/dial/DialModelTimeStr.h b/code/sdk/application/wearable/fd230app/nativeui/main/include/dial/DialModelTimeStr.h
new file mode 100644 (file)
index 0000000..707f159
--- /dev/null
@@ -0,0 +1,28 @@
+
+
+#ifndef DIAL_MODEL_TimeStr_H
+#define DIAL_MODEL_TimeStr_H
+
+#include "DialBaseModel.h"
+#include <cstdint>
+#include <string>
+
+namespace OHOS {
+class DialModelTimeStr : public DialBaseModel {
+public:
+  DialModelTimeStr() {}
+  virtual ~DialModelTimeStr() {}
+  static DialModelTimeStr *GetInstance() {
+    static DialModelTimeStr instance;
+    return &instance;
+  }
+  bool GetDialTextData(DialDataType &type, std::string *&out,
+                       int16_t &strNum) override;
+  void UpdateTimeStr(const std::string dialTimeStr);
+
+private:
+  struct tm tblock_;
+  std::string dialTimeStr_ = "00:00";
+};
+} // namespace OHOS
+#endif
\ No newline at end of file
diff --git a/code/sdk/application/wearable/fd230app/nativeui/main/src/dial/DialModelTimeStr.cpp b/code/sdk/application/wearable/fd230app/nativeui/main/src/dial/DialModelTimeStr.cpp
new file mode 100644 (file)
index 0000000..e7a9c27
--- /dev/null
@@ -0,0 +1,50 @@
+#include "DialModelTimeStr.h"
+#include "RegisterDialModel.h"
+#include "custom_runtime.h"
+#include "sys/time.h"
+#include "wearable_log.h"
+#include <ctime>
+#include <iomanip>
+#include <sstream>
+#include <string>
+#include <time.h>
+#include <vector>
+
+REGIST_DIAL_MODULE(DialDataType::DATA_TYPE_TIMESTR_STR,
+                   OHOS::DialModelTimeStr::GetInstance());
+
+namespace OHOS {
+
+bool DialModelTimeStr::GetDialTextData(DialDataType &type, std::string *&out,
+                                       int16_t &strNum) {
+  (void)type;
+  /*获取时间*/
+  struct timeval tv;
+  gettimeofday(&tv, NULL); // 获取当前时间
+  localtime_r(&tv.tv_sec, &tblock_);
+
+  // Use ostringstream to format the time with leading zeros
+  std::ostringstream oss;
+  oss << std::setw(2) << std::setfill('0') << tblock_.tm_hour << ":"
+      << std::setw(2) << std::setfill('0') << tblock_.tm_min;
+
+  std::string timeStr = oss.str(); // Get the formatted time string
+
+  if (!timeStr.empty()) {
+    dialTimeStr_ = timeStr;
+    out = &dialTimeStr_;
+  }
+
+  strNum = 1;
+  return true;
+}
+
+void DialModelTimeStr::UpdateTimeStr(const std::string dialTimeStr) {
+  dialTimeStr_.clear();
+  dialTimeStr_ = dialTimeStr; // 更新字符串
+  if (onChangeListener_ != nullptr) {
+    onChangeListener_->OnDialTextDataUpdate(DialDataType::DATA_TYPE_TIMESTR_STR,
+                                            &dialTimeStr_, 1);
+  }
+}
+} // namespace OHOS
\ No newline at end of file