summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Bugzilla32148.cs
blob: 00e107354687920d060364bb880aedc9db525bdd (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
using System;
using Xamarin.Forms.CustomAttributes;
using System.Linq.Expressions;
using System.Threading.Tasks;
using System.Collections.ObjectModel;
using System.Linq;
using System.Collections.Generic;

using Xamarin.Forms.Internals;
#if UITEST
using Xamarin.Forms.Core.UITests;
using Xamarin.UITest;
using NUnit.Framework;
#endif

namespace Xamarin.Forms.Controls.Issues
{
#if UITEST
	[Category(UITestCategories.ListView)]
#endif

	[Preserve (AllMembers = true)]
	[Issue (IssueTracker.Bugzilla, 32148, " Pull to refresh hides the first item on a list view")]
	public class Bugzilla32148 : TestContentPage // or TestMasterDetailPage, etc ...
	{
		Button _searchBtn;
		ListView _contactsListView;
		ObservableCollection<Grouping1<string, ContactViewModel1>> _listViewItemSource;

		protected override void Init ()
		{
			Title = "Contacts";
			Content = CreateContent();
			LoadContactsAsync();
		}

		Layout CreateContent()
		{
			_listViewItemSource = new ObservableCollection<Grouping1<string, ContactViewModel1>>();

			_contactsListView = new ListView()
			{
				ItemsSource = _listViewItemSource,
				IsPullToRefreshEnabled = true,
				IsGroupingEnabled = true,
				GroupShortNameBinding = new Binding("Key"),
				GroupHeaderTemplate = new DataTemplate(typeof(HeaderCell)),
				HasUnevenRows = true,
				ItemTemplate = new DataTemplate(typeof(ContactItemTemplate))
			};

			_contactsListView.Refreshing += contactsListView_Refreshing;
			_searchBtn = new Button() { Text = "Search" };
			_searchBtn.Clicked += (object sender, EventArgs e) => _contactsListView.BeginRefresh ();

			Grid grd = new Grid ();
			grd.RowDefinitions.Add (new RowDefinition () { Height = GridLength.Auto } );
			grd.RowDefinitions.Add (new RowDefinition ());
			grd.Children.Add (_searchBtn);
			grd.Children.Add (_contactsListView);
			Grid.SetRow (_contactsListView, 1);
			return grd;
		}

		async void contactsListView_Refreshing(object sender, EventArgs e)
		{
			await Task.Delay (1000);
			await LoadContactsAsync(true);
		}

		async Task LoadContactsAsync(bool isPullToRefresh = false)
		{
			await ReadFromDbAsync();
			_contactsListView.IsRefreshing &= !isPullToRefresh;
		}

		async Task ReadFromDbAsync(Expression<Func<Contact1, bool>> searchExpression = null)
		{
				await Task.Run(() =>
				{
					Device.BeginInvokeOnMainThread(() =>
						{
							// If we want to filter the data, GetItems by expression
							IList<Contact1> contactEntities = new List<Contact1>();
							List<ContactViewModel1> data = new List<ContactViewModel1>();

							if (contactEntities == null || contactEntities.Count == 0)
							{
								// Fill with dummy contacts
								for (int i = 0; i < 20; i++)
								{
									Contact1 contact = new Contact1()
									{
										FirstName = "Contact" + i,
										LastName = "LastName",
										Company = "Company" + i
									} ;
									contactEntities.Add(contact);
								}
							}

							// Create Contact items for the listView
							foreach (Contact1 contact in contactEntities)
							{
								ContactViewModel1 contactItem = new ContactViewModel1()
								{
									FirstName = contact.FirstName,
									LastName = contact.LastName,
									FullName = contact.FirstName + " " + contact.LastName,
									Company = contact.Company,
								} ;

								data.Add(contactItem);
							}

							// Sort, order and group the contacts
							var contacts = from contact in data
								orderby contact.LastName, contact.FirstName
							group contact by contact.FirstNameSort into contactGroup
							select new Grouping1<string, ContactViewModel1>(contactGroup.Key, contactGroup);

							// Create a new collection of groups
							var grouppedContacts = new ObservableCollection<Grouping1<string, ContactViewModel1>>(contacts);

							_contactsListView.ItemsSource = grouppedContacts;
						} );
				} );
		}

		public class Grouping1<K, T> : ObservableCollection<T>
		{
			public K Key { get; private set; }

			public Grouping1(K key, IEnumerable<T> items)
			{
				Key = key;
				foreach (T item in items)
				{
					Items.Add(item);
				}
			}
		}

		[Preserve (AllMembers = true)]
		public class ContactViewModel1
		{
			public string FirstName { get; set; }
			public string LastName { get; set; }
			public string FullName { get; set; }
			public string Company { get; set; }
			public ImageSource IconSource { get; set; }

			public string FirstNameSort
			{
				get
				{
					if (string.IsNullOrWhiteSpace(FirstName) || FirstName.Length == 0)
					{
						return "?";
					}

					return FirstName[0].ToString().ToUpper();
				}
			}
		}

		[Preserve (AllMembers = true)]
		public class Contact1
		{
			public string FirstName { get; set; }
			public string LastName { get; set; }
			public string Company { get; set; }
			public byte[] ProfilePicture { get; set; }
			public string Email { get; set; }
			public string Mobile { get; set; }
			public string RoomNumber { get; set; }
			public string Street { get; set; }
			public string Zip { get; set; }
			public string City { get; set; }
			public string CountryCode { get; set; }
		}

		[Preserve (AllMembers = true)]
		public class HeaderCell : ViewCell
		{
			public HeaderCell()
			{
				Height = 23;

				Label title = new Label
				{
					TextColor = Color.White,
					VerticalOptions = LayoutOptions.Center
				} ;

				title.SetBinding(Label.TextProperty, "Key");

				View = new StackLayout
				{
					HorizontalOptions = LayoutOptions.FillAndExpand,
					HeightRequest = 23,
					BackgroundColor = Color.Pink,
					Orientation = StackOrientation.Horizontal,
					Padding = new Thickness(Sizes.GroupingSidePadding, 0, 0, 0),
					Children = { title }
				};
			}

			struct Sizes
			{
				public static readonly double GroupingSidePadding = 5;
			}
		}

		[Preserve (AllMembers = true)]
		public class ContactItemTemplate : ImageCell
		{
			public ContactItemTemplate()
				: base()
			{

				SetBinding(TextProperty, new Binding("FullName"));
				SetBinding(DetailProperty, new Binding("Company"));
				SetBinding(ImageSourceProperty, new Binding("IconSource"));

				Height = 50;
			}
		}

		#if UITEST
		[Test]
		public void Bugzilla32148Test ()
		{
			RunningApp.WaitForElement (q => q.Marked ("Contact0 LastName"));
			RunningApp.Tap (q => q.Marked("Search"));
			RunningApp.WaitForElement (q => q.Marked ("Contact0 LastName"));
			RunningApp.Screenshot ("For manual review, is the first cell visible?");
		}
		#endif
	}

}