• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_PATTERN_CALENDAR_CALENDAR_PATTERN_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_CALENDAR_CALENDAR_PATTERN_H
18 
19 #include <cstdint>
20 #include <optional>
21 
22 #include "base/memory/referenced.h"
23 #include "core/components/calendar/calendar_data_adapter.h"
24 #include "core/components/picker/picker_data.h"
25 #include "core/components_ng/base/frame_node.h"
26 #include "core/components_ng/pattern/calendar/calendar_controller_ng.h"
27 #include "core/components_ng/pattern/calendar/calendar_event_hub.h"
28 #include "core/components_ng/pattern/pattern.h"
29 #include "core/components_ng/pattern/swiper/swiper_event_hub.h"
30 
31 namespace OHOS::Ace::NG {
32 class InspectorFilter;
33 
34 class CalendarPattern : public Pattern {
35     DECLARE_ACE_TYPE(CalendarPattern, Pattern);
36 
37 public:
38     CalendarPattern() = default;
39     ~CalendarPattern() override = default;
40 
CreateEventHub()41     RefPtr<EventHub> CreateEventHub() override
42     {
43         return MakeRefPtr<CalendarEventHub>();
44     }
45 
IsAtomicNode()46     bool IsAtomicNode() const override
47     {
48         return false;
49     }
50 
GetCurrentMonthData()51     ObtainedMonth GetCurrentMonthData() const
52     {
53         return currentMonth_;
54     }
55 
SetCurrentMonthData(const ObtainedMonth & currentData)56     void SetCurrentMonthData(const ObtainedMonth& currentData)
57     {
58         currentMonth_ = currentData;
59     }
60 
GetPreMonthData()61     ObtainedMonth GetPreMonthData() const
62     {
63         return preMonth_;
64     }
65 
SetPreMonthData(const ObtainedMonth & preData)66     void SetPreMonthData(const ObtainedMonth& preData)
67     {
68         preMonth_ = preData;
69     }
70 
GetNextMonthData()71     ObtainedMonth GetNextMonthData() const
72     {
73         return nextMonth_;
74     }
75 
SetNextMonthData(const ObtainedMonth & nextData)76     void SetNextMonthData(const ObtainedMonth& nextData)
77     {
78         nextMonth_ = nextData;
79     }
80 
GetCalendarDay()81     CalendarDay GetCalendarDay() const
82     {
83         return calendarDay_;
84     }
85 
SetCalendarDay(const CalendarDay & calendarDay)86     void SetCalendarDay(const CalendarDay& calendarDay)
87     {
88         calendarDay_ = calendarDay;
89     }
90 
SetStartDate(const PickerDate & startDate)91     void SetStartDate(const PickerDate& startDate)
92     {
93         startDate_ = startDate;
94     }
95 
SetEndDate(const PickerDate & endDate)96     void SetEndDate(const PickerDate& endDate)
97     {
98         endDate_ = endDate;
99     }
100 
SetMarkToday(bool markToday)101     void SetMarkToday(bool markToday)
102     {
103         markToday_ = markToday;
104     }
105 
SetDisabledDateRange(const std::vector<std::pair<PickerDate,PickerDate>> & disabledDateRange)106     void SetDisabledDateRange(const std::vector<std::pair<PickerDate, PickerDate>>& disabledDateRange)
107     {
108         disabledDateRange_ = disabledDateRange;
109     }
110 
SetMoveDirection(NG::Direction moveDirection)111     void SetMoveDirection(NG::Direction moveDirection)
112     {
113         moveDirection_ = moveDirection;
114     }
115 
GetMoveDirection()116     NG::Direction GetMoveDirection()
117     {
118         return moveDirection_;
119     }
120 
SetBackToToday(bool backToToday)121     void SetBackToToday(bool backToToday)
122     {
123         backToToday_ = backToToday;
124     }
125 
SetGoTo(bool goTo)126     void SetGoTo(bool goTo)
127     {
128         goTo_ = goTo;
129     }
130 
GetCalendarControllerNg()131     RefPtr<CalendarControllerNg> GetCalendarControllerNg() const
132     {
133         return calendarControllerNg_;
134     }
135 
GetSelectDate()136     std::string GetSelectDate() const
137     {
138         return selectedDay_.ToString(true);
139     }
140 
HasTitleNode()141     bool HasTitleNode() const
142     {
143         return titleId_.has_value();
144     }
145 
GetTitleId()146     int32_t GetTitleId()
147     {
148         if (!titleId_.has_value()) {
149             titleId_ = ElementRegister::GetInstance()->MakeUniqueId();
150         }
151         return titleId_.value();
152     }
153 
154     void UpdateTitleNode();
155     void SetCalendarControllerNg(const RefPtr<CalendarControllerNg>& calendarController);
156 
157     void FireFirstRequestData();
158     void FireGoToRequestData(int32_t year, int32_t month, int32_t day);
159     void ToJsonValue(std::unique_ptr<JsonValue>& json, const InspectorFilter& filter) const override;
160 
GetSelectedDay()161     PickerDate GetSelectedDay() const
162     {
163         return selectedDay_;
164     }
165 
SetSelectedDay(const PickerDate & selectedDay)166     void SetSelectedDay(const PickerDate& selectedDay)
167     {
168         selectedDay_ = selectedDay;
169     }
170 
171     void FlushDialogData();
172 
GetCurrentMonthIndex()173     int32_t GetCurrentMonthIndex()
174     {
175         return currentMonthIndex_;
176     }
177 
SetDialogClickEventState(bool isClickEvent)178     void SetDialogClickEventState(bool isClickEvent)
179     {
180         isClickEvent_ = isClickEvent;
181     }
182 private:
183     void OnAttachToFrameNode() override;
184     bool OnDirtyLayoutWrapperSwap(const RefPtr<LayoutWrapper>& dirty, bool skipMeasure, bool skipLayout) override;
185     void OnModifyDone() override;
186     void FireRequestData(MonthState monthState);
187     void JumpTo(const RefPtr<FrameNode>& preFrameNode, const RefPtr<FrameNode>& curFrameNode,
188         const RefPtr<FrameNode>& nextFrameNode, const RefPtr<FrameNode>& swiperFrameNode);
189     void FlushFocus(ObtainedMonth& obtainedMonth);
190     void JumpTo(ObtainedMonth& obtainedMonth);
191     void FlushDialogMonthData(ObtainedMonth& obtainedMonth);
192     void InitSwiperChangeDoneEvent();
193     void ReadTitleNode();
194     void ClearChildrenFocus();
195 
196     std::optional<int32_t> titleId_;
197     RefPtr<CalendarControllerNg> calendarControllerNg_;
198     CalendarDay calendarDay_;
199     PickerDate selectedDay_;
200     uint8_t curMonthIndex_ = 1;
201     PickerDate startDate_;
202     PickerDate endDate_;
203     bool markToday_ = false;
204     std::vector<std::pair<PickerDate, PickerDate>> disabledDateRange_;
205 
206     // Used to mark the jump action destination.
207     // eg. 2023-1-1
208     int32_t goToCalendarDay_;
209     int32_t goToCalendarMonth_;
210     int32_t goToCalendarYear_;
211 
212     int32_t currentMonthIndex_ = 1;
213     std::string selectedMonth_;
214     ObtainedMonth currentMonth_;
215     ObtainedMonth preMonth_;
216     ObtainedMonth nextMonth_;
217     NG::Direction moveDirection_;
218     bool initialize_ = true;
219     bool backToToday_ = false;
220     bool goTo_ = false;
221     bool isClickEvent_ = false;
222     ACE_DISALLOW_COPY_AND_MOVE(CalendarPattern);
223 };
224 } // namespace OHOS::Ace::NG
225 
226 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_CALENDAR_CALENDAR_PATTERN_H
227