summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Core/LockableObservableListWrapper.cs
blob: abb0b1fff332ac48bdb1ac667ca9df306eb4465f (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
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using Xamarin.Forms.Internals;
using Xamarin.Forms.Platform;

namespace Xamarin.Forms.Internals
{
	[EditorBrowsable(EditorBrowsableState.Never)]
	public class LockableObservableListWrapper : IList<string>, ICollection<string>, INotifyCollectionChanged, INotifyPropertyChanged, IReadOnlyList<string>, IReadOnlyCollection<string>, IEnumerable<string>, IEnumerable
	{
		public readonly ObservableCollection<string> _list = new ObservableCollection<string>();

		event NotifyCollectionChangedEventHandler INotifyCollectionChanged.CollectionChanged
		{
			add { ((INotifyCollectionChanged)_list).CollectionChanged += value; }
			remove { ((INotifyCollectionChanged)_list).CollectionChanged -= value; }
		}

		event PropertyChangedEventHandler INotifyPropertyChanged.PropertyChanged {
			add { ((INotifyPropertyChanged)_list).PropertyChanged += value; }
			remove { ((INotifyPropertyChanged)_list).PropertyChanged -= value; }
		}

		public bool IsLocked { get; set; }

		void ThrowOnLocked()
		{
			if (IsLocked)
				throw new InvalidOperationException("The Items list can not be manipulated if the ItemsSource property is set");
		}

		public string this [int index] {
			get { return _list [index]; }
			set {
				ThrowOnLocked();
				_list [index] = value; }
		}

		public int Count {
			get { return _list.Count; }
		}

		public bool IsReadOnly {
			get { return ((IList<string>)_list).IsReadOnly; }
		}

		public void InternalAdd(string item)
		{
			_list.Add(item);
		}

		public void Add(string item)
		{
			ThrowOnLocked();
			InternalAdd(item);
		}

		public void InternalClear()
		{ 
			_list.Clear();
		}

		public void Clear()
		{
			ThrowOnLocked();
			InternalClear();
		}

		public bool Contains(string item)
		{
			return _list.Contains(item);
		}

		public void CopyTo(string [] array, int arrayIndex)
		{
			_list.CopyTo(array, arrayIndex);
		}

		public IEnumerator<string> GetEnumerator()
		{
			return _list.GetEnumerator();
		}

		public int IndexOf(string item)
		{
			return _list.IndexOf(item);
		}

		public void InternalInsert(int index, string item)
		{
			_list.Insert(index, item);
		}

		public void Insert(int index, string item)
		{
			ThrowOnLocked();
			InternalInsert(index, item);
		}

		public bool InternalRemove(string item)
		{
			return _list.Remove(item);
		}

		public bool Remove(string item)
		{
			ThrowOnLocked();
			return InternalRemove(item);
		}

		public void InternalRemoveAt(int index)
		{
			_list.RemoveAt(index);
		}

		public void RemoveAt(int index)
		{
			ThrowOnLocked();
			InternalRemoveAt(index);
		}

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