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_IMAGE_IMAGE_EVENT_HUB_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_IMAGE_IMAGE_EVENT_HUB_H
18 
19 #include "base/memory/ace_type.h"
20 #include "core/components/image/image_event.h"
21 #include "core/components_ng/event/event_hub.h"
22 
23 namespace OHOS::Ace::NG {
24 
25 using CompleteEvent = std::function<void(const LoadImageSuccessEvent& info)>;
26 using ErrorEvent = std::function<void(const LoadImageFailEvent& info)>;
27 using FinishEvent = std::function<void()>;
28 
29 class ImageEventHub : public EventHub {
30     DECLARE_ACE_TYPE(ImageEventHub, EventHub)
31 
32 public:
33     ImageEventHub() = default;
34     ~ImageEventHub() override = default;
35 
SetOnError(ErrorEvent && errorEvent)36     void SetOnError(ErrorEvent&& errorEvent)
37     {
38         errorEvent_ = std::move(errorEvent);
39     }
40 
FireErrorEvent(const LoadImageFailEvent & info)41     void FireErrorEvent(const LoadImageFailEvent& info) const
42     {
43         if (errorEvent_) {
44             errorEvent_(info);
45         }
46     }
47 
SetOnComplete(CompleteEvent && completeEvent)48     void SetOnComplete(CompleteEvent&& completeEvent)
49     {
50         completeEvent_ = std::move(completeEvent);
51     }
52 
FireCompleteEvent(const LoadImageSuccessEvent & info)53     void FireCompleteEvent(const LoadImageSuccessEvent& info) const
54     {
55         if (completeEvent_) {
56             completeEvent_(info);
57         }
58     }
59 
SetOnFinish(FinishEvent && finishEvent)60     void SetOnFinish(FinishEvent&& finishEvent)
61     {
62         finishEvent_ = std::move(finishEvent);
63     }
64 
FireFinishEvent()65     void FireFinishEvent() const
66     {
67         if (finishEvent_) {
68             finishEvent_();
69         }
70     }
71 
72 private:
73     ErrorEvent errorEvent_;
74     CompleteEvent completeEvent_;
75     FinishEvent finishEvent_;
76 };
77 
78 } // namespace OHOS::Ace::NG
79 
80 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_IMAGE_IMAGE_EVENT_HUB_H
81