summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Pages/CompoundCollection.cs
blob: 47bc1ea7f9834dc7aa26ccea2bded6325af9f83f (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
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.Linq;
using Xamarin.Forms.Internals;

namespace Xamarin.Forms.Pages
{
	public class CompoundCollection : Element, IList, INotifyCollectionChanged
	{
		public static readonly BindableProperty MainListProperty = BindableProperty.Create(nameof(MainList), typeof(IReadOnlyList<object>), typeof(CompoundCollection), default(IReadOnlyList<object>),
			propertyChanged: OnMainListPropertyChanged);

		readonly ObservableCollection<object> _appendList = new ObservableCollection<object>();

		readonly ObservableCollection<object> _prependList = new ObservableCollection<object>();

		public CompoundCollection()
		{
			_prependList.CollectionChanged += OnPrependCollectionChanged;
			_appendList.CollectionChanged += OnAppendCollectionChanged;
		}

		public IList AppendList => _appendList;

		public IReadOnlyList<object> MainList
		{
			get { return (IReadOnlyList<object>)GetValue(MainListProperty); }
			set { SetValue(MainListProperty, value); }
		}

		public IList PrependList => _prependList;

		public void CopyTo(Array array, int index)
		{
			throw new NotSupportedException();
		}

		public int Count => AppendList.Count + PrependList.Count + (MainList?.Count ?? 0);

		public bool IsSynchronized => false;

		public object SyncRoot => null;

		public IEnumerator GetEnumerator()
		{
			foreach (object item in PrependList)
				yield return item;
			foreach (object item in MainList)
				yield return item;
			foreach (object item in AppendList)
				yield return item;
		}

		public int Add(object value)
		{
			throw new NotSupportedException();
		}

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

		public bool Contains(object value)
		{
			IReadOnlyList<object> mainList = MainList;
			bool masterContains;
			var masterList = mainList as IList;
			if (masterList != null)
			{
				masterContains = masterList.Contains(value);
			}
			else
			{
				masterContains = mainList.Contains(value);
			}
			return masterContains || PrependList.Contains(value) || AppendList.Contains(value);
		}

		public int IndexOf(object value)
		{
			int result;
			result = PrependList.IndexOf(value);
			if (result >= 0)
				return result;
			result = MainList.IndexOf(value);
			if (result >= 0)
				return result + PrependList.Count;

			result = AppendList.IndexOf(value);
			if (result >= 0)
				return result + PrependList.Count + MainList.Count;
			return -1;
		}

		public void Insert(int index, object value)
		{
			throw new NotSupportedException();
		}

		public bool IsFixedSize => false;

		public bool IsReadOnly => true;

		public object this[int index]
		{
			get
			{
				IReadOnlyList<object> mainList = MainList;
				int prependSize = PrependList.Count;
				if (index < prependSize)
					return PrependList[index];
				index -= prependSize;

				if (mainList != null)
				{
					if (index < mainList.Count)
						return mainList[index];
					index -= mainList.Count;
				}

				if (index >= AppendList.Count)
					throw new IndexOutOfRangeException();
				return AppendList[index];
			}
			set { throw new NotSupportedException(); }
		}

		public void Remove(object value)
		{
			throw new NotSupportedException();
		}

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

		public event NotifyCollectionChangedEventHandler CollectionChanged;

		void OnAppendCollectionChanged(object sender, NotifyCollectionChangedEventArgs args)
		{
			int offset = _prependList.Count + (MainList?.Count ?? 0);
			// here we just need to calculate the offset for the index, everything else is the same
			switch (args.Action)
			{
				case NotifyCollectionChangedAction.Add:
					OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, args.NewItems, offset + args.NewStartingIndex));
					break;
				case NotifyCollectionChangedAction.Move:
					OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Move, args.OldItems, offset + args.NewStartingIndex, offset + args.OldStartingIndex));
					break;
				case NotifyCollectionChangedAction.Remove:
					OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, args.OldItems, offset + args.OldStartingIndex));
					break;
				case NotifyCollectionChangedAction.Replace:
					OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Replace, args.NewItems, args.OldItems, offset + args.OldStartingIndex));
					break;
				case NotifyCollectionChangedAction.Reset:
					OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
					break;
				default:
					throw new ArgumentOutOfRangeException();
			}
		}

		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();
			}
		}

		static void OnMainListPropertyChanged(BindableObject bindable, object oldValue, object newValue)
		{
			var self = (CompoundCollection)bindable;
			var observable = oldValue as INotifyCollectionChanged;
			if (observable != null)
				observable.CollectionChanged -= self.OnMainCollectionChanged;
			observable = newValue as INotifyCollectionChanged;
			if (observable != null)
				observable.CollectionChanged += self.OnMainCollectionChanged;
			self.OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
		}

		void OnPrependCollectionChanged(object sender, NotifyCollectionChangedEventArgs args)
		{
			// this can basically be a passthrough as prepend has no masking and identical indexing
			OnCollectionChanged(args);
		}

		int PublicIndexFromMainIndex(int index)
		{
			return PrependList.Count + index;
		}
	}
}