1 /* 2 * Copyright (c) 2022-2023 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_TEXT_CLOCK_PATTERN_H 17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_TEXT_CLOCK_PATTERN_H 18 19 #include <functional> 20 #include <string> 21 22 #include "base/i18n/time_format.h" 23 #include "base/memory/referenced.h" 24 #include "base/utils/noncopyable.h" 25 #include "core/components/text_clock/text_clock_controller.h" 26 #include "core/components_ng/pattern/pattern.h" 27 #include "core/components_ng/pattern/text/text_pattern.h" 28 #include "core/components_ng/pattern/text_clock/text_clock_accessibility_property.h" 29 #include "core/components_ng/pattern/text_clock/text_clock_event_hub.h" 30 #include "core/components_ng/pattern/text_clock/text_clock_layout_algorithm.h" 31 #include "core/components_ng/pattern/text_clock/text_clock_layout_property.h" 32 #include "core/components_ng/pattern/text_clock/text_clock_model_ng.h" 33 #include "core/event/time/time_change_listener.h" 34 35 namespace OHOS::Ace::NG { 36 struct TextClockFormatElement { 37 char elementKey; 38 int32_t formatElementNum = 0; 39 std::string formatElement = ""; 40 }; 41 42 using TimeCallback = std::function<void()>; 43 class TextClockPattern : public Pattern, public TimeChangeListener { 44 DECLARE_ACE_TYPE(TextClockPattern, Pattern); 45 46 public: 47 TextClockPattern(); 48 ~TextClockPattern() override = default; 49 CreateLayoutProperty()50 RefPtr<LayoutProperty> CreateLayoutProperty() override 51 { 52 return MakeRefPtr<TextClockLayoutProperty>(); 53 } 54 CreateAccessibilityProperty()55 RefPtr<AccessibilityProperty> CreateAccessibilityProperty() override 56 { 57 return MakeRefPtr<TextClockAccessibilityProperty>(); 58 } 59 CreateEventHub()60 RefPtr<EventHub> CreateEventHub() override 61 { 62 return MakeRefPtr<TextClockEventHub>(); 63 } 64 CreateLayoutAlgorithm()65 RefPtr<LayoutAlgorithm> CreateLayoutAlgorithm() override 66 { 67 return MakeRefPtr<TextClockLayoutAlgorithm>(); 68 } 69 GetTextClockController()70 RefPtr<TextClockController> GetTextClockController() const 71 { 72 return textClockController_; 73 } 74 SetJSTextClockController(const RefPtr<Referenced> & jsController)75 void SetJSTextClockController(const RefPtr<Referenced>& jsController) 76 { 77 jsTextClockController_ = jsController; 78 } 79 GetJSTextClockController()80 RefPtr<Referenced> GetJSTextClockController() 81 { 82 return jsTextClockController_.Upgrade(); 83 } 84 GetTextId()85 int32_t GetTextId() 86 { 87 if (!textId_.has_value()) { 88 textId_ = ElementRegister::GetInstance()->MakeUniqueId(); 89 } 90 return textId_.value(); 91 } 92 93 void OnVisibleChange(bool isVisible) override; 94 95 void OnTimeChange() override; 96 SetBuilderFunc(TextClockMakeCallback && makeFunc)97 void SetBuilderFunc(TextClockMakeCallback&& makeFunc) 98 { 99 if (makeFunc == nullptr) { 100 makeFunc_ = std::nullopt; 101 contentModifierNode_ = nullptr; 102 OnModifyDone(); 103 return; 104 } 105 makeFunc_ = std::move(makeFunc); 106 } 107 UseContentModifier()108 bool UseContentModifier() const 109 { 110 return contentModifierNode_ != nullptr; 111 } 112 GetBuilderId()113 int32_t GetBuilderId() 114 { 115 return nodeId_; 116 } 117 GetPrefixHour()118 ZeroPrefixType GetPrefixHour() 119 { 120 auto textClockLayoutProperty = GetLayoutProperty<TextClockLayoutProperty>(); 121 CHECK_NULL_RETURN(textClockLayoutProperty, prefixHour_); 122 return textClockLayoutProperty->GetPrefixHourValue(ZeroPrefixType::AUTO); 123 } 124 125 private: 126 void OnModifyDone() override; 127 void OnAttachToFrameNode() override; 128 void OnDetachFromFrameNode(FrameNode* frameNode) override; 129 void OnLanguageConfigurationUpdate() override; 130 void DumpInfo() override; 131 void InitTextClockController(); 132 133 void InitUpdateTimeTextCallBack(); 134 void UpdateTimeText(bool isTimeChange = false); 135 void RequestUpdateForNextSecond(); 136 void FireChangeEvent() const; 137 std::string GetCurrentFormatDateTime(); 138 std::string ParseDateTime(const std::string& dateTimeValue, int32_t week, int32_t month, int32_t hour); 139 void RegistVisibleAreaChangeCallback(); 140 void OnVisibleAreaChange(bool visible); 141 static void UpdateTextLayoutProperty( 142 RefPtr<TextClockLayoutProperty>& layoutProperty, RefPtr<TextLayoutProperty>& textLayoutProperty); 143 void ParseInputFormat(); 144 std::vector<std::string> ParseDateTimeValue(const std::string& strDateTimeValue); 145 void GetDateTimeIndex(const char& element, TextClockFormatElement& tempFormatElement); 146 static std::string GetAmPm(int32_t hour); 147 static std::string Abstract(const std::string& strSource, const bool& abstractItem); 148 static int32_t GetDigitNumber(const std::string& strSource); 149 static std::string GetWeek(const bool& isShortType, const int32_t& week); 150 static std::string GetMonth(int32_t month); 151 std::string SpliceDateTime(const std::vector<std::string>& curDateTime); 152 static std::string CheckDateTimeElement(const std::vector<std::string>& curDateTime, const char& element, 153 const int32_t& elementIndex, const bool& oneElement); 154 std::string AddZeroPrefix(const std::string& strTimeValue); 155 std::string RemoveZeroPrefix(const std::string& strTimeValue); 156 std::string GetFormat() const; 157 float GetHoursWest() const; 158 RefPtr<FrameNode> GetTextNode(); 159 void FireBuilder(); 160 RefPtr<FrameNode> BuildContentModifierNode(); 161 162 std::optional<TextClockMakeCallback> makeFunc_ = std::nullopt; 163 RefPtr<FrameNode> contentModifierNode_ = nullptr; 164 int32_t nodeId_ = -1; 165 RefPtr<TextClockController> textClockController_; 166 WeakPtr<Referenced> jsTextClockController_; 167 float hourWest_ = 0.0f; 168 long timeValue_ = 0.0f; 169 std::optional<int32_t> textId_; 170 bool isStart_ = true; 171 bool is24H_ = SystemProperties::Is24HourClock(); 172 bool isSetVisible_ = true; 173 bool isInVisibleArea_ = true; 174 bool isForm_ = false; 175 std::string prevTime_; 176 std::map<int32_t, TextClockFormatElement> formatElementMap_; 177 CancelableCallback<void()> delayTask_; 178 ZeroPrefixType prefixHour_ = ZeroPrefixType::AUTO; 179 180 ACE_DISALLOW_COPY_AND_MOVE(TextClockPattern); 181 }; 182 } // namespace OHOS::Ace::NG 183 184 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_TEXT_CLOCK_PATTERN_H 185