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/timepicker_model_impl.h"
17 
18 #include "bridge/declarative_frontend/jsview/js_view_common_def.h"
19 #include "core/components/picker/picker_time_component.h"
20 
21 namespace OHOS::Ace::Framework {
CreateTimePicker(RefPtr<PickerTheme> pickerTheme,bool hasSecond)22 void TimePickerModelImpl::CreateTimePicker(RefPtr<PickerTheme> pickerTheme, bool hasSecond)
23 {
24     auto timePicker = AceType::MakeRefPtr<PickerTimeComponent>();
25     ViewStackProcessor::GetInstance()->ClaimElementId(timePicker);
26 
27     timePicker->SetIsDialog(false);
28     timePicker->SetHasButtons(false);
29     timePicker->SetTheme(pickerTheme);
30     ViewStackProcessor::GetInstance()->Push(timePicker);
31 }
32 
SetHour24(bool isUseMilitaryTime)33 void TimePickerModelImpl::SetHour24(bool isUseMilitaryTime)
34 {
35     JSViewSetProperty(&PickerTimeComponent::SetHour24, isUseMilitaryTime);
36 }
37 
SetSelectedTime(const PickerTime & value)38 void TimePickerModelImpl::SetSelectedTime(const PickerTime& value)
39 {
40     JSViewSetProperty(&PickerTimeComponent::SetSelectedTime, value);
41 }
42 
SetOnChange(ChangeEvent && onChange)43 void TimePickerModelImpl::SetOnChange(ChangeEvent&& onChange)
44 {
45     auto datePicker = EventMarker([func = std::move(onChange)](const BaseEventInfo* info) { func(info); });
46     JSViewSetProperty(&PickerBaseComponent::SetOnChange, std::move(datePicker));
47 }
48 
SetBackgroundColor(const Color & color)49 void TimePickerModelImpl::SetBackgroundColor(const Color& color)
50 {
51     auto pickerBase = AceType::DynamicCast<PickerBaseComponent>(ViewStackProcessor::GetInstance()->GetMainComponent());
52     if (!pickerBase) {
53         LOGE("PickerBaseComponent is null");
54         return;
55     }
56 
57     pickerBase->SetHasBackgroundColor(true);
58 }
59 
SetTimePickerDialogShow(PickerDialogInfo & pickerDialog,NG::TimePickerSettingData & settingData,std::function<void ()> && onCancel,std::function<void (const std::string &)> && onAccept,std::function<void (const std::string &)> && onChange,TimePickerDialogEvent & timePickerDialogEvent,const std::vector<ButtonInfo> & buttonInfos)60 void TimePickerDialogModelImpl::SetTimePickerDialogShow(PickerDialogInfo& pickerDialog,
61     NG::TimePickerSettingData& settingData, std::function<void()>&& onCancel,
62     std::function<void(const std::string&)>&& onAccept, std::function<void(const std::string&)>&& onChange,
63     TimePickerDialogEvent& timePickerDialogEvent, const std::vector<ButtonInfo>& buttonInfos)
64 {
65     RefPtr<Component> component;
66     auto timePicker = AceType::MakeRefPtr<PickerTimeComponent>();
67     bool isUseMilitaryTime = pickerDialog.isUseMilitaryTime;
68     if (pickerDialog.isSelectedTime == true) {
69         timePicker->SetSelectedTime(pickerDialog.pickerTime);
70     }
71     timePicker->SetIsDialog(true);
72     timePicker->SetIsCreateDialogComponent(true);
73     timePicker->SetHour24(isUseMilitaryTime);
74     component = timePicker;
75 
76     auto datePicker = AceType::DynamicCast<PickerBaseComponent>(component);
77     DialogProperties properties {};
78     properties.alignment = Container::LessThanAPIVersion(PlatformVersion::VERSION_ELEVEN) ? DialogAlignment::CENTER
79                                                                                           : DialogAlignment::DEFAULT;
80     properties.customComponent = datePicker;
81     properties.customStyle = true;
82     auto acceptId = EventMarker(std::move(onAccept));
83     datePicker->SetDialogAcceptEvent(acceptId);
84     auto cancelId = EventMarker(std::move(onCancel));
85     datePicker->SetDialogCancelEvent(cancelId);
86     auto changeId = EventMarker(std::move(onChange));
87     datePicker->SetDialogChangeEvent(changeId);
88     datePicker->SetDialogName("TimePickerDialog");
89     datePicker->OpenDialog(properties);
90 }
91 } // namespace OHOS::Ace::Framework
92