MainViewModel.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. namespace FormulaOneApp.Classic.Core.ViewModels
  2. {
  3. using System;
  4. using System.Collections.ObjectModel;
  5. using System.Diagnostics;
  6. using System.IO;
  7. using System.Net;
  8. using System.Threading.Tasks;
  9. using System.Windows.Input;
  10. using Cirrious.MvvmCross.ViewModels;
  11. using ErgastAPI.Model.Race;
  12. using ErgastAPI.Services.Race;
  13. using Rss;
  14. using Rss.Models;
  15. public class MainViewModel : MvxViewModel
  16. {
  17. //Consts
  18. private const string Formula1Url = "http://www.theguardian.com/sport/formulaone/rss";
  19. //Services
  20. private readonly IRaceService _raceService;
  21. //Variables
  22. private Race _nextRace;
  23. private ObservableCollection<RssItem> _news;
  24. private TimeSpan _countdown;
  25. // Commands
  26. private MvxCommand _standingsCommand;
  27. private MvxCommand _driversCommand;
  28. private MvxCommand _circuitsCommand;
  29. public MainViewModel(IRaceService raceService)
  30. {
  31. _raceService = raceService;
  32. LoadNextRacingEvent();
  33. LoadNews();
  34. }
  35. public Race NextRace
  36. {
  37. get { return _nextRace; }
  38. set
  39. {
  40. _nextRace = value;
  41. RaisePropertyChanged();
  42. }
  43. }
  44. public TimeSpan Countdown
  45. {
  46. get { return _countdown; }
  47. set
  48. {
  49. _countdown = value;
  50. RaisePropertyChanged();
  51. }
  52. }
  53. public ObservableCollection<RssItem> News
  54. {
  55. get { return _news; }
  56. set
  57. {
  58. _news = value;
  59. RaisePropertyChanged();
  60. }
  61. }
  62. public ICommand StandingsCommand
  63. {
  64. get { return _standingsCommand = _standingsCommand ?? new MvxCommand(StandingsCommandExecute); }
  65. }
  66. public ICommand DriversCommand
  67. {
  68. get { return _driversCommand = _driversCommand ?? new MvxCommand(DriversCommandExecute); }
  69. }
  70. public ICommand CircuitsCommand
  71. {
  72. get { return _circuitsCommand = _circuitsCommand ?? new MvxCommand(CircuitsCommandExecute); }
  73. }
  74. private async Task LoadNextRacingEvent()
  75. {
  76. var today = DateTime.Today;
  77. var seasonSchedule = await _raceService.GetSeasonScheduleCollectionAsync();
  78. foreach (var race in seasonSchedule.Races)
  79. {
  80. if (Convert.ToDateTime(race.Date) > today)
  81. {
  82. NextRace = race;
  83. break;
  84. }
  85. }
  86. }
  87. /// <summary>
  88. ///
  89. /// </summary>
  90. public void LoadNews()
  91. {
  92. try
  93. {
  94. HttpWebRequest request = WebRequest.CreateHttp(Formula1Url);
  95. request.BeginGetResponse(token =>
  96. {
  97. using (var response = request.EndGetResponse(token) as HttpWebResponse)
  98. if (response != null)
  99. {
  100. using (Stream stream = response.GetResponseStream())
  101. {
  102. using (var sReader = new StreamReader(stream))
  103. {
  104. string feedContent = sReader.ReadToEnd();
  105. var rssResult = FeedReader.LoadFeed(feedContent);
  106. News = new ObservableCollection<RssItem>();
  107. foreach (var item in rssResult.Items)
  108. {
  109. News.Add(item);
  110. }
  111. }
  112. }
  113. }
  114. }, null);
  115. }
  116. catch (Exception ex)
  117. {
  118. Debug.WriteLine(ex.Message);
  119. }
  120. }
  121. private void StandingsCommandExecute()
  122. {
  123. ShowViewModel<StandingsViewModel>();
  124. }
  125. private void DriversCommandExecute()
  126. {
  127. ShowViewModel<DriverListViewModel>();
  128. }
  129. private void CircuitsCommandExecute()
  130. {
  131. ShowViewModel<CircuitListViewModel>();
  132. }
  133. }
  134. }