PageRenderTime 52ms CodeModel.GetById 26ms RepoModel.GetById 1ms app.codeStats 0ms

/Samples/Controls/Marquee/WPF/Marquee-WPF/NewsViewModel.cs

https://bitbucket.org/bu5hm4nn/sharpfellows.toolkit
C# | 122 lines | 91 code | 19 blank | 12 comment | 2 complexity | aab81164e88d0b01b8cf0115e79ce251 MD5 | raw file
  1. using System;
  2. using System.Collections.ObjectModel;
  3. using System.ComponentModel;
  4. using System.Concurrency;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Net;
  8. using System.Text;
  9. using System.Windows.Input;
  10. using System.Windows.Threading;
  11. using System.Xml.Linq;
  12. namespace Marquee_WPF
  13. {
  14. public class NewsViewModel : INotifyPropertyChanged
  15. {
  16. private IDisposable _observation;
  17. private Headline _expandedHeadline;
  18. public NewsViewModel()
  19. {
  20. Headlines = new ObservableCollection<Headline>();
  21. var webRequest = WebRequest.Create("http://feeds.bbci.co.uk/news/rss.xml");
  22. _observation = Observable.FromAsyncPattern<WebResponse>(webRequest.BeginGetResponse, webRequest.EndGetResponse)() // Execute the web request to get XML
  23. .Select(response => response.GetResponseStream())
  24. .Select(stream =>
  25. {
  26. using (var reader = new StreamReader(stream, Encoding.UTF8))
  27. return reader.ReadToEnd();
  28. })
  29. .Select(xml => XDocument.Parse(xml)) // Parse the XML
  30. .SelectMany(document => document.Descendants("channel").Descendants("item").ToObservable()) // Extract the item elements
  31. .Select(xelem => new Headline(this) // Convert from XML into our model class
  32. {
  33. Title = xelem.Descendants("title").First().Value,
  34. Summary = xelem.Descendants("description").First().Value,
  35. })
  36. .ObserveOn(new DispatcherScheduler(Dispatcher.CurrentDispatcher))
  37. .Subscribe(headline => Headlines.Add(headline)); // And add the object into the bound collection
  38. }
  39. /// <summary>
  40. /// The headline which currently has its summary displayed
  41. /// </summary>
  42. public Headline ExpandedHeadline
  43. {
  44. get
  45. {
  46. return _expandedHeadline;
  47. }
  48. set
  49. {
  50. _expandedHeadline = value;
  51. OnPropertyChanged("ExpandedHeadline");
  52. }
  53. }
  54. /// <summary>
  55. /// List of all headlines
  56. /// </summary>
  57. public ObservableCollection<Headline> Headlines { get; private set; }
  58. public event PropertyChangedEventHandler PropertyChanged;
  59. public void OnPropertyChanged(string propertyName)
  60. {
  61. PropertyChangedEventHandler handler = PropertyChanged;
  62. if (handler != null)
  63. handler(this, new PropertyChangedEventArgs(propertyName));
  64. }
  65. }
  66. /// <summary>
  67. /// An implementation of ICommand. It's "temporary" in the sense that it's not a very good implementation of ICommand, it's just about enough for the purpose of this example.
  68. /// </summary>
  69. public class TemporaryDelegateCommand : ICommand
  70. {
  71. private Action _action;
  72. public TemporaryDelegateCommand(Action action)
  73. {
  74. _action = action;
  75. }
  76. public void Execute(object parameter)
  77. {
  78. _action();
  79. }
  80. public bool CanExecute(object parameter)
  81. {
  82. return true;
  83. }
  84. public event EventHandler CanExecuteChanged
  85. {
  86. add { }
  87. remove { }
  88. }
  89. }
  90. /// <summary>
  91. /// Holds details of a news headline
  92. /// </summary>
  93. public class Headline
  94. {
  95. public Headline(NewsViewModel owner)
  96. {
  97. SelectCommand = new TemporaryDelegateCommand(() => owner.ExpandedHeadline = this);
  98. }
  99. public string Title { get; set; }
  100. public string Summary { get; set; }
  101. public ICommand SelectCommand { get; set; }
  102. }
  103. }