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