summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Issue3319.xaml.cs
blob: 234248d5f491a60f1ddcbf23575b760ce5bcdb29 (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
using System;
using System.Collections.Generic;

using Xamarin.Forms;
using System.Threading.Tasks;
using System.Collections.ObjectModel;
using System.ComponentModel;
using Xamarin.Forms.CustomAttributes;
using Xamarin.Forms.Internals;

#if UITEST
using Xamarin.UITest;
using NUnit.Framework;
#endif

namespace Xamarin.Forms.Controls
{
	[Preserve (AllMembers = true)]
	[Issue (IssueTracker.Github, 3319, "[iOS] Clear and adding rows exception")]
	public partial class Issue3319 : TestContentPage
	{
		#if UITEST
		[Test]
		public void Issue3319Test ()
		{
			RunningApp.WaitForElement (q => q.Marked ("Will this repo work?"));
			
		}
		#endif

		FavoritesViewModel ViewModel {
			get { return BindingContext as FavoritesViewModel; }
		}

		public Issue3319 ()
		{
			#if APP
			InitializeComponent ();
			BindingContext = new FavoritesViewModel ();

			listView.SeparatorVisibility = SeparatorVisibility.Default;
			listView.SeparatorColor = Color.FromHex ("#ababab");

			listView.ItemTapped += (sender, args) => {
				if (listView.SelectedItem == null)
					return;

				//do nothing anyway
				return;
			};
			#endif

		}

		protected override void Init ()
		{
	
		}

#pragma warning disable 1998 // considered for removal
		public async void OnDelete (object sender, EventArgs e)
#pragma warning restore 1998
		{
			var mi = ((MenuItem)sender);
			if (mi.CommandParameter == null)
				return;

			var articlelistingitem = mi.CommandParameter;

			if (articlelistingitem != null)
				DisplayAlert ("Alert", "I'm not deleting just refreshing...", "Ok");
			ViewModel.LoadFavoritesCommand.Execute (null);
		}


		protected override void OnAppearing ()
		{
			base.OnAppearing ();
			if (ViewModel == null || !ViewModel.CanLoadMore || ViewModel.IsBusy)
				return;

			Device.BeginInvokeOnMainThread (() => {
				ViewModel.LoadFavoritesCommand.Execute (null);
			});
		}

		[Preserve (AllMembers = true)]
		public class FavoritesViewModel :BaseViewModelF
		{
			public ObservableCollection<ArticleListing> FavoriteArticles { get; set; }

			public FavoritesViewModel ()
			{
				Title = "Favorites";
				FavoriteArticles = new ObservableCollection<ArticleListing> ();
			}

			Command _loadFavoritesCommand;

			public Command LoadFavoritesCommand {
				get {
					return _loadFavoritesCommand ??
					(_loadFavoritesCommand = new Command (async () => {
						await ExecuteFavoritesCommand ();
					}, () => {
						return !IsBusy;
					}));
				}
			}

#pragma warning disable 1998 // considered for removal
			public async Task ExecuteFavoritesCommand ()
#pragma warning restore 1998
			{
				if (IsBusy)
					return;

				IsBusy = true;
				LoadFavoritesCommand.ChangeCanExecute ();
				FavoriteArticles.Clear ();
				var articles = new ObservableCollection<ArticleListing> {
					new ArticleListing {
						ArticleTitle = "Will this repo work?",
						AuthorString = "Ben Crispin",
						FormattedPostedDate = "7-28-2015"
					},
					new ArticleListing {
						ArticleTitle = "Xamarin Forms BugZilla",
						AuthorString = "Some Guy",
						FormattedPostedDate = "7-28-2015"
					}
				};
				var templist = new ObservableCollection<ArticleListing> ();
				foreach (var article in articles) {
					//templist.Add(article);
					FavoriteArticles.Add (article);
				}
				//FavoriteArticles = templist;
				OnPropertyChanged ("FavoriteArticles");
				IsBusy = false;
				LoadFavoritesCommand.ChangeCanExecute ();
			}

		}
	}

	public class BaseViewModelF : INotifyPropertyChanged
	{
		public BaseViewModelF ()
		{
		}

		string _title = string.Empty;
		public const string TitlePropertyName = "Title";

		/// <summary>
		/// Gets or sets the "Title" property
		/// </summary>
		/// <value>The title.</value>
		public string Title {
			get { return _title; }
			set { SetProperty (ref _title, value, TitlePropertyName); }
		}

		string _subTitle = string.Empty;
		/// <summary>
		/// Gets or sets the "Subtitle" property
		/// </summary>
		public const string SubtitlePropertyName = "Subtitle";

		public string Subtitle {
			get { return _subTitle; }
			set { SetProperty (ref _subTitle, value, SubtitlePropertyName); }
		}

		string _icon = null;
		/// <summary>
		/// Gets or sets the "Icon" of the viewmodel
		/// </summary>
		public const string IconPropertyName = "Icon";

		public string Icon {
			get { return _icon; }
			set { SetProperty (ref _icon, value, IconPropertyName); }
		}

		bool _isBusy;
		/// <summary>
		/// Gets or sets if the view is busy.
		/// </summary>
		public const string IsBusyPropertyName = "IsBusy";

		public bool IsBusy {
			get { return _isBusy; }
			set { SetProperty (ref _isBusy, value, IsBusyPropertyName); }
		}

		bool _canLoadMore = true;
		/// <summary>
		/// Gets or sets if we can load more.
		/// </summary>
		public const string CanLoadMorePropertyName = "CanLoadMore";

		public bool CanLoadMore {
			get { return _canLoadMore; }
			set { SetProperty (ref _canLoadMore, value, CanLoadMorePropertyName); }
		}

		protected void SetProperty<T> (
			ref T backingStore, T value,
			string propertyName,
			Action onChanged = null)
		{

			if (EqualityComparer<T>.Default.Equals (backingStore, value))
				return;

			backingStore = value;

			if (onChanged != null)
				onChanged ();

			OnPropertyChanged (propertyName);
		}

		#region INotifyPropertyChanged implementation

		public event PropertyChangedEventHandler PropertyChanged;

		#endregion

		public void OnPropertyChanged (string propertyName)
		{
			if (PropertyChanged == null)
				return;

			PropertyChanged (this, new PropertyChangedEventArgs (propertyName));
		}

	}

	[Preserve (AllMembers = true)]
	public class ArticleListing
	{
		public ArticleListing ()
		{
		}

		public string ArticleTitle { get; set; }

		public string ShortArticleTitle { get; set; }

		public string AuthorString { get; set; }

		public string FormattedPostedDate { get; set; }

		public string KickerName { get; set; }

		public string ArticleUrl { get; set; }
	}

	public class YearOloArticleList
	{
		public string Year { get; set; }

		public List<ArticleListing> ListArticleListing { get; set; }
	}
}