summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Controls/HanselForms/BlogPage.xaml.cs
blob: 67afb59d8c63bf180a7c1d61912915414d2af283 (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
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Net.Http;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Xml.Linq;
using System.Linq;
using Xamarin.Forms;

namespace Xamarin.Forms.Controls
{
	public partial class BlogPage : ContentPage
	{

		private BlogFeedViewModel ViewModel
		{
			get { return BindingContext as BlogFeedViewModel; }
		}

		public BlogPage()
		{
			InitializeComponent();
			BindingContext = new BlogFeedViewModel();

			listView.ItemTapped += (sender, args) =>
			{
				if (listView.SelectedItem == null)
					return;
				this.Navigation.PushAsync(new BlogDetailsView(listView.SelectedItem as FeedItem));
				listView.SelectedItem = null;
			};
		}

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

			ViewModel.LoadItemsCommand.Execute(null);
		}
	}


	public class BlogDetailsView : BaseView
	{
		public BlogDetailsView(FeedItem item)
		{
			BindingContext = item;
			var webView = new WebView
			{
				VerticalOptions = LayoutOptions.FillAndExpand,
				HorizontalOptions = LayoutOptions.FillAndExpand
			};
			webView.Source = new HtmlWebViewSource
			{
				Html = item.Description
			};
			Content = new StackLayout
			{
				Children =
		{
		  webView
		}
			};
			var share = new ToolbarItem
			{
				Icon = "ic_share.png",
				Text = "Share",
				//Command = new Command(() => CrossShare.Current
				//  .Share("Be sure to read @shanselman's " + item.Title + " " + item.Link))
			};

			ToolbarItems.Add(share);
		}
	}


	public class BlogFeedViewModel : HBaseViewModel
	{
		public BlogFeedViewModel()
		{
			Title = "Blog";
			Icon = "blog.png";
		}

		private ObservableCollection<FeedItem> feedItems = new ObservableCollection<FeedItem>();

		/// <summary>
		/// gets or sets the feed items
		/// </summary>
		public ObservableCollection<FeedItem> FeedItems
		{
			get { return feedItems; }
			set { feedItems = value; OnPropertyChanged(); }
		}

		private FeedItem selectedFeedItem;
		/// <summary>
		/// Gets or sets the selected feed item
		/// </summary>
		public FeedItem SelectedFeedItem
		{
			get { return selectedFeedItem; }
			set
			{
				selectedFeedItem = value;
				OnPropertyChanged();
			}
		}

		private Command loadItemsCommand;
		/// <summary>
		/// Command to load/refresh items
		/// </summary>
		public Command LoadItemsCommand
		{
			get { return loadItemsCommand ?? (loadItemsCommand = new Command(async () => await ExecuteLoadItemsCommand())); }
		}

		private async Task ExecuteLoadItemsCommand()
		{
			if (IsBusy)
				return;

			IsBusy = true;
			var error = false;
			try
			{
				var responseString = string.Empty;
				using (var httpClient = new HttpClient())
				{
					var feed = "http://feeds.hanselman.com/ScottHanselman";
					responseString = await httpClient.GetStringAsync(feed);
				}

				FeedItems.Clear();
				var items = await ParseFeed(responseString);
				foreach (var item in items)
				{
					FeedItems.Add(item);
				}
			}
			catch
			{
				error = true;
			}

			if (error)
			{
				var page = new ContentPage();
				await page.DisplayAlert("Error", "Unable to load blog.", "OK");

			}

			IsBusy = false;
		}



		/// <summary>
		/// Parse the RSS Feed
		/// </summary>
		/// <param name="rss"></param>
		/// <returns></returns>
		private async Task<List<FeedItem>> ParseFeed(string rss)
		{
			return await Task.Run(() =>
				{
					var xdoc = XDocument.Parse(rss);
					var id = 0;
					return (from item in xdoc.Descendants("item")
							select new FeedItem
							{
								Title = (string)item.Element("title"),
								Description = (string)item.Element("description"),
								Link = (string)item.Element("link"),
								PublishDate = (string)item.Element("pubDate"),
								Category = (string)item.Element("category"),
								Id = id++
							}).ToList();
				});
		}

		/// <summary>
		/// Gets a specific feed item for an Id
		/// </summary>
		/// <param name="id"></param>
		/// <returns></returns>
		public FeedItem GetFeedItem(int id)
		{
			return FeedItems.FirstOrDefault(i => i.Id == id);
		}
	}

	public class FeedItem : INotifyPropertyChanged
	{



		public string Description { get; set; }
		public string Link { get; set; }
		private string publishDate;
		public string PublishDate
		{
			get { return publishDate; }
			set
			{
				DateTime time;
				if (DateTime.TryParse(value, out time))
					publishDate = time.ToLocalTime().ToString("D");
				else
					publishDate = value;
			}
		}
		public string Author { get; set; }
		public string AuthorEmail { get; set; }
		public int Id { get; set; }
		public string CommentCount { get; set; }
		public string Category { get; set; }

		public string Mp3Url { get; set; }

		private string title;
		public string Title
		{
			get
			{
				return title;
			}
			set
			{
				title = value;

			}
		}

		private string caption;

		public string Caption
		{
			get
			{
				if (!string.IsNullOrWhiteSpace(caption))
					return caption;


				//get rid of HTML tags
				caption = Regex.Replace(Description, "<[^>]*>", string.Empty);


				//get rid of multiple blank lines
				caption = Regex.Replace(caption, @"^\s*$\n", string.Empty, RegexOptions.Multiline);

				caption = caption.Substring(0, caption.Length < 200 ? caption.Length : 200).Trim() + "...";
				return caption;
			}
		}

		public string Length { get; set; }

		private bool showImage = true;

		public bool ShowImage
		{
			get { return showImage; }
			set { showImage = value; }
		}

		private string image = @"https://secure.gravatar.com/avatar/70148d964bb389d42547834e1062c886?s=60&r=x&d=http%3a%2f%2fd1iqk4d73cu9hh.cloudfront.net%2fcomponents%2fimg%2fuser-icon.png";

		/// <summary>
		/// When we set the image, mark show image as true
		/// </summary>
		public string Image
		{
			get { return image; }
			set
			{
				image = value;
				showImage = true;
			}

		}

		private string firstImage;
		public string FirstImage
		{
			get
			{
				if (!string.IsNullOrWhiteSpace(firstImage))
					return firstImage;


				var regx = new Regex("http://([\\w+?\\.\\w+])+([a-zA-Z0-9\\~\\!\\@\\#\\$\\%\\^\\&amp;\\*\\(\\)_\\-\\=\\+\\\\\\/\\?\\.\\:\\;\\'\\,]*)?.(?:jpg|bmp|gif|png)", RegexOptions.IgnoreCase);
				var matches = regx.Matches(Description);

				if (matches.Count == 0)
					firstImage = ScottHead;
				else
					firstImage = matches[0].Value;

				return firstImage;
			}
		}

		public ImageSource FirstImageSource
		{
			get
			{
				var image = FirstImage;
				return UriImageSource.FromUri(new Uri(image));
			}
		}

		public string ScottHead { get { return "http://www.hanselman.com/images/photo-scott-tall.jpg"; } }

		private decimal progress = 0.0M;
		public decimal Progress
		{
			get { return progress; }
			set { progress = value; OnPropertyChanged("Progress"); }
		}


		public event PropertyChangedEventHandler PropertyChanged;
		private void OnPropertyChanged(string name)
		{
			if (PropertyChanged == null)
				return;
			PropertyChanged(this, new PropertyChangedEventArgs(name));
		}
	}
}