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 SetOnUpdate(FormCallback && onUpdate)61 void SetOnUpdate(FormCallback&& onUpdate) 62 { 63 onUpdate_ = std::move(onUpdate); 64 } 65 SetOnCache(FormCacheCallback && onCache)66 void SetOnCache(FormCacheCallback&& onCache) 67 { 68 onCache_ = std::move(onCache); 69 } 70 FireOnAcquired(const std::string & param)71 void FireOnAcquired(const std::string& param) const 72 { 73 if (onAcquired_) { 74 auto onAcquired = onAcquired_; 75 onAcquired(param); 76 } 77 } 78 FireOnError(const std::string & param)79 void FireOnError(const std::string& param) const 80 { 81 if (onError_) { 82 auto onError = onError_; 83 onError(param); 84 } 85 } 86 FireOnUninstall(const std::string & param)87 void FireOnUninstall(const std::string& param) const 88 { 89 if (onUninstall_) { 90 auto onUninstall = onUninstall_; 91 onUninstall(param); 92 } 93 } 94 FireOnRouter(const std::string & param)95 void FireOnRouter(const std::string& param) const 96 { 97 if (onRouter_) { 98 auto onRouter = onRouter_; 99 onRouter(param); 100 } 101 } 102 FireOnLoad(const std::string & param)103 void FireOnLoad(const std::string& param) const 104 { 105 if (onLoad_) { 106 auto onLoad = onLoad_; 107 onLoad(param); 108 } 109 } 110 FireOnUpdate(const std::string & param)111 void FireOnUpdate(const std::string& param) const 112 { 113 if (onUpdate_) { 114 auto onUpdate = onUpdate_; 115 onUpdate(param); 116 } 117 } 118 FireOnCache()119 void FireOnCache() const 120 { 121 if (onCache_) { 122 auto onCache = onCache_; 123 onCache(); 124 } 125 } 126 127 private: 128 FormCallback onAcquired_; 129 FormCallback onError_; 130 FormCallback onUninstall_; 131 FormCallback onRouter_; 132 FormCallback onLoad_; 133 FormCallback onUpdate_; 134 FormCacheCallback onCache_; 135 }; 136 137 } // namespace OHOS::Ace::NG 138 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_FORM_FORM_EVENT_HUB_H