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_PATTERN_FORM_FORM_EVENT_HUB_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_FORM_FORM_EVENT_HUB_H
18 
19 #include <functional>
20 
21 #include "base/memory/ace_type.h"
22 #include "core/components_ng/event/event_hub.h"
23 
24 namespace OHOS::Ace::NG {
25 
26 using FormCallback = std::function<void(const std::string&)>;
27 using FormCacheCallback = std::function<void()>;
28 
29 class FormEventHub : public EventHub {
30     DECLARE_ACE_TYPE(FormEventHub, EventHub)
31 
32 public:
33     FormEventHub() = default;
34     ~FormEventHub() override = default;
35 
SetOnAcquired(FormCallback && onAcquired)36     void SetOnAcquired(FormCallback&& onAcquired)
37     {
38         onAcquired_ = std::move(onAcquired);
39     }
40 
SetOnError(FormCallback && onError)41     void SetOnError(FormCallback&& onError)
42     {
43         onError_ = std::move(onError);
44     }
45 
SetOnUninstall(FormCallback && onUninstall)46     void SetOnUninstall(FormCallback&& onUninstall)
47     {
48         onUninstall_ = std::move(onUninstall);
49     }
50 
SetOnRouter(FormCallback && onRouter)51     void SetOnRouter(FormCallback&& onRouter)
52     {
53         onRouter_ = std::move(onRouter);
54     }
55 
SetOnLoad(FormCallback && onLoad)56     void SetOnLoad(FormCallback&& onLoad)
57     {
58         onLoad_ = std::move(onLoad);
59     }
60 
SetOnCache(FormCacheCallback && onCache)61     void SetOnCache(FormCacheCallback&& onCache)
62     {
63         onCache_ = std::move(onCache);
64     }
65 
FireOnAcquired(const std::string & param)66     void FireOnAcquired(const std::string& param) const
67     {
68         if (onAcquired_) {
69             auto onAcquired = onAcquired_;
70             onAcquired(param);
71         }
72     }
73 
FireOnError(const std::string & param)74     void FireOnError(const std::string& param) const
75     {
76         if (onError_) {
77             auto onError = onError_;
78             onError(param);
79         }
80     }
81 
FireOnUninstall(const std::string & param)82     void FireOnUninstall(const std::string& param) const
83     {
84         if (onUninstall_) {
85             auto onUninstall = onUninstall_;
86             onUninstall(param);
87         }
88     }
89 
FireOnRouter(const std::string & param)90     void FireOnRouter(const std::string& param) const
91     {
92         if (onRouter_) {
93             auto onRouter = onRouter_;
94             onRouter(param);
95         }
96     }
97 
FireOnLoad(const std::string & param)98     void FireOnLoad(const std::string& param) const
99     {
100         if (onLoad_) {
101             auto onLoad = onLoad_;
102             onLoad(param);
103         }
104     }
105 
FireOnCache()106     void FireOnCache() const
107     {
108         if (onCache_) {
109             auto onCache = onCache_;
110             onCache();
111         }
112     }
113 
114 private:
115     FormCallback onAcquired_;
116     FormCallback onError_;
117     FormCallback onUninstall_;
118     FormCallback onRouter_;
119     FormCallback onLoad_;
120     FormCacheCallback onCache_;
121 };
122 
123 } // namespace OHOS::Ace::NG
124 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_FORM_FORM_EVENT_HUB_H