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/textpicker_model_impl.h"
17 
18 #include "bridge/declarative_frontend/jsview/js_view_common_def.h"
19 #include "core/components/picker/picker_text_component.h"
20 
21 namespace OHOS::Ace::Framework {
Create(RefPtr<PickerTheme> pickerTheme,uint32_t columnKind)22 void TextPickerModelImpl::Create(RefPtr<PickerTheme> pickerTheme, uint32_t columnKind)
23 {
24     (void)columnKind;
25     auto textPicker = AceType::MakeRefPtr<PickerTextComponent>();
26     ViewStackProcessor::GetInstance()->ClaimElementId(textPicker);
27 
28     textPicker->SetIsDialog(false);
29     textPicker->SetHasButtons(false);
30     textPicker->SetTheme(pickerTheme);
31     ViewStackProcessor::GetInstance()->Push(textPicker);
32 }
33 
SetDefaultPickerItemHeight(const Dimension & value)34 void TextPickerModelImpl::SetDefaultPickerItemHeight(const Dimension& value)
35 {
36     auto textPicker = AceType::DynamicCast<PickerTextComponent>(ViewStackProcessor::GetInstance()->GetMainComponent());
37     textPicker->SetDefaultHeight(true);
38     JSViewSetProperty(&PickerTextComponent::SetColumnHeight, value);
39 }
40 
SetSelected(uint32_t value)41 void TextPickerModelImpl::SetSelected(uint32_t value)
42 {
43     JSViewSetProperty(&PickerTextComponent::SetSelected, value);
44 }
45 
SetRange(const std::vector<NG::RangeContent> & value)46 void TextPickerModelImpl::SetRange(const std::vector<NG::RangeContent>& value)
47 {
48     if (!value.size()) {
49         return;
50     }
51     std::vector<std::string> textRange;
52     for (auto& range : value) {
53         textRange.emplace_back(range.text_);
54     }
55     JSViewSetProperty(&PickerTextComponent::SetRange, textRange);
56 }
57 
SetOnCascadeChange(TextCascadeChangeEvent && onChange)58 void TextPickerModelImpl::SetOnCascadeChange(TextCascadeChangeEvent&& onChange)
59 {
60     auto func = onChange;
61     auto onChangeEvent = [func](const std::string& value, const double& index) {
62         std::vector<std::string> changeValue { value };
63         std::vector<double> changeIndex { index };
64         func(changeValue, changeIndex);
65     };
66     JSViewSetProperty(&PickerBaseComponent::SetOnTextChange, std::move(onChangeEvent));
67 }
68 
SetOnScrollStop(TextCascadeChangeEvent && onScrollStop)69 void TextPickerModelImpl::SetOnScrollStop(TextCascadeChangeEvent&& onScrollStop) {}
70 
SetBackgroundColor(const Color & color)71 void TextPickerModelImpl::SetBackgroundColor(const Color& color)
72 {
73     auto pickerBase = AceType::DynamicCast<PickerBaseComponent>(ViewStackProcessor::GetInstance()->GetMainComponent());
74     if (!pickerBase) {
75         LOGE("PickerBaseComponent is null");
76         return;
77     }
78 
79     pickerBase->SetHasBackgroundColor(true);
80 }
81 
CreateObject()82 RefPtr<AceType> TextPickerDialogModelImpl::CreateObject()
83 {
84     return AceType::MakeRefPtr<PickerTextComponent>();
85 }
86 
SetTextPickerDialogShow(RefPtr<AceType> & PickerText,NG::TextPickerSettingData & settingData,std::function<void ()> && onCancel,std::function<void (const std::string &)> && onAccept,std::function<void (const std::string &)> && onChange,std::function<void (const std::string &)> && onScrollStop,TextPickerDialog & textPickerDialog,TextPickerDialogEvent & textPickerDialogEvent,const std::vector<ButtonInfo> & buttonInfos)87 void TextPickerDialogModelImpl::SetTextPickerDialogShow(RefPtr<AceType>& PickerText,
88     NG::TextPickerSettingData& settingData, std::function<void()>&& onCancel,
89     std::function<void(const std::string&)>&& onAccept, std::function<void(const std::string&)>&& onChange,
90     std::function<void(const std::string&)>&& onScrollStop, TextPickerDialog& textPickerDialog,
91     TextPickerDialogEvent& textPickerDialogEvent, const std::vector<ButtonInfo>& buttonInfos)
92 {
93     auto pickerText = AceType::DynamicCast<PickerTextComponent>(PickerText);
94     pickerText->SetIsDialog(true);
95     pickerText->SetIsCreateDialogComponent(true);
96     if (textPickerDialog.isDefaultHeight == true) {
97         pickerText->SetColumnHeight(textPickerDialog.height);
98         pickerText->SetDefaultHeight(true);
99     }
100     pickerText->SetSelected(textPickerDialog.selectedValue);
101     pickerText->SetRange(textPickerDialog.getRangeVector);
102 
103     DialogProperties properties {};
104     properties.alignment = Container::LessThanAPIVersion(PlatformVersion::VERSION_ELEVEN) ? DialogAlignment::CENTER
105                                                                                           : DialogAlignment::DEFAULT;
106     properties.customComponent = pickerText;
107     properties.customStyle = true;
108     auto acceptId = EventMarker(std::move(onAccept));
109     pickerText->SetDialogAcceptEvent(acceptId);
110     auto cancelId = EventMarker(std::move(onCancel));
111     pickerText->SetDialogCancelEvent(cancelId);
112     auto changeId = EventMarker(std::move(onChange));
113     pickerText->SetDialogChangeEvent(changeId);
114     pickerText->SetDialogName("pickerTextDialog");
115     pickerText->OpenDialog(properties);
116 }
117 } // namespace OHOS::Ace::Framework
118