• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-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 "frameworks/bridge/declarative_frontend/jsview/js_refresh.h"
17 
18 #include <cstdint>
19 
20 #include "base/log/ace_scoring_log.h"
21 #include "bridge/declarative_frontend/jsview/js_view_common_def.h"
22 #include "bridge/declarative_frontend/jsview/models/refresh_model_impl.h"
23 #include "core/components/refresh/refresh_theme.h"
24 #include "core/components_ng/pattern/refresh/refresh_model_ng.h"
25 
26 namespace OHOS::Ace {
27 namespace {
28 constexpr int32_t DEFAULT_FRICTION = 62;
29 constexpr int32_t MAX_FRICTION = 100;
30 } // namespace
31 std::unique_ptr<RefreshModel> RefreshModel::instance_ = nullptr;
32 
GetInstance()33 RefreshModel* RefreshModel::GetInstance()
34 {
35     if (!instance_) {
36 #ifdef NG_BUILD
37         instance_.reset(new NG::RefreshModelNG());
38 #else
39         if (Container::IsCurrentUseNewPipeline()) {
40             instance_.reset(new NG::RefreshModelNG());
41         } else {
42             instance_.reset(new Framework::RefreshModelImpl());
43         }
44 #endif
45     }
46     return instance_.get();
47 }
48 
49 } // namespace OHOS::Ace
50 
51 namespace OHOS::Ace::Framework {
52 
ParseRefreshingObject(const JSCallbackInfo & info,const JSRef<JSObject> & refreshing)53 void ParseRefreshingObject(const JSCallbackInfo& info, const JSRef<JSObject>& refreshing)
54 {
55     JSRef<JSVal> changeEventVal = refreshing->GetProperty("changeEvent");
56     CHECK_NULL_VOID(changeEventVal->IsFunction());
57 
58     auto jsFunc = AceType::MakeRefPtr<JsFunction>(JSRef<JSObject>(), JSRef<JSFunc>::Cast(changeEventVal));
59     auto changeEvent = [execCtx = info.GetExecutionContext(), func = std::move(jsFunc)](const std::string& param) {
60         JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx);
61         if (param != "true" && param != "false") {
62             LOGE("param is not equal true or false, invalid.");
63             return;
64         }
65         bool newValue = StringToBool(param);
66         ACE_SCORING_EVENT("Refresh.ChangeEvent");
67         auto newJSVal = JSRef<JSVal>::Make(ToJSValue(newValue));
68         func->ExecuteJS(1, &newJSVal);
69     };
70     RefreshModel::GetInstance()->SetChangeEvent(std::move(changeEvent));
71 }
72 
JSBind(BindingTarget globalObj)73 void JSRefresh::JSBind(BindingTarget globalObj)
74 {
75     JSClass<JSRefresh>::Declare("Refresh");
76     MethodOptions opt = MethodOptions::NONE;
77     JSClass<JSRefresh>::StaticMethod("create", &JSRefresh::Create, opt);
78     JSClass<JSRefresh>::StaticMethod("pop", &JSRefresh::Pop);
79     JSClass<JSRefresh>::StaticMethod("onStateChange", &JSRefresh::OnStateChange);
80     JSClass<JSRefresh>::StaticMethod("onRefreshing", &JSRefresh::OnRefreshing);
81     JSClass<JSRefresh>::StaticMethod("onAppear", &JSInteractableView::JsOnAppear);
82     JSClass<JSRefresh>::StaticMethod("onDisAppear", &JSInteractableView::JsOnDisAppear);
83     JSClass<JSRefresh>::Inherit<JSViewAbstract>();
84     JSClass<JSRefresh>::Bind(globalObj);
85 }
86 
Create(const JSCallbackInfo & info)87 void JSRefresh::Create(const JSCallbackInfo& info)
88 {
89     if (info.Length() < 1 || !info[0]->IsObject()) {
90         LOGE("refresh create error, info is non-valid");
91         return;
92     }
93     RefPtr<RefreshTheme> theme = GetTheme<RefreshTheme>();
94     if (!theme) {
95         LOGE("Refresh Theme is null");
96         return;
97     }
98     auto paramObject = JSRef<JSObject>::Cast(info[0]);
99     auto refreshing = paramObject->GetProperty("refreshing");
100     auto jsOffset = paramObject->GetProperty("offset");
101     auto friction = paramObject->GetProperty("friction");
102     RefreshModel::GetInstance()->Create();
103     RefreshModel::GetInstance()->SetLoadingDistance(theme->GetLoadingDistance());
104     RefreshModel::GetInstance()->SetRefreshDistance(theme->GetRefreshDistance());
105     RefreshModel::GetInstance()->SetProgressDistance(theme->GetProgressDistance());
106     RefreshModel::GetInstance()->SetProgressDiameter(theme->GetProgressDiameter());
107     RefreshModel::GetInstance()->SetMaxDistance(theme->GetMaxDistance());
108     RefreshModel::GetInstance()->SetShowTimeDistance(theme->GetShowTimeDistance());
109     RefreshModel::GetInstance()->SetTextStyle(theme->GetTextStyle());
110     RefreshModel::GetInstance()->SetProgressColor(theme->GetProgressColor());
111     RefreshModel::GetInstance()->SetProgressBackgroundColor(theme->GetBackgroundColor());
112 
113     if (refreshing->IsBoolean()) {
114         RefreshModel::GetInstance()->SetRefreshing(refreshing->ToBoolean());
115     } else {
116         JSRef<JSObject> refreshingObj = JSRef<JSObject>::Cast(refreshing);
117         ParseRefreshingObject(info, refreshingObj);
118         RefreshModel::GetInstance()->SetRefreshing(refreshingObj->GetProperty("value")->ToBoolean());
119     }
120     Dimension offset;
121     if (ParseJsDimensionVp(jsOffset, offset)) {
122         if (LessNotEqual(offset.Value(), 0.0) || offset.Unit() == DimensionUnit::PERCENT) {
123             RefreshModel::GetInstance()->SetRefreshDistance(theme->GetRefreshDistance());
124         } else {
125             RefreshModel::GetInstance()->SetUseOffset(true);
126             RefreshModel::GetInstance()->SetIndicatorOffset(offset);
127         }
128     }
129     if (friction->IsNumber()) {
130         auto frictionNumber = friction->ToNumber<int32_t>();
131         if (frictionNumber < 0 || frictionNumber > MAX_FRICTION) {
132             frictionNumber = DEFAULT_FRICTION;
133         }
134         RefreshModel::GetInstance()->SetFriction(frictionNumber);
135         if (friction->ToNumber<int32_t>() <= 0) {
136             RefreshModel::GetInstance()->IsRefresh(true);
137         }
138     }
139 }
140 
Pop()141 void JSRefresh::Pop()
142 {
143     RefreshModel::GetInstance()->Pop();
144 }
145 
OnStateChange(const JSCallbackInfo & args)146 void JSRefresh::OnStateChange(const JSCallbackInfo& args)
147 {
148     auto jsFunc = AceType::MakeRefPtr<JsFunction>(JSRef<JSObject>(), JSRef<JSFunc>::Cast(args[0]));
149     auto onStateChange = [execCtx = args.GetExecutionContext(), func = std::move(jsFunc)](const int32_t& value) {
150         JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx);
151         ACE_SCORING_EVENT("Refresh.OnStateChange");
152         auto newJSVal = JSRef<JSVal>::Make(ToJSValue(value));
153         func->ExecuteJS(1, &newJSVal);
154     };
155     RefreshModel::GetInstance()->SetOnStateChange(std::move(onStateChange));
156 }
157 
OnRefreshing(const JSCallbackInfo & args)158 void JSRefresh::OnRefreshing(const JSCallbackInfo& args)
159 {
160     auto jsFunc = AceType::MakeRefPtr<JsFunction>(JSRef<JSObject>(), JSRef<JSFunc>::Cast(args[0]));
161     auto onRefreshing = [execCtx = args.GetExecutionContext(), func = std::move(jsFunc)]() {
162         JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx);
163         ACE_SCORING_EVENT("Refresh.OnRefreshing");
164         auto newJSVal = JSRef<JSVal>::Make();
165         func->ExecuteJS(1, &newJSVal);
166     };
167     RefreshModel::GetInstance()->SetOnRefreshing(std::move(onRefreshing));
168 }
169 
170 } // namespace OHOS::Ace::Framework
171