WeatherViewModel.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. using MyWeather.Helpers;
  2. using MyWeather.Models;
  3. using MyWeather.Services;
  4. using Plugin.Geolocator;
  5. using Plugin.TextToSpeech;
  6. using System;
  7. using System.ComponentModel;
  8. using System.Runtime.CompilerServices;
  9. using System.Threading.Tasks;
  10. using System.Windows.Input;
  11. using Xamarin.Forms;
  12. namespace MyWeather.ViewModels
  13. {
  14. public class WeatherViewModel : INotifyPropertyChanged
  15. {
  16. WeatherService WeatherService { get; } = new WeatherService();
  17. string location = Settings.City;
  18. public string Location
  19. {
  20. get { return location; }
  21. set
  22. {
  23. location = value;
  24. OnPropertyChanged();
  25. Settings.City = value;
  26. }
  27. }
  28. bool useGPS;
  29. public bool UseGPS
  30. {
  31. get { return useGPS; }
  32. set
  33. {
  34. useGPS = value;
  35. OnPropertyChanged();
  36. }
  37. }
  38. bool isImperial = Settings.IsImperial;
  39. public bool IsImperial
  40. {
  41. get { return isImperial; }
  42. set
  43. {
  44. isImperial = value;
  45. OnPropertyChanged();
  46. Settings.IsImperial = value;
  47. }
  48. }
  49. string temp = string.Empty;
  50. public string Temp
  51. {
  52. get { return temp; }
  53. set { temp = value; OnPropertyChanged(); }
  54. }
  55. string condition = string.Empty;
  56. public string Condition
  57. {
  58. get { return condition; }
  59. set { condition = value; OnPropertyChanged(); }
  60. }
  61. bool isBusy = false;
  62. public bool IsBusy
  63. {
  64. get { return isBusy; }
  65. set { isBusy = value; OnPropertyChanged(); }
  66. }
  67. WeatherForecastRoot forecast;
  68. public WeatherForecastRoot Forecast
  69. {
  70. get { return forecast; }
  71. set { forecast = value; OnPropertyChanged(); }
  72. }
  73. ICommand getWeather;
  74. public ICommand GetWeatherCommand =>
  75. getWeather ??
  76. (getWeather = new Command(async () => await ExecuteGetWeatherCommand()));
  77. private async Task ExecuteGetWeatherCommand()
  78. {
  79. if (IsBusy)
  80. return;
  81. IsBusy = true;
  82. try
  83. {
  84. WeatherRoot weatherRoot = null;
  85. var units = IsImperial ? Units.Imperial : Units.Metric;
  86. if (UseGPS)
  87. {
  88. var gps = await CrossGeolocator.Current.GetPositionAsync(10000);
  89. weatherRoot = await WeatherService.GetWeather(gps.Latitude, gps.Longitude, units);
  90. }
  91. else
  92. {
  93. //Get weather by city
  94. weatherRoot = await WeatherService.GetWeather(Location.Trim(), units);
  95. }
  96. //Get forecast based on cityId
  97. Forecast = await WeatherService.GetForecast(weatherRoot, units);
  98. var unit = IsImperial ? "F" : "C";
  99. Temp = $"Temp: {weatherRoot?.MainWeather?.Temperature ?? 0}°{unit}";
  100. Condition = $"{weatherRoot.Name}: {weatherRoot?.Weather?[0]?.Description ?? string.Empty}";
  101. CrossTextToSpeech.Current.Speak(Temp + " " + Condition);
  102. }
  103. catch (Exception ex)
  104. {
  105. Temp = "Unable to get Weather";
  106. }
  107. finally
  108. {
  109. IsBusy = false;
  110. }
  111. }
  112. public event PropertyChangedEventHandler PropertyChanged;
  113. public void OnPropertyChanged([CallerMemberName]string name = "") =>
  114. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
  115. }
  116. }