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/picker_model_impl.h"
17 
18 #include "bridge/declarative_frontend/jsview/js_view_common_def.h"
19 #include "bridge/declarative_frontend/view_stack_processor.h"
20 #include "core/components/picker/picker_date_component.h"
21 #include "core/components/picker/picker_time_component.h"
22 
23 namespace OHOS::Ace::Framework {
CreateDatePicker(RefPtr<PickerTheme> theme)24 void DatePickerModelImpl::CreateDatePicker(RefPtr<PickerTheme> theme)
25 {
26     auto datePicker = AceType::MakeRefPtr<PickerDateComponent>();
27     ViewStackProcessor::GetInstance()->ClaimElementId(datePicker);
28 
29     datePicker->SetIsDialog(false);
30     datePicker->SetHasButtons(false);
31     datePicker->SetTheme(theme);
32     ViewStackProcessor::GetInstance()->Push(datePicker);
33 }
34 
CreateTimePicker(RefPtr<PickerTheme> theme)35 void DatePickerModelImpl::CreateTimePicker(RefPtr<PickerTheme> theme)
36 {
37     auto timePicker = AceType::MakeRefPtr<PickerTimeComponent>();
38     ViewStackProcessor::GetInstance()->ClaimElementId(timePicker);
39     timePicker->SetIsDialog(false);
40     timePicker->SetHasButtons(false);
41 
42     timePicker->SetTheme(theme);
43     ViewStackProcessor::GetInstance()->Push(timePicker);
44 }
45 
SetStartDate(const PickerDate & value)46 void DatePickerModelImpl::SetStartDate(const PickerDate& value)
47 {
48     JSViewSetProperty(&PickerDateComponent::SetStartDate, value);
49 }
50 
SetEndDate(const PickerDate & value)51 void DatePickerModelImpl::SetEndDate(const PickerDate& value)
52 {
53     JSViewSetProperty(&PickerDateComponent::SetEndDate, value);
54 }
55 
SetSelectedDate(const PickerDate & value)56 void DatePickerModelImpl::SetSelectedDate(const PickerDate& value)
57 {
58     JSViewSetProperty(&PickerDateComponent::SetSelectedDate, value);
59 }
60 
SetSelectedTime(const PickerTime & selectedTime)61 void DatePickerModelImpl::SetSelectedTime(const PickerTime& selectedTime)
62 {
63     JSViewSetProperty(&PickerTimeComponent::SetSelectedTime, selectedTime);
64 }
65 
SetShowLunar(bool lunar)66 void DatePickerModelImpl::SetShowLunar(bool lunar)
67 {
68     JSViewSetProperty(&PickerDateComponent::SetShowLunar, lunar);
69 }
70 
SetHour24(bool value)71 void DatePickerModelImpl::SetHour24(bool value)
72 {
73     JSViewSetProperty(&PickerTimeComponent::SetHour24, value);
74 }
75 
SetOnChange(DateChangeEvent && onChange)76 void DatePickerModelImpl::SetOnChange(DateChangeEvent&& onChange)
77 {
78     auto datePicker = EventMarker([func = std::move(onChange)](const BaseEventInfo* info) { func(info); });
79     JSViewSetProperty(&PickerBaseComponent::SetOnChange, std::move(datePicker));
80 }
81 
SetBackgroundColor(const Color & color)82 void DatePickerModelImpl::SetBackgroundColor(const Color& color)
83 {
84     auto pickerBase = AceType::DynamicCast<PickerBaseComponent>(ViewStackProcessor::GetInstance()->GetMainComponent());
85     if (!pickerBase) {
86         LOGE("PickerBaseComponent is null");
87         return;
88     }
89 
90     pickerBase->SetHasBackgroundColor(true);
91 }
92 
SetDatePickerDialogShow(PickerDialogInfo & pickerDialog,NG::DatePickerSettingData & settingData,std::function<void ()> && onCancel,std::function<void (const std::string &)> && onAccept,std::function<void (const std::string &)> && onDateAccept,std::function<void (const std::string &)> && onDateChange,std::function<void (const std::string &)> && onChange,DatePickerType pickerType,PickerDialogEvent & pickerDialogEvent,const std::vector<ButtonInfo> & buttonInfos)93 void DatePickerDialogModelImpl::SetDatePickerDialogShow(PickerDialogInfo& pickerDialog,
94     NG::DatePickerSettingData& settingData, std::function<void()>&& onCancel,
95     std::function<void(const std::string&)>&& onAccept, std::function<void(const std::string&)>&& onDateAccept,
96     std::function<void(const std::string&)>&& onDateChange, std::function<void(const std::string&)>&& onChange,
97     DatePickerType pickerType, PickerDialogEvent& pickerDialogEvent, const std::vector<ButtonInfo>& buttonInfos)
98 {
99     std::string name;
100     RefPtr<Component> component;
101     switch (pickerType) {
102         case DatePickerType::TIME: {
103             auto timePicker = AceType::MakeRefPtr<PickerTimeComponent>();
104             if (pickerDialog.isSelectedDate == true) {
105                 timePicker->SetSelectedTime(pickerDialog.pickerTime);
106             }
107             timePicker->SetIsDialog(true);
108             timePicker->SetIsCreateDialogComponent(true);
109             timePicker->SetHour24(settingData.useMilitary);
110             component = timePicker;
111             name = "TimePickerDialog";
112             break;
113         }
114         case DatePickerType::DATE: {
115             auto datePicker = AceType::MakeRefPtr<PickerDateComponent>();
116             auto startDays = pickerDialog.parseStartDate.ToDays();
117             auto endDays = pickerDialog.parseEndDate.ToDays();
118             auto selectedDays = pickerDialog.parseSelectedDate.ToDays();
119             if (startDays > endDays || selectedDays < startDays || selectedDays > endDays) {
120                 LOGE("date error");
121             }
122             if (pickerDialog.isStartDate == true) {
123                 datePicker->SetStartDate(pickerDialog.parseStartDate);
124             }
125             if (pickerDialog.isEndDate == true) {
126                 datePicker->SetEndDate(pickerDialog.parseEndDate);
127             }
128             if (pickerDialog.isSelectedDate) {
129                 datePicker->SetSelectedDate(pickerDialog.parseSelectedDate);
130             }
131             datePicker->SetIsDialog(true);
132             datePicker->SetIsCreateDialogComponent(true);
133             datePicker->SetShowLunar(settingData.isLunar);
134 
135             component = datePicker;
136             name = "DatePickerDialog";
137             break;
138         }
139         default: {
140             LOGE("Undefined date picker type.");
141             return;
142         }
143     }
144     auto datePicker = AceType::DynamicCast<PickerBaseComponent>(component);
145     DialogProperties properties {};
146     properties.alignment = Container::LessThanAPIVersion(PlatformVersion::VERSION_ELEVEN) ? DialogAlignment::CENTER
147                                                                                           : DialogAlignment::DEFAULT;
148     properties.customComponent = datePicker;
149     properties.customStyle = true;
150     auto acceptId = EventMarker(std::move(onAccept));
151     datePicker->SetDialogAcceptEvent(acceptId);
152     auto cancelId = EventMarker(std::move(onCancel));
153     datePicker->SetDialogCancelEvent(cancelId);
154     auto changeId = EventMarker(std::move(onChange));
155     datePicker->SetDialogChangeEvent(changeId);
156     datePicker->SetDialogName(name);
157     datePicker->OpenDialog(properties);
158 }
159 } // namespace OHOS::Ace::Framework
160