summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Controls/GalleryPages/GroupedListContactsGallery.cs
blob: 68115f9fb4d6a714f2ef1544d4d7b41c9ed1522b (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
234
235
236
237
238
239
240
241
242
243
244
245
246
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;

namespace Xamarin.Forms.Controls
{
	[Preserve (AllMembers = true)]
	public class GroupedListContactsGallery
		: ContentPage
	{
		internal class Contact
		{
			public string FirstName
			{
				get;
				set;
			}

			public string LastName
			{
				get;
				set;
			}

			public string FullName
			{
				get { return FirstName + " " + LastName; }
			}

			public string Title
			{
				get;
				set;
			}
		}

		readonly ListView _list = new ListView {
			ItemTemplate = new DataTemplate (() => {
				Label name = new Label();
				name.SetBinding (Label.TextProperty, "FullName");

#pragma warning disable 618
				Label title = new Label { Font = Font.SystemFontOfSize (NamedSize.Micro) };
#pragma warning restore 618
				title.SetBinding (Label.TextProperty, "Title");

				return new ViewCell { View = new StackLayout  {
					Children = {
						name,
						title
					}
				} };
			}),

			GroupDisplayBinding = new Binding ("Key"),
			GroupShortNameBinding = new Binding ("Key"),
			IsGroupingEnabled = true
		};

		readonly List<Contact> _contacts = new List<Contact> {
			new Contact { FirstName = "Jason", LastName = "Smith", Title = "Software Engineer" },
			new Contact { FirstName = "Eric", LastName = "Maupin", Title = "Software Engineer" },
			new Contact { FirstName = "Seth", LastName = "Rosetter", Title = "Software Engineer"  },
			new Contact { FirstName = "Stephane", LastName = "Delcroix", Title = "Software Engineer" }
		};

		readonly Random _rand = new Random (42);

		[Preserve (AllMembers = true)]
		internal class Group
			: ObservableCollection<Contact>
		{
			public Group (string key)
			{
				Key = key;
			}

			public string Key
			{
				get;
				private set;
			}
		}

		bool _sortedByFirst = true;
		ObservableCollection<Group> _sortedContacts;

		public GroupedListContactsGallery()
		{
			var addRandom = new Button { Text = "Random" };
			addRandom.Clicked += (sender, args) => {
				Contact contact = GetRandomContact();
				_contacts.Add (contact);

				AddContact (_sortedContacts, contact);
			};

			var addRandomToExisting = new Button { Text = "Random Group" };
			addRandomToExisting.Clicked += (sender, args) => {
				Contact contact;
				do {
					contact = GetRandomContact();
				} while (!_sortedContacts.Any (g => GetSortChar (g.First()) == GetSortChar (contact)));

				_contacts.Add (contact);
				AddContact (_sortedContacts, contact);
			};

			var groupByFirst = new Button { Text = "First" };
			groupByFirst.Clicked += (sender, args) => {
				_sortedByFirst = true;

				SetupContacts();
				_list.ItemsSource = _sortedContacts;
			};

			var groupByLast = new Button { Text = "Last" };
			groupByLast.Clicked += (sender, args) => {
				_sortedByFirst = false;

				SetupContacts();
				_list.ItemsSource = _sortedContacts;
			};

			Content = new StackLayout {
				Orientation = StackOrientation.Vertical,
				Children = {
					new StackLayout {
						Orientation = StackOrientation.Vertical,
						Children = {
							new StackLayout {
								Orientation = StackOrientation.Horizontal,
								Children = {
									new Label { Text = "Sort: " },
									groupByFirst,
									groupByLast
								}
							},

							new StackLayout {
								Orientation = StackOrientation.Horizontal,
								Children = {
									addRandom,
									addRandomToExisting,
								}
							}
						},
					},
					

					_list
				}
			};

			SetupContacts();
			_list.ItemsSource = _sortedContacts;
		}

		const string Chars = "abcdefghijklmnopqrstuvwxyz";

		void SetupContacts()
		{
			var coll = new ObservableCollection<Group>();
			foreach (var contact in _contacts)
				AddContact (coll, contact);

			_sortedContacts = coll;
		}

		void AddContact (ObservableCollection<Group> contactGroups, Contact contact)
		{
			char sortChar = GetSortChar (contact);

			var collection = contactGroups.FirstOrDefault (col => {
				var c = col.First();
				return (GetSortChar (c) == sortChar);
			});

			if (collection == null) {
				var ocontacts = new Group (GetSortChar (contact).ToString()) { contact };
				InsertBasedOnSort (contactGroups, ocontacts, c => GetSortChar (c.First()));
			} else
				InsertBasedOnSort (collection, contact, c => GetSortString (c)[0]);
		}

		int IndexOf<T> (IEnumerable<T> elements, T element)
		{
			int i = 0;
			foreach (T e in elements) {
				if (Equals (e, element))
					return i;

				i++;
			}

			return -1;
		}

		void InsertBasedOnSort<T,TSort> (IList<T> items, T item, Func<T, TSort> sortBy)
		{
			List<T> newItems = new List<T> (items);
			newItems.Add (item);
			int index = IndexOf (newItems.OrderBy (sortBy), item);
			items.Insert (index, item);
		}

		char GetSortChar (Contact contact)
		{
			return GetSortString (contact)[0];
		}

		string GetSortString (Contact contact)
		{
			return (_sortedByFirst) ? contact.FirstName : contact.LastName;
		}

		Contact GetRandomContact()
		{
			Contact contact = new Contact();

			int firstLen = _rand.Next (3, 7);
			
			var builder = new StringBuilder (firstLen);
			for (int i = 0; i < firstLen; i++) {
				char c = Chars[_rand.Next (0, Chars.Length)];
				builder.Append ((i != 0) ? c : char.ToUpper (c));
			}
			
			contact.FirstName = builder.ToString();

			int lastLen = _rand.Next (3, 7);
			builder.Clear();
			for (int i = 0; i < lastLen; i++) {
				char c = Chars[_rand.Next (0, Chars.Length)];
				builder.Append ((i != 0) ? c : char.ToUpper (c));
			}

			contact.LastName = builder.ToString();
			contact.Title = "Software Engineer";

			return contact;
		}
	}
}