From 0ef51c3fe3f04ca4ce2c5edb87b0b1de3a88c307 Mon Sep 17 00:00:00 2001 From: Anthony RICHARD Date: Fri, 9 Jun 2023 19:52:42 +0200 Subject: [PATCH] =?UTF-8?q?fix=20onpropertychange=20+=20r=C3=A9f=C3=A9renc?= =?UTF-8?q?es?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Sources/Persistance/Persistance.cs | 13 +++++-------- Sources/Stim.Model/Manager.cs | 14 ++++++++++++++ Sources/Stim.Model/User.cs | 2 ++ Sources/Stim/DetailledPage.xaml.cs | 8 ++++++++ Sources/Stim/LoginPage.xaml.cs | 3 ++- Sources/TestProject1/TestGame.cs | 2 ++ 6 files changed, 33 insertions(+), 9 deletions(-) diff --git a/Sources/Persistance/Persistance.cs b/Sources/Persistance/Persistance.cs index 80609bc..f0149bb 100644 --- a/Sources/Persistance/Persistance.cs +++ b/Sources/Persistance/Persistance.cs @@ -1,10 +1,7 @@ using System.Collections.ObjectModel; using System.Runtime.Serialization; using System.Xml; -using Microsoft.VisualBasic; -using Microsoft.VisualBasic.FileIO; using Model; -using System.Runtime.InteropServices; using System.Diagnostics.CodeAnalysis; namespace StimPersistance @@ -26,7 +23,7 @@ namespace StimPersistance public void SaveGame(List games) { XmlWriterSettings settings = new() { Indent = true }; - DataContractSerializer serializer = new(typeof(List)); + DataContractSerializer serializer = new(typeof(List), new DataContractSerializerSettings() { PreserveObjectReferences = true }); using (TextWriter tw = File.CreateText(fullGamePath)) using (XmlWriter writer = XmlWriter.Create(tw, settings)) serializer.WriteObject(writer, games); @@ -35,7 +32,7 @@ namespace StimPersistance public void SaveUser(HashSet users) { XmlWriterSettings settings = new() { Indent = true }; - DataContractSerializer serializer = new(typeof(HashSet)); + DataContractSerializer serializer = new(typeof(HashSet), new DataContractSerializerSettings() { PreserveObjectReferences = true}); using (TextWriter tw = File.CreateText(fullUserPath)) using (XmlWriter writer = XmlWriter.Create(tw, settings)) serializer.WriteObject(writer, users); @@ -45,7 +42,7 @@ namespace StimPersistance { if (File.Exists(fullGamePath)) { - DataContractSerializer serializer = new(typeof(List)); + DataContractSerializer serializer = new(typeof(List), new DataContractSerializerSettings() { PreserveObjectReferences = true }); using (Stream stream = File.OpenRead(fullGamePath)) return serializer.ReadObject(stream) as List ?? new(); } return new(); @@ -55,10 +52,10 @@ namespace StimPersistance { if (File.Exists(fullUserPath)) { - DataContractSerializer serializer = new(typeof(HashSet)); + DataContractSerializer serializer = new(typeof(HashSet), new DataContractSerializerSettings() { PreserveObjectReferences = true }); using (Stream stream = File.OpenRead(fullUserPath)) return serializer.ReadObject(stream) as HashSet ?? new(); } return new(); - } + } } } \ No newline at end of file diff --git a/Sources/Stim.Model/Manager.cs b/Sources/Stim.Model/Manager.cs index af3e69f..2ea1e89 100644 --- a/Sources/Stim.Model/Manager.cs +++ b/Sources/Stim.Model/Manager.cs @@ -71,5 +71,19 @@ namespace Model { mgrpersistance.SaveUser(Users); } + + [ExcludeFromCodeCoverage] + public void UpdateReferences() + { + if (CurrentUser != null && CurrentUser.Followed_Games.Count != 0) + { + foreach (var game in CurrentUser.Followed_Games.ToList()) + { + CurrentUser.RemoveAGame(game); + if (GameList.Contains(game)) CurrentUser.FollowAGame(gameList.Where(g => g.Name == game.Name).First()); + } + } + SaveUser(); + } } } diff --git a/Sources/Stim.Model/User.cs b/Sources/Stim.Model/User.cs index 7541656..b8c99e7 100644 --- a/Sources/Stim.Model/User.cs +++ b/Sources/Stim.Model/User.cs @@ -142,11 +142,13 @@ namespace Model { if (Followed_Games.Contains(game)) return; followed_Games.Add(game); + NotifyPropertyChanged(nameof(Followed_Games)); } public void RemoveAGame(Game game) { if (!Followed_Games.Contains(game)) return; followed_Games.Remove(game); + NotifyPropertyChanged(nameof(Followed_Games)); } public override string ToString() diff --git a/Sources/Stim/DetailledPage.xaml.cs b/Sources/Stim/DetailledPage.xaml.cs index e1cba53..0034e68 100644 --- a/Sources/Stim/DetailledPage.xaml.cs +++ b/Sources/Stim/DetailledPage.xaml.cs @@ -74,9 +74,17 @@ public partial class DetailledPage : ContentPage var res = await this.ShowPopupAsync(new ConfirmationPopup("Voulez-vous vraiment supprimer " + (App.Current as App).Manager.SelectedGame.Name + " ?")); if (res != null && res is bool && (bool)res) { + (App.Current as App).Manager.CurrentUser.RemoveAGame((App.Current as App).Manager.SelectedGame); (App.Current as App).Manager.RemoveGameFromGamesList((App.Current as App).Manager.SelectedGame); + (App.Current as App).Manager.SaveUser(); await Navigation.PopAsync(); await this.ShowPopupAsync(new MessagePopup("Jeu supprimé !")); } } + + protected override void OnDisappearing() + { + Navigation.PopAsync(); + base.OnDisappearing(); + } } \ No newline at end of file diff --git a/Sources/Stim/LoginPage.xaml.cs b/Sources/Stim/LoginPage.xaml.cs index a7a8c1e..58e19bd 100644 --- a/Sources/Stim/LoginPage.xaml.cs +++ b/Sources/Stim/LoginPage.xaml.cs @@ -21,8 +21,9 @@ public partial class LoginPage : ContentPage if (user.Password == Pswd.Text) { ((App)App.Current).Manager.CurrentUser = user; + ((App)App.Current).Manager.UpdateReferences(); Application.Current.MainPage = new AppShell(); - await Navigation.PushModalAsync(new MainPage());//Shell.Current.GoToAsync("//MainPage"); + await Navigation.PushModalAsync(new MainPage()); } else Error.Children.Add(new Label { Text = "Mot de passe incorrect", TextColor = Colors.Red, diff --git a/Sources/TestProject1/TestGame.cs b/Sources/TestProject1/TestGame.cs index 3486007..c3b20b1 100644 --- a/Sources/TestProject1/TestGame.cs +++ b/Sources/TestProject1/TestGame.cs @@ -174,12 +174,14 @@ namespace Test Game game2 = new("name2", "description", 2020, new List { "1" }, "cover2", "www.link.com"); Game? game3 = null; string game4 = ""; + Game game5 = new("name", "description2", 2012, new List { "1", "2", "3" }, "cover", "www.link.com"); Assert.False(game.Equals(game2 as Game)); Assert.False(game.Equals(game3 as object)); Assert.True(game.Equals(game as object)); Assert.False(game.Equals(game4 as object)); Assert.False(game.Equals(game2 as object)); + Assert.True(game.Equals(game5 as Game)); } [Fact]