1 /*
2  * Copyright (c) 2022-2023 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_IMAGE_PROVIDER_IMAGE_STATE_MANAGER_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_IMAGE_PROVIDER_IMAGE_STATE_MANAGER_H
18 
19 #include <functional>
20 #include <utility>
21 
22 #include "base/log/log_wrapper.h"
23 #include "base/memory/ace_type.h"
24 
25 namespace OHOS::Ace::NG {
26 
27 enum class ImageLoadingState {
28     UNLOADED = 0,
29     DATA_LOADING,
30     DATA_READY,
31     MAKE_CANVAS_IMAGE,
32     LOAD_SUCCESS,
33     LOAD_FAIL,
34 };
35 
36 enum class ImageLoadingCommand {
37     LOAD_DATA = 0,
38     LOAD_FAIL,
39     LOAD_DATA_SUCCESS,
40     MAKE_CANVAS_IMAGE,
41     MAKE_CANVAS_IMAGE_SUCCESS,
42     RETRY_LOADING,
43     RESET_STATE,
44 };
45 
46 class ImageLoadingContext;
47 
48 class ImageStateManager : public AceType {
49     DECLARE_ACE_TYPE(ImageStateManager, AceType);
50 
51 public:
ImageStateManager(WeakPtr<ImageLoadingContext> ctx)52     explicit ImageStateManager(WeakPtr<ImageLoadingContext> ctx) : ctx_(std::move(ctx)) {}
53     ~ImageStateManager() override = default;
54 
55     ImageLoadingState GetCurrentState();
56     void HandleCommand(ImageLoadingCommand command);
57     void SetState(ImageLoadingState state);
58 
59 private:
60     void HandleCommandByUnloadedState(ImageLoadingCommand command);
61     void HandleCommandByDataLoadingState(ImageLoadingCommand command);
62     void HandleCommandByDataReadyState(ImageLoadingCommand command);
63     void HandleCommandByMakeCanvasImageState(ImageLoadingCommand command);
64     void HandleCommandByLoadSuccessState(ImageLoadingCommand command);
65     void HandleCommandByLoadFailState(ImageLoadingCommand command);
66 
67     WeakPtr<ImageLoadingContext> ctx_;
68     ImageLoadingState state_ = ImageLoadingState::UNLOADED;
69 };
70 
71 } // namespace OHOS::Ace::NG
72 
73 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_IMAGE_PROVIDER_IMAGE_STATE_MANAGER_H
74