From 6018f1c21a214807d15961855445e0ff1f27ee69 Mon Sep 17 00:00:00 2001 From: Daniel Roth Date: Tue, 4 May 2021 02:21:54 -0700 Subject: [PATCH] Unit conversions, favorites, countries --- BlazorWeather/AppState.cs | 17 ++++++ BlazorWeather/DisplayHelper.cs | 56 +++++++++++++++++++ BlazorWeather/Pages/Favorites/Favorites.razor | 51 ++++++++++++++--- BlazorWeather/Pages/Home/Home.razor | 54 +++++++++--------- BlazorWeather/Pages/Home/Next14Days.razor | 8 ++- BlazorWeather/Pages/Home/Next24Hours.razor | 4 +- .../Pages/Home/WeatherStationStat.razor | 10 ++-- BlazorWeather/Pages/Settings/Settings.razor | 40 +++++++++---- BlazorWeather/ServiceCollectionExtensions.cs | 12 ++-- .../Services/PinnedLocationsService.cs | 2 +- BlazorWeather/WeatherIcons.cs | 10 +++- BlazorWeather/_Imports.razor | 2 +- WeatherClient/IWeatherService.cs | 2 +- WeatherClient/WeatherModel.cs | 5 +- WeatherClient/WeatherService.cs | 32 +++++------ 15 files changed, 220 insertions(+), 85 deletions(-) create mode 100644 BlazorWeather/AppState.cs create mode 100644 BlazorWeather/DisplayHelper.cs diff --git a/BlazorWeather/AppState.cs b/BlazorWeather/AppState.cs new file mode 100644 index 0000000..b6971af --- /dev/null +++ b/BlazorWeather/AppState.cs @@ -0,0 +1,17 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using WeatherClient; + +namespace BlazorWeather +{ + public class AppState + { + public Location CurrentLocation { get; set; } + public WeatherResponse Weather { get; set; } + public string UnitOption { get; set; } = "imperial"; + public IList FavoriteLocations { get; set; } + } +} diff --git a/BlazorWeather/DisplayHelper.cs b/BlazorWeather/DisplayHelper.cs new file mode 100644 index 0000000..64fc0ce --- /dev/null +++ b/BlazorWeather/DisplayHelper.cs @@ -0,0 +1,56 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using WeatherClient; + +namespace BlazorWeather +{ + public class DisplayHelper + { + private readonly AppState appState; + + public DisplayHelper(AppState appState) + { + this.appState = appState; + } + + public Temperature Convert(Temperature temp) + { + if (appState.UnitOption == "imperial" && temp.Unit == "C") + { + return new Temperature { Value = Math.Round(temp.Value * 9 / 5 + 32), Unit = "F" }; + } + else if (appState.UnitOption != "imperial" && temp.Unit == "F") + { + return new Temperature { Value = Math.Round((temp.Value - 32) * 5 / 9), Unit = "C" }; + } + else + { + return temp; + } + } + + public string DisplayTemp(Temperature temp) + { + var convertedTemp = Convert(temp); + return $"{convertedTemp.Value}˚{convertedTemp.Unit}"; + } + + public string DisplayCurrentTemp() => DisplayTemp(appState.Weather.CurrentWeather.Temperature); + + public string TempUnit() => appState.UnitOption == "imperial" ? "F" : "C"; + public string SpeedUnit() => appState.UnitOption == "metric" ? "km/h" : "mph"; + + public double ConvertSpeed(double mph) => appState.UnitOption == "metric" ? Math.Round(mph * 1.609344) : mph; + + public string DisplayWindSpeed(WeatherSnapshot weather) => $"{ConvertSpeed(weather.WindSpeed)} {SpeedUnit()}"; + + public string DisplayCurrentWindSpeed() => DisplayWindSpeed(appState.Weather.CurrentWeather); + + public string DisplayPrecipationProbability(WeatherResponse weather) => $"{weather.DailyForecasts.First().Day.PrecipitationProbability}%"; + + public string DisplayCurrentPrecipationProbability() => DisplayPrecipationProbability(appState.Weather); + } +} diff --git a/BlazorWeather/Pages/Favorites/Favorites.razor b/BlazorWeather/Pages/Favorites/Favorites.razor index 4a2b5a8..3d692de 100644 --- a/BlazorWeather/Pages/Favorites/Favorites.razor +++ b/BlazorWeather/Pages/Favorites/Favorites.razor @@ -1,4 +1,8 @@ @page "/favorites" +@inject IWeatherService WeatherService +@inject AppState AppState +@inject DisplayHelper DH +@inject NavigationManager NavigationManager
@@ -9,23 +13,23 @@
- @for (var i = 0; i < 5; i++) + @foreach (var favorite in favoriteWeather) { -
- -
18˚
+
+ +
@DH.DisplayTemp(favorite.Weather.CurrentWeather.Temperature)
- St. Louis -
USA
+ @favorite.Location.Name +
@favorite.Location.Country
- 13% + @DH.DisplayPrecipationProbability(favorite.Weather)
- 9 mph + @DH.DisplayWindSpeed(favorite.Weather.CurrentWeather);
@@ -37,3 +41,34 @@
+ +@code { + IList favoriteWeather = new List(); + + protected override async Task OnInitializedAsync() + { + if (AppState.FavoriteLocations == null) + { + var locations = await WeatherService.GetLocations("o"); + AppState.FavoriteLocations = locations.Take(5).ToList(); + } + foreach (var location in AppState.FavoriteLocations) + { + var weather = await WeatherService.GetWeather(location.Coordinate); + favoriteWeather.Add(new WeatherAtLocation { Location = location, Weather = weather }); + } + } + + void SelectLocation(Location location) + { + Console.WriteLine($"Change location to {location.Name}"); + AppState.CurrentLocation = location; + NavigationManager.NavigateTo("/"); + } + + class WeatherAtLocation + { + public Location Location { get; set; } + public WeatherResponse Weather { get; set; } + } +} \ No newline at end of file diff --git a/BlazorWeather/Pages/Home/Home.razor b/BlazorWeather/Pages/Home/Home.razor index adf7494..f2d1d09 100644 --- a/BlazorWeather/Pages/Home/Home.razor +++ b/BlazorWeather/Pages/Home/Home.razor @@ -1,29 +1,29 @@ @page "/" @inject IWeatherService WeatherService +@inject AppState AppState +@inject DisplayHelper DH -@if (weather == null) { +@if (AppState.Weather == null) {
Loading...
} else { - var temp = weather.CurrentWeather.Temperature; -
-

@location.Name

-

@temp

- @weather.CurrentWeather.Phrase - +

@AppState.CurrentLocation.Name

+

@DH.DisplayCurrentTemp()

+ @AppState.Weather.CurrentWeather.Phrase +
-
@weather.CurrentWeather.PrecipitationProbability%
+
@DH.DisplayCurrentPrecipationProbability()
-
9mph
+
@DH.DisplayCurrentWindSpeed()
-
9mph
+
@AppState.Weather.CurrentWeather.RelativeHumidity%
@@ -38,22 +38,22 @@ else {
- - + +
} @code { - WeatherResponse weather; - Location location; - protected override async Task OnInitializedAsync() { - var locations = await WeatherService.GetLocations("Nairobi"); - location = locations.First(); - weather = await WeatherService.GetWeather(location.Coordinate); + if (AppState.CurrentLocation == null) + { + var locations = await WeatherService.GetLocations("Boston"); + AppState.CurrentLocation = locations.First(); + } + AppState.Weather = await WeatherService.GetWeather(AppState.CurrentLocation.Coordinate); } } \ No newline at end of file diff --git a/BlazorWeather/Pages/Home/Next14Days.razor b/BlazorWeather/Pages/Home/Next14Days.razor index 2047cd1..2c5a0be 100644 --- a/BlazorWeather/Pages/Home/Next14Days.razor +++ b/BlazorWeather/Pages/Home/Next14Days.razor @@ -1,4 +1,6 @@ -

Daily

+@inject DisplayHelper DH + +

Daily Forecasts

@foreach (var forecast in DailyForecasts) { @@ -8,11 +10,11 @@
@forecast.DateTime.DayOfWeek.ToString().First()
@forecast.DateTime.Day
-
@max
+
@DH.DisplayTemp(max)
-
@min
+
@DH.DisplayTemp(min)
@forecast.Day.PrecipitationProbability% diff --git a/BlazorWeather/Pages/Home/Next24Hours.razor b/BlazorWeather/Pages/Home/Next24Hours.razor index 3418d89..55f15c0 100644 --- a/BlazorWeather/Pages/Home/Next24Hours.razor +++ b/BlazorWeather/Pages/Home/Next24Hours.razor @@ -1,3 +1,5 @@ +@inject DisplayHelper DH +

Next 24 Hours

@foreach (var forecast in HourlyForecasts) @@ -5,7 +7,7 @@
@forecast.DateTime.ToString("h tt")
-
@forecast.Temperature
+
@DH.DisplayTemp(forecast.Temperature)
} diff --git a/BlazorWeather/Pages/Home/WeatherStationStat.razor b/BlazorWeather/Pages/Home/WeatherStationStat.razor index 2bbfbb1..324b848 100644 --- a/BlazorWeather/Pages/Home/WeatherStationStat.razor +++ b/BlazorWeather/Pages/Home/WeatherStationStat.razor @@ -1,22 +1,24 @@ -
+@inject DisplayHelper DH + +
@switch (stat) { case WeatherStat.WindSpeed:
- @WeatherStation.CurrentWeather.WindSpeed mph + @DH.ConvertSpeed(WeatherStation.CurrentWeather.WindSpeed) @DH.SpeedUnit()
break; case WeatherStat.Temperature:
- @WeatherStation.CurrentWeather.Temperature + @DH.Convert(WeatherStation.CurrentWeather.Temperature).Value ˚@DH.TempUnit()
break; case WeatherStat.RelativeHumidity:
- @WeatherStation.CurrentWeather.RelativeHumidity% + @WeatherStation.CurrentWeather.RelativeHumidity %
break; } diff --git a/BlazorWeather/Pages/Settings/Settings.razor b/BlazorWeather/Pages/Settings/Settings.razor index 7b55a91..2da1bec 100644 --- a/BlazorWeather/Pages/Settings/Settings.razor +++ b/BlazorWeather/Pages/Settings/Settings.razor @@ -1,19 +1,22 @@ @page "/settings" +@inject AppState AppState +@inject DisplayHelper DH +@inject NavigationManager NavigationManager -
+

Your Location Now

-

St. Louis, Missouri, USA

- -

18˚

- Cloudy +

@AppState.CurrentLocation.Name

+ +

@DH.DisplayCurrentTemp()

+ @AppState.Weather.CurrentWeather.Phrase
-
13%
+
@AppState.Weather.DailyForecasts.First().Day.PrecipitationProbability%
-
9mph
+
@DH.DisplayCurrentWindSpeed()
-
7%
+
@AppState.Weather.CurrentWeather.RelativeHumidity%
@@ -23,7 +26,7 @@ @foreach (var unitOption in unitOptions) {