From bb3aabf408997378ba3cc610bdbc2952b1cb5023 Mon Sep 17 00:00:00 2001 From: Lucas Evard Date: Sun, 22 Oct 2023 17:18:42 +0200 Subject: [PATCH] :sparkles: Gestion des favoris --- sources/Model/IUserLibraryManager.cs | 4 +- sources/Model/Manager.cs | 16 +++++ sources/PocketBook/AppShell.xaml.cs | 1 - .../PocketBook/Applicative_VM/ButtonsVM.cs | 5 +- .../PocketBook/Applicative_VM/DetailPageVM.cs | 20 ++++-- .../PocketBook/Applicative_VM/LaterPageVM.cs | 11 ++- sources/PocketBook/Applicative_VM/LikeVM.cs | 32 +++++++++ .../PocketBook/Applicative_VM/PaginationVM.cs | 70 ++++++++++--------- .../PocketBook/Applicative_VM/TousPageVM.cs | 7 +- sources/PocketBook/MauiProgram.cs | 3 +- sources/PocketBook/Pages/LikePage.xaml | 40 ----------- sources/PocketBook/Pages/LikePage.xaml.cs | 16 ----- sources/PocketBook/Pages/MainPage.xaml | 4 +- sources/PocketBook/Pages/MainPage.xaml.cs | 7 +- sources/PocketBook/PocketBook.csproj | 3 - sources/Stub/UserLibraryStub.cs | 7 ++ sources/ViewModel/ManagerVM.cs | 68 ++++++++++-------- 17 files changed, 172 insertions(+), 142 deletions(-) create mode 100644 sources/PocketBook/Applicative_VM/LikeVM.cs delete mode 100644 sources/PocketBook/Pages/LikePage.xaml delete mode 100644 sources/PocketBook/Pages/LikePage.xaml.cs diff --git a/sources/Model/IUserLibraryManager.cs b/sources/Model/IUserLibraryManager.cs index 3e05af1..d47bfc6 100644 --- a/sources/Model/IUserLibraryManager.cs +++ b/sources/Model/IUserLibraryManager.cs @@ -21,8 +21,8 @@ namespace Model Task AddContact(Contact contact); Task RemoveContact(Contact contact); - - Task LendBook(Book book, Contact contact, DateTime? loanDate); + Task>> GetFavoritesBooks(int index, int count); + Task LendBook(Book book, Contact contact, DateTime? loanDate); Task GetBackBook(Book book, DateTime? returnedDate); Task BorrowBook(Book book, Contact owner, DateTime? borrowedDate); Task GiveBackBook(Book book, DateTime? returnedDate); diff --git a/sources/Model/Manager.cs b/sources/Model/Manager.cs index 308ef15..9d272a5 100644 --- a/sources/Model/Manager.cs +++ b/sources/Model/Manager.cs @@ -62,6 +62,22 @@ namespace Model return UserLibraryManager.RemoveBook(book); } + public Task AddBookInFavoriteList(Book book) + { + return UserLibraryManager.AddToFavorites(book); + } + + public Task DeleteBookInFavoriteList(Book book) + { + return UserLibraryManager.RemoveFromFavorites(book); + } + + public async Task<(long count, IEnumerable books)> GetAllBooksFromFavorite(int index, int count) + { + var result= await UserLibraryManager.GetFavoritesBooks(index, count); + return (result.Item1, result.Item2); + } + public async Task GetBookByIdFromCollection(string id) => await UserLibraryManager.GetBookById(id); diff --git a/sources/PocketBook/AppShell.xaml.cs b/sources/PocketBook/AppShell.xaml.cs index 689ee14..33e05ab 100644 --- a/sources/PocketBook/AppShell.xaml.cs +++ b/sources/PocketBook/AppShell.xaml.cs @@ -8,7 +8,6 @@ public partial class AppShell : Shell Routing.RegisterRoute("StatutPage", typeof(StatutPage)); Routing.RegisterRoute("SharePage", typeof(SharePage)); Routing.RegisterRoute("LaterPage", typeof(LaterPage)); - Routing.RegisterRoute("LikePage", typeof(LikePage)); Routing.RegisterRoute("Filter", typeof(Filter)); Routing.RegisterRoute("BookDetail", typeof(BookDetail)); InitializeComponent(); diff --git a/sources/PocketBook/Applicative_VM/ButtonsVM.cs b/sources/PocketBook/Applicative_VM/ButtonsVM.cs index 303bea1..3e6ad4f 100644 --- a/sources/PocketBook/Applicative_VM/ButtonsVM.cs +++ b/sources/PocketBook/Applicative_VM/ButtonsVM.cs @@ -36,10 +36,7 @@ namespace PocketBook.Applicative_VM string isbn = await App.Current.MainPage.DisplayPromptAsync("Ajout d'un livre par ISBN", "Entrez l'ISBN présent sur votre livre pour l'ajouter à votre librarie"); if (isbn != null) { Manager.AddBookByISBN(isbn); - ((Command)PaginationVM.NormalIncreasePage).ChangeCanExecute(); - ((Command)PaginationVM.NormalDecreasePage).ChangeCanExecute(); - ((Command)PaginationVM.LaterIncreasePage).ChangeCanExecute(); - ((Command)PaginationVM.LaterDecreasePage).ChangeCanExecute(); + PaginationVM.Refresh(); } } ); ; diff --git a/sources/PocketBook/Applicative_VM/DetailPageVM.cs b/sources/PocketBook/Applicative_VM/DetailPageVM.cs index 176727c..e4c0105 100644 --- a/sources/PocketBook/Applicative_VM/DetailPageVM.cs +++ b/sources/PocketBook/Applicative_VM/DetailPageVM.cs @@ -34,14 +34,20 @@ namespace PocketBook.Applicative_VM }); MoveBook = new Command(async o => { - string action = await App.Current.MainPage.DisplayActionSheet("Selectionne où tu veux déplacer ce livre", "Retour", "Mettre à la poubelle", "Déplacer en favoris"); + string action; + if (PaginationVM.Page == "Like") + { + action = await App.Current.MainPage.DisplayActionSheet("Selectionne où tu veux déplacer ce livre", "Retour", "Mettre à la poubelle", "Supprimer des favoris"); + } + else action = await App.Current.MainPage.DisplayActionSheet("Selectionne où tu veux déplacer ce livre", "Retour", "Mettre à la poubelle", "Déplacer en favoris"); + Manager.MoveBook(action); - _ = Toast.Make("Déplacement du livre en => " + action, ToastDuration.Short, 14).Show(new CancellationTokenSource().Token); - ((Command)PaginationVM.NormalIncreasePage).ChangeCanExecute(); - ((Command)PaginationVM.NormalDecreasePage).ChangeCanExecute(); - ((Command)PaginationVM.LaterIncreasePage).ChangeCanExecute(); - ((Command)PaginationVM.LaterDecreasePage).ChangeCanExecute(); - await App.Current.MainPage.Navigation.PopAsync(); + _ = Toast.Make("Confirmation pour l'action: " + action, ToastDuration.Short, 14).Show(new CancellationTokenSource().Token); + if (action == "Mettre à la poubelle" || action== "Supprimer des favoris") + { + PaginationVM.Refresh(); + await App.Current.MainPage.Navigation.PopAsync(); + } }); } } diff --git a/sources/PocketBook/Applicative_VM/LaterPageVM.cs b/sources/PocketBook/Applicative_VM/LaterPageVM.cs index c919e03..a8afb4e 100644 --- a/sources/PocketBook/Applicative_VM/LaterPageVM.cs +++ b/sources/PocketBook/Applicative_VM/LaterPageVM.cs @@ -8,6 +8,8 @@ namespace PocketBook.Applicative_VM public class LaterPageVM { public ICommand NavigateDetailPage { get; private set; } + public ICommand NavigateAndLoadData { get; private set; } + public NavigatorVM NavigateCommandBooks { get; private set; } public ButtonsVM ButtonsVM { get; private set; } public ManagerVM Manager { get; private set; } @@ -20,12 +22,19 @@ namespace PocketBook.Applicative_VM Manager = manager; PaginationVM = paginationVM; NavigateCommandBooks = navigation; - Manager.LoadBooksReadLaterFromManager(); NavigateDetailPage = new Command(book => { Manager.SelectedBook = book; NavigateCommandBooks.Navigateto.Execute("BookDetail"); }); + NavigateAndLoadData = new Command(async o => + { + Manager.Index = 0; + PaginationVM.Page = "Later"; + await Manager.LoadBooksReadLaterFromManager(); + PaginationVM.Refresh(); + NavigateCommandBooks.Navigateto.Execute("TousPage"); + }); } } } diff --git a/sources/PocketBook/Applicative_VM/LikeVM.cs b/sources/PocketBook/Applicative_VM/LikeVM.cs new file mode 100644 index 0000000..ee344d9 --- /dev/null +++ b/sources/PocketBook/Applicative_VM/LikeVM.cs @@ -0,0 +1,32 @@ +using System; +using System.Windows.Input; +using ViewModel; + +namespace PocketBook.Applicative_VM +{ + public class LikeVM + { + public ManagerVM Manager { get; private set; } + public NavigatorVM NavigatorVM { get; private set; } + public PaginationVM PaginationVM { get; private set; } + + public ICommand NavigateAndLoad { get; private set; } + + public LikeVM(ManagerVM manager, NavigatorVM navigatorVM,PaginationVM paginationVM) + { + PaginationVM = paginationVM; + Manager = manager; + NavigatorVM = navigatorVM; + + NavigateAndLoad = new Command( async o => + { + Manager.Index = 0; + PaginationVM.Page = "Like"; + await Manager.GetFavoriteBooks(); + PaginationVM.Refresh(); + NavigatorVM.Navigateto.Execute("TousPage"); + }); + } + } +} + diff --git a/sources/PocketBook/Applicative_VM/PaginationVM.cs b/sources/PocketBook/Applicative_VM/PaginationVM.cs index 5c64290..d174389 100644 --- a/sources/PocketBook/Applicative_VM/PaginationVM.cs +++ b/sources/PocketBook/Applicative_VM/PaginationVM.cs @@ -10,60 +10,62 @@ namespace PocketBook.Applicative_VM public ManagerVM Manager { get; private set; } public ICommand NormalIncreasePage { get; private set; } public ICommand NormalDecreasePage { get; private set; } - public ICommand LaterIncreasePage { get; private set; } - public ICommand LaterDecreasePage { get; private set; } + public string Page; + + + public void Refresh() + { + ((Command)NormalIncreasePage).ChangeCanExecute(); + ((Command)NormalDecreasePage).ChangeCanExecute(); + } public PaginationVM(ManagerVM managerVM) { Manager = managerVM; - NormalIncreasePage = new Command(execute: () => + NormalIncreasePage = new Command(execute:async () => { Manager.Index++; - Manager.LoadBooksFromManager(); - ((Command)NormalIncreasePage).ChangeCanExecute(); - ((Command)NormalDecreasePage).ChangeCanExecute(); + switch (Page) + { + case "TousPage": + await Manager.LoadBooksFromManager(); + break; + case "Later": + await Manager.LoadBooksReadLaterFromManager(); + break; + case "Like": + await Manager.GetFavoriteBooks(); + break; + } + Refresh(); }, canExecute: () => { return Manager.NbBook / Manager.NBLIVREPARPAGE > Manager.Index; }); - NormalDecreasePage = new Command(execute:() => + NormalDecreasePage = new Command(execute:async() => { if (Manager.Index > 0) { Manager.Index--; - Manager.LoadBooksFromManager(); - ((Command)NormalIncreasePage).ChangeCanExecute(); - ((Command)NormalDecreasePage).ChangeCanExecute(); + switch (Page) + { + case "TousPage": + await Manager.LoadBooksFromManager(); + break; + case "Later": + await Manager.LoadBooksReadLaterFromManager(); + break; + case "Like": + await Manager.GetFavoriteBooks(); + break; + } + Refresh(); } }, canExecute: () => { return Manager.Index > 0; }); - - LaterIncreasePage = new Command(execute: () => - { - Manager.IndexLater++; - Manager.LoadBooksFromManager(); - ((Command)LaterIncreasePage).ChangeCanExecute(); - ((Command)LaterDecreasePage).ChangeCanExecute(); - }, - canExecute: () => - { - return Manager.NbBookLater / Manager.NBLIVREPARPAGE > Manager.IndexLater; - }); - - - LaterDecreasePage = new Command(execute: () => - { - Manager.IndexLater--; - Manager.LoadBooksFromManager(); - ((Command)LaterIncreasePage).ChangeCanExecute(); - ((Command)LaterDecreasePage).ChangeCanExecute(); - }, - canExecute: () => { - return Manager.IndexLater > 0; - }); } } } diff --git a/sources/PocketBook/Applicative_VM/TousPageVM.cs b/sources/PocketBook/Applicative_VM/TousPageVM.cs index 191d987..25f1033 100644 --- a/sources/PocketBook/Applicative_VM/TousPageVM.cs +++ b/sources/PocketBook/Applicative_VM/TousPageVM.cs @@ -28,9 +28,12 @@ namespace PocketBook.Applicative_VM Manager.SelectedBook = book; NavigateCommandBooks.Navigateto.Execute("BookDetail"); }); - NavigateAndLoadData = new Command(o => + NavigateAndLoadData = new Command(async o => { - Manager.LoadBooksFromManager(); + Manager.Index = 0; + PaginationVM.Page = "TousPage"; + await Manager.LoadBooksFromManager(); + PaginationVM.Refresh(); NavigateCommandBooks.Navigateto.Execute("TousPage"); }); } diff --git a/sources/PocketBook/MauiProgram.cs b/sources/PocketBook/MauiProgram.cs index 2e64d92..96e5962 100644 --- a/sources/PocketBook/MauiProgram.cs +++ b/sources/PocketBook/MauiProgram.cs @@ -54,13 +54,14 @@ public static class MauiProgram .AddSingleton() .AddSingleton() .AddSingleton() - .AddSingleton() .AddSingleton() .AddSingleton() .AddSingleton() .AddSingleton() .AddSingleton() .AddSingleton() + .AddSingleton() + ; #if DEBUG builder.Logging.AddDebug(); diff --git a/sources/PocketBook/Pages/LikePage.xaml b/sources/PocketBook/Pages/LikePage.xaml deleted file mode 100644 index fbb8f48..0000000 --- a/sources/PocketBook/Pages/LikePage.xaml +++ /dev/null @@ -1,40 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/sources/PocketBook/Pages/LikePage.xaml.cs b/sources/PocketBook/Pages/LikePage.xaml.cs deleted file mode 100644 index 7e0b995..0000000 --- a/sources/PocketBook/Pages/LikePage.xaml.cs +++ /dev/null @@ -1,16 +0,0 @@ -using CommunityToolkit.Maui.Views; -using PocketBook.Applicative_VM; - -namespace PocketBook; - -public partial class LikePage : ContentPage -{ - public NavigatorVM NavigateCommandBooks { get; private set; } - - public LikePage(NavigatorVM navigator) - { - NavigateCommandBooks = navigator; - InitializeComponent(); - } - -} \ No newline at end of file diff --git a/sources/PocketBook/Pages/MainPage.xaml b/sources/PocketBook/Pages/MainPage.xaml index e3758c7..4bf38f2 100644 --- a/sources/PocketBook/Pages/MainPage.xaml +++ b/sources/PocketBook/Pages/MainPage.xaml @@ -43,14 +43,14 @@ - - diff --git a/sources/PocketBook/Pages/MainPage.xaml.cs b/sources/PocketBook/Pages/MainPage.xaml.cs index 0de43d1..a05b283 100644 --- a/sources/PocketBook/Pages/MainPage.xaml.cs +++ b/sources/PocketBook/Pages/MainPage.xaml.cs @@ -8,8 +8,13 @@ public partial class MainPage : ContentPage public NavigatorVM NavigateCommandBooks { get; private set; } public NavigatorAndFilterVM NavigatorAndFilterVM { get; private set; } public TousPageVM TousPageVM { get; private set; } - public MainPage(NavigatorVM navigation, NavigatorAndFilterVM navigatorAndFilterVM, TousPageVM tousPageVM) + public LikeVM LikeVM { get; private set; } + public LaterPageVM LaterPageVM { get; private set; } + + public MainPage(NavigatorVM navigation, NavigatorAndFilterVM navigatorAndFilterVM, TousPageVM tousPageVM,LikeVM likeVM,LaterPageVM laterPageVM) { + LikeVM = likeVM; + LaterPageVM = laterPageVM; TousPageVM = tousPageVM; NavigatorAndFilterVM = navigatorAndFilterVM; NavigateCommandBooks = navigation; diff --git a/sources/PocketBook/PocketBook.csproj b/sources/PocketBook/PocketBook.csproj index 0316738..8169742 100644 --- a/sources/PocketBook/PocketBook.csproj +++ b/sources/PocketBook/PocketBook.csproj @@ -77,9 +77,6 @@ MSBuild:Compile - - MSBuild:Compile - MSBuild:Compile diff --git a/sources/Stub/UserLibraryStub.cs b/sources/Stub/UserLibraryStub.cs index 7ea3f98..dcfd7d9 100644 --- a/sources/Stub/UserLibraryStub.cs +++ b/sources/Stub/UserLibraryStub.cs @@ -4,6 +4,7 @@ using Model; using System.Linq; using LibraryDTO; using System.Xml.Linq; +using System.Reflection; namespace StubLib { @@ -416,6 +417,12 @@ namespace StubLib { return Task.FromResult(Tuple.Create(Contacts.LongCount(), Contacts.Skip(index*count).Take(count))); } + + public Task>> GetFavoritesBooks(int index, int count) + { + return Task.FromResult(Tuple.Create(Favorites.LongCount(), Favorites.Skip(index * count).Take(count))); + } + } } diff --git a/sources/ViewModel/ManagerVM.cs b/sources/ViewModel/ManagerVM.cs index af2fc64..791e397 100644 --- a/sources/ViewModel/ManagerVM.cs +++ b/sources/ViewModel/ManagerVM.cs @@ -29,7 +29,7 @@ public class ManagerVM : BaseViewModel :this(new Manager(libraryManager,userLibraryManager)) {} - public async void LoadBooksFromManager() + public async Task LoadBooksFromManager() { var result = await Model.GetBooksFromCollection(index, NBLIVREPARPAGE, ""); NbBook = (int)result.count; @@ -42,17 +42,17 @@ public class ManagerVM : BaseViewModel Refresh(); } - public async void LoadBooksReadLaterFromManager() + public async Task LoadBooksReadLaterFromManager() { - var result = await Model.GetBooksFromCollection(indexLater, NBLIVREPARPAGE, ""); + var result = await Model.GetBooksFromCollection(index, NBLIVREPARPAGE, ""); books.Clear(); - nbBookLater = 0; + nbBook = 0; foreach (Book book in result.books) { if (book.Status == Status.ToBeRead) { books.Add(new BookVM(book)); - nbBookLater++; + nbBook++; } } Refresh(); @@ -192,17 +192,16 @@ public class ManagerVM : BaseViewModel public ManagerVM(Manager model) : base(model) { - LoadBooks = new RelayCommand(o => LoadBooksFromManager()); Books = new ReadOnlyObservableCollection(books); Filters= new ReadOnlyObservableCollection>(filters); } - public void AddBookByISBN(string isbn) + public async void AddBookByISBN(string isbn) { var result= Model.GetBookByISBN(isbn).Result; if (result != null) { - Model.AddBookToCollection(result.Id); - LoadBooksFromManager(); + await Model.AddBookToCollection(result.Id); + await LoadBooksFromManager(); Refresh(); } } @@ -216,6 +215,34 @@ public class ManagerVM : BaseViewModel Refresh(); } + public async void AddFavoriteBook() + { + var result = await Model.GetBookById(SelectedBook.Id); + await Model.AddBookInFavoriteList(result); + } + + public async void RemoveFavoriteBook() + { + var result = await Model.GetBookById(SelectedBook.Id); + NbBook--; + await Model.DeleteBookInFavoriteList(result); + books.Remove(SelectedBook); + Refresh(); + } + + public async Task GetFavoriteBooks() + { + var result = await Model.GetAllBooksFromFavorite(index, NBLIVREPARPAGE); + NbBook = (int)result.count; + books.Clear(); + + foreach (Book book in result.books) + { + books.Add(new BookVM(book)); + } + Refresh(); + } + public void MoveBook(string action) { switch (action) @@ -224,6 +251,10 @@ public class ManagerVM : BaseViewModel RemoveSelectedBookToCollection(); break; case "Déplacer en favoris": + AddFavoriteBook(); + break; + case "Supprimer des favoris": + RemoveFavoriteBook(); break; } } @@ -256,24 +287,5 @@ public class ManagerVM : BaseViewModel private int nbBook; - public int NbBookLater - { - get => nbBookLater; - set => SetProperty(ref nbBookLater, value); - } - - private int nbBookLater; - - public int IndexLater - { - get => indexLater; - set - { - SetProperty(ref indexLater, value); - } - } - - private int indexLater; - }