• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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 #ifndef FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_REFRESH_EVENT_HUB_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_REFRESH_EVENT_HUB_H
18 
19 #include "frameworks/base/memory/ace_type.h"
20 #include "frameworks/core/components_ng/event/event_hub.h"
21 
22 namespace OHOS::Ace::NG {
23 
24 using StateChangeEvent = std::function<void(const int32_t)>;
25 using ChangeEvent = std::function<void(const std::string)>;
26 using RefreshingEvent = std::function<void()>;
27 
28 class RefreshEventHub : public EventHub {
29     DECLARE_ACE_TYPE(RefreshEventHub, EventHub)
30 
31 public:
32     RefreshEventHub() = default;
33     ~RefreshEventHub() override = default;
34 
SetOnStateChange(StateChangeEvent && stateChange)35     void SetOnStateChange(StateChangeEvent&& stateChange)
36     {
37         stateChange_ = std::move(stateChange);
38     }
39 
FireOnStateChange(const int32_t value)40     void FireOnStateChange(const int32_t value) const
41     {
42         if (stateChange_) {
43             stateChange_(value);
44         }
45     }
SetOnRefreshing(RefreshingEvent && refreshing)46     void SetOnRefreshing(RefreshingEvent&& refreshing)
47     {
48         refreshing_ = std::move(refreshing);
49     }
50 
FireOnRefreshing()51     void FireOnRefreshing() const
52     {
53         if (refreshing_) {
54             refreshing_();
55         }
56     }
57 
SetChangeEvent(ChangeEvent && changeEvent)58     void SetChangeEvent(ChangeEvent&& changeEvent)
59     {
60         changeEvent_ = std::move(changeEvent);
61     }
62 
FireChangeEvent(const std::string & value)63     void FireChangeEvent(const std::string& value) const
64     {
65         if (changeEvent_) {
66             changeEvent_(value);
67         }
68     }
69 
70 private:
71     StateChangeEvent stateChange_;
72     RefreshingEvent refreshing_;
73     ChangeEvent changeEvent_;
74     ACE_DISALLOW_COPY_AND_MOVE(RefreshEventHub);
75 };
76 } // namespace OHOS::Ace::NG
77 
78 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_REFRESH_EVENT_HUB_H