• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2022 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_PICKER_PICKER_DATE_COMPONENT_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_PICKER_PICKER_DATE_COMPONENT_H
18 
19 #include "base/i18n/localization.h"
20 #include "core/components/picker/picker_base_component.h"
21 
22 namespace OHOS::Ace {
23 
24 class ACE_EXPORT PickerDateComponent : public PickerBaseComponent {
25     DECLARE_ACE_TYPE(PickerDateComponent, PickerBaseComponent);
26 
27 public:
28     PickerDateComponent();
29 
30     ~PickerDateComponent() override = default;
31 
GetStartDate()32     const PickerDate& GetStartDate() const
33     {
34         return startDateSolar_;
35     }
SetStartDate(const PickerDate & value)36     void SetStartDate(const PickerDate& value)
37     {
38         startDateSolar_ = value;
39         AdjustSolarDate(startDateSolar_, limitStartDate_, limitEndDate_);
40         startDateLunar_ = SolarToLunar(startDateSolar_);
41     }
42 
GetEndDate()43     const PickerDate& GetEndDate() const
44     {
45         return endDateSolar_;
46     }
SetEndDate(const PickerDate & value)47     void SetEndDate(const PickerDate& value)
48     {
49         endDateSolar_ = value;
50         AdjustSolarDate(endDateSolar_, limitStartDate_, limitEndDate_);
51         endDateLunar_ = SolarToLunar(endDateSolar_);
52     }
53 
GetSelectedDate()54     const PickerDate& GetSelectedDate() const
55     {
56         return selectedDate_;
57     }
SetSelectedDate(const PickerDate & value)58     void SetSelectedDate(const PickerDate& value)
59     {
60         selectedDate_ = value;
61     }
62 
GetMode()63     const DatePickerMode& GetMode() const
64     {
65         return mode_;
66     }
SetMode(const DatePickerMode & value)67     void SetMode(const DatePickerMode& value)
68     {
69         mode_ = value;
70     }
71 
SetOnDateChange(const std::function<void (const PickerDate &)> & value)72     void SetOnDateChange(const std::function<void(const PickerDate&)>& value)
73     {
74         onDateChange_ = value;
75     }
76 
SetShowLunar(bool value)77     void SetShowLunar(bool value)
78     {
79         lunar_ = value;
80     }
81 
NeedRtlColumnOrder()82     bool NeedRtlColumnOrder() const override
83     {
84         return true;
85     }
86 
IsShowLunar()87     bool IsShowLunar() const override
88     {
89         return lunar_;
90     }
91 
92     void OnTitleBuilding() override;
93 
94     void OnColumnsBuilding() override;
95 
96     void OnSelectedSaving() override;
97 
98     std::string GetSelectedObject(bool isColumnChange,
99         const std::string& changeColumnTag, int status = -1) const override;
100 
101     void OnDataLinking(
102         const std::string& tag, bool isAdd, uint32_t index, std::vector<std::string>& resultTags) override;
103 
104     void OnLunarCallback(bool checked, std::vector<std::string>& resultTags) override;
105 
106     void OnAnimationPlaying() override;
107 
108 private:
109     PickerDate GetCurrentDate() const;
110     LunarDate GetCurrentLunarDate(uint32_t lunarYear) const;
111 
112     void HandleYearChange(bool isAdd, uint32_t index, std::vector<std::string>& resultTags);
113 
114     void HandleLunarYearChange(bool isAdd, uint32_t index);
115 
116     void HandleSolarYearChange(bool isAdd, uint32_t index);
117 
118     void HandleMonthChange(bool isAdd, uint32_t index, std::vector<std::string>& resultTags);
119 
120     void HandleLunarMonthChange(bool isAdd, uint32_t index);
121 
122     void HandleSolarMonthChange(bool isAdd, uint32_t index);
123 
124     void HandleDayChange(bool isAdd, uint32_t index, std::vector<std::string>& resultTags);
125 
126     void HandleSolarDayChange(bool isAdd, uint32_t index);
127 
128     void HandleLunarDayChange(bool isAdd, uint32_t index);
129 
130     void HandleAddLunarDayChange(uint32_t index);
131 
132     void HandleReduceLunarDayChange(uint32_t index);
133 
134     std::string GetYearFormatString(uint32_t year) const;
135 
136     std::string GetMonthFormatString(uint32_t month, bool isLunar, bool isLeap) const;
137 
138     std::string GetDayFormatString(uint32_t day, bool isLunar) const;
139 
140     LunarDate SolarToLunar(const PickerDate& date) const;
141     PickerDate LunarToSolar(const LunarDate& date) const;
142 
143     bool GetLunarLeapMonth(uint32_t year, uint32_t& outLeapMonth) const;
144 
145     uint32_t GetLunarMaxDay(uint32_t year, uint32_t month, bool isLeap) const;
146 
147     void SolarColumnsBuilding(const PickerDate& current);
148 
149     void LunarColumnsBuilding(const LunarDate& current);
150 
151     int SolarDateCompare(const PickerDate& left, const PickerDate& right) const;
152 
153     int LunarDateCompare(const LunarDate& left, const LunarDate& right) const;
154 
155     void AdjustSolarDate(PickerDate& date) const;
156     void AdjustSolarDate(PickerDate& date, const PickerDate& start, const PickerDate& end) const;
157 
158     void AdjustLunarDate(LunarDate& date) const;
159 
160     bool lunar_ = false;
161     PickerDate startDateSolar_ = PickerDate(1970, 1, 1); // default start date is 1970-1-1 from FA document.
162     LunarDate startDateLunar_;
163     PickerDate endDateSolar_ = PickerDate(2100, 12, 31); // default end date is 2100-12-31 from FA document.
164     LunarDate endDateLunar_;
165     PickerDate selectedDate_ = PickerDate::Current();
166     DatePickerMode mode_ = DatePickerMode::DATE;
167     std::function<void(const PickerDate&)> onDateChange_;
168 
169     static const PickerDate limitStartDate_;
170     static const PickerDate limitEndDate_;
171 };
172 
173 } // namespace OHOS::Ace
174 
175 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_PICKER_PICKER_DATE_COMPONENT_H
176