1 /*
2  * Copyright (c) 2020-2021 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 /**
17  * @addtogroup UI_Components
18  * @{
19  *
20  * @brief Defines UI components such as buttons, texts, images, lists, and progress bars.
21  *
22  * @since 1.0
23  * @version 1.0
24  */
25 
26 /**
27  * @file abstract_adapter.h
28  *
29  * @brief Defines the base class for adapters.
30  *
31  * @since 1.0
32  * @version 1.0
33  */
34 
35 #ifndef GRAPHIC_LITE_ABSTRACT_ADAPTER_H
36 #define GRAPHIC_LITE_ABSTRACT_ADAPTER_H
37 
38 #include "components/ui_view.h"
39 
40 namespace OHOS {
41 /**
42  * @brief Defines the base class for adapters. You can derive <b>AbstractAdapter</b> based on actual requirements
43  *        and use the <b>GetView()</b> and <b>GetCount()</b> functions to implement adapters of different data types.
44  *        For details, see {@link TextAdapter}.
45  * @since 1.0
46  * @version 1.0
47  */
48 class AbstractAdapter : public HeapBase {
49 public:
50     /**
51      * @brief A constructor used to create an <b>AbstractAdapter</b> instance.
52      * @since 1.0
53      * @version 1.0
54      */
AbstractAdapter()55     AbstractAdapter() {}
56 
57     /**
58      * @brief A destructor used to delete the <b>AbstractAdapter</b> instance.
59      * @since 1.0
60      * @version 1.0
61      */
~AbstractAdapter()62     virtual ~AbstractAdapter() {}
63 
64     /**
65      * @brief Obtains the number of adapter data items.
66      *
67      * @return Returns the number of adapter data items.
68      * @since 1.0
69      * @version 1.0
70      */
71     virtual uint16_t GetCount() = 0;
72 
73     /**
74      * @brief Obtains a <b>UIView</b> instance to convert adapter data into another <b>UIView</b> instance.
75      *
76      * @param inView Indicates the pointer to the reusable instance. If this parameter is not <b>NULL</b>, a reusable
77      *               <b>UIView</b> instance is available. In this case, this function does not need to create a new
78      *               <b>UIView</b> instance, just reusing the instance specified by <b>inView</b> to update the
79      *               <b>inView</b> data.
80      *               If this parameter is <b>NULL</b>, there is no reusable <b>UIView</b> instance. In this case, this
81      *               function needs to create a new <b>UIView</b> instance.
82      *
83      * @param index Indicates the adapter data index.
84      *
85      * @return UIView Returns the pointer to the <b>UIView</b> instance constructed by the adapter.
86      * @since 1.0
87      * @version 1.0
88      */
89     virtual UIView* GetView(UIView* inView, int16_t index) = 0;
90 
DeleteView(UIView * & view)91     virtual void DeleteView(UIView*& view)
92     {
93         delete view;
94         view = nullptr;
95     };
96 
GetItemWidthWithMargin(int16_t index)97     virtual int16_t GetItemWidthWithMargin(int16_t index)
98     {
99         UIView* view = GetView(nullptr, index);
100         if (view == nullptr) {
101             return 0;
102         }
103         int16_t width = view->GetWidthWithMargin();
104         DeleteView(view);
105         return width;
106     }
107 
GetItemHeightWithMargin(int16_t index)108     virtual int16_t GetItemHeightWithMargin(int16_t index)
109     {
110         UIView* view = GetView(nullptr, index);
111         if (view == nullptr) {
112             return 0;
113         }
114         int16_t height = view->GetHeightWithMargin();
115         DeleteView(view);
116         return height;
117     }
118 };
119 } // namespace OHOS
120 #endif // GRAPHIC_LITE_ABSTRACT_ADAPTER_H
121