From de10d8a179f9bd2b315743f139acda84246a9bc9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javier=20Su=C3=A1rez=20Ruiz?= Date: Tue, 12 Dec 2017 21:43:38 +0100 Subject: [PATCH] Added WeatherApp Sample --- Samples/Program.cs | 1 + Samples/Samples.csproj | 4 ++ Samples/WeatherApp/Core.cs | 41 ++++++++++++ Samples/WeatherApp/DataService.cs | 25 +++++++ Samples/WeatherApp/Weather.cs | 26 ++++++++ Samples/WeatherApp/WeatherPage.xaml | 92 ++++++++++++++++++++++++++ Samples/WeatherApp/WeatherPage.xaml.cs | 31 +++++++++ Samples/WeatherAppSample.cs | 21 ++++++ 8 files changed, 241 insertions(+) create mode 100644 Samples/WeatherApp/Core.cs create mode 100644 Samples/WeatherApp/DataService.cs create mode 100644 Samples/WeatherApp/Weather.cs create mode 100644 Samples/WeatherApp/WeatherPage.xaml create mode 100644 Samples/WeatherApp/WeatherPage.xaml.cs create mode 100644 Samples/WeatherAppSample.cs diff --git a/Samples/Program.cs b/Samples/Program.cs index 15c5c98..6e1152a 100644 --- a/Samples/Program.cs +++ b/Samples/Program.cs @@ -33,6 +33,7 @@ namespace Samples new DotMatrixClockSample().Publish(); new EditorSample().Publish(); new TipCalcSample().Publish(); + new WeatherAppSample().Publish(); new XuzzleSample().Publish(); UI.Present ("/display-alert"); diff --git a/Samples/Samples.csproj b/Samples/Samples.csproj index d488c4c..bdbc539 100644 --- a/Samples/Samples.csproj +++ b/Samples/Samples.csproj @@ -44,6 +44,9 @@ MSBuild:Compile + + MSBuild:Compile + MSBuild:UpdateDesignTimeXaml @@ -54,6 +57,7 @@ + Exe diff --git a/Samples/WeatherApp/Core.cs b/Samples/WeatherApp/Core.cs new file mode 100644 index 0000000..31ee5e9 --- /dev/null +++ b/Samples/WeatherApp/Core.cs @@ -0,0 +1,41 @@ +using System; +using System.Threading.Tasks; + +namespace WeatherApp +{ + public class Core + { + public static async Task GetWeather(string zipCode) + { + //Sign up for a free API key at http://openweathermap.org/appid + string key = "fc9f6c524fc093759cd28d41fda89a1b"; + string queryString = "http://api.openweathermap.org/data/2.5/weather?zip=" + + zipCode + "&appid=" + key; + + var results = await DataService.getDataFromService(queryString).ConfigureAwait(false); + + if (results["weather"] != null) + { + Weather weather = new Weather + { + Title = (string)results["name"], + Temperature = (string)results["main"]["temp"] + " F", + Wind = (string)results["wind"]["speed"] + " mph", + Humidity = (string)results["main"]["humidity"] + " %", + Visibility = (string)results["weather"][0]["main"] + }; + + DateTime time = new DateTime(1970, 1, 1, 0, 0, 0, 0); + DateTime sunrise = time.AddSeconds((double)results["sys"]["sunrise"]); + DateTime sunset = time.AddSeconds((double)results["sys"]["sunset"]); + weather.Sunrise = sunrise.ToString() + " UTC"; + weather.Sunset = sunset.ToString() + " UTC"; + return weather; + } + else + { + return null; + } + } + } +} diff --git a/Samples/WeatherApp/DataService.cs b/Samples/WeatherApp/DataService.cs new file mode 100644 index 0000000..ca1516e --- /dev/null +++ b/Samples/WeatherApp/DataService.cs @@ -0,0 +1,25 @@ +using System.Threading.Tasks; +using Newtonsoft.Json; +using System.Net.Http; +using Newtonsoft.Json.Linq; + +namespace WeatherApp +{ + public class DataService + { + public static async Task getDataFromService(string queryString) + { + HttpClient client = new HttpClient(); + var response = await client.GetAsync(queryString); + + JContainer data = null; + if (response != null) + { + string json = response.Content.ReadAsStringAsync().Result; + data = (JContainer)JsonConvert.DeserializeObject(json); + } + + return data; + } + } +} \ No newline at end of file diff --git a/Samples/WeatherApp/Weather.cs b/Samples/WeatherApp/Weather.cs new file mode 100644 index 0000000..e6e30f4 --- /dev/null +++ b/Samples/WeatherApp/Weather.cs @@ -0,0 +1,26 @@ +namespace WeatherApp +{ + public class Weather + { + public string Title { get; set; } + public string Temperature { get; set; } + public string Wind { get; set; } + public string Humidity { get; set; } + public string Visibility { get; set; } + public string Sunrise { get; set; } + public string Sunset { get; set; } + + public Weather() + { + //Because labels bind to these values, set them to an empty string to + //ensure that the label appears on all platforms by default. + this.Title = " "; + this.Temperature = " "; + this.Wind = " "; + this.Humidity = " "; + this.Visibility = " "; + this.Sunrise = " "; + this.Sunset = " "; + } + } +} \ No newline at end of file diff --git a/Samples/WeatherApp/WeatherPage.xaml b/Samples/WeatherApp/WeatherPage.xaml new file mode 100644 index 0000000..29bc1e0 --- /dev/null +++ b/Samples/WeatherApp/WeatherPage.xaml @@ -0,0 +1,92 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Samples/WeatherApp/WeatherPage.xaml.cs b/Samples/WeatherApp/WeatherPage.xaml.cs new file mode 100644 index 0000000..b1f3c07 --- /dev/null +++ b/Samples/WeatherApp/WeatherPage.xaml.cs @@ -0,0 +1,31 @@ +using System; +using Xamarin.Forms; + +namespace WeatherApp +{ + public partial class WeatherPage : ContentPage + { + public WeatherPage() + { + InitializeComponent(); + this.Title = "Sample Weather App"; + getWeatherBtn.Clicked += GetWeatherBtn_Clicked; + + //Set the default binding to a default object for now + this.BindingContext = new Weather(); + } + + private async void GetWeatherBtn_Clicked(object sender, EventArgs e) + { + if (!String.IsNullOrEmpty(zipCodeEntry.Text)) + { + Weather weather = await Core.GetWeather(zipCodeEntry.Text); + if (weather != null) + { + this.BindingContext = weather; + getWeatherBtn.Text = "Search Again"; + } + } + } + } +} \ No newline at end of file diff --git a/Samples/WeatherAppSample.cs b/Samples/WeatherAppSample.cs new file mode 100644 index 0000000..3119138 --- /dev/null +++ b/Samples/WeatherAppSample.cs @@ -0,0 +1,21 @@ +using Ooui; +using Xamarin.Forms; + +namespace Samples +{ + public class WeatherAppSample : ISample + { + public string Title => "Xamarin.Forms WeatherApp"; + + public Ooui.Element CreateElement() + { + var page = new WeatherApp.WeatherPage(); + return page.GetOouiElement(); + } + + public void Publish() + { + UI.Publish("/weatherapp", CreateElement); + } + } +}