summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Pages/DataSourceList.cs
blob: 7f3a59c12f1fb09cd4b66084479dcf9ea52a427f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;

namespace Xamarin.Forms.Pages
{
	internal class DataSourceList : IList<IDataItem>, IReadOnlyList<IDataItem>, INotifyCollectionChanged
	{
		readonly List<int> _maskedIndexes = new List<int>(); // Indices  
		readonly HashSet<string> _maskedKeys = new HashSet<string>();
		IList<IDataItem> _mainList;

		public IList<IDataItem> MainList
		{
			get { return _mainList; }
			set
			{
				var observable = _mainList as INotifyCollectionChanged;
				if (observable != null)
					observable.CollectionChanged -= OnMainCollectionChanged;
				_mainList = value;
				observable = _mainList as INotifyCollectionChanged;
				if (observable != null)
					observable.CollectionChanged += OnMainCollectionChanged;
				_maskedIndexes.Clear();
				for (var i = 0; i < _mainList.Count; i++)
				{
					IDataItem data = _mainList[i];
					if (_maskedKeys.Contains(data.Name))
						_maskedIndexes.Add(i);
				}
				OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
			}
		}

		public IEnumerable<string> MaskedKeys => _maskedKeys;

		public void Add(IDataItem item)
		{
			throw new NotSupportedException();
		}

		public void Clear()
		{
			throw new NotSupportedException();
		}

		public bool Contains(IDataItem item)
		{
			return MainList != null && !_maskedKeys.Contains(item.Name) && MainList.Contains(item);
		}

		public void CopyTo(IDataItem[] array, int arrayIndex)
		{
			throw new NotSupportedException();
		}

		public int Count
		{
			get
			{
				if (MainList == null)
					return 0;
				var result = 0;
				result += MainList.Count;
				result -= _maskedIndexes.Count;
				return result;
			}
		}

		public bool IsReadOnly => true;

		public bool Remove(IDataItem item)
		{
			throw new NotSupportedException();
		}

		IEnumerator IEnumerable.GetEnumerator()
		{
			return GetEnumerator();
		}

		public IEnumerator<IDataItem> GetEnumerator()
		{
			var index = 0;
			if (MainList == null)
				yield break;
			foreach (IDataItem item in MainList)
			{
				if (!_maskedIndexes.Contains(index))
					yield return item;
				index++;
			}
		}

		public int IndexOf(IDataItem item)
		{
			if (_maskedKeys.Contains(item.Name))
				return -1;

			if (MainList != null)
			{
				int result = MainList.IndexOf(item);
				if (result >= 0)
					return PublicIndexFromMainIndex(result);
			}
			return -1;
		}

		public void Insert(int index, IDataItem item)
		{
			throw new NotSupportedException();
		}

		public IDataItem this[int index]
		{
			get
			{
				foreach (int i in _maskedIndexes)
				{
					if (i <= index)
						index++;
				}
				if (_mainList == null)
					throw new IndexOutOfRangeException();
				return _mainList[index];
			}
			set { throw new NotSupportedException(); }
		}

		public void RemoveAt(int index)
		{
			throw new NotSupportedException();
		}

		public event NotifyCollectionChangedEventHandler CollectionChanged;

		public void MaskKey(string key)
		{
			if (_maskedKeys.Contains(key) || _mainList == null)
				return;
			_maskedKeys.Add(key);
			var index = 0;
			foreach (IDataItem item in _mainList)
			{
				if (item.Name == key)
				{
					// We need to keep our indexes list sorted, so we insert everything pre-sorted
					var added = false;
					for (var i = 0; i < _maskedIndexes.Count; i++)
					{
						if (_maskedIndexes[i] > index)
						{
							_maskedIndexes.Insert(i, index);
							added = true;
							break;
						}
					}
					if (!added)
						_maskedIndexes.Add(index);
					OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, item, PublicIndexFromMainIndex(index)));
					break;
				}
				index++;
			}
		}

		public void UnmaskKey(string key)
		{
			_maskedKeys.Remove(key);
			if (_mainList == null)
				return;
			var index = 0;
			foreach (IDataItem item in _mainList)
			{
				if (item.Name == key)
				{
					bool removed = _maskedIndexes.Remove(index);
					if (removed)
					{
						OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, item, PublicIndexFromMainIndex(index)));
					}
					break;
				}
				index++;
			}
		}

		protected void OnCollectionChanged(NotifyCollectionChangedEventArgs args)
		{
			CollectionChanged?.Invoke(this, args);
		}

		void OnMainCollectionChanged(object sender, NotifyCollectionChangedEventArgs args)
		{
			// much complexity to be had here
			switch (args.Action)
			{
				case NotifyCollectionChangedAction.Add:
					OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, args.NewItems, PublicIndexFromMainIndex(args.NewStartingIndex)));
					break;
				case NotifyCollectionChangedAction.Move:
					OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Move, args.OldItems, PublicIndexFromMainIndex(args.NewStartingIndex),
						PublicIndexFromMainIndex(args.OldStartingIndex)));
					break;
				case NotifyCollectionChangedAction.Remove:
					OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, args.OldItems, PublicIndexFromMainIndex(args.OldStartingIndex)));
					break;
				case NotifyCollectionChangedAction.Replace:
					OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Replace, args.NewItems, args.OldItems, PublicIndexFromMainIndex(args.OldStartingIndex)));
					break;
				case NotifyCollectionChangedAction.Reset:
					OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
					break;
				default:
					throw new ArgumentOutOfRangeException();
			}
		}

		int PublicIndexFromMainIndex(int index)
		{
			var count = 0;
			for (var x = 0; x < _maskedIndexes.Count; x++)
			{
				int i = _maskedIndexes[x];
				if (i < index)
					count++;
			}
			return index - count;
		}
	}
}