/// <summary> /// Read in the JSON settings, if the JSON file exists. /// </summary> private void readjsonSettingsFile() { // Initialize index to 0 int recentPlayedIndex = 0; // Initialize the length of the recent champs to 0 int recentChampLen = 0; // Only read in JSON file if the file exists if (System.IO.File.Exists(this.jsonSettingsFile)) { // Read in the JSON file first System.IO.StreamReader reader = new System.IO.StreamReader(this.jsonSettingsFile); string jsonString = reader.ReadToEnd(); // Convert JSON String to a C# object SettingsJsonObject jsonStuff = JsonConvert.DeserializeObject <SettingsJsonObject> (jsonString); // Set the correct sites for (int i = 0, i2 = jsonStuff.sitesSelected.Length; i < i2; i++) { bool boolSite = jsonStuff.sitesSelected[i]; ((CheckBox)this.FindName(this.m_siteNames[i])).IsChecked = boolSite; } // Set the most recent 5 champions mostRecentChamps.Items.Clear(); recentChampLen = jsonStuff.fiveRecentChampions.Length; for (int i = 0, i2 = recentChampLen; i < i2; i++) { String curChamp = jsonStuff.fiveRecentChampions[i]; CustomListViewItem lvi = new CustomListViewItem(); lvi.Content = curChamp; lvi.Selected += SelectItemHandler; this.mostRecentChamps.Items.Add(lvi); this.m_arrRecentlyPlayedChamps[recentPlayedIndex++] = lvi; } reader.Close(); } // If there are less than 5 champs, add in empty placeholders int diff = MAX_RECENT_CHAMPS - recentChampLen; for (int i = 0; i < diff; i++) { CustomListViewItem lvi = new CustomListViewItem(); lvi.Focusable = false; lvi.Content = ""; this.mostRecentChamps.Items.Add(lvi); this.m_arrRecentlyPlayedChamps[recentPlayedIndex++] = lvi; } }
/// <summary> /// Handle when the Window closes. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void Window_Closing(object sender, CancelEventArgs e) { // Save settings to JSON file // Get the most recent champions int length1 = mostRecentChamps.Items.Count; List <String> recentChamps = new List <String>(); for (int i = 0; i < length1; i++) { String content = ((CustomListViewItem)mostRecentChamps.Items[i]).Content.ToString(); if (false == String.IsNullOrEmpty(content)) { recentChamps.Add(content); } } // Then get the sites selected length1 = this.m_siteNames.Length; bool[] tempSitesSelected = new bool[length1]; for (int i = 0; i < length1; i++) { CheckBox checkBox = (CheckBox)this.FindName(this.m_siteNames[i]); tempSitesSelected[i] = (bool)checkBox.IsChecked; } SettingsJsonObject sjo = new SettingsJsonObject(); sjo.fiveRecentChampions = recentChamps.ToArray(); sjo.sitesSelected = tempSitesSelected; // Write to file // serialize JSON directly to a file using (System.IO.StreamWriter file = System.IO.File.CreateText(this.jsonSettingsFile)) { JsonSerializer serializer = new JsonSerializer(); serializer.Serialize(file, sjo); } }