summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Bugzilla31114.cs
blob: 6aa00319fddb4f9594f2b33160e36d63db820939 (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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
using System;
using Xamarin.Forms.CustomAttributes;
using System.Collections.ObjectModel;
using Xamarin.Forms.Internals;

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

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

	[Preserve (AllMembers = true)]
	[Issue (IssueTracker.Bugzilla, 31114, "iOS ContextAction leaves blank line after swiping in ListView")]
	public class Bugzilla31114 : TestContentPage 
	{
		ObservableCollection<ListItem> _items = new ObservableCollection<ListItem>();
		ListView _listView;
		Button _btbLoad;
		public Command RefreshListCommand;
		bool _isBusy = false;

		protected override void Init ()
		{
			
			RefreshListCommand = new Command(LoadItemsFromCommand, CanRefreshList);
			_listView = new ListView();
			_listView.ItemsSource = _items;
			_listView.ItemTemplate = new DataTemplate(typeof(TaskItemTemplate));
			_listView.RowHeight = 64;
			_listView.RefreshCommand = RefreshListCommand;
			_listView.IsPullToRefreshEnabled = true;

			_btbLoad = new Button { Text = "Load", AutomationId = "btnLoad", Command = RefreshListCommand };
			TaskItemTemplate.RefreshFromQuickComplete += TaskListPageRefreshFromQuickComplete;

			LoadItems();

			Content = new StackLayout {
				VerticalOptions = LayoutOptions.FillAndExpand,
				HorizontalOptions = LayoutOptions.FillAndExpand,
				Children = {
					_listView,_btbLoad
				}
			};
		}

		protected override void OnDisappearing ()
		{
			TaskItemTemplate.RefreshFromQuickComplete -= TaskListPageRefreshFromQuickComplete;
			base.OnDisappearing ();
		}

		bool CanRefreshList()
		{
			return !isBusy;
		}

		void LoadItemsFromCommand()
		{
			LoadItems();
		}

		void TaskListPageRefreshFromQuickComplete(object sender, ListItemEventArgs e)
		{
			Device.BeginInvokeOnMainThread(() =>
				{
					LoadItemsFromCommand();
				});
		}

		void LoadItems()
		{
			isBusy = true; 

			Random random = new Random(DateTime.Now.Millisecond);

			int count = random.Next(20, 30);

			_items.Clear();

			for (int i = 0; i < count - 1; i++)
			{
				var newItem = new ListItem()
				{
					Id = Guid.NewGuid().ToString(),
					EntityTypeId = 1350,
					BackgroundColor = "00aa00",
					TextColor = "FFFFFF",
					PrimaryText = "PIPE #"+(i+1000).ToString(),
					CircleColor = "0000aa",
					Icon = "",
					OtherText = random.Next(100, 200).ToString() + " ft",
					SecondaryText = "LENGTH " + i.ToString(),
					SupportsQuickComplete = true,
				};

				//Debug.WriteLine(newItem.PrimaryText);

				_items.Add(newItem);
			}


			isBusy = false;
		}

		bool isBusy
		{
			get { return _isBusy; }
			set
			{
				_isBusy = value;
				_listView.IsRefreshing = _isBusy;
			}
		}

		[Preserve (AllMembers = true)]
		public class ListItem
		{
			public string Id { get; set; }

			public string PrimaryText { get; set; }

			public string SecondaryText { get; set; }

			public string TertiaryText { get; set; }

			public string OtherText { get; set; }

			public string ListControl { get; set; }

			public string Icon { get; set; }

			public string Params { get; set; }

			public string BackgroundColor { get; set; }

			public string TextColor { get; set; }

			public string CircleColor { get; set; }

			public long EntityTypeId { get; set; }

			public bool SupportsQuickComplete { get; set; }

			public ListItem ()
			{
			}

			public string BackgroundColorColor
			{
				get
				{
					return BackgroundColor;
				}
				set
				{
					if (BackgroundColor != value)
					{
						BackgroundColor = value;
					}
				}
			}

			public string PrimaryLabelText
			{
				get
				{
					return PrimaryText;
				}
			}

			public string SecondaryLabelText
			{
				get
				{
					return SecondaryText;
				}
			}

			public string OtherLabelText
			{
				get
				{
					return OtherText;
				}
			}
		}

		public class ListItemEventArgs : EventArgs
		{
			public ListItem ListItem { get; set; }

			public ListItemEventArgs(ListItem item)
			{
				ListItem = item;
			}
		}

		[Preserve (AllMembers = true)]
		public class TaskItemTemplate : ViewCell
		{
			Image _photo;
			Label _mainLabel;
			Label _secondaryLabel;
			Label _distanceLabel;
			Label _statusCircle;
			StackLayout _stackLayout;
			StackLayout _primaryContent;
			StackLayout _secondaryContent;
			AbsoluteLayout _masterLayout;

			MenuItem _quickCompleteMenu;

			public static event EventHandler<ListItemEventArgs> RefreshFromQuickComplete;

			public TaskItemTemplate()
			{
				Init(true);
			}

			public TaskItemTemplate(bool fast = true)
			{
				Init(fast);
			}

			void Init(bool fast)
			{
				_photo = new Image
				{
					HeightRequest = 52,
					WidthRequest = 52,                
				};


				_mainLabel = new Label() { HeightRequest = 40, FontSize = 24, TranslationY = 5, LineBreakMode = LineBreakMode.TailTruncation };
				_mainLabel.SetBinding(Label.TextProperty, "PrimaryLabelText");

				_secondaryLabel = new Label() { HeightRequest = 40, FontSize = 16, TranslationY = -5, LineBreakMode = LineBreakMode.TailTruncation };
				_secondaryLabel.SetBinding(Label.TextProperty, "SecondaryLabelText");

#pragma warning disable 618
				_distanceLabel = new Label() { XAlign = TextAlignment.End, HorizontalOptions = LayoutOptions.EndAndExpand, FontSize = 11, LineBreakMode = LineBreakMode.NoWrap };
#pragma warning restore 618
				_distanceLabel.SetBinding(Label.TextProperty, "OtherLabelText");

				_statusCircle = new Label()
				{                
					HorizontalOptions = LayoutOptions.EndAndExpand, 
					FontSize = 30,
					TranslationY = 0,
				};

				_primaryContent = new StackLayout()
				{
					HorizontalOptions = LayoutOptions.StartAndExpand,
					Orientation = StackOrientation.Vertical,
					Children =
					{                            
						_mainLabel,
						_secondaryLabel,
					},                    
					Padding = new Thickness(12, 0, 0, 0),
				};

				_secondaryContent = new StackLayout()
				{
					MinimumWidthRequest = 50, 
					HorizontalOptions = LayoutOptions.EndAndExpand,
					Children =
					{
						_distanceLabel,
						_statusCircle,
					},
					Padding = new Thickness(0, 5, 5, 0),
				};

				_stackLayout = new StackLayout
				{
					Orientation = StackOrientation.Horizontal,
					Children =
					{
						_photo,
						_primaryContent,
						_secondaryContent,
					},
					Padding = new Thickness(5, 0, 0, 0) 
				};

				if (!fast)
				{
					View = _stackLayout;
				}
				else
				{
					_quickCompleteMenu = new MenuItem { Text = "Complete", IsDestructive = false };
					_quickCompleteMenu.SetBinding(MenuItem.CommandParameterProperty, new Binding("."));

					// Delete context menu action
					_quickCompleteMenu.Clicked += (sender, e) =>
					{
						FastCompleteForCmd(sender);
					};

					// Add this action to the cell
					ContextActions.Add(_quickCompleteMenu);

					_masterLayout = new AbsoluteLayout();

					_masterLayout.Children.Add(_stackLayout);

					AbsoluteLayout.SetLayoutFlags(_stackLayout, AbsoluteLayoutFlags.All);
					AbsoluteLayout.SetLayoutBounds(_stackLayout, new Rectangle(0.0, 0.0, 1.0f, 1.0f));

					View = _masterLayout;
				}
			}


			protected override void OnPropertyChanged(string propertyName = null)
			{
				base.OnPropertyChanged(propertyName);
				if (propertyName == "BackgroundColor")
				{
					var item = BindingContext as ListItem;
					if (item != null && !string.IsNullOrEmpty(item.BackgroundColor))
						View.BackgroundColor = Color.FromHex(item.BackgroundColor);
				}
			}

			protected override void OnBindingContextChanged()
			{
				try
				{
					base.OnBindingContextChanged();
					var item = BindingContext as ListItem;
					if (item != null)
					{
						Color transformedColor;

						if (!string.IsNullOrWhiteSpace(item.TextColor))
						{
							transformedColor = Color.FromHex(item.TextColor);

							_mainLabel.TextColor = transformedColor;
							_secondaryLabel.TextColor = transformedColor;
							_distanceLabel.TextColor = transformedColor;
						}

						if (string.IsNullOrEmpty(item.Icon))
							item.Icon = "https://beehive.blob.core.windows.net/staticimages/FeatureImages/MutantLizard01.png";

						_photo.Source = new UriImageSource()
						{
							Uri = new Uri(item.Icon),
							CachingEnabled = true,
							CacheValidity = new TimeSpan(30, 0, 0, 0),
						};

						if (!string.IsNullOrWhiteSpace(item.BackgroundColor))
						{
							transformedColor = Color.FromHex(item.BackgroundColor);
							View.BackgroundColor = transformedColor;
						}

						if (!string.IsNullOrWhiteSpace(item.CircleColor))
						{
							_statusCircle.Text = "\u25CF "; // ascii circle
							_statusCircle.TextColor = Color.FromHex(item.CircleColor);
							_statusCircle.FontSize = 30;
						}
					}
				}
				catch (Exception ex)
				{
				}
			}

#pragma warning disable 1998 // considered for removal
			async void FastCompleteForCmd(object sender)
#pragma warning restore 1998
			{
				try
				{
					{
						var item = BindingContext as ListItem;
						bool success = true; // await _taskListManager.FastComplete(item);

						if (success)
							RefreshFromQuickComplete(this, new ListItemEventArgs(item));
					}
				}
				catch (Exception ex)
				{

				}
			}

		}

#if UITEST && __IOS__
		[Test]
		[Ignore("Fails sometimes - needs a better test")]
		public void Bugzilla31114Test ()
		{
			for (int i = 0; i < 5; i++) {
				RunningApp.DragCoordinates (10, 300, 10, 10);
			}
			RunningApp.Tap (q => q.Marked ("btnLoad"));
			RunningApp.DragCoordinates (10, 300, 10, 10);
			RunningApp.WaitForElement (q => q.Marked ("PIPE #1007"));
			RunningApp.WaitForElement (q => q.Marked ("PIPE #1008"));
			RunningApp.WaitForElement (q => q.Marked ("PIPE #1009"));
			RunningApp.DragCoordinates (10, 300, 10, 10);
			RunningApp.WaitForElement (q => q.Marked ("PIPE #1010"));
			RunningApp.WaitForElement (q => q.Marked ("PIPE #1011"));
			RunningApp.WaitForElement (q => q.Marked ("PIPE #1012"));
			RunningApp.WaitForElement (q => q.Marked ("PIPE #1013"));
		}
#endif
	}
}