Example #1
0
        private async Task AddFavorite()
        {
            var favoriteText = await _alertService.DisplayInputEntryAsync("MCWeather", "Enter city name", validator : (text) =>
            {
                return(WeatherRequests?.Where((WeatherRequest i) => (i as WeatherRequest)?.City?.Name == text).Count() == 0);
            });

            if (!string.IsNullOrWhiteSpace(favoriteText))
            {
                Weather weather = null;

                try
                {
                    weather = await _weatherService.GetWeatherAsync(favoriteText);
                }
                catch (Exception ex)
                {
                    Debug.WriteLine(ex.Message);
                }
                finally
                {
                    if (weather == null)
                    {
                        await _alertService.DisplayAsync("MCWeather", $"Cannot find weather data for {favoriteText}", "OK");
                    }
                    else
                    {
                        var favoriteCity = await _favoritesService.AddFavoriteCityAsync(new City { Name = favoriteText });

                        WeatherRequests.Add(new WeatherRequest {
                            City = new City {
                                Name = favoriteText
                            }
                        });
                        ReloadAction?.Invoke();
                    }
                }
            }
        }