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 #ifndef FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_REFRESH_RENDER_REFRESH_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_REFRESH_RENDER_REFRESH_H
18 
19 #include "base/memory/ace_type.h"
20 #include "base/utils/system_properties.h"
21 #include "core/animation/animation.h"
22 #include "core/animation/animator.h"
23 #include "core/components/box/box_component.h"
24 #include "core/components/box/render_box.h"
25 #include "core/components/display/render_display.h"
26 #include "core/components/progress/loading_progress_component.h"
27 #include "core/components/progress/render_loading_progress.h"
28 #include "core/components/refresh/refresh_component.h"
29 #include "core/components/refresh/refresh_controller.h"
30 #include "core/components/scroll/scrollable.h"
31 #include "core/components/text/render_text.h"
32 #include "core/components/text/text_component.h"
33 #include "core/gestures/drag_recognizer.h"
34 #include "core/pipeline/base/render_node.h"
35 
36 namespace OHOS::Ace {
37 
38 class RenderRefresh : public RenderNode {
39     DECLARE_ACE_TYPE(RenderRefresh, RenderNode)
40 
41 public:
42     RenderRefresh();
43     ~RenderRefresh() override = default;
44 
45     static RefPtr<RenderNode> Create();
46     void UpdateTouchRect() override;
47     void Update(const RefPtr<Component>& component) override;
48     void OnHiddenChanged(bool hidden) override;
49     void PerformLayout() override;
50     void UpdateScrollableOffset(double delta);
51     void HandleDragUpdate(double delta);
52     void HandleDragEnd();
53     void HandleDragCancel();
SetHasScrollableChild(bool hasScrollableChild)54     void SetHasScrollableChild(bool hasScrollableChild)
55     {
56         hasScrollableChild_ = hasScrollableChild;
57     }
58 
GetStatus()59     RefreshStatus GetStatus() const
60     {
61         return refreshStatus_;
62     }
63 
SetRefreshStatus(bool refreshing)64     void SetRefreshStatus(bool refreshing)
65     {
66         refreshing_ = refreshing;
67     }
68 
GetRefreshing()69     bool GetRefreshing() const
70     {
71         return refreshing_;
72     }
73 
GetInspectorOffset()74     Dimension GetInspectorOffset() const
75     {
76         return inspectorOffset_;
77     }
78 
GetFriction()79     double GetFriction() const
80     {
81         // Percent
82         return frictionRatio_ * 100.0;
83     }
84 
85 protected:
86     void OnTouchTestHit(
87         const Offset& coordinateOffset, const TouchRestrict& touchRestrict, TouchTestResult& result) override;
88 
GetTimeBox()89     const RefPtr<RenderBox>& GetTimeBox() const
90     {
91         return timeBox_;
92     }
93 
GetDisplay()94     const RefPtr<RenderDisplay>& GetDisplay() const
95     {
96         return display_;
97     }
98 
IsShowLastTime()99     bool IsShowLastTime() const
100     {
101         return showLastTime_;
102     }
103 
IsWatch()104     bool IsWatch() const
105     {
106         return SystemProperties::GetDeviceType() == DeviceType::WATCH;
107     }
108 
109 private:
110     void CalcLoadingParams(const RefPtr<Component>& component);
111     void Initialize();
112     void InitAccessibilityEventListener();
113 
114     void UpdateScrollOffset(double value);
115     void FireRefreshEvent() const;
116     void FirePullDownEvent(const std::string& state) const;
117     void StartAnimation(double start, double end, bool isFinished);
118     void HandleStopListener(const bool isFinished);
119     void ResetStatus();
120     RefreshStatus GetNextStatus();
121 
122     double MaxScrollableHeight() const;
123     double GetFriction(double percentage) const;
124     double GetOffset(double offset) const;
125     double GetLoadingDiameter() const;
126     double GetOpacity() const;
127     Offset GetShowTimeOffset() const;
128     Offset GetLoadingOffset() const;
129     std::string GetFormatDateTime();
130 
131     Offset scrollableOffset_;
132     RefPtr<DragRecognizer> dragDetector_;
133     RefPtr<Animation<double>> translate_;
134     RefPtr<Animator> animator_;
135 
136     // onRefresh event
137     std::function<void(const std::string&)> refreshEvent_;
138 
139     // OnPullDownStartEvent
140     std::function<void(const std::string&)> pullDownEvent_;
141 
142     std::function<void(int)> onStateChange_;
143     std::function<void(void)> onRefreshing_;
144 
145     bool showLastTime_ = false;
146     bool refreshing_ = false;
147     bool isInitialized_ = false;
148     bool isRefresh_ = false;
149     bool isUseOffset_ = false; // Whether use the indicator offset or default
150     RefreshType refreshType_ = RefreshType::AUTO;
151     Color progressColor_;
152     Color backgroundColor_;
153 
154     RefPtr<RefreshController> refreshController_;
155     RefreshStatus refreshStatus_ = RefreshStatus::INACTIVE;
156 
157     // Used for loading
158     RefPtr<LoadingProgressComponent> loadingComponent_;
159     RefPtr<BoxComponent> loadingBoxComponent_;
160     RefPtr<BoxComponent> loadingBackgroundBoxComponent_;
161 
162     // This is the progress loading
163     RefPtr<RenderLoadingProgress> loading_;
164 
165     // This is used for position
166     RefPtr<RenderBox> loadingBox_;
167 
168     // This is used for progress background include circle and color
169     RefPtr<Decoration> decoration_;
170     RefPtr<RenderBox> loadingBackgroundBox_;
171     RefPtr<RenderNode> columnChild_;
172 
173     // Used for loading time
174     RefPtr<TextComponent> timeComponent_;
175     RefPtr<BoxComponent> timeBoxComponent_;
176     RefPtr<DisplayComponent> displayComponent_;
177     RefPtr<RenderText> time_;
178     RefPtr<RenderBox> timeBox_;
179     RefPtr<RenderDisplay> display_;
180 
181     // Used for different status
182     double triggerRefreshDistance_ = 0.0;
183     double triggerLoadingDistance_ = 0.0;
184     double triggerShowTimeDistance_ = 0.0;
185     double loadingDiameter_ = 0.0;
186     double maxScrollOffset_ = 0.0;
187     double indicatorOffset_ = 0.0;
188     double frictionRatio_ = 0.0;
189     double timeDistance_ = 0.0;
190     double timeOffset_ = 0.0;
191     bool hasScrollableChild_ = false;
192     Dimension inspectorOffset_;
193 
194     // Use for update loading size when screen size changed.
195     double scale_ = 0.0;
196     WeakPtr<Component> refreshComponent_;
197 
198     std::string timeText_;
199     std::string lastTimeText_;
200     std::function<void(const std::string&)> changeEvent_;
201 };
202 
203 } // namespace OHOS::Ace
204 
205 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_REFRESH_RENDER_REFRESH_H
206