Show correct unit in weather sample

This commit is contained in:
mallibone 2018-09-02 16:29:53 +02:00
parent bfb9713484
commit f714f60d75
5 changed files with 42 additions and 8 deletions

View File

@ -193,7 +193,7 @@ namespace Ooui
public static string GetUrl (string path)
{
var localhost = host == "*" ? "localhost" : host;
var url = Path.Combine($"http://{localhost}:{port}{path}");
var url = $"http://{localhost}:{port}{path}";
return url;
}

View File

@ -16,11 +16,13 @@ namespace WeatherApp
if (results["weather"] != null)
{
string tempUnit = GetTempUnit(units);
string speedUnit = GetSpeedUnit(units);
Weather weather = new Weather
{
Title = (string)results["name"],
Temperature = (string)results["main"]["temp"] + " F",
Wind = (string)results["wind"]["speed"] + " mph",
Temperature = (string)results["main"]["temp"] + tempUnit,
Wind = (string)results["wind"]["speed"] + speedUnit,
Humidity = (string)results["main"]["humidity"] + " %",
Visibility = (string)results["weather"][0]["main"]
};
@ -37,5 +39,29 @@ namespace WeatherApp
return null;
}
}
private static string GetSpeedUnit(string units)
{
switch (units)
{
case "imperial":
return " mph";
default:
return " kph";
}
}
private static string GetTempUnit(string units)
{
switch (units)
{
case "metric":
return " °C";
case "imperial":
return " °F";
default:
return " °K";
}
}
}
}

View File

@ -1,4 +1,6 @@
namespace WeatherApp
using System.Collections.Generic;
namespace WeatherApp
{
public class Weather
{
@ -9,6 +11,7 @@
public string Visibility { get; set; }
public string Sunrise { get; set; }
public string Sunset { get; set; }
public List<string> UnitOfMeasures { get; set; }
public Weather()
{
@ -21,6 +24,7 @@
this.Visibility = " ";
this.Sunrise = " ";
this.Sunset = " ";
this.UnitOfMeasures = new List<string>{"kelvin", "metric", "imperial"};
}
}
}
}

View File

@ -35,7 +35,7 @@
<Label x:Name="zipCodeLabel" Text="Zip Code" Style="{StaticResource labelStyle}" />
<Entry x:Name="zipCodeEntry" />
<Label x:Name="unitOfMeasureLabel" Text="Unit Of Measure" Style="{StaticResource labelStyle}" />
<Entry x:Name="unitOfMeasure" Placeholder="kelvin (default), metric, imperial"/>
<Picker x:Name="unitOfMeasure" />
</StackLayout>
<StackLayout Padding="0,0,0,10" VerticalOptions="End">
<Button x:Name="getWeatherBtn" Text="Get Weather" WidthRequest="185" BorderWidth="1" >

View File

@ -12,14 +12,18 @@ namespace WeatherApp
getWeatherBtn.Clicked += GetWeatherBtn_Clicked;
//Set the default binding to a default object for now
this.BindingContext = new Weather();
this.BindingContext = Weather;
unitOfMeasure.ItemsSource = Weather.UnitOfMeasures;
}
public Weather Weather { get; } = new Weather();
private async void GetWeatherBtn_Clicked(object sender, EventArgs e)
{
if (!String.IsNullOrEmpty(zipCodeEntry.Text))
{
Weather weather = await Core.GetWeather(zipCodeEntry.Text, unitOfMeasure.Text);
Weather weather = await Core.GetWeather(zipCodeEntry.Text, (string)unitOfMeasure.SelectedItem);
if (weather != null)
{
this.BindingContext = weather;