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.Media;
using System.Reflection; using System.Reflection;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
using System.Xml.Serialization;
using System.IO;
namespace EduNetworkBuilder namespace EduNetworkBuilder
{ {
@ -367,6 +368,11 @@ namespace EduNetworkBuilder
public static NBSettings GetSettings() 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"]; BuilderWindow myWin = (BuilderWindow)Application.OpenForms["BuilderWindow"];
if (myWin != null) 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() public static List<string> GetPuzzleTags()
{ {
BuilderWindow myWin = (BuilderWindow)Application.OpenForms["BuilderWindow"]; BuilderWindow myWin = (BuilderWindow)Application.OpenForms["BuilderWindow"];

View File

@ -108,12 +108,7 @@ namespace EduNetworkBuilder
} }
public void MarkAsDone(string PuzzleName) 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 //we just mark it in the settings class
if (!ScoreList.Contains(PuzzleName)) if (!ScoreList.Contains(PuzzleName))
{ {
@ -123,12 +118,7 @@ namespace EduNetworkBuilder
} }
public bool CheckIfDone(string PuzzleName) 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 //we just mark it in the settings class
if (ScoreList.Contains(PuzzleName)) if (ScoreList.Contains(PuzzleName))
return true; return true;

View File

@ -535,7 +535,7 @@ namespace EduNetworkBuilder
{ {
if ("Level_" + pi.Level.ToString() == LevelName) 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 return true; //We have one puzzle in the level which has not been solved
} }

View File

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