summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Core/DefinitionCollection.cs
blob: bf0e4d06f7d24de498bb7cb665668db0a7cbf978 (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
using System;
using System.Collections;
using System.Collections.Generic;

namespace Xamarin.Forms
{
	public class DefinitionCollection<T> : IList<T>, ICollection<T> where T : IDefinition
	{
		readonly List<T> _internalList = new List<T>();

		internal DefinitionCollection()
		{
		}

		public void Add(T item)
		{
			_internalList.Add(item);
			item.SizeChanged += OnItemSizeChanged;
			OnItemSizeChanged(this, EventArgs.Empty);
		}

		public void Clear()
		{
			foreach (T item in _internalList)
				item.SizeChanged -= OnItemSizeChanged;
			_internalList.Clear();
			OnItemSizeChanged(this, EventArgs.Empty);
		}

		public bool Contains(T item)
		{
			return _internalList.Contains(item);
		}

		public void CopyTo(T[] array, int arrayIndex)
		{
			_internalList.CopyTo(array, arrayIndex);
		}

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

		public bool IsReadOnly
		{
			get { return false; }
		}

		public bool Remove(T item)
		{
			item.SizeChanged -= OnItemSizeChanged;
			bool success = _internalList.Remove(item);
			if (success)
				OnItemSizeChanged(this, EventArgs.Empty);
			return success;
		}

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

		public IEnumerator<T> GetEnumerator()
		{
			return _internalList.GetEnumerator();
		}

		public int IndexOf(T item)
		{
			return _internalList.IndexOf(item);
		}

		public void Insert(int index, T item)
		{
			_internalList.Insert(index, item);
			item.SizeChanged += OnItemSizeChanged;
			OnItemSizeChanged(this, EventArgs.Empty);
		}

		public T this[int index]
		{
			get { return _internalList[index]; }
			set
			{
				_internalList[index] = value;
				value.SizeChanged += OnItemSizeChanged;
				OnItemSizeChanged(this, EventArgs.Empty);
			}
		}

		public void RemoveAt(int index)
		{
			T item = _internalList[index];
			_internalList.RemoveAt(index);
			item.SizeChanged -= OnItemSizeChanged;
			OnItemSizeChanged(this, EventArgs.Empty);
		}

		public event EventHandler ItemSizeChanged;

		void OnItemSizeChanged(object sender, EventArgs e)
		{
			EventHandler eh = ItemSizeChanged;
			if (eh != null)
				eh(this, EventArgs.Empty);
		}
	}
}