From 5d8bb4d6887173dcbed64064cdffc1be2f0185e0 Mon Sep 17 00:00:00 2001 From: Anthony RICHARD Date: Thu, 8 Jun 2023 05:06:09 +0200 Subject: [PATCH] fix persistance qui marchait pas tout le temps --- Sources/Persistance/Persistance.cs | 22 ++++++++++++------- Sources/Stim/App.xaml.cs | 34 ++++++++---------------------- 2 files changed, 23 insertions(+), 33 deletions(-) diff --git a/Sources/Persistance/Persistance.cs b/Sources/Persistance/Persistance.cs index 9f6b0d8..80609bc 100644 --- a/Sources/Persistance/Persistance.cs +++ b/Sources/Persistance/Persistance.cs @@ -12,9 +12,15 @@ namespace StimPersistance [ExcludeFromCodeCoverage] 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 games) @@ -22,7 +28,7 @@ namespace StimPersistance XmlWriterSettings settings = new() { Indent = true }; DataContractSerializer serializer = new(typeof(List)); - using (TextWriter tw = File.CreateText("games.xml")) + using (TextWriter tw = File.CreateText(fullGamePath)) using (XmlWriter writer = XmlWriter.Create(tw, settings)) serializer.WriteObject(writer, games); } @@ -31,26 +37,26 @@ namespace StimPersistance XmlWriterSettings settings = new() { Indent = true }; DataContractSerializer serializer = new(typeof(HashSet)); - using (TextWriter tw = File.CreateText("users.xml")) + using (TextWriter tw = File.CreateText(fullUserPath)) using (XmlWriter writer = XmlWriter.Create(tw, settings)) serializer.WriteObject(writer, users); } public List LoadGame() { - if (File.Exists("games.xml")) + if (File.Exists(fullGamePath)) { DataContractSerializer serializer = new(typeof(List)); - using (Stream stream = File.OpenRead("games.xml")) return serializer.ReadObject(stream) as List ?? new(); + using (Stream stream = File.OpenRead(fullGamePath)) return serializer.ReadObject(stream) as List ?? new(); } return new(); } public HashSet LoadUser() { - if (File.Exists("users.xml")) + if (File.Exists(fullUserPath)) { DataContractSerializer serializer = new(typeof(HashSet)); - using (Stream stream = File.OpenRead("users.xml")) return serializer.ReadObject(stream) as HashSet ?? new(); + using (Stream stream = File.OpenRead(fullUserPath)) return serializer.ReadObject(stream) as HashSet ?? new(); } return new(); } diff --git a/Sources/Stim/App.xaml.cs b/Sources/Stim/App.xaml.cs index e4d3290..ce9295e 100644 --- a/Sources/Stim/App.xaml.cs +++ b/Sources/Stim/App.xaml.cs @@ -8,34 +8,18 @@ namespace Stim; public partial class App : Application { public Manager Manager { get; set; } + public App() { InitializeComponent(); - MainPage = new LoginPage(); - if (File.Exists(Path.Combine(FileSystem.Current.AppDataDirectory, "games.xml"))) Manager = new Manager(new Persistance(FileSystem.Current.AppDataDirectory)); - else Manager = new(new Stub()); + MainPage = new NavigationPage(new LoginPage()); + Manager = new(new Persistance(FileSystem.Current.AppDataDirectory)); + 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); + } }