PageRenderTime 50ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/Rider/ViewModels/FluxViewModel.cs

https://bitbucket.org/lduparc/berider
C# | 205 lines | 169 code | 28 blank | 8 comment | 13 complexity | 519a6f5a6e060ee7c0d222db3d03c351 MD5 | raw file
  1. using System;
  2. using System.Net;
  3. using System.Windows;
  4. using System.Windows.Controls;
  5. using System.Windows.Documents;
  6. using System.Windows.Ink;
  7. using System.Windows.Input;
  8. using System.Windows.Media;
  9. using System.Windows.Media.Animation;
  10. using System.Windows.Shapes;
  11. using System.Collections.Generic;
  12. using System.IO;
  13. using System.Xml;
  14. using Rider.Persistent;
  15. using Rider.Utils;
  16. using System.Collections.ObjectModel;
  17. using System.Xml.Linq;
  18. using System.Text.RegularExpressions;
  19. using GalaSoft.MvvmLight.Command;
  20. namespace Rider.ViewModels
  21. {
  22. public class FluxViewModel : BaseViewModel
  23. {
  24. public ObservableCollection<RssViewModel> WebsiteFeed { get; private set; }
  25. public ObservableCollection<RssViewModel> FacebookFeed { get; private set; }
  26. public ObservableCollection<TweetViewModel> TwitterFeed { get; private set; }
  27. private ICommand shareFeedCommand = null;
  28. private ICommand showFeedCommand = null;
  29. private ICommand shareTweetCommand = null;
  30. public FluxViewModel()
  31. {
  32. WebsiteFeed = new ObservableCollection<RssViewModel>();
  33. FacebookFeed = new ObservableCollection<RssViewModel>();
  34. TwitterFeed = new ObservableCollection<TweetViewModel>();
  35. this.shareFeedCommand = new RelayCommand<RssViewModel>(this.ShareFeedAction);
  36. this.showFeedCommand = new RelayCommand<RssViewModel>(this.ShowFeedAction);
  37. this.shareTweetCommand = new RelayCommand<TweetViewModel>(this.ShareTweetAction);
  38. }
  39. #region command
  40. public ICommand ShareFeedCommand
  41. {
  42. get
  43. {
  44. return this.shareFeedCommand;
  45. }
  46. }
  47. public ICommand ShowFeedCommand
  48. {
  49. get
  50. {
  51. return this.showFeedCommand;
  52. }
  53. }
  54. public ICommand ShareTweetCommand
  55. {
  56. get
  57. {
  58. return this.shareTweetCommand;
  59. }
  60. }
  61. private void ShareFeedAction(RssViewModel rss)
  62. {
  63. if (rss != null)
  64. {
  65. MessageBox.Show(string.Format("share rss: {0}", rss.Title));
  66. }
  67. }
  68. private void ShowFeedAction(RssViewModel rss)
  69. {
  70. if (rss != null)
  71. {
  72. MessageBox.Show(string.Format("rss: {0}", rss.Title));
  73. }
  74. }
  75. private void ShareTweetAction(TweetViewModel tweet)
  76. {
  77. if (tweet != null)
  78. {
  79. MessageBox.Show(string.Format("share tweet: {0}", tweet.Title));
  80. }
  81. }
  82. #endregion
  83. #region NewsUtils
  84. public void LoadNews()
  85. {
  86. List<RssViewModel> flux = UserData.Get<List<RssViewModel>>("news_rssfeed");
  87. // TEST
  88. if (flux == null) {
  89. flux = new List<RssViewModel>();
  90. flux.Add(new RssViewModel()
  91. {
  92. Title = "TEST TITLE",
  93. Description = "some content",
  94. Link = "http://cespage.com/silverlight/tutorials.xml"
  95. });
  96. }
  97. if (flux != null && flux.Count > 0)
  98. {
  99. WebClient clientFeed = new WebClient();
  100. clientFeed.DownloadStringCompleted += Weblient_DownloadStringCompleted;
  101. foreach (RssViewModel rss in flux)
  102. {
  103. // todo : http web request sur chaque flux
  104. clientFeed.DownloadStringAsync(new Uri(rss.Link), rss.Title);
  105. }
  106. // todo : sort syndication feed by publish Date
  107. }
  108. }
  109. private void Weblient_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
  110. {
  111. XElement _xml;
  112. try
  113. {
  114. if (!e.Cancelled)
  115. {
  116. _xml = XElement.Parse(e.Result);
  117. WebsiteFeed.Clear();
  118. //Results.Items.Clear();
  119. foreach (XElement value in _xml.Elements("channel").Elements("item"))
  120. {
  121. RssViewModel _item = new RssViewModel();
  122. _item.Title = value.Element("title").Value;
  123. _item.Description = Regex.Replace(value.Element("description").Value,
  124. @"<(.|\n)*?>", String.Empty);
  125. _item.Link = value.Element("link").Value;
  126. _item.Guid = value.Element("guid").Value;
  127. _item.Published = DateTime.Parse(value.Element("pubDate").Value);
  128. DebugUtils.Log("trololo", string.Format("item : name:{0}", _item.Title));
  129. WebsiteFeed.Add(_item);
  130. //Results.Items.Add(_item);
  131. }
  132. }
  133. }
  134. catch
  135. {
  136. // Ignore Errors
  137. }
  138. }
  139. #endregion
  140. #region Twitter
  141. public void LoadTweets()
  142. {
  143. WebClient clientTweet = new WebClient();
  144. clientTweet.DownloadStringCompleted += WeblientTwitter_DownloadStringCompleted;
  145. //clientTweet.DownloadStringAsync(new Uri("https://api.twitter.com/1/statuses/user_timeline.json?trim_user=true&screen_name=roller_station&count=20"));
  146. clientTweet.DownloadStringAsync(new Uri(("http://api.twitter.com/1/statuses/user_timeline.xml?screen_name=" + HttpUtility.UrlEncode("Air_Roller"))));
  147. }
  148. private void WeblientTwitter_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
  149. {
  150. XElement _xml;
  151. System.Globalization.CultureInfo provider = System.Globalization.CultureInfo.InvariantCulture;
  152. try
  153. {
  154. if (!e.Cancelled)
  155. {
  156. _xml = XElement.Parse(e.Result);
  157. foreach (XElement value in _xml.Elements("status"))
  158. {
  159. TweetViewModel _tweet = new TweetViewModel();
  160. _tweet.Avatar = value.Element("user").Element("profile_image_url").Value;
  161. _tweet.Title = value.Element("text").Value;
  162. _tweet.Published = DateTime.ParseExact(value.Element("created_at").Value, "ddd MMM dd HH:mm:ss zzzzz yyyy", provider);
  163. _tweet.Author = value.Element("user").Element("screen_name").Value;
  164. TwitterFeed.Add(_tweet);
  165. }
  166. }
  167. }
  168. catch
  169. {
  170. // todo catch exception
  171. }
  172. }
  173. #endregion
  174. #region facebook
  175. #endregion
  176. }
  177. }