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_rating_ffi.h"
17 
18 #include "base/log/ace_scoring_log.h"
19 #include "cj_lambda.h"
20 #include "bridge/cj_frontend/interfaces/cj_ffi/cj_view_abstract_ffi.h"
21 #include "bridge/cj_frontend/interfaces/cj_ffi/utils.h"
22 #include "core/components_ng/pattern/rating/rating_model_ng.h"
23 
24 namespace {
25     constexpr double RATING_SCORE_DEFAULT = 0;
26     constexpr int32_t STARS_DEFAULT = 5;
27     constexpr double STEPS_DEFAULT = 0.5;
28 }
29 
30 using namespace OHOS::Ace;
31 using namespace OHOS::Ace::Framework;
32 
33 extern "C" {
FfiOHOSAceFrameworkRatingCreate(double rating,bool indicator)34 void FfiOHOSAceFrameworkRatingCreate(double rating, bool indicator)
35 {
36     double ratnum = rating <= 0 ?  RATING_SCORE_DEFAULT : rating;
37     RatingModel::GetInstance()->Create(ratnum, indicator);
38 }
39 
FfiOHOSAceFrameworkRatingSetStars(int32_t value)40 void FfiOHOSAceFrameworkRatingSetStars(int32_t value)
41 {
42     auto stars = value <= 0 ? STARS_DEFAULT : value;
43     RatingModel::GetInstance()->SetStars(stars);
44 }
45 
FfiOHOSAceFrameworkRatingSetStepSize(double value)46 void FfiOHOSAceFrameworkRatingSetStepSize(double value)
47 {
48     static const double stepSizeMin = 0.1;
49     double stepSize = LessNotEqual(value, stepSizeMin) ? STEPS_DEFAULT : value;
50     RatingModel::GetInstance()->SetStepSize(stepSize);
51 }
52 
FfiOHOSAceFrameworkRatingSetStarStyle(const char * backgroundUri,const char * foregroundUri,const char * secondaryUri)53 void FfiOHOSAceFrameworkRatingSetStarStyle(
54     const char* backgroundUri, const char* foregroundUri, const char* secondaryUri)
55 {
56     bool backgroundisempty = !(strlen(backgroundUri) != 0);
57     bool secondaryisempty = !(strlen(secondaryUri) != 0);
58     bool foregroundisempty = !(strlen(foregroundUri) != 0);
59     RatingModel::GetInstance()->SetForegroundSrc(foregroundUri, foregroundisempty);
60     RatingModel::GetInstance()->SetSecondarySrc(secondaryUri, secondaryisempty);
61     RatingModel::GetInstance()->SetBackgroundSrc(backgroundUri, backgroundisempty);
62 }
63 
FfiOHOSAceFrameworkRatingSetOnChange(void (* callback)(double))64 void FfiOHOSAceFrameworkRatingSetOnChange(void (*callback)(double))
65 {
66     auto onChange = [lambda = CJLambda::Create(callback)](const std::string& value) {
67         lambda(std::stod(value));
68     };
69     RatingModel::GetInstance()->SetOnChange(onChange);
70 }
71 }
72