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;
20 using System.Collections.Specialized;
22 namespace Tizen.NUI.Components
24 // Used by the CollectionView to keep track of (and respond to changes in) the SelectedItems property
25 internal class SelectionList : IList<object>
27 static readonly IList<object> selectEmpty = new List<object>(0);
28 readonly CollectionView ColView;
29 readonly IList<object> internalList;
30 IList<object> shadowList;
33 public SelectionList(CollectionView colView, IList<object> items = null)
35 ColView = colView ?? throw new ArgumentNullException(nameof(colView));
36 internalList = items ?? new List<object>();
39 if (items is INotifyCollectionChanged incc)
41 incc.CollectionChanged += OnCollectionChanged;
45 public object this[int index] { get => internalList[index]; set => internalList[index] = value; }
47 public int Count => internalList.Count;
49 public bool IsReadOnly => false;
51 public void Add(object item)
53 externalChange = true;
54 internalList.Add(item);
55 externalChange = false;
57 ColView.SelectedItemsPropertyChanged(shadowList, internalList);
63 externalChange = true;
65 externalChange = false;
67 ColView.SelectedItemsPropertyChanged(shadowList, selectEmpty);
71 public bool Contains(object item)
73 return internalList.Contains(item);
76 public void CopyTo(object[] array, int arrayIndex)
78 internalList.CopyTo(array, arrayIndex);
81 public IEnumerator<object> GetEnumerator()
83 return internalList.GetEnumerator();
86 public int IndexOf(object item)
88 return internalList.IndexOf(item);
91 public void Insert(int index, object item)
93 externalChange = true;
94 internalList.Insert(index, item);
95 externalChange = false;
97 ColView.SelectedItemsPropertyChanged(shadowList, internalList);
98 shadowList.Insert(index, item);
101 public bool Remove(object item)
103 externalChange = true;
104 var removed = internalList.Remove(item);
105 externalChange = false;
109 ColView.SelectedItemsPropertyChanged(shadowList, internalList);
110 shadowList.Remove(item);
116 public void RemoveAt(int index)
118 externalChange = true;
119 internalList.RemoveAt(index);
120 externalChange = false;
122 ColView.SelectedItemsPropertyChanged(shadowList, internalList);
123 shadowList.RemoveAt(index);
126 IEnumerator IEnumerable.GetEnumerator()
128 return internalList.GetEnumerator();
133 var items = new List<object>();
134 for (int n = 0; n < internalList.Count; n++)
136 items.Add(internalList[n]);
142 void OnCollectionChanged(object sender, NotifyCollectionChangedEventArgs args)
146 // If this change was initiated by a renderer or direct manipulation of ColllectionView.SelectedItems,
147 // we don't need to send a selection change notification
151 // This change is coming from a bound viewmodel property
152 // Emit a selection change notification, then bring the shadow copy up-to-date
153 ColView.SelectedItemsPropertyChanged(shadowList, internalList);