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 
SetMoveDirection(NG::Direction moveDirection)91     void SetMoveDirection(NG::Direction moveDirection)
92     {
93         moveDirection_ = moveDirection;
94     }
95 
GetMoveDirection()96     NG::Direction GetMoveDirection()
97     {
98         return moveDirection_;
99     }
100 
SetBackToToday(bool backToToday)101     void SetBackToToday(bool backToToday)
102     {
103         backToToday_ = backToToday;
104     }
105 
SetGoTo(bool goTo)106     void SetGoTo(bool goTo)
107     {
108         goTo_ = goTo;
109     }
110 
GetCalendarControllerNg()111     RefPtr<CalendarControllerNg> GetCalendarControllerNg() const
112     {
113         return calendarControllerNg_;
114     }
115 
GetSelectDate()116     std::string GetSelectDate() const
117     {
118         return selectedDay_.ToString(true);
119     }
120 
HasTitleNode()121     bool HasTitleNode() const
122     {
123         return titleId_.has_value();
124     }
125 
GetTitleId()126     int32_t GetTitleId()
127     {
128         if (!titleId_.has_value()) {
129             titleId_ = ElementRegister::GetInstance()->MakeUniqueId();
130         }
131         return titleId_.value();
132     }
133 
134     void UpdateTitleNode();
135     void SetCalendarControllerNg(const RefPtr<CalendarControllerNg>& calendarController);
136 
137     void FireFirstRequestData();
138     void FireGoToRequestData(int32_t year, int32_t month, int32_t day);
139     void ToJsonValue(std::unique_ptr<JsonValue>& json, const InspectorFilter& filter) const override;
140 
GetSelectedDay()141     PickerDate GetSelectedDay() const
142     {
143         return selectedDay_;
144     }
145 
SetSelectedDay(const PickerDate & selectedDay)146     void SetSelectedDay(const PickerDate& selectedDay)
147     {
148         selectedDay_ = selectedDay;
149     }
150 
151     void FlushDialogData();
152 
GetCurrentMonthIndex()153     int32_t GetCurrentMonthIndex()
154     {
155         return currentMonthIndex_;
156     }
157 
SetDialogClickEventState(bool isClickEvent)158     void SetDialogClickEventState(bool isClickEvent)
159     {
160         isClickEvent_ = isClickEvent;
161     }
162 private:
163     void OnAttachToFrameNode() override;
164     bool OnDirtyLayoutWrapperSwap(const RefPtr<LayoutWrapper>& dirty, bool skipMeasure, bool skipLayout) override;
165     void OnModifyDone() override;
166     void FireRequestData(MonthState monthState);
167     void JumpTo(const RefPtr<FrameNode>& preFrameNode, const RefPtr<FrameNode>& curFrameNode,
168         const RefPtr<FrameNode>& nextFrameNode, const RefPtr<FrameNode>& swiperFrameNode);
169     void FlushFocus(ObtainedMonth& obtainedMonth);
170     void JumpTo(ObtainedMonth& obtainedMonth);
171     void FlushDialogMonthData(ObtainedMonth& obtainedMonth);
172     void InitSwiperChangeDoneEvent();
173     void ReadTitleNode();
174     void ClearChildrenFocus();
175 
176     std::optional<int32_t> titleId_;
177     RefPtr<CalendarControllerNg> calendarControllerNg_;
178     CalendarDay calendarDay_;
179     PickerDate selectedDay_;
180 
181     // Used to mark the jump action destination.
182     // eg. 2023-1-1
183     int32_t goToCalendarDay_;
184     int32_t goToCalendarMonth_;
185     int32_t goToCalendarYear_;
186 
187     int32_t currentMonthIndex_ = 1;
188     std::string selectedMonth_;
189     ObtainedMonth currentMonth_;
190     ObtainedMonth preMonth_;
191     ObtainedMonth nextMonth_;
192     NG::Direction moveDirection_;
193     bool initialize_ = true;
194     bool backToToday_ = false;
195     bool goTo_ = false;
196     bool isClickEvent_ = false;
197     ACE_DISALLOW_COPY_AND_MOVE(CalendarPattern);
198 };
199 } // namespace OHOS::Ace::NG
200 
201 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_CALENDAR_CALENDAR_PATTERN_H
202