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 
SetOnDateChange(const std::function<void (const PickerDate &)> & value)63     void SetOnDateChange(const std::function<void(const PickerDate&)>& value)
64     {
65         onDateChange_ = value;
66     }
67 
SetShowLunar(bool value)68     void SetShowLunar(bool value)
69     {
70         lunar_ = value;
71     }
72 
NeedRtlColumnOrder()73     bool NeedRtlColumnOrder() const override
74     {
75         return true;
76     }
77 
IsShowLunar()78     bool IsShowLunar() const override
79     {
80         return lunar_;
81     }
82 
83     void OnTitleBuilding() override;
84 
85     void OnColumnsBuilding() override;
86 
87     void OnSelectedSaving() override;
88 
89     std::string GetSelectedObject(bool isColumnChange,
90         const std::string& changeColumnTag, int status = -1) const override;
91 
92     void OnDataLinking(
93         const std::string& tag, bool isAdd, uint32_t index, std::vector<std::string>& resultTags) override;
94 
95     void OnLunarCallback(bool checked, std::vector<std::string>& resultTags) override;
96 
97     void OnAnimationPlaying() override;
98 
99 private:
100     PickerDate GetCurrentDate() const;
101     LunarDate GetCurrentLunarDate(uint32_t lunarYear) const;
102 
103     void HandleYearChange(bool isAdd, uint32_t index, std::vector<std::string>& resultTags);
104 
105     void HandleLunarYearChange(bool isAdd, uint32_t index);
106 
107     void HandleSolarYearChange(bool isAdd, uint32_t index);
108 
109     void HandleMonthChange(bool isAdd, uint32_t index, std::vector<std::string>& resultTags);
110 
111     void HandleLunarMonthChange(bool isAdd, uint32_t index);
112 
113     void HandleSolarMonthChange(bool isAdd, uint32_t index);
114 
115     void HandleDayChange(bool isAdd, uint32_t index, std::vector<std::string>& resultTags);
116 
117     void HandleSolarDayChange(bool isAdd, uint32_t index);
118 
119     void HandleLunarDayChange(bool isAdd, uint32_t index);
120 
121     void HandleAddLunarDayChange(uint32_t index);
122 
123     void HandleReduceLunarDayChange(uint32_t index);
124 
125     std::string GetYearFormatString(uint32_t year) const;
126 
127     std::string GetMonthFormatString(uint32_t month, bool isLunar, bool isLeap) const;
128 
129     std::string GetDayFormatString(uint32_t day, bool isLunar) const;
130 
131     LunarDate SolarToLunar(const PickerDate& date) const;
132     PickerDate LunarToSolar(const LunarDate& date) const;
133 
134     bool GetLunarLeapMonth(uint32_t year, uint32_t& outLeapMonth) const;
135 
136     uint32_t GetLunarMaxDay(uint32_t year, uint32_t month, bool isLeap) const;
137 
138     void SolarColumnsBuilding(const PickerDate& current);
139 
140     void LunarColumnsBuilding(const LunarDate& current);
141 
142     int SolarDateCompare(const PickerDate& left, const PickerDate& right) const;
143 
144     int LunarDateCompare(const LunarDate& left, const LunarDate& right) const;
145 
146     void AdjustSolarDate(PickerDate& date) const;
147     void AdjustSolarDate(PickerDate& date, const PickerDate& start, const PickerDate& end) const;
148 
149     void AdjustLunarDate(LunarDate& date) const;
150 
151     bool lunar_ = false;
152     PickerDate startDateSolar_ = PickerDate(1970, 1, 1); // default start date is 1970-1-1 from FA document.
153     LunarDate startDateLunar_;
154     PickerDate endDateSolar_ = PickerDate(2100, 12, 31); // default end date is 2100-12-31 from FA document.
155     LunarDate endDateLunar_;
156     PickerDate selectedDate_ = PickerDate::Current();
157     std::function<void(const PickerDate&)> onDateChange_;
158 
159     static const PickerDate limitStartDate_;
160     static const PickerDate limitEndDate_;
161 };
162 
163 } // namespace OHOS::Ace
164 
165 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_PICKER_PICKER_DATE_COMPONENT_H
166