1 /*
2  * Copyright (c) 2022 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/rating_model_impl.h"
17 
18 #include "core/components/box/box_component.h"
19 #include "core/components/rating/rating_component.h"
20 #include "core/components/rating/rating_theme.h"
21 #include "frameworks/bridge/declarative_frontend/view_stack_processor.h"
22 
23 namespace OHOS::Ace::Framework {
24 
Create(double rating,bool indicator)25 void RatingModelImpl::Create(double rating, bool indicator)
26 {
27     auto component = AceType::MakeRefPtr<RatingComponent>();
28     component->SetMouseAnimationType(HoverAnimationType::NONE);
29     component->SetRatingScore(rating);
30     component->SetIndicator(indicator);
31     ViewStackProcessor::GetInstance()->ClaimElementId(component);
32     ViewStackProcessor::GetInstance()->Push(component);
33 
34     auto pipelineContext = PipelineContext::GetCurrentContext();
35     CHECK_NULL_VOID(pipelineContext);
36     auto themeManager = pipelineContext->GetThemeManager();
37     CHECK_NULL_VOID(themeManager);
38     auto theme = themeManager->GetTheme<RatingTheme>();
39     CHECK_NULL_VOID(theme);
40 
41     auto boxComponent = ViewStackProcessor::GetInstance()->GetBoxComponent();
42     if (component->GetIndicator()) {
43         if (boxComponent->GetHeightDimension().Value() < 0.0) {
44             boxComponent->SetHeight(theme->GetRatingMiniHeight());
45         }
46         if (boxComponent->GetWidthDimension().Value() < 0.0) {
47             boxComponent->SetWidth(theme->GetRatingMiniWidth());
48         }
49         component->SetPaddingVertical(Dimension());
50         component->SetMiniResIdFromTheme(theme);
51     } else {
52         if (boxComponent->GetHeightDimension().Value() < 0.0) {
53             boxComponent->SetHeight(theme->GetRatingHeight());
54         }
55         if (boxComponent->GetWidthDimension().Value() < 0.0) {
56             boxComponent->SetWidth(theme->GetRatingWidth());
57         }
58         component->SetPaddingVertical(theme->GetPaddingVertical());
59         component->SetResIdFromTheme(theme);
60     }
61     component->SetThemeStyle(theme);
62 }
63 
SetRatingScore(double value)64 void RatingModelImpl::SetRatingScore(double value)
65 {
66     auto ratingComponent = AceType::DynamicCast<RatingComponent>(ViewStackProcessor::GetInstance()->GetMainComponent());
67     CHECK_NULL_VOID(ratingComponent);
68     ratingComponent->SetRatingScore(value);
69 }
70 
SetIndicator(bool value)71 void RatingModelImpl::SetIndicator(bool value)
72 {
73     auto ratingComponent = AceType::DynamicCast<RatingComponent>(ViewStackProcessor::GetInstance()->GetMainComponent());
74     CHECK_NULL_VOID(ratingComponent);
75     ratingComponent->SetIndicator(value);
76 }
77 
SetStars(int32_t value)78 void RatingModelImpl::SetStars(int32_t value)
79 {
80     auto ratingComponent = AceType::DynamicCast<RatingComponent>(ViewStackProcessor::GetInstance()->GetMainComponent());
81     CHECK_NULL_VOID(ratingComponent);
82     ratingComponent->SetStarNum(value);
83 }
84 
SetStepSize(double value)85 void RatingModelImpl::SetStepSize(double value)
86 {
87     auto ratingComponent = AceType::DynamicCast<RatingComponent>(ViewStackProcessor::GetInstance()->GetMainComponent());
88     CHECK_NULL_VOID(ratingComponent);
89     ratingComponent->SetStepSize(value);
90 }
91 
SetForegroundSrc(const std::string & value,bool flag)92 void RatingModelImpl::SetForegroundSrc(const std::string& value, bool flag)
93 {
94     auto ratingComponent = AceType::DynamicCast<RatingComponent>(ViewStackProcessor::GetInstance()->GetMainComponent());
95     CHECK_NULL_VOID(ratingComponent);
96     ratingComponent->SetForegroundSrc(value);
97 }
98 
SetSecondarySrc(const std::string & value,bool flag)99 void RatingModelImpl::SetSecondarySrc(const std::string& value, bool flag)
100 {
101     auto ratingComponent = AceType::DynamicCast<RatingComponent>(ViewStackProcessor::GetInstance()->GetMainComponent());
102     CHECK_NULL_VOID(ratingComponent);
103     ratingComponent->SetSecondarySrc(value);
104 }
105 
SetBackgroundSrc(const std::string & value,bool flag)106 void RatingModelImpl::SetBackgroundSrc(const std::string& value, bool flag)
107 {
108     auto ratingComponent = AceType::DynamicCast<RatingComponent>(ViewStackProcessor::GetInstance()->GetMainComponent());
109     CHECK_NULL_VOID(ratingComponent);
110     ratingComponent->SetBackgroundSrc(value);
111 }
112 
SetOnChange(ChangeEvent && onChange)113 void RatingModelImpl::SetOnChange(ChangeEvent&& onChange)
114 {
115     auto ratingComponent = AceType::DynamicCast<RatingComponent>(ViewStackProcessor::GetInstance()->GetMainComponent());
116     CHECK_NULL_VOID(ratingComponent);
117     ratingComponent->SetOnChange(onChange);
118 }
119 
120 } // namespace OHOS::Ace::Framework