summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Bugzilla43313.cs
blob: f0426a47a63e26686391d5f6fb70d150a3655adc (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
using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Runtime.CompilerServices;
using Xamarin.Forms.CustomAttributes;
using Xamarin.Forms.Internals;

namespace Xamarin.Forms.Controls
{
	[Preserve(AllMembers = true)]
	public abstract class Bugzilla43313_Template : ContentPage
	{
		public static int ItemCount = 20;
		readonly ListView _listView;
		protected abstract DataTemplate CellTemplate();

		protected Bugzilla43313_Template()
		{
			BindingContext = new _43313ViewModel();

			var btnAdd = new Button
			{
				Text = "Add item",
				WidthRequest = 100
			};
			btnAdd.Clicked += BtnAddOnClicked;

			var btnBottom = new Button
			{
				Text = "Scroll to end",
				WidthRequest = 100
			};
			btnBottom.Clicked += BtnBottomOnClicked;

			var btnPanel = new StackLayout
			{
				Orientation = StackOrientation.Horizontal,
				HorizontalOptions = LayoutOptions.Center,
				Children = { btnAdd, btnBottom }
			};

			_listView = new ListView
			{
				HasUnevenRows = true,
				BackgroundColor = Color.Transparent,
				VerticalOptions = LayoutOptions.FillAndExpand,
				ItemTemplate = CellTemplate()
			};

			_listView.SetBinding(ListView.ItemsSourceProperty, new Binding("ListViewContent"));
			_listView.ItemTapped += (sender, e) => ((ListView)sender).SelectedItem = null;

			var instructions = new Label() { FontSize = 12, Text = "Tap the 'Add Item' button; a new item should be added to the bottom of the list and the list should scroll smoothly to display it. If the list scrolls back to the top before scrolling down to the new item, the test has failed." };

			Content = new StackLayout
			{
				Padding = new Thickness(0, 40, 0, 0),
				Children =
				{
					instructions,
					btnPanel,
					_listView
				}
			};
		}

		void BtnAddOnClicked(object sender, EventArgs eventArgs)
		{
			string str = $"Item {ItemCount++}";
			var item = new _43313Model { Name = str };
			(BindingContext as _43313ViewModel).ListViewContent.Add(item);

			_listView.ScrollTo(item, ScrollToPosition.End, true);
		}

		void BtnBottomOnClicked(object sender, EventArgs e)
		{
			_43313Model item = (BindingContext as _43313ViewModel).ListViewContent.Last();
			_listView.ScrollTo(item, ScrollToPosition.End, true);
		}

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

		[Preserve(AllMembers = true)]
		public class _43313ViewModel : INotifyPropertyChanged
		{
			ObservableCollection<_43313Model> _listViewContent;

			public _43313ViewModel()
			{
				ListViewContent = new ObservableCollection<_43313Model>();

				for (int n = 0; n < ItemCount; n++)
				{
					_listViewContent.Add(new _43313Model { Name = $"Item {n}" });
				}
			}

			public ObservableCollection<_43313Model> ListViewContent
			{
				get { return _listViewContent; }
				set
				{
					_listViewContent = value;
					OnPropertyChanged();
				}
			}

			public event PropertyChangedEventHandler PropertyChanged;

			protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
			{
				PropertyChangedEventHandler handler = PropertyChanged;
				handler?.Invoke(this, new PropertyChangedEventArgs(propertyName));
			}
		}
	}

	[Preserve(AllMembers = true)]
	public class Bugzilla43313_KnownHeight : Bugzilla43313_Template
	{
		protected override DataTemplate CellTemplate()
		{
			return new DataTemplate(() =>
			{
				var label = new Label { FontSize = 16, VerticalOptions = LayoutOptions.Center };
				label.SetBinding(Label.TextProperty, "Name");
				int height = 60 + new Random().Next(10, 100);

				return new ViewCell
				{
					Height = height,
					View = new StackLayout
					{
						Padding = new Thickness(0, 5, 0, 5),
						BackgroundColor = Color.Transparent,
						Children =
							{
								label
							}
					}
				};
			});
		}
	}

	[Preserve(AllMembers = true)]
	public class Bugzilla43313_EstimatedHeight : Bugzilla43313_Template
	{
		protected override DataTemplate CellTemplate()
		{
			return new DataTemplate(() =>
			{
				var label = new Label { FontSize = 16, VerticalOptions = LayoutOptions.Center };
				label.SetBinding(Label.TextProperty, "Name");

				label.FontSize = 12 + new Random().Next(1, 6);

				return new ViewCell
				{
					View = new StackLayout
					{
						Padding = new Thickness(0, 5, 0, 5),
						BackgroundColor = Color.Transparent,
						Children =
							{
								label
							}
					}
				};
			});
		}
	}

	[Preserve(AllMembers = true)]
	[Issue(IssueTracker.Bugzilla, 43313, "Adding an item to ListView ItemSource has unexpected animation with different height rows and HasUnevenRows is true", PlatformAffected.iOS)]
	public class Bugzilla43313 : TestNavigationPage
	{
		protected override void Init()
		{
			var root = new ContentPage();
			
			var layout = new StackLayout();

			var knownHeightButton = new Button() { Text = "Known Height (original bug report test case)" };
			knownHeightButton.Clicked += (sender, args) => PushAsync(new Bugzilla43313_KnownHeight());

			var estimatedHeightButton = new Button() { Text = "Estimated Height" };
			estimatedHeightButton.Clicked += (sender, args) => PushAsync(new Bugzilla43313_EstimatedHeight());

			layout.Children.Add(knownHeightButton);
			layout.Children.Add(estimatedHeightButton);

			root.Content = layout;

			PushAsync(root);
		}
	}
}