fix persistance qui marchait pas tout le temps

Popup_qui_marche_pas
Anthony RICHARD 2 years ago
parent ce9bb903af
commit 5d8bb4d688

@ -12,9 +12,15 @@ namespace StimPersistance
[ExcludeFromCodeCoverage] [ExcludeFromCodeCoverage]
public class Persistance : IPersistance public class Persistance : IPersistance
{ {
public Persistance(string chemin) private const string gameFileName = "games.xml";
private const string userFileName = "users.xml";
private readonly string fullGamePath;
private readonly string fullUserPath;
public Persistance(string path)
{ {
Directory.SetCurrentDirectory(chemin); fullGamePath = Path.Combine(path, gameFileName);
fullUserPath = Path.Combine(path, userFileName);
} }
public void SaveGame(List<Game> games) public void SaveGame(List<Game> games)
@ -22,7 +28,7 @@ namespace StimPersistance
XmlWriterSettings settings = new() { Indent = true }; XmlWriterSettings settings = new() { Indent = true };
DataContractSerializer serializer = new(typeof(List<Game>)); DataContractSerializer serializer = new(typeof(List<Game>));
using (TextWriter tw = File.CreateText("games.xml")) using (TextWriter tw = File.CreateText(fullGamePath))
using (XmlWriter writer = XmlWriter.Create(tw, settings)) serializer.WriteObject(writer, games); using (XmlWriter writer = XmlWriter.Create(tw, settings)) serializer.WriteObject(writer, games);
} }
@ -31,26 +37,26 @@ namespace StimPersistance
XmlWriterSettings settings = new() { Indent = true }; XmlWriterSettings settings = new() { Indent = true };
DataContractSerializer serializer = new(typeof(HashSet<User>)); DataContractSerializer serializer = new(typeof(HashSet<User>));
using (TextWriter tw = File.CreateText("users.xml")) using (TextWriter tw = File.CreateText(fullUserPath))
using (XmlWriter writer = XmlWriter.Create(tw, settings)) serializer.WriteObject(writer, users); using (XmlWriter writer = XmlWriter.Create(tw, settings)) serializer.WriteObject(writer, users);
} }
public List<Game> LoadGame() public List<Game> LoadGame()
{ {
if (File.Exists("games.xml")) if (File.Exists(fullGamePath))
{ {
DataContractSerializer serializer = new(typeof(List<Game>)); DataContractSerializer serializer = new(typeof(List<Game>));
using (Stream stream = File.OpenRead("games.xml")) return serializer.ReadObject(stream) as List<Game> ?? new(); using (Stream stream = File.OpenRead(fullGamePath)) return serializer.ReadObject(stream) as List<Game> ?? new();
} }
return new(); return new();
} }
public HashSet<User> LoadUser() public HashSet<User> LoadUser()
{ {
if (File.Exists("users.xml")) if (File.Exists(fullUserPath))
{ {
DataContractSerializer serializer = new(typeof(HashSet<User>)); DataContractSerializer serializer = new(typeof(HashSet<User>));
using (Stream stream = File.OpenRead("users.xml")) return serializer.ReadObject(stream) as HashSet<User> ?? new(); using (Stream stream = File.OpenRead(fullUserPath)) return serializer.ReadObject(stream) as HashSet<User> ?? new();
} }
return new(); return new();
} }

@ -8,34 +8,18 @@ namespace Stim;
public partial class App : Application public partial class App : Application
{ {
public Manager Manager { get; set; } public Manager Manager { get; set; }
public App() public App()
{ {
InitializeComponent(); InitializeComponent();
MainPage = new LoginPage(); MainPage = new NavigationPage(new LoginPage());
if (File.Exists(Path.Combine(FileSystem.Current.AppDataDirectory, "games.xml"))) Manager = new Manager(new Persistance(FileSystem.Current.AppDataDirectory)); Manager = new(new Persistance(FileSystem.Current.AppDataDirectory));
else Manager = new(new Stub()); if (!File.Exists(Path.Combine(FileSystem.Current.AppDataDirectory, "games.xml"))) FirstStart();
} }
protected override Window CreateWindow(IActivationState activationState)
{
Window window = base.CreateWindow(activationState);
window.Stopped += (s, e) =>
{
if (!(File.Exists(Path.Combine(FileSystem.Current.AppDataDirectory, "games.xml"))))
{
Manager Manager2 = new(new Persistance(FileSystem.Current.AppDataDirectory));
foreach (var game in Manager.GameList) Manager2.AddGametoGamesList(game);
foreach (var user in Manager.Users) Manager2.AddUsertoUserList(user);
Manager2.SaveGames();
Manager2.SaveUser();
}
else
{
Manager.SaveGames();
Manager.SaveUser();
}
};
return window; private void FirstStart()
} {
Manager mgrtmp = new(new Stub());
foreach (var game in mgrtmp.GameList) Manager.AddGametoGamesList(game);
}
} }

Loading…
Cancel
Save