DriverDetailViewModel.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. namespace FormulaOneApp.Classic.Core.ViewModels
  2. {
  3. using System;
  4. using System.Threading.Tasks;
  5. using Cirrious.MvvmCross.ViewModels;
  6. using ErgastAPI.Model.Driver;
  7. using ErgastAPI.Services.Driver;
  8. using OxyPlot;
  9. using OxyPlot.Series;
  10. public class DriverDetailViewModel : MvxViewModel
  11. {
  12. // Variables
  13. private Driver _driver;
  14. private int _champs;
  15. private int _poles;
  16. private int _wins;
  17. private int _races;
  18. private PlotModel _plotModel;
  19. // Services
  20. private IDriverService _driverService;
  21. public DriverDetailViewModel(IDriverService driverService)
  22. {
  23. _driverService = driverService;
  24. }
  25. public Driver Driver
  26. {
  27. get { return _driver; }
  28. set
  29. {
  30. _driver = value;
  31. RaisePropertyChanged();
  32. }
  33. }
  34. public int Champs
  35. {
  36. get { return _champs; }
  37. set
  38. {
  39. _champs = value;
  40. RaisePropertyChanged();
  41. }
  42. }
  43. public int Poles
  44. {
  45. get { return _poles; }
  46. set
  47. {
  48. _poles = value;
  49. RaisePropertyChanged();
  50. }
  51. }
  52. public int Wins
  53. {
  54. get { return _wins; }
  55. set
  56. {
  57. _wins = value;
  58. RaisePropertyChanged();
  59. }
  60. }
  61. public int Races
  62. {
  63. get { return _races; }
  64. set
  65. {
  66. _races = value;
  67. RaisePropertyChanged();
  68. }
  69. }
  70. public PlotModel PlotModel
  71. {
  72. get { return _plotModel; }
  73. set
  74. {
  75. _plotModel = value;
  76. RaisePropertyChanged();
  77. }
  78. }
  79. private async Task LoadDriverInfoAsync(string driver)
  80. {
  81. var currentYear = DateTime.Today.Year;
  82. int second = 0;
  83. int third = 0;
  84. int four = 0;
  85. int five = 0;
  86. for (int i = currentYear; i > 2000; i--)
  87. {
  88. var driverResult = await _driverService.GetDriverResultsAsync(driver, i.ToString());
  89. foreach (var race in driverResult.Races)
  90. {
  91. Races = Races + 1;
  92. foreach (var result in race.Results)
  93. {
  94. if (result.Position.Equals("1", StringComparison.CurrentCultureIgnoreCase))
  95. {
  96. Wins = Wins + 1;
  97. }
  98. if (result.Position.Equals("2", StringComparison.CurrentCultureIgnoreCase))
  99. {
  100. second = second + 1;
  101. }
  102. if (result.Position.Equals("3", StringComparison.CurrentCultureIgnoreCase))
  103. {
  104. third = third + 1;
  105. }
  106. if (result.Position.Equals("4", StringComparison.CurrentCultureIgnoreCase))
  107. {
  108. four = four + 1;
  109. }
  110. if (result.Position.Equals("5", StringComparison.CurrentCultureIgnoreCase))
  111. {
  112. five = five + 1;
  113. }
  114. }
  115. }
  116. }
  117. PlotModel = new PlotModel
  118. {
  119. Title = "Positions"
  120. };
  121. var pieSlice = new PieSeries
  122. {
  123. StrokeThickness = 2.0,
  124. InsideLabelPosition = 0.8,
  125. AngleSpan = 360,
  126. StartAngle = 0
  127. };
  128. pieSlice.Slices.Add(new PieSlice("1º", Wins) { IsExploded = true });
  129. pieSlice.Slices.Add(new PieSlice("2º", second) { IsExploded = true });
  130. pieSlice.Slices.Add(new PieSlice("3º", third) { IsExploded = true });
  131. pieSlice.Slices.Add(new PieSlice("4º", four) { IsExploded = true });
  132. pieSlice.Slices.Add(new PieSlice("5º", five) { IsExploded = true });
  133. PlotModel.Series.Add(pieSlice);
  134. RaisePropertyChanged("PlotModel");
  135. }
  136. }
  137. }