1 /*
2 * Copyright (c) 2021 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 #include "core/components/refresh/refresh_component.h"
18 #include "core/components/refresh/refresh_theme.h"
19 #include "frameworks/bridge/declarative_frontend/jsview/js_view_common_def.h"
20
21 namespace OHOS::Ace::Framework {
22
JSBind(BindingTarget globalObj)23 void JSRefresh::JSBind(BindingTarget globalObj)
24 {
25 JSClass<JSRefresh>::Declare("Refresh");
26 MethodOptions opt = MethodOptions::NONE;
27 JSClass<JSRefresh>::StaticMethod("create", &JSRefresh::Create, opt);
28 JSClass<JSRefresh>::StaticMethod("onStateChange", &JSRefresh::OnStateChange);
29 JSClass<JSRefresh>::StaticMethod("onRefreshing", &JSRefresh::OnRefreshing);
30 JSClass<JSRefresh>::StaticMethod("onAppear", &JSInteractableView::JsOnAppear);
31 JSClass<JSRefresh>::StaticMethod("onDisAppear", &JSInteractableView::JsOnDisAppear);
32 JSClass<JSRefresh>::Inherit<JSViewAbstract>();
33 JSClass<JSRefresh>::Bind(globalObj);
34 }
35
Create(const JSCallbackInfo & info)36 void JSRefresh::Create(const JSCallbackInfo& info)
37 {
38 if (info.Length() < 1 || !info[0]->IsObject()) {
39 LOGE("refresh create error, info is non-valid");
40 return;
41 }
42
43 RefPtr<RefreshComponent> refreshComponent = AceType::MakeRefPtr<RefreshComponent>();
44 RefPtr<RefreshTheme> theme = GetTheme<RefreshTheme>();
45 if (!theme) {
46 LOGE("Refresh Theme is null");
47 return;
48 }
49 refreshComponent->SetLoadingDistance(theme->GetLoadingDistance());
50 refreshComponent->SetRefreshDistance(theme->GetRefreshDistance());
51 refreshComponent->SetProgressDistance(theme->GetProgressDistance());
52 refreshComponent->SetProgressDiameter(theme->GetProgressDiameter());
53 refreshComponent->SetMaxDistance(theme->GetMaxDistance());
54 refreshComponent->SetShowTimeDistance(theme->GetShowTimeDistance());
55 refreshComponent->SetTextStyle(theme->GetTextStyle());
56 refreshComponent->SetProgressColor(theme->GetProgressColor());
57 refreshComponent->SetBackgroundColor(theme->GetBackgroundColor());
58
59 auto paramObject = JSRef<JSObject>::Cast(info[0]);
60 auto refreshing = paramObject->GetProperty("refreshing");
61 if (!refreshing->IsBoolean()) {
62 LOGE("refresh create error, info is non-valid");
63 return;
64 }
65 refreshComponent->SetRefreshing(refreshing->ToBoolean());
66
67 auto jsOffset = paramObject->GetProperty("offset");
68 Dimension offset;
69 if (ParseJsDimensionVp(jsOffset, offset)) {
70 if (offset.Value() <= 0.0) {
71 refreshComponent->SetRefreshDistance(theme->GetRefreshDistance());
72 } else {
73 refreshComponent->SetUseOffset(true);
74 refreshComponent->SetIndicatorOffset(offset);
75 }
76 }
77
78 auto friction = paramObject->GetProperty("friction");
79 if (friction->IsNumber()) {
80 refreshComponent->SetFriction(friction->ToNumber<int32_t>());
81 if (friction->ToNumber<int32_t>() <= 0) {
82 refreshComponent->IsRefresh(true);
83 }
84 }
85 ViewStackProcessor::GetInstance()->Push(refreshComponent);
86 }
87
OnStateChange(const JSCallbackInfo & args)88 void JSRefresh::OnStateChange(const JSCallbackInfo& args)
89 {
90 if (!JSViewBindEvent(&RefreshComponent::SetOnStateChange, args)) {
91 LOGW("Failed to bind event");
92 }
93 args.ReturnSelf();
94 }
95
OnRefreshing(const JSCallbackInfo & args)96 void JSRefresh::OnRefreshing(const JSCallbackInfo& args)
97 {
98 if (!JSViewBindEvent(&RefreshComponent::SetOnRefreshing, args)) {
99 LOGW("Failed to bind event");
100 }
101 args.ReturnSelf();
102 }
103
104 } // namespace OHOS::Ace::Framework
105