1 /*
2  * Copyright (c) 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 #include "bridge/declarative_frontend/jsview/models/calendar_model_impl.h"
17 
18 #include "bridge/declarative_frontend/view_stack_processor.h"
19 #include "core/event/ace_event_handler.h"
20 #include "core/pipeline/pipeline_base.h"
21 
22 namespace OHOS::Ace::Framework {
Create(const CalendarModelData & calendarModelData)23 void CalendarModelImpl::Create(const CalendarModelData& calendarModelData)
24 {
25     auto calendarComponent = AceType::MakeRefPtr<OHOS::Ace::CalendarComponentV2>("", "calendar");
26     calendarComponent->SetCalendarDate(calendarModelData.date);
27     calendarComponent->SetCalendarData(calendarModelData.currentData);
28     calendarComponent->SetCalendarData(calendarModelData.preData);
29     calendarComponent->SetCalendarData(calendarModelData.nextData);
30     calendarComponent->SetControllerV2(AceType::DynamicCast<CalendarControllerV2>(calendarModelData.controller));
31     auto pipeline = PipelineBase::GetCurrentContext();
32     CHECK_NULL_VOID(pipeline);
33     auto theme = pipeline->GetTheme<CalendarTheme>();
34     CHECK_NULL_VOID(theme);
35     calendarComponent->SetCalendarTheme(theme);
36     calendarComponent->SetV2Component(true);
37     ViewStackProcessor::GetInstance()->Push(calendarComponent);
38 }
39 
SetOffDays(const std::string & offDays)40 void CalendarModelImpl::SetOffDays(const std::string& offDays)
41 {
42     auto component = GetComponent();
43     CHECK_NULL_VOID(component);
44     component->SetOffDays(offDays);
45 }
46 
SetShowHoliday(const bool showHoliday)47 void CalendarModelImpl::SetShowHoliday(const bool showHoliday)
48 {
49     auto component = GetComponent();
50     CHECK_NULL_VOID(component);
51     component->SetShowHoliday(showHoliday);
52 }
53 
SetShowLunar(const bool showLunar)54 void CalendarModelImpl::SetShowLunar(const bool showLunar)
55 {
56     auto component = GetComponent();
57     CHECK_NULL_VOID(component);
58     component->SetShowLunar(showLunar);
59 }
60 
SetStartOfWeek(const int32_t startOfWeek)61 void CalendarModelImpl::SetStartOfWeek(const int32_t startOfWeek)
62 {
63     auto component = GetComponent();
64     CHECK_NULL_VOID(component);
65     int32_t startOfWeekMax = 7;
66     int32_t startOfWeekMin = 0;
67     if (startOfWeek >= startOfWeekMin && startOfWeek < startOfWeekMax) {
68         component->SetStartDayOfWeek(startOfWeek);
69     }
70 }
71 
SetNeedSlide(const bool needSlide)72 void CalendarModelImpl::SetNeedSlide(const bool needSlide)
73 {
74     auto component = GetComponent();
75     CHECK_NULL_VOID(component);
76     component->SetNeedSlide(needSlide);
77 }
78 
SetSelectedChangeEvent(std::function<void (const std::string &)> && selectedChangeEvent)79 void CalendarModelImpl::SetSelectedChangeEvent(std::function<void(const std::string&)>&& selectedChangeEvent)
80 {
81     auto component = GetComponent();
82     CHECK_NULL_VOID(component);
83     auto onSelectedChangeId = EventMarker(std::move(selectedChangeEvent));
84     component->SetSelectedChangeEvent(onSelectedChangeId);
85 }
86 
SetOnRequestDataEvent(std::function<void (const std::string &)> && requestData)87 void CalendarModelImpl::SetOnRequestDataEvent(std::function<void(const std::string&)>&& requestData)
88 {
89     auto component = GetComponent();
90     CHECK_NULL_VOID(component);
91     auto onRequestDataId = EventMarker(std::move(requestData));
92     component->SetRequestDataEvent(onRequestDataId);
93 }
94 
SetDirection(const int32_t dir)95 void CalendarModelImpl::SetDirection(const int32_t dir)
96 {
97     auto component = GetComponent();
98     CHECK_NULL_VOID(component);
99     if (dir == 0) {
100         component->SetAxis(Axis::VERTICAL);
101     } else if (dir == 1) {
102         component->SetAxis(Axis::HORIZONTAL);
103     }
104 }
105 
SetCurrentDayStyle(const CurrentDayStyleData & currentDayStyleData,const CurrentDayStyleData & CurrentDayStyleDataImpl)106 void CalendarModelImpl::SetCurrentDayStyle(const CurrentDayStyleData& currentDayStyleData,
107     const CurrentDayStyleData& CurrentDayStyleDataImpl)
108 {
109     auto component = GetComponent();
110     CHECK_NULL_VOID(component);
111     auto& themePtr = component->GetCalendarTheme();
112     CHECK_NULL_VOID(themePtr);
113     auto& theme = themePtr->GetCalendarTheme();
114 
115     if (CurrentDayStyleDataImpl.dayColor.has_value()) {
116         theme.dayColor = CurrentDayStyleDataImpl.dayColor.value();
117     }
118 
119     if (CurrentDayStyleDataImpl.lunarColor.has_value()) {
120         theme.lunarColor = CurrentDayStyleDataImpl.lunarColor.value();
121     }
122 
123     if (CurrentDayStyleDataImpl.markLunarColor.has_value()) {
124         theme.markLunarColor = CurrentDayStyleDataImpl.markLunarColor.value();
125     }
126 
127     if (CurrentDayStyleDataImpl.dayFontSize.has_value()) {
128         theme.dayFontSize = CurrentDayStyleDataImpl.dayFontSize.value();
129     }
130 
131     if (CurrentDayStyleDataImpl.lunarDayFontSize.has_value()) {
132         theme.lunarDayFontSize = CurrentDayStyleDataImpl.lunarDayFontSize.value();
133     }
134 
135     if (CurrentDayStyleDataImpl.dayHeight.has_value()) {
136         theme.dayHeight = CurrentDayStyleDataImpl.dayHeight.value();
137     }
138 
139     if (CurrentDayStyleDataImpl.dayWidth.has_value()) {
140         theme.dayWidth = CurrentDayStyleDataImpl.dayWidth.value();
141     }
142 
143     if (CurrentDayStyleDataImpl.gregorianCalendarHeight.has_value()) {
144         theme.gregorianCalendarHeight = CurrentDayStyleDataImpl.gregorianCalendarHeight.value();
145     }
146 
147     if (CurrentDayStyleDataImpl.lunarHeight.has_value()) {
148         theme.lunarHeight = CurrentDayStyleDataImpl.lunarHeight.value();
149     }
150 
151     if (CurrentDayStyleDataImpl.dayYAxisOffset.has_value()) {
152         theme.dayYAxisOffset = CurrentDayStyleDataImpl.dayYAxisOffset.value();
153     }
154 
155     if (CurrentDayStyleDataImpl.lunarDayYAxisOffset.has_value()) {
156         theme.lunarDayYAxisOffset = CurrentDayStyleDataImpl.lunarDayYAxisOffset.value();
157     }
158 
159     if (CurrentDayStyleDataImpl.underscoreXAxisOffset.has_value()) {
160         theme.underscoreXAxisOffset = CurrentDayStyleDataImpl.underscoreXAxisOffset.value();
161     }
162 
163     if (CurrentDayStyleDataImpl.underscoreYAxisOffset.has_value()) {
164         theme.underscoreYAxisOffset = CurrentDayStyleDataImpl.underscoreYAxisOffset.value();
165     }
166 
167     if (CurrentDayStyleDataImpl.scheduleMarkerXAxisOffset.has_value()) {
168         theme.scheduleMarkerXAxisOffset = CurrentDayStyleDataImpl.scheduleMarkerXAxisOffset.value();
169     }
170 
171     if (CurrentDayStyleDataImpl.scheduleMarkerYAxisOffset.has_value()) {
172         theme.scheduleMarkerYAxisOffset = CurrentDayStyleDataImpl.scheduleMarkerYAxisOffset.value();
173     }
174 
175     if (CurrentDayStyleDataImpl.colSpace.has_value()) {
176         theme.colSpace = CurrentDayStyleDataImpl.colSpace.value();
177     }
178 
179     if (CurrentDayStyleDataImpl.dailyFiveRowSpace.has_value()) {
180         theme.dailyFiveRowSpace = CurrentDayStyleDataImpl.dailyFiveRowSpace.value();
181     }
182 
183     if (CurrentDayStyleDataImpl.dailySixRowSpace.has_value()) {
184         theme.dailySixRowSpace = CurrentDayStyleDataImpl.dailySixRowSpace.value();
185     }
186 
187     if (CurrentDayStyleDataImpl.underscoreWidth.has_value()) {
188         theme.underscoreWidth = CurrentDayStyleDataImpl.underscoreWidth.value();
189     }
190 
191     if (CurrentDayStyleDataImpl.underscoreLength.has_value()) {
192         theme.underscoreLength = CurrentDayStyleDataImpl.underscoreLength.value();
193     }
194 
195     if (CurrentDayStyleDataImpl.scheduleMarkerRadius.has_value()) {
196         theme.scheduleMarkerRadius = CurrentDayStyleDataImpl.scheduleMarkerRadius.value();
197     }
198 
199     if (CurrentDayStyleDataImpl.boundaryRowOffset.has_value()) {
200         theme.boundaryRowOffset = CurrentDayStyleDataImpl.boundaryRowOffset.value();
201     }
202 
203     if (CurrentDayStyleDataImpl.boundaryColOffset.has_value()) {
204         theme.boundaryColOffset = CurrentDayStyleDataImpl.boundaryColOffset.value();
205     }
206 
207     if (CurrentDayStyleDataImpl.touchCircleStrokeWidth.has_value()) {
208         theme.touchCircleStrokeWidth = CurrentDayStyleDataImpl.touchCircleStrokeWidth.value();
209     }
210 }
211 
SetNonCurrentDayStyle(const NonCurrentDayStyleData & nonCurrentDayStyleData)212 void CalendarModelImpl::SetNonCurrentDayStyle(const NonCurrentDayStyleData& nonCurrentDayStyleData)
213 {
214     auto component = GetComponent();
215     CHECK_NULL_VOID(component);
216     auto& themePtr = component->GetCalendarTheme();
217     CHECK_NULL_VOID(themePtr);
218     auto& theme = themePtr->GetCalendarTheme();
219 
220     if (nonCurrentDayStyleData.nonCurrentMonthDayColor.has_value()) {
221         theme.nonCurrentMonthDayColor = nonCurrentDayStyleData.nonCurrentMonthDayColor.value();
222     }
223 
224     if (nonCurrentDayStyleData.nonCurrentMonthLunarColor.has_value()) {
225         theme.nonCurrentMonthLunarColor = nonCurrentDayStyleData.nonCurrentMonthLunarColor.value();
226     }
227 
228     if (nonCurrentDayStyleData.nonCurrentMonthWorkDayMarkColor.has_value()) {
229         theme.nonCurrentMonthWorkDayMarkColor = nonCurrentDayStyleData.nonCurrentMonthWorkDayMarkColor.value();
230     }
231 
232     if (nonCurrentDayStyleData.nonCurrentMonthOffDayMarkColor.has_value()) {
233         theme.nonCurrentMonthOffDayMarkColor = nonCurrentDayStyleData.nonCurrentMonthOffDayMarkColor.value();
234     }
235 }
236 
SetTodayStyle(const TodayStyleData & style)237 void CalendarModelImpl::SetTodayStyle(const TodayStyleData& style)
238 {
239     auto component = GetComponent();
240     CHECK_NULL_VOID(component);
241     auto& themePtr = component->GetCalendarTheme();
242     CHECK_NULL_VOID(themePtr);
243     auto& theme = themePtr->GetCalendarTheme();
244 
245     if (style.focusedDayColor.has_value()) {
246         theme.focusedDayColor = style.focusedDayColor.value();
247     }
248 
249     if (style.focusedLunarColor.has_value()) {
250         theme.focusedLunarColor = style.focusedLunarColor.value();
251     }
252 
253     if (style.focusedAreaBackgroundColor.has_value()) {
254         theme.focusedAreaBackgroundColor = style.focusedAreaBackgroundColor.value();
255     }
256 
257     if (style.focusedAreaRadius.has_value()) {
258         theme.focusedAreaRadius = style.focusedAreaRadius.value();
259     }
260 }
261 
SetWeekStyle(const WeekStyleData & style)262 void CalendarModelImpl::SetWeekStyle(const WeekStyleData& style)
263 {
264     auto component = GetComponent();
265     CHECK_NULL_VOID(component);
266     auto& themePtr = component->GetCalendarTheme();
267     CHECK_NULL_VOID(themePtr);
268     auto& theme = themePtr->GetCalendarTheme();
269 
270     if (style.weekColor.has_value()) {
271         theme.weekColor = style.weekColor.value();
272     }
273 
274     if (style.weekendDayColor.has_value()) {
275         theme.weekendDayColor = style.weekendDayColor.value();
276     }
277 
278     if (style.weekendLunarColor.has_value()) {
279         theme.weekendLunarColor = style.weekendLunarColor.value();
280     }
281 
282     if (style.weekFontSize.has_value()) {
283         theme.weekFontSize = style.weekFontSize.value();
284     }
285     if (style.weekHeight.has_value()) {
286         theme.weekHeight = style.weekHeight.value();
287     }
288 
289     if (style.weekWidth.has_value()) {
290         theme.weekWidth = style.weekWidth.value();
291     }
292 
293     if (style.weekAndDayRowSpace.has_value()) {
294         theme.weekAndDayRowSpace = style.weekAndDayRowSpace.value();
295     }
296 }
297 
SetWorkStateStyle(const WorkStateStyleData & style)298 void CalendarModelImpl::SetWorkStateStyle(const WorkStateStyleData& style)
299 {
300     auto component = GetComponent();
301     CHECK_NULL_VOID(component);
302     auto& themePtr = component->GetCalendarTheme();
303     CHECK_NULL_VOID(themePtr);
304     auto& theme = themePtr->GetCalendarTheme();
305 
306     if (style.workDayMarkColor.has_value()) {
307         theme.workDayMarkColor = style.workDayMarkColor.value();
308     }
309 
310     if (style.offDayMarkColor.has_value()) {
311         theme.offDayMarkColor = style.offDayMarkColor.value();
312     }
313 
314     if (style.workDayMarkSize.has_value()) {
315         theme.workDayMarkSize = style.workDayMarkSize.value();
316     }
317 
318     if (style.offDayMarkSize.has_value()) {
319         theme.offDayMarkSize = style.offDayMarkSize.value();
320     }
321     if (style.workStateWidth.has_value()) {
322         theme.workStateWidth = style.workStateWidth.value();
323     }
324 
325     if (style.workStateHorizontalMovingDistance.has_value()) {
326         theme.workStateHorizontalMovingDistance = style.workStateHorizontalMovingDistance.value();
327     }
328 
329     if (style.workStateVerticalMovingDistance.has_value()) {
330         theme.workStateVerticalMovingDistance = style.workStateVerticalMovingDistance.value();
331     }
332 }
333 
GetComponent()334 RefPtr<CalendarComponentV2> CalendarModelImpl::GetComponent()
335 {
336     auto stack = ViewStackProcessor::GetInstance();
337     if (!stack) {
338         return nullptr;
339     }
340     auto component = AceType::DynamicCast<CalendarComponentV2>(stack->GetMainComponent());
341     if (AceApplicationInfo::GetInstance().IsRightToLeft()) {
342         component->SetTextDirection(TextDirection::RTL);
343     }
344 
345     return component;
346 }
347 } // namespace OHOS::Ace::Framework
348