1 /* Copyright (c) 2021 Samsung Electronics 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
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 using System.Collections;
19 using System.Collections.Generic;
21 namespace Tizen.NUI.Components
23 sealed class ListSource : IItemSource, IList
31 public ListSource(IEnumerable<object> enumerable)
33 _itemsSource = new List<object>(enumerable);
36 public ListSource(IEnumerable enumerable)
38 _itemsSource = new List<object>();
40 if (enumerable == null)
43 foreach (object item in enumerable)
45 _itemsSource.Add(item);
49 public int Count => _itemsSource.Count + (HasHeader ? 1 : 0) + (HasFooter ? 1 : 0);
51 public bool HasHeader { get; set; }
52 public bool HasFooter { get; set; }
54 public bool IsReadOnly => _itemsSource.IsReadOnly;
56 public bool IsFixedSize => _itemsSource.IsFixedSize;
58 public object SyncRoot => _itemsSource.SyncRoot;
60 public bool IsSynchronized => _itemsSource.IsSynchronized;
62 object IList.this[int index] { get => _itemsSource[index]; set => _itemsSource[index] = value; }
69 public bool IsFooter(int index)
71 return HasFooter && index == Count - 1;
74 public bool IsHeader(int index)
76 return HasHeader && index == 0;
79 public int GetPosition(object item)
81 for (int n = 0; n < _itemsSource.Count; n++)
83 var elementByIndex = _itemsSource[n];
84 var isEqual = elementByIndex == item || (elementByIndex != null && item != null && elementByIndex.Equals(item));
88 return AdjustPosition(n);
95 public object GetItem(int position)
97 return _itemsSource[AdjustIndexRequest(position)];
100 int AdjustIndexRequest(int index)
102 return index - (HasHeader ? 1 : 0);
105 int AdjustPosition(int index)
107 return index + (HasHeader ? 1 : 0);
109 public int Add(object value)
111 return _itemsSource.Add(value);
114 public bool Contains(object value)
116 return _itemsSource.Contains(value);
121 _itemsSource.Clear();
124 public int IndexOf(object value)
126 return _itemsSource.IndexOf(value);
129 public void Insert(int index, object value)
131 _itemsSource.Insert(index, value);
134 public void Remove(object value)
136 _itemsSource.Remove(value);
139 public void RemoveAt(int index)
141 _itemsSource.RemoveAt(index);
144 public void CopyTo(Array array, int index)
146 _itemsSource.CopyTo(array, index);
149 public IEnumerator GetEnumerator()
151 return _itemsSource.GetEnumerator();