[NUI] Introduce CollectionView and related classes. (#2525)
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI.Components / Controls / RecyclerView / ItemSource / ListSource.cs
1 /* Copyright (c) 2021 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
17 using System;
18 using System.Collections;
19 using System.Collections.Generic;
20
21 namespace Tizen.NUI.Components
22 {
23     sealed class ListSource : IItemSource, IList
24     {
25         IList _itemsSource;
26
27         public ListSource()
28         {
29         }
30
31         public ListSource(IEnumerable<object> enumerable)
32         {
33             _itemsSource = new List<object>(enumerable);
34         }
35
36         public ListSource(IEnumerable enumerable)
37         {
38             _itemsSource = new List<object>();
39
40             if (enumerable == null)
41                 return;
42
43             foreach (object item in enumerable)
44             {
45                 _itemsSource.Add(item);
46             }
47         }
48
49         public int Count => _itemsSource.Count + (HasHeader ? 1 : 0) + (HasFooter ? 1 : 0);
50
51         public bool HasHeader { get; set; }
52         public bool HasFooter { get; set; }
53
54         public bool IsReadOnly => _itemsSource.IsReadOnly;
55
56         public bool IsFixedSize => _itemsSource.IsFixedSize;
57
58         public object SyncRoot => _itemsSource.SyncRoot;
59
60         public bool IsSynchronized => _itemsSource.IsSynchronized;
61
62         object IList.this[int index] { get => _itemsSource[index]; set => _itemsSource[index] = value; }
63
64         public void Dispose()
65         {
66
67         }
68
69         public bool IsFooter(int index)
70         {
71             return HasFooter && index == Count - 1;
72         }
73
74         public bool IsHeader(int index)
75         {
76             return HasHeader && index == 0;
77         }
78
79         public int GetPosition(object item)
80         {
81             for (int n = 0; n < _itemsSource.Count; n++)
82             {
83                 var elementByIndex = _itemsSource[n];
84                 var isEqual = elementByIndex == item || (elementByIndex != null && item != null && elementByIndex.Equals(item));
85
86                 if (isEqual)
87                 {
88                     return AdjustPosition(n);
89                 }
90             }
91
92             return -1;
93         }
94
95         public object GetItem(int position)
96         {
97             return _itemsSource[AdjustIndexRequest(position)];
98         }
99
100         int AdjustIndexRequest(int index)
101         {
102             return index - (HasHeader ? 1 : 0);
103         }
104
105         int AdjustPosition(int index)
106         {
107             return index + (HasHeader ? 1 : 0);
108         }
109         public int Add(object value)
110         {
111             return _itemsSource.Add(value);
112         }
113
114         public bool Contains(object value)
115         {
116             return _itemsSource.Contains(value);
117         }
118
119         public void Clear()
120         {
121             _itemsSource.Clear();
122         }
123
124         public int IndexOf(object value)
125         {
126             return _itemsSource.IndexOf(value);
127         }
128
129         public void Insert(int index, object value)
130         {
131             _itemsSource.Insert(index, value);
132         }
133
134         public void Remove(object value)
135         {
136             _itemsSource.Remove(value);
137         }
138
139         public void RemoveAt(int index)
140         {
141             _itemsSource.RemoveAt(index);
142         }
143
144         public void CopyTo(Array array, int index)
145         {
146             _itemsSource.CopyTo(array, index);
147         }
148
149         public IEnumerator GetEnumerator()
150         {
151             return _itemsSource.GetEnumerator();
152         }
153     }
154 }