From af325027927c22dbda73313a77f9817f15a81c56 Mon Sep 17 00:00:00 2001 From: lobroda Date: Sun, 22 Oct 2023 19:27:33 +0100 Subject: [PATCH] FIX : Data Loads with CommunityToolkit --- ViewModels/AuthorVM.cs | 33 +++++++++++---- ViewModels/BookVM.cs | 88 ++++++++++++++++++++++++++------------- ViewModels/BorrowingVM.cs | 46 +++++++++++++------- ViewModels/ContactVM.cs | 34 +++++++++------ ViewModels/LoanVM.cs | 46 +++++++++++++------- ViewModels/RatingsVM.cs | 27 +++++++----- 6 files changed, 180 insertions(+), 94 deletions(-) diff --git a/ViewModels/AuthorVM.cs b/ViewModels/AuthorVM.cs index 0b931e9..06a1e08 100644 --- a/ViewModels/AuthorVM.cs +++ b/ViewModels/AuthorVM.cs @@ -8,31 +8,46 @@ using System.Threading.Tasks; namespace ViewModels { - [ObservableObject] - public partial class AuthorVM + public class AuthorVM : ObservableObject { #region Fields - [ObservableProperty] - private int nbBooksWritten; - - [ObservableProperty] - private string name; + private int nbBooksWritten { get; set; } #endregion #region Properties + public Author Model { get; } + + public string Name + { + get => Model.Name; + set + { + Model.Name = value; + } + } + + public int NbBooksWritten + { + get => nbBooksWritten; + set + { + nbBooksWritten = value; + } + } + #endregion #region Constructor public AuthorVM(Author model) { - + Model = model; } #endregion } -} +} \ No newline at end of file diff --git a/ViewModels/BookVM.cs b/ViewModels/BookVM.cs index 89de8cd..bc1a802 100644 --- a/ViewModels/BookVM.cs +++ b/ViewModels/BookVM.cs @@ -8,51 +8,79 @@ using System.Threading.Tasks; namespace ViewModels { - [ObservableObject] - public partial class BookVM + public class BookVM : ObservableObject { #region Fields - [ObservableProperty] - private string id; + #endregion - [ObservableProperty] - private string isbn13; + #region Properties - [ObservableProperty] - private string title; + public Book Model { get; } - [ObservableProperty] - private List publishers; + public string Id + { + get => Model.Id; + } - [ObservableProperty] - private DateTime publishDate; + public string ISBN13 + { + get => Model.ISBN13; + } - [ObservableProperty] - private List authors; + public string Title + { + get => Model.Title; + } - [ObservableProperty] - private string author; + public List Publishers + { + get => Model.Publishers; + } - [ObservableProperty] - private Status status; + public DateTime PublishDate + { + get => Model.PublishDate; + } - [ObservableProperty] - private int nbPages; + public List Authors + { + get => Model.Authors.Select(a => new AuthorVM(a)).ToList(); + } - [ObservableProperty] - private Languages language; + public string Author => Model.Authors.Count > 0 ? Model.Authors.First().Name : "Auteur inconnu"; - [ObservableProperty] - private string imageSmall; + public Status Status + { + get => Model.Status; + set => SetProperty(Model.Status, value, status => Model.Status = status); + } - [ObservableProperty] - private float? userRating; + public int NbPages + { + get => Model.NbPages; + } - #endregion + public Languages Language + { + get => Model.Language; + } - #region Properties + public string ImageSmall + { + get => Model.ImageSmall; + } + + public float? UserRating + { + get => Model?.UserRating; + set + { + if (Model == null) return; + SetProperty(Model.UserRating, value, rating => Model.UserRating = rating); + } + } #endregion @@ -60,9 +88,9 @@ namespace ViewModels public BookVM(Book b) { - + Model = b; } #endregion } -} +} \ No newline at end of file diff --git a/ViewModels/BorrowingVM.cs b/ViewModels/BorrowingVM.cs index 6725f72..24d4e8a 100644 --- a/ViewModels/BorrowingVM.cs +++ b/ViewModels/BorrowingVM.cs @@ -8,29 +8,43 @@ using System.Threading.Tasks; namespace ViewModels { - [ObservableObject] - public partial class BorrowingVM + public class BorrowingVM : ObservableObject { #region Fields - [ObservableProperty] - private string id; + #endregion - [ObservableProperty] - private BookVM book; + #region Properties - [ObservableProperty] - private ContactVM owner; + public Borrowing Model { get; } - [ObservableProperty] - private DateTime borrowedAt; + public string Id + { + get => Model.Id; + set => SetProperty(Model.Id, value, v => Model.Id = value); + } - [ObservableProperty] - private DateTime returnedAt; + public BookVM Book + { + get => new BookVM(Model.Book); + } - #endregion + public ContactVM Owner + { + get => new ContactVM(Model.Owner); + } - #region Properties + public DateTime BorrowedAt + { + get => Model.BorrowedAt; + set => SetProperty(Model.BorrowedAt, value, v => Model.BorrowedAt = value); + } + + public DateTime? ReturnedAt + { + get => Model.ReturnedAt; + set => SetProperty(Model.ReturnedAt, value, v => Model.ReturnedAt = value); + } #endregion @@ -38,9 +52,9 @@ namespace ViewModels public BorrowingVM(Borrowing b) { - + Model = b; } #endregion } -} +} \ No newline at end of file diff --git a/ViewModels/ContactVM.cs b/ViewModels/ContactVM.cs index 959b908..d8509e4 100644 --- a/ViewModels/ContactVM.cs +++ b/ViewModels/ContactVM.cs @@ -8,23 +8,33 @@ using System.Threading.Tasks; namespace ViewModels { - [ObservableObject] - public partial class ContactVM + public class ContactVM : ObservableObject { #region Fields - [ObservableProperty] - private string id; + #endregion - [ObservableProperty] - private string firstName; + #region Properties - [ObservableProperty] - private string lastName; + public Model.Contact Model { get; } - #endregion + public string Id + { + get => Model.Id; + set => SetProperty(Model.Id, value, v => Model.Id = value); + } - #region Properties + public string FirstName + { + get => Model.FirstName; + set => SetProperty(Model.FirstName, value, v => Model.FirstName = value); + } + + public string LastName + { + get => Model.LastName; + set => SetProperty(Model.LastName, value, v => Model.LastName = value); + } #endregion @@ -32,9 +42,9 @@ namespace ViewModels public ContactVM(Model.Contact c) { - + Model = c; } #endregion } -} +} \ No newline at end of file diff --git a/ViewModels/LoanVM.cs b/ViewModels/LoanVM.cs index 21d92b3..dc1e75e 100644 --- a/ViewModels/LoanVM.cs +++ b/ViewModels/LoanVM.cs @@ -8,29 +8,43 @@ using System.Threading.Tasks; namespace ViewModels { - [ObservableObject] - public partial class LoanVM + public class LoanVM : ObservableObject { #region Fields - [ObservableProperty] - private string id; + #endregion - [ObservableProperty] - private BookVM book; + #region Properties - [ObservableProperty] - private ContactVM loaner; + public Loan Model { get; } - [ObservableProperty] - private DateTime loanedAt; + public string Id + { + get => Model.Id; + set => SetProperty(Model.Id, value, v => Model.Id = value); + } - [ObservableProperty] - private DateTime? returnedAt; + public BookVM Book + { + get => new BookVM(Model.Book); + } - #endregion + public ContactVM Loaner + { + get => new ContactVM(Model.Loaner); + } - #region Properties + public DateTime LoanedAt + { + get => Model.LoanedAt; + set => SetProperty(Model.LoanedAt, value, v => Model.LoanedAt = value); + } + + public DateTime? ReturnedAt + { + get => Model.ReturnedAt; + set => SetProperty(Model.ReturnedAt, value, v => Model.ReturnedAt = value); + } #endregion @@ -38,9 +52,9 @@ namespace ViewModels public LoanVM(Loan l) { - + Model = l; } #endregion } -} +} \ No newline at end of file diff --git a/ViewModels/RatingsVM.cs b/ViewModels/RatingsVM.cs index 4db1bcc..77ae605 100644 --- a/ViewModels/RatingsVM.cs +++ b/ViewModels/RatingsVM.cs @@ -8,24 +8,29 @@ using System.Threading.Tasks; namespace ViewModels { - [ObservableObject] - public partial class RatingsVM + public partial class RatingsVM : ObservableObject { #region Fields - [ObservableProperty] - private int nbBooksWritten; - - [ObservableProperty] - private float? average; - - [ObservableProperty] - private int count; + private int nbBooksWritten { get; set; } #endregion #region Properties + public float? Average { get; set; } + + public int Count { get; set; } + + public int NbBooksWritten + { + get => nbBooksWritten; + set + { + nbBooksWritten = value; + } + } + #endregion #region Constructor @@ -38,4 +43,4 @@ namespace ViewModels #endregion } -} +} \ No newline at end of file