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 #include "core/components_ng/pattern/scrollable/refresh_coordination.h"
16
17 #include "core/common/container.h"
18 #include "core/components_ng/pattern/refresh/refresh_pattern.h"
19
20 namespace OHOS::Ace::NG {
FindRefreshNode() const21 RefPtr<FrameNode> RefreshCoordination::FindRefreshNode() const
22 {
23 auto scrollableNode = scrollableNode_.Upgrade();
24 CHECK_NULL_RETURN(scrollableNode, nullptr);
25 auto parent = scrollableNode->GetParent();
26 while (parent) {
27 if (InstanceOf<FrameNode>(parent)) {
28 auto parentFrameNode = DynamicCast<FrameNode>(parent);
29 if (InstanceOf<RefreshPattern>(parentFrameNode->GetPattern())) {
30 return DynamicCast<FrameNode>(parent);
31 }
32 }
33 parent = parent->GetParent();
34 }
35 return nullptr;
36 }
37
CreateCoordinationEvent()38 RefPtr<ScrollableCoordinationEvent> RefreshCoordination::CreateCoordinationEvent()
39 {
40 if (coordinationEvent_) {
41 return coordinationEvent_;
42 }
43 auto refreshNode = refreshNode_.Upgrade();
44 CHECK_NULL_RETURN(refreshNode, nullptr);
45 auto refreshPattern = DynamicCast<RefreshPattern>(refreshNode->GetPattern());
46 CHECK_NULL_RETURN(refreshPattern, nullptr);
47 auto coordinationEvent = AceType::MakeRefPtr<ScrollableCoordinationEvent>();
48 refreshPattern->InitCoordinationEvent(coordinationEvent);
49 return coordinationEvent;
50 }
51
OnScrollStart(bool isDrag,float mainVelocity) const52 void RefreshCoordination::OnScrollStart(bool isDrag, float mainVelocity) const
53 {
54 CHECK_NULL_VOID(coordinationEvent_);
55 auto onScrollStart = coordinationEvent_->GetOnScrollStartEvent();
56 if (onScrollStart) {
57 onScrollStart(isDrag, mainVelocity);
58 }
59 }
60
OnScroll(float offset,float mainVelocity) const61 bool RefreshCoordination::OnScroll(float offset, float mainVelocity) const
62 {
63 CHECK_NULL_RETURN(coordinationEvent_, false);
64 auto onScroll = coordinationEvent_->GetOnScroll();
65 CHECK_NULL_RETURN(onScroll, false);
66 return onScroll(offset, mainVelocity);
67 }
68
OnScrollEnd(float mainVelocity) const69 void RefreshCoordination::OnScrollEnd(float mainVelocity) const
70 {
71 CHECK_NULL_VOID(coordinationEvent_);
72 auto onScrollEnd = coordinationEvent_->GetOnScrollEndEvent();
73 if (onScrollEnd) {
74 onScrollEnd(mainVelocity);
75 }
76 }
77
IsRefreshInScroll() const78 bool RefreshCoordination::IsRefreshInScroll() const
79 {
80 auto refreshNode = refreshNode_.Upgrade();
81 CHECK_NULL_RETURN(refreshNode, false);
82 auto pattern = refreshNode->GetPattern<RefreshPattern>();
83 CHECK_NULL_RETURN(pattern, false);
84 return GreatNotEqual(pattern->GetScrollOffsetValue(), 0.0);
85 }
86 } // namespace OHOS::Ace::NG