[ACR-564] deprecate unused API
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI.Wearable / src / public / RecyclerView / GridRecycleLayoutManager.cs
1 /* Copyright (c) 2020 Samsung Electronics Co., Ltd.
2  *
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 using System;
17 using Tizen.NUI.BaseComponents;
18 using Tizen.NUI.Components;
19 using System.Collections.Generic;
20 using System.ComponentModel;
21
22 namespace Tizen.NUI.Wearable
23 {
24     /// <summary>
25     /// [Draft] This class implements a grid box layout.
26     /// </summary>
27     /// <since_tizen> 8 </since_tizen>
28     [Obsolete("This has been deprecated in API12")]
29     [EditorBrowsable(EditorBrowsableState.Never)]
30     public class GridRecycleLayoutManager : RecycleLayoutManager
31     {
32         private int rows = 1;
33
34         /// <summary>
35         /// [draft ]Get/Set the number of rows in the grid
36         /// </summary>
37         /// <since_tizen> 8 </since_tizen>
38         [Obsolete("This has been deprecated in API12")]
39         [EditorBrowsable(EditorBrowsableState.Never)]
40         public int Rows
41         {
42             get
43             {
44                 return rows;
45             }
46             set
47             {
48                 rows = value;
49
50                 if (Container != null)
51                 {
52                     Layout(PrevScrollPosition);
53                 }
54             }
55         }
56
57         private int columns = 1;
58
59
60         /// <summary>
61         /// [Draft] Get/Set the number of columns in the grid
62         /// </summary>
63         /// <since_tizen> 8 </since_tizen>
64         [Obsolete("This has been deprecated in API12")]
65         [EditorBrowsable(EditorBrowsableState.Never)]
66         public int Columns
67         {
68             get
69             {
70                 return columns;
71             }
72             set
73             {
74                 columns = value;
75
76                 if (Container != null)
77                 {
78                     Layout(PrevScrollPosition);
79                 }
80             }
81         }
82
83         private int firstVisibleItemIndex = -1;
84         private int lastVisibleItemIndex = -1;
85
86         private bool IsItemVisible(float scrollPosition, RecycleItem item)
87         {
88             bool result = false;
89             View list = Container.GetParent() as View;
90             if (list == null)
91             {
92                 return result;
93             }
94             Vector2 visibleArea = new Vector2(Math.Abs(scrollPosition),
95                 Math.Abs(scrollPosition) + (LayoutOrientation == Orientation.Vertical ?
96                                                 list.Size.Width : list.Size.Height)
97             );
98
99             float firstCheckPoint = LayoutOrientation == Orientation.Vertical ? item.Position.X : item.Position.Y;
100             float secondCheckPoint = LayoutOrientation == Orientation.Vertical ?
101                                         firstCheckPoint + item.Size.Width :
102                                         firstCheckPoint + item.Size.Height;
103
104             result = (firstCheckPoint >= visibleArea.X && firstCheckPoint <= visibleArea.Y) || (secondCheckPoint >= visibleArea.X && secondCheckPoint <= visibleArea.Y);
105
106             return result;
107         }
108
109         /// <summary>
110         /// This is called to find out how much container size can be.
111         /// </summary>
112         /// <since_tizen> 8 </since_tizen>
113         [Obsolete("This has been deprecated in API12")]
114         [EditorBrowsable(EditorBrowsableState.Never)]
115         public override float CalculateLayoutOrientationSize()
116         {
117             float orientationFactor = LayoutOrientation == Orientation.Vertical ? Rows : Columns;
118             return StepSize * (int)Math.Ceiling((double)DataCount / (double)orientationFactor);
119         }
120
121
122         /// <summary>
123         /// This is called to find out where items are lain out according to current scroll position.
124         /// </summary>
125         /// <param name="scrollPosition">Scroll position which is calculated by ScrollableBase</param>
126         /// <since_tizen> 8 </since_tizen>
127         [Obsolete("This has been deprecated in API12")]
128         public override void Layout(float scrollPosition)
129         {
130             int itemInGroup = LayoutOrientation == Orientation.Vertical ? Rows : Columns;
131             firstVisibleItemIndex = -1;
132             lastVisibleItemIndex = -1;
133
134             RecycleItem previousItem = null;
135
136             for (int i = 0; i < Container.Children.Count; i++)
137             {
138                 RecycleItem item = Container.Children[i] as RecycleItem;
139
140                 if (previousItem != null && item != null)
141                 {
142                     item.Position = LayoutOrientation == Orientation.Vertical ?
143                         new Position(
144                             (i % itemInGroup == 0 ?
145                             previousItem.Position.X + (previousItem.CurrentSize.Width != 0 ?
146                                                             previousItem.CurrentSize.Width :
147                                                             previousItem.Size.Width) :
148                             previousItem.Position.X),
149                             (i % itemInGroup == 0 ?
150                             0 :
151                             previousItem.PositionY + (previousItem.CurrentSize.Height != 0 ?
152                                                             previousItem.CurrentSize.Height :
153                                                             previousItem.Size.Height))
154                         ) :
155                         new Position(
156                             (i % itemInGroup == 0 ?
157                             0 :
158                             previousItem.PositionX + (previousItem.CurrentSize.Width != 0 ?
159                                                             previousItem.CurrentSize.Width :
160                                                             previousItem.Size.Width)),
161                             (i % itemInGroup == 0 ?
162                             previousItem.Position.Y + (previousItem.CurrentSize.Height != 0 ?
163                                                             previousItem.CurrentSize.Height :
164                                                             previousItem.Size.Height) :
165                             previousItem.Position.Y)
166                         );
167                 }
168
169                 bool isVisible = IsItemVisible(scrollPosition, item);
170
171                 if (isVisible)
172                 {
173                     firstVisibleItemIndex = firstVisibleItemIndex == -1 ? i : firstVisibleItemIndex;
174                     lastVisibleItemIndex = i;
175                 }
176
177                 previousItem = item;
178             }
179
180             if (StepSize == 0)
181             {
182                 StepSize = LayoutOrientation == Orientation.Vertical ? ItemSize.Width : ItemSize.Height;
183             }
184         }
185
186
187         /// <summary>
188         /// This is called to find out which items should be recycled according to current scroll position.
189         /// </summary>
190         /// <param name="scrollPosition">Scroll position which is calculated by ScrollableBase</param>
191         /// <returns>List of RecycleItems which should be recycled.</returns>
192         /// <since_tizen> 8 </since_tizen>
193         [Obsolete("This has been deprecated in API12")]
194         public override List<RecycleItem> Recycle(float scrollPosition)
195         {
196             List<RecycleItem> result = new List<RecycleItem>();
197             bool checkFront = (PrevScrollPosition - scrollPosition) > 0;
198
199             int itemInGroup = LayoutOrientation == Orientation.Vertical ? Rows : Columns;
200
201             if (checkFront)
202             {
203                 int currentGroupNum = (int)(firstVisibleItemIndex / itemInGroup) + 1;
204
205                 if (currentGroupNum > 2)
206                 {
207                     // Too many item is in front!!! move first item to back!!!!
208                     for (int i = 0; i < itemInGroup; i++)
209                     {
210                         RecycleItem target = Container.Children[0] as RecycleItem;
211                         if (target != null)
212                         {
213                             target.DataIndex = target.DataIndex + Container.Children.Count;
214                             target.SiblingOrder = Container.Children.Count - 1;
215
216                             result.Add(target);
217                         }
218                     }
219                 }
220             }
221             else
222             {
223                 int currentGroupNum = (int)(lastVisibleItemIndex / itemInGroup) + 1;
224
225                 if (currentGroupNum < (int)(Container.Children.Count / itemInGroup) - 3)
226                 {
227                     for (int i = 0; i < itemInGroup; i++)
228                     {
229                         RecycleItem prevFirstItem = Container.Children[itemInGroup] as RecycleItem;
230                         RecycleItem target = Container.Children[Container.Children.Count - 1] as RecycleItem;
231                         if (prevFirstItem != null && target != null)
232                         {
233                             target.Position = new Position(
234                                 LayoutOrientation == Orientation.Vertical ? (prevFirstItem.Position.X - target.Size.Width) : prevFirstItem.Position.X,
235                                 LayoutOrientation == Orientation.Vertical ? prevFirstItem.Position.Y : (prevFirstItem.Position.Y - target.Size.Height)
236                             );
237                             target.DataIndex = target.DataIndex - Container.Children.Count;
238                             target.SiblingOrder = 0;
239
240                             result.Add(target);
241                         }
242                     }
243                 }
244             }
245
246
247             PrevScrollPosition = scrollPosition;
248
249             return result;
250         }
251
252         /// <summary>
253         /// Adjust scrolling position by own scrolling rules.
254         /// </summary>
255         /// <param name="scrollPosition">Scroll position which is calculated by ScrollableBase</param>
256         /// <since_tizen> 8 </since_tizen>
257         [Obsolete("This has been deprecated in API12")]
258         public override float CalculateCandidateScrollPosition(float scrollPosition)
259         {
260             return scrollPosition;
261         }
262
263         /// <inheritdoc/>
264         [Obsolete("This has been deprecated in API12")]
265         public override View RequestNextFocusableView(View currentFocusedView, View.FocusDirection direction, bool loopEnabled)
266         {
267             View nextFocusedView = null;
268             int targetSibling = -1;
269             bool isHorizontal = LayoutOrientation == Orientation.Horizontal;
270
271             switch (direction)
272             {
273                 case View.FocusDirection.Left:
274                     {
275                         targetSibling = isHorizontal ? currentFocusedView.SiblingOrder - 1 : currentFocusedView.SiblingOrder - Rows;
276                         break;
277                     }
278                 case View.FocusDirection.Right:
279                     {
280                         targetSibling = isHorizontal ? currentFocusedView.SiblingOrder + 1 : currentFocusedView.SiblingOrder + Rows;
281                         break;
282                     }
283                 case View.FocusDirection.Up:
284                     {
285                         targetSibling = isHorizontal ? currentFocusedView.SiblingOrder - Columns : currentFocusedView.SiblingOrder - 1;
286                         break;
287                     }
288                 case View.FocusDirection.Down:
289                     {
290                         targetSibling = isHorizontal ? currentFocusedView.SiblingOrder + Columns : currentFocusedView.SiblingOrder + 1;
291                         break;
292                     }
293                 default:
294                     break;
295             }
296
297             if (targetSibling > -1 && targetSibling < Container.Children.Count)
298             {
299                 RecycleItem candidate = Container.Children[targetSibling] as RecycleItem;
300                 if (candidate != null && candidate.DataIndex >= 0 && candidate.DataIndex < DataCount)
301                 {
302                     nextFocusedView = candidate;
303                 }
304             }
305
306             return nextFocusedView;
307         }
308     }
309 }