Skip to content

Commit

Permalink
Show correct unit in weather sample
Browse files Browse the repository at this point in the history
  • Loading branch information
mallibone committed Sep 2, 2018
1 parent bfb9713 commit f714f60
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 8 deletions.
2 changes: 1 addition & 1 deletion Ooui/UI.cs
Expand Up @@ -193,7 +193,7 @@ public static void Present (string path, object presenter = null)
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;
}

Expand Down
30 changes: 28 additions & 2 deletions Samples/WeatherApp/Core.cs
Expand Up @@ -16,11 +16,13 @@ public static async Task<Weather> GetWeather(string zipCode, string units = "kel

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"]
};
Expand All @@ -37,5 +39,29 @@ public static async Task<Weather> GetWeather(string zipCode, string units = "kel
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";
}
}
}
}
8 changes: 6 additions & 2 deletions Samples/WeatherApp/Weather.cs
@@ -1,4 +1,6 @@
namespace WeatherApp
using System.Collections.Generic;

namespace WeatherApp
{
public class Weather
{
Expand All @@ -9,6 +11,7 @@ public class Weather
public string Visibility { get; set; }
public string Sunrise { get; set; }
public string Sunset { get; set; }
public List<string> UnitOfMeasures { get; set; }

public Weather()
{
Expand All @@ -21,6 +24,7 @@ public Weather()
this.Visibility = " ";
this.Sunrise = " ";
this.Sunset = " ";
this.UnitOfMeasures = new List<string>{"kelvin", "metric", "imperial"};
}
}
}
}
2 changes: 1 addition & 1 deletion Samples/WeatherApp/WeatherPage.xaml
Expand Up @@ -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" >
Expand Down
8 changes: 6 additions & 2 deletions Samples/WeatherApp/WeatherPage.xaml.cs
Expand Up @@ -12,14 +12,18 @@ public WeatherPage()
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;
Expand Down

0 comments on commit f714f60

Please sign in to comment.