From 2dfc29591d854e57f8195bb7c74c3aa75e271725 Mon Sep 17 00:00:00 2001 From: Kenneth van Ewijk Date: Wed, 2 Dec 2015 22:27:30 +0100 Subject: [PATCH] Added progress rings and error feedback --- HueUWP/APIHandler.cs | 23 +++++++++++++++-------- HueUWP/MainPage.xaml | 16 +++++++++++----- HueUWP/MainPage.xaml.cs | 14 +++++++++++++- HueUWP/NetworkHandler.cs | 16 ++++++++-------- HueUWP/SettingsView.xaml | 4 ++-- HueUWP/SettingsView.xaml.cs | 11 +++++++++-- 6 files changed, 58 insertions(+), 26 deletions(-) diff --git a/HueUWP/APIHandler.cs b/HueUWP/APIHandler.cs index 7948fe1..e2e6413 100755 --- a/HueUWP/APIHandler.cs +++ b/HueUWP/APIHandler.cs @@ -20,17 +20,21 @@ namespace HueUWP this.nwh = nwh; } - public async Task Register() + public async Task Register() { - try { - var json = await nwh.RegisterName("Hue", "Kenneth&Yorick"); + try + { + var json = await nwh.RegisterName("YK Hue", "YK"); json = json.Replace("[", "").Replace("]", ""); JObject o = JObject.Parse(json); string id = o["success"]["username"].ToString(); - MainPage.LOCAL_SETTINGS.Values["id"] = id; } - catch(Exception e) + MainPage.LOCAL_SETTINGS.Values["id"] = id; + return "success"; + } + catch (Exception e) { Debug.WriteLine("Could not register."); + return "error"; } } @@ -45,15 +49,17 @@ namespace HueUWP lights.ForEach(l => SetLightState(l)); } - public async void SetLightValues(Light l) + public async Task SetLightValues(Light l) { if(l.IsOn) { Debug.WriteLine(l.Hue); var json = await nwh.SetLightInfo(l.ID, $"{{\"bri\": {l.Brightness},\"hue\": {(l.Hue)},\"sat\": {l.Saturation}}}"); Debug.WriteLine(json); + return "success"; } + return "error"; } public async void SetLightValues(List lights) @@ -85,14 +91,15 @@ namespace HueUWP // ); //lightlist.ForEach(q => alllights.Add(q)); + + return "success"; } catch(Exception e) { Debug.WriteLine(e.StackTrace); Debug.WriteLine("Could not get all lights."); + return "error"; } - - return string.Empty; } diff --git a/HueUWP/MainPage.xaml b/HueUWP/MainPage.xaml index 0e2ae27..705dbd2 100644 --- a/HueUWP/MainPage.xaml +++ b/HueUWP/MainPage.xaml @@ -16,6 +16,7 @@ + @@ -24,10 +25,15 @@ Hue Lights - + + + + + - - + + + @@ -48,8 +54,8 @@ - - + + diff --git a/HueUWP/MainPage.xaml.cs b/HueUWP/MainPage.xaml.cs index f17d15e..bfa0287 100644 --- a/HueUWP/MainPage.xaml.cs +++ b/HueUWP/MainPage.xaml.cs @@ -58,9 +58,21 @@ namespace HueUWP private async void Load_Lights() { + ErrorMessage.Text = ""; + FeedbackPanel.Visibility = Visibility.Visible; _lightsViewModel.Clear(); + Loading.IsActive = true; - await api.GetAllLights(_lightsViewModel); + await Task.Delay(TimeSpan.FromMilliseconds(200)); + string s = await api.GetAllLights(_lightsViewModel); + + if (s == "error") + ErrorMessage.Text = "There was a problem connecting..."; + else if (_lightsViewModel.Count < 1) + ErrorMessage.Text = "No lights found..."; + else + FeedbackPanel.Visibility = Visibility.Collapsed; + Loading.IsActive = false; } diff --git a/HueUWP/NetworkHandler.cs b/HueUWP/NetworkHandler.cs index c4510b1..e6dede6 100755 --- a/HueUWP/NetworkHandler.cs +++ b/HueUWP/NetworkHandler.cs @@ -36,7 +36,7 @@ namespace HueUWP if (!response.IsSuccessStatusCode) { - return string.Empty; + return "error"; } string jsonResponse = await response.Content.ReadAsStringAsync(); @@ -48,7 +48,7 @@ namespace HueUWP catch (Exception ex) { System.Diagnostics.Debug.WriteLine(ex.Message); - return string.Empty; + return "error"; } } @@ -68,7 +68,7 @@ namespace HueUWP if (!response.IsSuccessStatusCode) { - return string.Empty; + return "error"; } string jsonResponse = await response.Content.ReadAsStringAsync(); @@ -80,7 +80,7 @@ namespace HueUWP catch (Exception ex) { System.Diagnostics.Debug.WriteLine(ex.Message); - return string.Empty; + return "error"; } } @@ -99,7 +99,7 @@ namespace HueUWP if (!response.IsSuccessStatusCode) { - return string.Empty; + return "error"; } string jsonResponse = await response.Content.ReadAsStringAsync(); @@ -111,7 +111,7 @@ namespace HueUWP catch (Exception ex) { System.Diagnostics.Debug.WriteLine(ex.Message); - return string.Empty; + return "error"; } } @@ -126,7 +126,7 @@ namespace HueUWP { var response = await Post("",$"{{\"devicetype\":\"{AppName}#{UserName}\"}}"); if (string.IsNullOrEmpty(response)) - await new MessageDialog("Error while setting username. ….").ShowAsync(); + return "error"; return response; } @@ -134,7 +134,7 @@ namespace HueUWP { var response = await Get($"{(String)MainPage.LOCAL_SETTINGS.Values["id"]}/lights"); if (string.IsNullOrEmpty(response)) - await new MessageDialog("Error while getting all lights. ….").ShowAsync(); + return "error"; return response; } diff --git a/HueUWP/SettingsView.xaml b/HueUWP/SettingsView.xaml index c38a231..d690314 100644 --- a/HueUWP/SettingsView.xaml +++ b/HueUWP/SettingsView.xaml @@ -29,9 +29,9 @@ User ID - + -