public async Task DeleteThemeAsync(HueTheme theme) { try { StorageFolder themeFolder = await ApplicationData.Current.LocalFolder.CreateFolderAsync("Themes", CreationCollisionOption.OpenIfExists); StorageFile themeFile = await themeFolder.GetFileAsync(theme.FileName); await themeFile.DeleteAsync(); if (Themes.Contains(theme)) { Themes.Remove(theme); // Notify theme list changes if (ThemeListChanged != null) { ThemeListChanged(this, null); } } } catch (Exception ex) { Debug.WriteLine(ex); } }
public async Task LoadThemesFromCacheAsync() { Themes.Clear(); StorageFolder themeFolder = await ApplicationData.Current.LocalFolder.CreateFolderAsync("Themes", CreationCollisionOption.OpenIfExists); IReadOnlyList <StorageFile> themeFiles = await themeFolder.GetFilesAsync(); if (themeFiles.Count == 0) { await CreateDefaultThemesAsync(themeFolder); } else { foreach (var file in themeFiles) { Debug.WriteLine(file.Name); HueTheme theme = await LoadThemeFromFile(file); if (theme != null) { Themes.Add(theme); } } } return; }
public void InvalidateTheme(HueTheme theme) { if (ThemeChanged != null) { ThemeChanged(theme, null); } }
public async Task <HueTheme> CreateThemeAsync() { var newTheme = new HueTheme(); var newIndex = Themes.Count + 1; newTheme.Name = "theme " + newIndex.ToString(); newTheme.FileName = "theme_" + newIndex.ToString(); newTheme.IsSystemTheme = false; // Add a default color newTheme.ColorList.Add(HSBColor.FromColor(Color.FromArgb(0xff, 0xec, 0xcd, 0x67))); newTheme.ColorList.Add(HSBColor.FromColor(Color.FromArgb(0xff, 0xec, 0xcd, 0x67))); newTheme.ColorList.Add(HSBColor.FromColor(Color.FromArgb(0xff, 0xec, 0xcd, 0x67))); // Add to themes Themes.Add(newTheme); await SaveThemeToFileAsync(newTheme); // Notify theme list changes if (ThemeListChanged != null) { ThemeListChanged(this, null); } return(newTheme); }
public async Task <bool> ApplyThemeAsync(HueTheme theme) { if (theme.ColorList.Count == 0) { return(true); } int colorIndex = 0; foreach (var light in BridgeManager.Instance.CurrentBridge.LightList) { HSBColor color = theme.ColorList[colorIndex]; var attrs = new { hue = (int)color.H, sat = (int)color.S, bri = (int)color.B }; await HueAPI.Instance.SetLightStateAsync(light.LightId, attrs); // Update light attrs light.Hue = (int)color.H; light.Saturation = (int)color.S; light.Brightness = (int)color.B; BridgeManager.Instance.InvalidateLightProperties(light); colorIndex++; if (colorIndex >= theme.ColorList.Count - 1) { colorIndex = 0; } } return(true); }
public async Task SaveThemeToFileAsync(HueTheme theme) { try { StorageFolder themeFolder = await ApplicationData.Current.LocalFolder.CreateFolderAsync("Themes", CreationCollisionOption.OpenIfExists); var name = (theme.FileName != null) ? theme.FileName : theme.Name; CreationCollisionOption options; if (theme.IsSystemTheme) { options = CreationCollisionOption.OpenIfExists; } else { options = CreationCollisionOption.GenerateUniqueName; } var file = await themeFolder.CreateFileAsync(name, options); // Update theme filename theme.FileName = file.Name; string content = theme.ToJsonString(); await FileIO.WriteTextAsync(file, content); } catch (Exception ex) { Debug.WriteLine(ex); } }
public async Task RevertThemeAsync(HueTheme theme) { if (!theme.IsSystemTheme) { return; } theme.ColorList.Clear(); foreach (var color in theme.DefaultColorList) { theme.ColorList.Add(color.Clone()); } await UpdateThemeAsync(theme); InvalidateTheme(theme); }
public async Task UpdateThemeAsync(HueTheme theme) { try { StorageFolder themeFolder = await ApplicationData.Current.LocalFolder.CreateFolderAsync("Themes", CreationCollisionOption.OpenIfExists); var name = (theme.FileName != null) ? theme.FileName : theme.Name; var file = await themeFolder.CreateFileAsync(name, CreationCollisionOption.ReplaceExisting); string content = theme.ToJsonString(); await FileIO.WriteTextAsync(file, content); } catch (Exception ex) { Debug.WriteLine(ex); } }
private async Task <HueTheme> LoadThemeFromFile(StorageFile file) { try { string content = await FileIO.ReadTextAsync(file); HueTheme theme = ThemeFactory.CreateThemeFromJson(content); theme.FileName = file.Name; return(theme); } catch (Exception ex) { Debug.WriteLine(ex); return(null); } }
protected override void OnNavigatedTo(NavigationEventArgs e) { this.navigationHelper.OnNavigatedTo(e); // Get theme object if(e.Parameter != null) { theme = e.Parameter as HueTheme; } else { theme = new HueTheme(); theme.Name = "New Theme"; } if (theme.IsSystemTheme) { EditNameButton.Visibility = Visibility.Collapsed; RevertSystemThemeButton.Visibility = Visibility.Visible; DeleteButton.Visibility = Visibility.Collapsed; } else { EditNameButton.Visibility = Visibility.Visible; RevertSystemThemeButton.Visibility = Visibility.Collapsed; DeleteButton.Visibility = Visibility.Visible; } TitleLabel.Text = theme.Name; // Make a clone of colors foreach (var color in theme.ColorList) { localColorList.Add(color.Clone()); } ColorListView.ItemsSource = localColorList; // Events ColorRenderer.ColorChanged += OnThemeColorChanged; ColorRenderer.ColorDeleted += OnThemeColorDeleted; ThemeManager.Instance.ThemeChanged += OnThemeChanged; }
private async void EditThemeAsync(HueTheme theme) { await ThemeManager.Instance.ApplyThemeAsync(theme as HueTheme); var frame = Window.Current.Content as Frame; frame.Navigate(typeof(ThemePage), theme); }
public static HueTheme CreateThemeFromJson(string content) { try { HueTheme theme = new HueTheme(); JObject json = (JObject)JObject.Parse(content); // Name JToken nameToken; if (json.TryGetValue("name", out nameToken)) { theme.Name = json["name"].ToString(); } // Is system theme JToken isSystemToken; if (json.TryGetValue("is_system_default", out isSystemToken)) { theme.IsSystemTheme = bool.Parse(json["is_system_default"].ToString()); } // Banner image JToken imageToken; if (json.TryGetValue("banner_image", out imageToken)) { string imageString = json["banner_image"].ToString(); if (imageString.Length == 0) { theme.BannerImage = null; } else { theme.BannerImage = imageString; } } // Color list JToken colorToken; if (json.TryGetValue("colors", out colorToken)) { JArray colorArray = (JArray)json["colors"]; foreach (JObject colorJson in colorArray) { int h = int.Parse(colorJson["h"].ToString()); int s = int.Parse(colorJson["s"].ToString()); int b = int.Parse(colorJson["b"].ToString()); var color = new HSBColor(h, s, b); theme.ColorList.Add(color); } } // Default color list JToken defaultColorToken; if (json.TryGetValue("default_colors", out defaultColorToken)) { JArray colorArray = (JArray)json["default_colors"]; foreach (JObject colorJson in colorArray) { int h = int.Parse(colorJson["h"].ToString()); int s = int.Parse(colorJson["s"].ToString()); int b = int.Parse(colorJson["b"].ToString()); var color = new HSBColor(h, s, b); theme.DefaultColorList.Add(color); } } return theme; } catch (Exception ex) { Debug.WriteLine(ex); return null; } }
public async Task<HueTheme> CreateThemeAsync() { var newTheme = new HueTheme(); var newIndex = Themes.Count + 1; newTheme.Name = "theme " + newIndex.ToString(); newTheme.FileName = "theme_" + newIndex.ToString(); newTheme.IsSystemTheme = false; // Add a default color newTheme.ColorList.Add(HSBColor.FromColor(Color.FromArgb(0xff, 0xec, 0xcd, 0x67))); newTheme.ColorList.Add(HSBColor.FromColor(Color.FromArgb(0xff, 0xec, 0xcd, 0x67))); newTheme.ColorList.Add(HSBColor.FromColor(Color.FromArgb(0xff, 0xec, 0xcd, 0x67))); // Add to themes Themes.Add(newTheme); await SaveThemeToFileAsync(newTheme); // Notify theme list changes if (ThemeListChanged != null) { ThemeListChanged(this, null); } return newTheme; }
public async Task<bool> ApplyThemeAsync(HueTheme theme) { if (theme.ColorList.Count == 0) { return true; } int colorIndex = 0; foreach (var light in BridgeManager.Instance.CurrentBridge.LightList) { HSBColor color = theme.ColorList[colorIndex]; var attrs = new { hue = (int)color.H, sat = (int)color.S, bri = (int)color.B }; await HueAPI.Instance.SetLightStateAsync(light.LightId, attrs); // Update light attrs light.Hue = (int)color.H; light.Saturation = (int)color.S; light.Brightness = (int)color.B; BridgeManager.Instance.InvalidateLightProperties(light); colorIndex++; if (colorIndex >= theme.ColorList.Count - 1) { colorIndex = 0; } } return true; }