From 39fb86e8817d6da993e2062ad3d6ca44b6b696b6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20LAVERGNE?= Date: Mon, 3 Jun 2024 22:05:44 +0200 Subject: [PATCH] =?UTF-8?q?=E2=80=BC=EF=B8=8F=20Ajout=20du=20n=C3=A9cessai?= =?UTF-8?q?re=20pour=20impl=C3=A9menter=20la=20persistance=20et=20son=20ma?= =?UTF-8?q?nagement=20au=20sein=20de=20toute=20les=20autres=20vues?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- source/Trek-12/Trek-12/App.xaml.cs | 48 ++++++++++++++++++++++++------ 1 file changed, 39 insertions(+), 9 deletions(-) diff --git a/source/Trek-12/Trek-12/App.xaml.cs b/source/Trek-12/Trek-12/App.xaml.cs index 35a7a0c..2265b67 100644 --- a/source/Trek-12/Trek-12/App.xaml.cs +++ b/source/Trek-12/Trek-12/App.xaml.cs @@ -1,28 +1,58 @@ -using DataContractPersistence; +using System.Diagnostics; +using System.Runtime.Serialization.DataContracts; +using DataContractPersistence; using Models.Game; +using Models.Interfaces; namespace Trek_12 { public partial class App : Application { public string FileName { get; set; } = "data.json"; - - public string FilePath { get; set; } = Path.Combine(AppDomain.CurrentDomain.BaseDirectory); + + public string FilePath { get; set; } = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "Trek_12"); public Game Manager { get; private set; } - + public App() { InitializeComponent(); - - if(File.Exists(Path.Combine(FilePath, FileName))) + + Manager = new Game(new DataContractJson()); + + if (!Directory.Exists(FilePath)) + { + Directory.CreateDirectory(FilePath); + } + + //File.Delete(Path.Combine(FilePath, FileName)); + + string fullPath = Path.Combine(FilePath, FileName); + if (File.Exists(fullPath)) { - Manager = new Game(new DataContractJson()); + Debug.WriteLine("Data loaded from " + fullPath); + Manager.LoadData(); } - - Manager.LoadData(); + MainPage = new AppShell(); + + // If the MainPage is closed, we save the data + MainPage.Disappearing += (sender, e) => + { + Debug.WriteLine("Saving data..."); + Manager.SaveData(); + }; + + } + + /// + /// Save the data when the app is in background + /// + protected override void OnSleep() + { + Debug.WriteLine("Zzz Secure save..."); + Manager.SaveData(); } } }