1 /*
2 * Copyright (c) 2024 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/cj_frontend/interfaces/cj_ffi/cj_datepicker_ffi.h"
17
18 #include <vector>
19
20 #include "cj_lambda.h"
21 #include "bridge/common/utils/utils.h"
22 #include "bridge/cj_frontend/interfaces/cj_ffi/utils.h"
23 #include "bridge/cj_frontend/interfaces/cj_ffi/cj_view_abstract_ffi.h"
24 #include "core/components/chart/chart_component.h"
25 #include "core/components_ng/base/view_stack_processor.h"
26 #include "core/components_ng/pattern/picker/datepicker_model_ng.h"
27
28 using namespace OHOS::Ace;
29 using namespace OHOS::Ace::Framework;
30
31 namespace {
32 const int MAX_YEAR = 2100;
33 const int MIN_YEAR = 1900;
34
ParseDate(FfiTime data)35 PickerDate ParseDate(FfiTime data)
36 {
37 auto pickerDate = PickerDate();
38 pickerDate.SetYear(data.year);
39 pickerDate.SetMonth(data.month);
40 pickerDate.SetDay(data.day);
41
42 return pickerDate;
43 }
44
DatePickerChangeEventToFfi(const DatePickerChangeEvent & eventInfo)45 FFiDatePickerResult DatePickerChangeEventToFfi(const DatePickerChangeEvent& eventInfo)
46 {
47 FFiDatePickerResult result;
48 auto infoStr = eventInfo.GetSelectedStr();
49 std::unique_ptr<JsonValue> argsPtr = JsonUtil::ParseJsonString(eventInfo.GetSelectedStr());
50 if (!argsPtr) {
51 LOGW("selectedStr is not exist.");
52 return result;
53 }
54 const auto year = argsPtr->GetValue("year")->GetInt();
55 const auto month = argsPtr->GetValue("month")->GetInt() + 1; // 0-11 means 1 to 12 months
56 const auto day = argsPtr->GetValue("day")->GetInt();
57
58 result.year = year;
59 result.month = month;
60 result.day = day;
61
62 return result;
63 }
64 }
65
66 extern "C" {
ConvertToNoramlDate(PickerDate & date)67 void ConvertToNoramlDate(PickerDate& date)
68 {
69 auto theme = GetTheme<PickerTheme>();
70 if (date.GetYear() < MIN_YEAR) {
71 date = theme->GetDefaultStartDate();
72 }
73 if (date.GetYear() > MAX_YEAR) {
74 date = theme->GetDefaultEndDate();
75 }
76 }
77
FfiOHOSAceFrameworkDatePickerCreate(FfiTime startDate,FfiTime endDate,FfiTime selectedDate)78 void FfiOHOSAceFrameworkDatePickerCreate(FfiTime startDate, FfiTime endDate, FfiTime selectedDate)
79 {
80 auto theme = GetTheme<PickerTheme>();
81 CHECK_NULL_VOID(theme);
82 auto parseStartDate = ParseDate(startDate);
83 auto parseEndDate = ParseDate(endDate);
84 auto parseSelectedDate = ParseDate(selectedDate);
85
86 ConvertToNoramlDate(parseStartDate);
87 ConvertToNoramlDate(parseEndDate);
88 ConvertToNoramlDate(parseSelectedDate);
89
90 auto startDays = parseStartDate.ToDays();
91 auto endDays = parseEndDate.ToDays();
92 auto selectedDays = parseSelectedDate.ToDays();
93 if (startDays > endDays) {
94 parseStartDate = theme->GetDefaultStartDate();
95 parseEndDate = theme->GetDefaultEndDate();
96 startDays = parseStartDate.ToDays();
97 endDays = parseEndDate.ToDays();
98 }
99 if (selectedDays > endDays) {
100 parseSelectedDate = parseEndDate;
101 }
102 if (selectedDays < startDays) {
103 parseSelectedDate = parseStartDate;
104 }
105
106 DatePickerModel::GetInstance()->CreateDatePicker(theme);
107 DatePickerModel::GetInstance()->SetStartDate(parseStartDate);
108 DatePickerModel::GetInstance()->SetEndDate(parseEndDate);
109 DatePickerModel::GetInstance()->SetSelectedDate(parseSelectedDate);
110 FfiOHOSAceFrameworkDatePickerSetDefaultAttributes();
111 }
112
FfiOHOSAceFrameworkDatePickerSetLunar(bool isLunar)113 void FfiOHOSAceFrameworkDatePickerSetLunar(bool isLunar)
114 {
115 DatePickerModel::GetInstance()->SetShowLunar(isLunar);
116 }
117
FfiOHOSAceFrameworkDatePickerUseMilitaryTime(bool isUseMilitaryTime)118 void FfiOHOSAceFrameworkDatePickerUseMilitaryTime(bool isUseMilitaryTime)
119 {
120 DatePickerModel::GetInstance()->SetHour24(isUseMilitaryTime);
121 }
122
FfiOHOSAceFrameworkDatePickerSetOnChange(void (* callback)(int32_t year,int32_t month,int32_t day))123 void FfiOHOSAceFrameworkDatePickerSetOnChange(
124 void (*callback)(int32_t year, int32_t month, int32_t day))
125 {
126 auto onChange = [lambda = CJLambda::Create(callback)](const BaseEventInfo* index) -> void {
127 auto* eventInfo = TypeInfoHelper::DynamicCast<DatePickerChangeEvent>(index);
128 const auto infoResult = DatePickerChangeEventToFfi(*eventInfo);
129 lambda(infoResult.year, infoResult.month, infoResult.day);
130 };
131 auto* getInstance = NG::ViewStackProcessor::GetInstance();
132
133 CHECK_NULL_VOID(getInstance);
134 auto getMainFrameNode = getInstance->GetMainFrameNode();
135 CHECK_NULL_VOID(getMainFrameNode);
136
137 DatePickerModel::GetInstance()->SetOnChange(std::move(onChange));
138 }
139
FfiOHOSAceFrameworkDatePickerSetOnDateChange(void (* callback)(int32_t year,int32_t month,int32_t day))140 void FfiOHOSAceFrameworkDatePickerSetOnDateChange(
141 void (*callback)(int32_t year, int32_t month, int32_t day))
142 {
143 auto onDateChange = [lambda = CJLambda::Create(callback)](const BaseEventInfo* index) -> void {
144 auto* eventInfo = TypeInfoHelper::DynamicCast<DatePickerChangeEvent>(index);
145 const auto infoResult = DatePickerChangeEventToFfi(*eventInfo);
146 lambda(infoResult.year, infoResult.month, infoResult.day);
147 };
148 auto* getInstance = NG::ViewStackProcessor::GetInstance();
149
150 CHECK_NULL_VOID(getInstance);
151 auto getMainFrameNode = getInstance->GetMainFrameNode();
152 CHECK_NULL_VOID(getMainFrameNode);
153
154 DatePickerModel::GetInstance()->SetOnDateChange(std::move(onDateChange));
155 }
156
FfiOHOSAceFrameworkDatePickerSetDefaultAttributes(void)157 void FfiOHOSAceFrameworkDatePickerSetDefaultAttributes(void)
158 {
159 auto theme = GetTheme<PickerTheme>();
160 CHECK_NULL_VOID(theme);
161 NG::PickerTextStyle textStyle;
162 auto selectedStyle = theme->GetOptionStyle(true, false);
163 textStyle.textColor = selectedStyle.GetTextColor();
164 textStyle.fontSize = selectedStyle.GetFontSize();
165 textStyle.fontWeight = selectedStyle.GetFontWeight();
166 DatePickerModel::GetInstance()->SetSelectedTextStyle(theme, textStyle);
167
168 auto disappearStyle = theme->GetDisappearOptionStyle();
169 textStyle.textColor = disappearStyle.GetTextColor();
170 textStyle.fontSize = disappearStyle.GetFontSize();
171 textStyle.fontWeight = disappearStyle.GetFontWeight();
172 DatePickerModel::GetInstance()->SetDisappearTextStyle(theme, textStyle);
173
174 auto normalStyle = theme->GetOptionStyle(false, false);
175 textStyle.textColor = normalStyle.GetTextColor();
176 textStyle.fontSize = normalStyle.GetFontSize();
177 textStyle.fontWeight = normalStyle.GetFontWeight();
178 DatePickerModel::GetInstance()->SetNormalTextStyle(theme, textStyle);
179 }
180
FfiOHOSAceFrameworkDatePickerSetBackgroundColor(uint32_t color)181 void FfiOHOSAceFrameworkDatePickerSetBackgroundColor(uint32_t color)
182 {
183 DatePickerModel::GetInstance()->SetBackgroundColor(Color(color));
184 }
185
FfiOHOSAceFrameworkDatePickerSetDisappearTextStyle(uint32_t color,double size,int32_t unit,const char * weight,const char * family,uint32_t style)186 void FfiOHOSAceFrameworkDatePickerSetDisappearTextStyle(uint32_t color, double size, int32_t unit,
187 const char* weight, const char* family, uint32_t style)
188 {
189 auto theme = GetTheme<PickerTheme>();
190 CHECK_NULL_VOID(theme);
191 NG::PickerTextStyle textStyle;
192
193 textStyle.textColor = Color(color);
194 CalcDimension fontSize = CalcDimension(size, DimensionUnit(unit));
195 textStyle.fontSize = fontSize;
196
197 std::string weightVal = weight;
198 textStyle.fontWeight = ConvertStrToFontWeight(weightVal);
199
200 std::string familyVal = family;
201 textStyle.fontFamily = ConvertStrToFontFamilies(familyVal);
202 textStyle.fontStyle = static_cast<FontStyle>(style);
203
204 DatePickerModel::GetInstance()->SetDisappearTextStyle(theme, textStyle);
205 }
206
FfiOHOSAceFrameworkDatePickerSetTextStyle(uint32_t color,double size,int32_t unit,const char * weight,const char * family,uint32_t style)207 void FfiOHOSAceFrameworkDatePickerSetTextStyle(uint32_t color, double size, int32_t unit,
208 const char* weight, const char* family, uint32_t style)
209 {
210 auto theme = GetTheme<PickerTheme>();
211 CHECK_NULL_VOID(theme);
212 NG::PickerTextStyle textStyle;
213
214 textStyle.textColor = Color(color);
215 CalcDimension fontSize = CalcDimension(size, DimensionUnit(unit));
216 textStyle.fontSize = fontSize;
217
218 std::string weightVal = weight;
219 textStyle.fontWeight = ConvertStrToFontWeight(weightVal);
220
221 std::string familyVal = family;
222 textStyle.fontFamily = ConvertStrToFontFamilies(familyVal);
223 textStyle.fontStyle = static_cast<FontStyle>(style);
224
225 DatePickerModel::GetInstance()->SetNormalTextStyle(theme, textStyle);
226 }
227
FfiOHOSAceFrameworkDatePickerSetSelectedTextStyle(uint32_t color,double size,int32_t unit,const char * weight,const char * family,uint32_t style)228 void FfiOHOSAceFrameworkDatePickerSetSelectedTextStyle(uint32_t color, double size, int32_t unit,
229 const char* weight, const char* family, uint32_t style)
230 {
231 auto theme = GetTheme<PickerTheme>();
232 CHECK_NULL_VOID(theme);
233 NG::PickerTextStyle textStyle;
234
235 textStyle.textColor = Color(color);
236 CalcDimension fontSize = CalcDimension(size, DimensionUnit(unit));
237 textStyle.fontSize = fontSize;
238
239 std::string weightVal = weight;
240 textStyle.fontWeight = ConvertStrToFontWeight(weightVal);
241
242 std::string familyVal = family;
243 textStyle.fontFamily = ConvertStrToFontFamilies(familyVal);
244 textStyle.fontStyle = static_cast<FontStyle>(style);
245
246 DatePickerModel::GetInstance()->SetSelectedTextStyle(theme, textStyle);
247 }
248 }
249