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 onAcquired_(param); 70 } 71 } 72 FireOnError(const std::string & param)73 void FireOnError(const std::string& param) const 74 { 75 if (onError_) { 76 onError_(param); 77 } 78 } 79 FireOnUninstall(const std::string & param)80 void FireOnUninstall(const std::string& param) const 81 { 82 if (onUninstall_) { 83 onUninstall_(param); 84 } 85 } 86 FireOnRouter(const std::string & param)87 void FireOnRouter(const std::string& param) const 88 { 89 if (onRouter_) { 90 onRouter_(param); 91 } 92 } 93 FireOnLoad(const std::string & param)94 void FireOnLoad(const std::string& param) const 95 { 96 if (onLoad_) { 97 onLoad_(param); 98 } 99 } 100 FireOnCache()101 void FireOnCache() const 102 { 103 if (onCache_) { 104 onCache_(); 105 } 106 } 107 108 private: 109 FormCallback onAcquired_; 110 FormCallback onError_; 111 FormCallback onUninstall_; 112 FormCallback onRouter_; 113 FormCallback onLoad_; 114 FormCacheCallback onCache_; 115 }; 116 117 } // namespace OHOS::Ace::NG 118 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_FORM_FORM_EVENT_HUB_H