Store a settings record in the user class. Serialize it for ease of loading / saving. Use this settings class for all storing / retrieving stuff. Works awesome.

This commit is contained in:
Tim Young 2017-07-30 14:55:37 -05:00
parent ba3048cc07
commit d25cfcbade
4 changed files with 49 additions and 36 deletions

View File

@ -12,7 +12,8 @@ using System.Drawing;
using System.Media;
using System.Reflection;
using System.Text.RegularExpressions;
using System.Xml.Serialization;
using System.IO;
namespace EduNetworkBuilder
{
@ -367,6 +368,11 @@ namespace EduNetworkBuilder
public static NBSettings GetSettings()
{
//Try getting the user settings
PersonClass User = GetUser();
if (User != null) return User.UserSettings;
//If no user, get the settings for the program.
BuilderWindow myWin = (BuilderWindow)Application.OpenForms["BuilderWindow"];
if (myWin != null)
{
@ -580,6 +586,36 @@ namespace EduNetworkBuilder
}
}
/// <summary>
/// Serialize any serializable class to an XML string.
/// </summary>
/// <typeparam name="T">The type of object</typeparam>
/// <param name="toSerialize">The object to serialize</param>
/// <returns>A string containing the serialized object in XML</returns>
public static string SerializeObject<T>(T toSerialize)
{
XmlSerializer xmlSerializer = new XmlSerializer(toSerialize.GetType());
using (StringWriter textWriter = new StringWriter())
{
xmlSerializer.Serialize(textWriter, toSerialize);
return textWriter.ToString();
}
}
/// <summary>
/// Deserialize (oposite of SerializeObject) an object from an xml string.
/// </summary>
/// <typeparam name="T">The type of object to deserialize</typeparam>
/// <param name="toDeserialize">the xml string containing the object</param>
/// <returns>An object of the specified type</returns>
public static T Deserialize<T>(string toDeserialize)
{
XmlSerializer xmlSerializer = new XmlSerializer(typeof(T));
StringReader textReader = new StringReader(toDeserialize);
return (T)xmlSerializer.Deserialize(textReader);
}
public static List<string> GetPuzzleTags()
{
BuilderWindow myWin = (BuilderWindow)Application.OpenForms["BuilderWindow"];

View File

@ -109,11 +109,6 @@ namespace EduNetworkBuilder
public void MarkAsDone(string PuzzleName)
{
PersonClass PC = NB.GetUser();
if(PC != null)
{
PC.MarkAsDone(PuzzleName); //Mark it on the user's personal settings.
}
//we just mark it in the settings class
if (!ScoreList.Contains(PuzzleName))
{
@ -124,11 +119,6 @@ namespace EduNetworkBuilder
public bool CheckIfDone(string PuzzleName)
{
PersonClass PC = NB.GetUser();
if (PC != null)
{
return PC.CheckIfDone(PuzzleName); //Mark it on the user's personal settings.
}
//we just mark it in the settings class
if (ScoreList.Contains(PuzzleName))
return true;

View File

@ -535,7 +535,7 @@ namespace EduNetworkBuilder
{
if ("Level_" + pi.Level.ToString() == LevelName)
{
if (!OurSettings.ScoreList.Contains(pi.PuzzleName))
if (!OurSettings.CheckIfDone(pi.PuzzleName))
{
return true; //We have one puzzle in the level which has not been solved
}

View File

@ -39,10 +39,13 @@ namespace EduNetworkBuilder
/// <summary>
/// EverCompletedPuzzles is a list of puzzles that they have completed once.
/// </summary>
List<string> EverCompletedPuzzles = new List<string>();
public List<SchoolworkClass> Projects = new List<SchoolworkClass>();
public NBSettings UserSettings = new NBSettings();
private PersonClass()
{ }
@ -121,8 +124,8 @@ namespace EduNetworkBuilder
case "password":
Passwords.Add(Individual.InnerText);
break;
case "scorelist":
EverCompletedPuzzles.Add(Individual.InnerText);
case "settings":
UserSettings = NB.Deserialize<NBSettings>(Individual.InnerText);
break;
case "project":
case "schoolwork":
@ -171,10 +174,9 @@ namespace EduNetworkBuilder
writer.WriteElementString("PasswordHint", PasswordHint);
writer.WriteElementString("AltPassword", AltPassword);
writer.WriteElementString("IsAdmin", isAdmin.ToString());
foreach(string one in EverCompletedPuzzles)
{
writer.WriteElementString("ScoreList", one);
}
string settingsstring = NB.SerializeObject<NBSettings>(UserSettings);
writer.WriteElementString("Settings", settingsstring);
foreach(string One in Passwords)
writer.WriteElementString("Password", One);
//Save all the devices
@ -186,20 +188,5 @@ namespace EduNetworkBuilder
}
#endregion
public void MarkAsDone(string PuzzleName)
{
if (!EverCompletedPuzzles.Contains(PuzzleName))
{
EverCompletedPuzzles.Add(PuzzleName);
}
}
public bool CheckIfDone(string PuzzleName)
{
if (EverCompletedPuzzles.Contains(PuzzleName))
return true;
else
return false;
}
}
}