| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143 |
- using MyWeather.Helpers;
- using MyWeather.Models;
- using MyWeather.Services;
- using Plugin.Geolocator;
- using Plugin.TextToSpeech;
- using System;
- using System.ComponentModel;
- using System.Runtime.CompilerServices;
- using System.Threading.Tasks;
- using System.Windows.Input;
- using Xamarin.Forms;
- namespace MyWeather.ViewModels
- {
- public class WeatherViewModel : INotifyPropertyChanged
- {
- WeatherService WeatherService { get; } = new WeatherService();
- string location = Settings.City;
- public string Location
- {
- get { return location; }
- set
- {
- location = value;
- OnPropertyChanged();
- Settings.City = value;
- }
- }
- bool useGPS;
- public bool UseGPS
- {
- get { return useGPS; }
- set
- {
- useGPS = value;
- OnPropertyChanged();
- }
- }
- bool isImperial = Settings.IsImperial;
- public bool IsImperial
- {
- get { return isImperial; }
- set
- {
- isImperial = value;
- OnPropertyChanged();
- Settings.IsImperial = value;
- }
- }
- string temp = string.Empty;
- public string Temp
- {
- get { return temp; }
- set { temp = value; OnPropertyChanged(); }
- }
- string condition = string.Empty;
- public string Condition
- {
- get { return condition; }
- set { condition = value; OnPropertyChanged(); }
- }
- bool isBusy = false;
- public bool IsBusy
- {
- get { return isBusy; }
- set { isBusy = value; OnPropertyChanged(); }
- }
- WeatherForecastRoot forecast;
- public WeatherForecastRoot Forecast
- {
- get { return forecast; }
- set { forecast = value; OnPropertyChanged(); }
- }
- ICommand getWeather;
- public ICommand GetWeatherCommand =>
- getWeather ??
- (getWeather = new Command(async () => await ExecuteGetWeatherCommand()));
- private async Task ExecuteGetWeatherCommand()
- {
- if (IsBusy)
- return;
- IsBusy = true;
- try
- {
- WeatherRoot weatherRoot = null;
- var units = IsImperial ? Units.Imperial : Units.Metric;
- if (UseGPS)
- {
- var gps = await CrossGeolocator.Current.GetPositionAsync(10000);
- weatherRoot = await WeatherService.GetWeather(gps.Latitude, gps.Longitude, units);
- }
- else
- {
- //Get weather by city
- weatherRoot = await WeatherService.GetWeather(Location.Trim(), units);
- }
-
- //Get forecast based on cityId
- Forecast = await WeatherService.GetForecast(weatherRoot.CityId, units);
- var unit = IsImperial ? "F" : "C";
- Temp = $"Temp: {weatherRoot?.MainWeather?.Temperature ?? 0}°{unit}";
- Condition = $"{weatherRoot.Name}: {weatherRoot?.Weather?[0]?.Description ?? string.Empty}";
- CrossTextToSpeech.Current.Speak(Temp + " " + Condition);
- }
- catch (Exception ex)
- {
- Temp = "Unable to get Weather";
- }
- finally
- {
- IsBusy = false;
- }
- }
- public event PropertyChangedEventHandler PropertyChanged;
- public void OnPropertyChanged([CallerMemberName]string name = "") =>
- PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
- }
- }
|