summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Bugzilla34720.cs
blob: 15f601ae04df6eb683daede796d9af8fef3311f7 (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
using Xamarin.Forms.CustomAttributes;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Threading.Tasks;
using System;
using Xamarin.Forms.Internals;

namespace Xamarin.Forms.Controls
{
	[Preserve (AllMembers = true)]
	[Issue (IssueTracker.Bugzilla, 34720, "Incorrect iOS button IsEnabled when scrolling ListView with command binding ")]
	public class Bugzilla34720 : TestContentPage // or TestMasterDetailPage, etc ...
	{
		protected override void Init ()
		{
			Title = "Test Command Binding";
			_list = new ListView () { 
				ClassId = "SampleList",
				// Note: Turning on and off row height does not effect the issue, 
				// but with row heights on there is a visual glitch with the recyclyed row spacing
				//RowHeight = SampleViewCell.RowHeight,
				HasUnevenRows = true,
				ItemTemplate = new DataTemplate (typeof(SampleViewCell)),
				BackgroundColor = Color.FromHex ("E0E0E0"),
				VerticalOptions = LayoutOptions.FillAndExpand,
				HorizontalOptions = LayoutOptions.FillAndExpand,
			};
			_list.SetBinding (ListView.ItemsSourceProperty, Binding.Create<TestListViewModel> (r => r.Items));
			_list.SetBinding (ListView.RefreshCommandProperty, Binding.Create<TestListViewModel> (r => r.RefreshCommand));
			_list.SetBinding (ListView.IsRefreshingProperty, Binding.Create<TestListViewModel> (r => r.IsRefreshing));

			var listViewModel = new TestListViewModel ();
			listViewModel.AddTestData ();
			BindingContext = listViewModel;


			_list.ItemTapped += (sender, e) =>
			{
				DisplayAlert("hello", "You tapped " + e.Item.ToString(), "OK", "Cancel");
			};

			var btnDisable = new Button () {
				Text = "Disable ListView",
			};

			btnDisable.Clicked += (object sender, EventArgs e) => {
				if (_list.IsEnabled == true){
					_list.IsEnabled = false;
					btnDisable.Text = "Enable ListView";
				}
				else {
					_list.IsEnabled = true;
					btnDisable.Text = "Disable ListView";
				}
			};

			Content = new StackLayout
			{
				VerticalOptions = LayoutOptions.FillAndExpand,
				Children = { btnDisable, _list }
			};
		}

		ListView _list;

		[Preserve (AllMembers = true)]
		public class SampleViewCell : ViewCell
		{
			public static int RowHeight = 120;
			static int s_idSeed;
			int id;

			public SampleViewCell ()
			{
				id = s_idSeed++;
				var grid = new Grid {
					ClassId = "SampleCard",
					Padding = new Thickness (7, 10),
					RowSpacing = 0,
					ColumnSpacing = 0,
					RowDefinitions = {
						new RowDefinition{ Height = new GridLength (80, GridUnitType.Absolute) },	
						new RowDefinition{ Height = new GridLength (40, GridUnitType.Absolute) },	
					},

				};

				var head = new SampleHeaderView ();
				grid.Children.Add (head);

				var foot = new SampleListActionView ();
				Grid.SetRow (foot, 1);
				grid.Children.Add (foot);

				View = grid;
			}

			#region Testing Code

			// Note this block can be removed it is just used to observing the ViewCell creation.
			int _counter;


			protected override void OnAppearing ()
			{

				base.OnAppearing ();
				_counter++;
				System.Diagnostics.Debug.WriteLine ("OnAppearing {0}, {1}", id, _counter);
			}

			protected override void OnDisappearing ()
			{
				base.OnDisappearing ();
				_counter--;
				System.Diagnostics.Debug.WriteLine ("OnDisappearing {0}, {1}", id, _counter);
			}

			#endregion

			public class SampleHeaderView : ContentView
			{

				public SampleHeaderView ()
				{
					//+-----------+----------------+
					//|       1   |                |
					//+-----------+----------------+
					//|       2   |                |
					//+-----------+----------------+


					var grid = new Grid {
						Padding = new Thickness (5, 5, 5, 1),
						RowSpacing = 1,
						ColumnSpacing = 1,
						BackgroundColor = Color.FromHex ("FAFAFA"),
						RowDefinitions = {
							new RowDefinition{ Height = new GridLength (1.25, GridUnitType.Star) },	
							new RowDefinition{ Height = new GridLength (0.8, GridUnitType.Star) },	
						},
						ColumnDefinitions = {
							new ColumnDefinition{ Width = new GridLength (1, GridUnitType.Star) },
							new ColumnDefinition{ Width = new GridLength (1, GridUnitType.Star) },
						} 
					};
					//1 number
					var materialNumber = new Label () {
						VerticalOptions = LayoutOptions.StartAndExpand,
						HorizontalOptions = LayoutOptions.StartAndExpand
					};
					Grid.SetColumnSpan (materialNumber, 2);
					materialNumber.SetBinding (Label.TextProperty, Binding.Create<TestViewModel> (vm => vm.Number));
					grid.Children.Add (materialNumber);

					//2 Description
					var materialDescription = new Label () {
						VerticalOptions = LayoutOptions.StartAndExpand,
						HorizontalOptions = LayoutOptions.StartAndExpand
					};
					Grid.SetColumnSpan (materialDescription, 2);
					Grid.SetRow (materialDescription, 1);
					materialDescription.SetBinding (Label.TextProperty, Binding.Create<TestViewModel> (vm => vm.Description));
					//grid.Children.Add (materialDescription);

					//3 Approve Label
					var canApprove = new Label () {
						VerticalOptions = LayoutOptions.StartAndExpand,
						HorizontalOptions = LayoutOptions.StartAndExpand
					};
					Grid.SetColumn (canApprove, 1);
					Grid.SetRow (canApprove, 1);
					canApprove.SetBinding (Label.TextProperty, Binding.Create<TestViewModel> (vm => vm.CanApprove, stringFormat: "Can Approve: {0}"));
					grid.Children.Add (canApprove);

					//3 Approve Label
					var canDeny = new Label () {
						VerticalOptions = LayoutOptions.StartAndExpand,
						HorizontalOptions = LayoutOptions.StartAndExpand
					};
					Grid.SetColumn (canDeny, 0);
					Grid.SetRow (canDeny, 1);
					canDeny.SetBinding (Label.TextProperty, Binding.Create<TestViewModel> (vm => vm.CanDeny, stringFormat: "Can Deny: {0}"));
					grid.Children.Add (canDeny);

					Content = grid;

				}
			}

			public class SampleListActionView : ContentView
			{
				public SampleListActionView ()
				{
					var overallGrid = new Grid {
						BackgroundColor = Color.FromHex ("FAFAFA"),
						HorizontalOptions = LayoutOptions.FillAndExpand,
						VerticalOptions = LayoutOptions.CenterAndExpand,
						ColumnDefinitions = {
							new ColumnDefinition{ Width = new GridLength (1, GridUnitType.Star) },
							new ColumnDefinition{ Width = new GridLength (1, GridUnitType.Star) },
						}
					};

					var grid = new Grid {
						VerticalOptions = LayoutOptions.FillAndExpand,
						HorizontalOptions = LayoutOptions.FillAndExpand,
						ColumnDefinitions = {
							new ColumnDefinition{ Width = new GridLength (1, GridUnitType.Star) },
							new ColumnDefinition{ Width = new GridLength (1, GridUnitType.Star) },
						}
					};
					// 1 Deny
					var denyBtn = new Button {
						ClassId = "btnReject",
						Text = "DENY",
						HorizontalOptions = LayoutOptions.FillAndExpand,
						VerticalOptions = LayoutOptions.FillAndExpand
					};

					denyBtn.SetBinding (Button.CommandProperty, Binding.Create<TestViewModel> (r => r.DenyCommand));

					grid.Children.Add (denyBtn);

					// 2 Approve
					var approveBtn = new Button {
						ClassId = "btnApprove",
						Text = "Approve",
						HorizontalOptions = LayoutOptions.FillAndExpand,
						VerticalOptions = LayoutOptions.FillAndExpand,

					};
					Grid.SetColumn (approveBtn, 1);
					approveBtn.SetBinding (Button.CommandProperty, Binding.Create<TestViewModel> (r => r.ApproveCommand));
					grid.Children.Add (approveBtn);


					Grid.SetColumn (grid, 1);
					overallGrid.Children.Add (grid);
					Content = overallGrid;
				}

			}


		}

		[Preserve (AllMembers = true)]
		public class TestListViewModel : INotifyPropertyChanged
		{
			Collection<TestViewModel> _items = new ObservableCollection<TestViewModel> ();

			public void AddTestData ()
			{
				for (int i = 0; i < 20; i++) {
					_items.Add (new TestViewModel () {
						Description = string.Format ("Sample Description {0}", i),
						Number = (i + 1).ToString (),
						CanApprove = i % 2 == 0,
						CanDeny = i % 2 == 0
					});
				}
				RaisePropertyChanged ("Items");			
			}

			public Collection<TestViewModel> Items { get { return _items; } set { _items = value; } }

			public Command RefreshCommand {
				get {
					return new Command (OnRefresh);
				}
			}

			public bool IsRefreshing { get; set; }

			async void OnRefresh ()
			{
				IsRefreshing = true;
				RaisePropertyChanged ("IsRefreshing");	
				_items.Clear ();
				await Task.Delay (1000);
				AddTestData ();
				IsRefreshing = false;
				RaisePropertyChanged ("IsRefreshing");
			}

			#region INotifyPropertyChanged implementation

			public event PropertyChangedEventHandler PropertyChanged;

			#endregion

			protected virtual void RaisePropertyChanged (string propertyName)
			{
				PropertyChangedEventHandler propertyChanged = PropertyChanged;
				if (propertyChanged != null) {
					propertyChanged (this, new PropertyChangedEventArgs (propertyName));
				}
			}
		}

		[Preserve (AllMembers = true)]
		public class TestViewModel
		{

			public string Number {	get; set; }

			public string Description {	get; set; }

			bool _canApprove;

			public bool CanApprove {
				get{ return _canApprove; }
				set {
					_canApprove = value;
					ApproveCommand.ChangeCanExecute ();
				}
			}

			bool _canDeny;

			public bool CanDeny {
				get { return _canDeny; }
				set {
					_canDeny = value;
					DenyCommand.ChangeCanExecute ();
				}
			}

			public Command ApproveCommand {
				get {
					return new Command (OnApprove, () => CanApprove);
				}
			}

			public Command DenyCommand {
				get {
					return new Command (OnDeny, () => CanDeny);
				}
			}

			async void OnApprove ()
			{
				await Application.Current.MainPage.DisplayAlert ("Approve", string.Format ("Can Approve {0} {1}", Number, CanApprove), "Ok");
			}

			async void OnDeny ()
			{
				await Application.Current.MainPage.DisplayAlert ("Deny", string.Format ("Can Deny {0} {1}", Number, CanDeny), "Ok");
			}
		}
	}
}