From c2f133206bba8bec51ff87f15cecb32a42e3e53f Mon Sep 17 00:00:00 2001 From: Lucas Evard Date: Sun, 22 Oct 2023 15:16:23 +0200 Subject: [PATCH] :sparkles: Gestion de la suppresion de livres --- sources/Model/Manager.cs | 7 +++- .../{ScanMenuVM.cs => ButtonsVM.cs} | 12 +++++- .../PocketBook/Applicative_VM/DetailPageVM.cs | 20 ++++++++-- .../PocketBook/Applicative_VM/PaginationVM.cs | 16 ++++---- .../PocketBook/Applicative_VM/TousPageVM.cs | 2 - sources/PocketBook/Pages/BookDetail.xaml | 2 +- sources/PocketBook/Pages/MainPage.xaml | 1 - sources/ViewModel/ManagerVM.cs | 37 +++++++++++++++---- 8 files changed, 72 insertions(+), 25 deletions(-) rename sources/PocketBook/Applicative_VM/{ScanMenuVM.cs => ButtonsVM.cs} (63%) diff --git a/sources/Model/Manager.cs b/sources/Model/Manager.cs index 32bedf6..308ef15 100644 --- a/sources/Model/Manager.cs +++ b/sources/Model/Manager.cs @@ -57,7 +57,12 @@ namespace Model return UserLibraryManager.AddBook(id); } - public async Task GetBookByIdFromCollection(string id) + public Task RemoveBookToCollection(Book book) + { + return UserLibraryManager.RemoveBook(book); + } + + public async Task GetBookByIdFromCollection(string id) => await UserLibraryManager.GetBookById(id); diff --git a/sources/PocketBook/Applicative_VM/ScanMenuVM.cs b/sources/PocketBook/Applicative_VM/ButtonsVM.cs similarity index 63% rename from sources/PocketBook/Applicative_VM/ScanMenuVM.cs rename to sources/PocketBook/Applicative_VM/ButtonsVM.cs index 262deb7..303bea1 100644 --- a/sources/PocketBook/Applicative_VM/ScanMenuVM.cs +++ b/sources/PocketBook/Applicative_VM/ButtonsVM.cs @@ -13,6 +13,7 @@ namespace PocketBook.Applicative_VM public ICommand ShowScanMenu { get; private set; } public ICommand ModalISBN { get; private set; } public ManagerVM Manager { get; private set; } + public PaginationVM PaginationVM { get; private set; } private bool scanMenuIsVisible; @@ -22,9 +23,10 @@ namespace PocketBook.Applicative_VM set => SetProperty(ref scanMenuIsVisible,value); } - public ButtonsVM(ManagerVM manager) + public ButtonsVM(ManagerVM manager, PaginationVM paginationVM) { Manager = manager; + PaginationVM = paginationVM; ShowScanMenu = new Command(() => { ScanMenuIsVisible = !ScanMenuIsVisible; @@ -32,7 +34,13 @@ namespace PocketBook.Applicative_VM ModalISBN = new Command( async () => { 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); } + if (isbn != null) { + Manager.AddBookByISBN(isbn); + ((Command)PaginationVM.NormalIncreasePage).ChangeCanExecute(); + ((Command)PaginationVM.NormalDecreasePage).ChangeCanExecute(); + ((Command)PaginationVM.LaterIncreasePage).ChangeCanExecute(); + ((Command)PaginationVM.LaterDecreasePage).ChangeCanExecute(); + } } ); ; } diff --git a/sources/PocketBook/Applicative_VM/DetailPageVM.cs b/sources/PocketBook/Applicative_VM/DetailPageVM.cs index 404ea72..176727c 100644 --- a/sources/PocketBook/Applicative_VM/DetailPageVM.cs +++ b/sources/PocketBook/Applicative_VM/DetailPageVM.cs @@ -9,12 +9,16 @@ namespace PocketBook.Applicative_VM public class DetailPageVM : BaseViewModel { public ManagerVM Manager { get; private set; } - public ICommand ChangeStatus { get; private set; } + public PaginationVM PaginationVM { get; private set; } + + public ICommand ChangeStatus { get; private set; } public ICommand AddReadLaterList { get; private set; } + public ICommand MoveBook { get; private set; } - public DetailPageVM(ManagerVM manager) + public DetailPageVM(ManagerVM manager, PaginationVM paginationVM) { Manager = manager; + PaginationVM = paginationVM; ChangeStatus = new Command(async o => { @@ -28,7 +32,17 @@ namespace PocketBook.Applicative_VM Manager.ChangeStatut("À lire"); _ = Toast.Make("Ajout dans la liste de livre à lire plus tard", ToastDuration.Short, 14).Show(new CancellationTokenSource().Token); }); - + 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"); + 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(); + }); } } } diff --git a/sources/PocketBook/Applicative_VM/PaginationVM.cs b/sources/PocketBook/Applicative_VM/PaginationVM.cs index 5dceab1..5c64290 100644 --- a/sources/PocketBook/Applicative_VM/PaginationVM.cs +++ b/sources/PocketBook/Applicative_VM/PaginationVM.cs @@ -24,7 +24,7 @@ namespace PocketBook.Applicative_VM ((Command)NormalDecreasePage).ChangeCanExecute(); }, canExecute: () => { - return Manager.NbBook / 5 > Manager.Index; + return Manager.NbBook / Manager.NBLIVREPARPAGE > Manager.Index; }); NormalDecreasePage = new Command(execute:() => @@ -43,17 +43,17 @@ namespace PocketBook.Applicative_VM LaterIncreasePage = new Command(execute: () => { - if(Manager.NbBookLater / 5 > Manager.IndexLater) { - Manager.IndexLater++; - Manager.LoadBooksFromManager(); - ((Command)LaterIncreasePage).ChangeCanExecute(); - ((Command)LaterDecreasePage).ChangeCanExecute(); - } + Manager.IndexLater++; + Manager.LoadBooksFromManager(); + ((Command)LaterIncreasePage).ChangeCanExecute(); + ((Command)LaterDecreasePage).ChangeCanExecute(); }, canExecute: () => { - return Manager.NbBookLater / 5 > Manager.IndexLater; + return Manager.NbBookLater / Manager.NBLIVREPARPAGE > Manager.IndexLater; }); + + LaterDecreasePage = new Command(execute: () => { Manager.IndexLater--; diff --git a/sources/PocketBook/Applicative_VM/TousPageVM.cs b/sources/PocketBook/Applicative_VM/TousPageVM.cs index 39fb56b..191d987 100644 --- a/sources/PocketBook/Applicative_VM/TousPageVM.cs +++ b/sources/PocketBook/Applicative_VM/TousPageVM.cs @@ -23,8 +23,6 @@ namespace PocketBook.Applicative_VM Manager = manager; PaginationVM = paginationVM; NavigateCommandBooks = navigation; - //Manager.LoadBooksFromManager(); - NavigateDetailPage = new Command(book => { Manager.SelectedBook = book; diff --git a/sources/PocketBook/Pages/BookDetail.xaml b/sources/PocketBook/Pages/BookDetail.xaml index 2d8d800..ca561d4 100644 --- a/sources/PocketBook/Pages/BookDetail.xaml +++ b/sources/PocketBook/Pages/BookDetail.xaml @@ -81,7 +81,7 @@ - + diff --git a/sources/PocketBook/Pages/MainPage.xaml b/sources/PocketBook/Pages/MainPage.xaml index 1f883ba..e3758c7 100644 --- a/sources/PocketBook/Pages/MainPage.xaml +++ b/sources/PocketBook/Pages/MainPage.xaml @@ -37,7 +37,6 @@ - diff --git a/sources/ViewModel/ManagerVM.cs b/sources/ViewModel/ManagerVM.cs index e540df1..af2fc64 100644 --- a/sources/ViewModel/ManagerVM.cs +++ b/sources/ViewModel/ManagerVM.cs @@ -1,13 +1,14 @@ namespace ViewModel; using System.Collections.ObjectModel; -using System.Diagnostics; using Model; using MyToolkitMVVM; -using static System.Runtime.InteropServices.JavaScript.JSType; public class ManagerVM : BaseViewModel { + + public int NBLIVREPARPAGE = 6; + public ReadOnlyObservableCollection Books { get; set; } private readonly ObservableCollection books = new ObservableCollection(); public RelayCommand LoadBooks { get; private set; } @@ -30,7 +31,7 @@ public class ManagerVM : BaseViewModel public async void LoadBooksFromManager() { - var result = await Model.GetBooksFromCollection(index, 5, ""); + var result = await Model.GetBooksFromCollection(index, NBLIVREPARPAGE, ""); NbBook = (int)result.count; books.Clear(); @@ -43,7 +44,7 @@ public class ManagerVM : BaseViewModel public async void LoadBooksReadLaterFromManager() { - var result = await Model.GetBooksFromCollection(indexLater, 5, ""); + var result = await Model.GetBooksFromCollection(indexLater, NBLIVREPARPAGE, ""); books.Clear(); nbBookLater = 0; foreach (Book book in result.books) @@ -74,7 +75,7 @@ public class ManagerVM : BaseViewModel { filters.Add(date); } - OnPropertyChanged(nameof(filters)); + Refresh(); } public async Task GetAuthorFromCollection() @@ -94,7 +95,7 @@ public class ManagerVM : BaseViewModel { filters.Add(author); } - OnPropertyChanged(nameof(filters)); + Refresh(); } public async Task GetMarkFromCollection() @@ -113,7 +114,7 @@ public class ManagerVM : BaseViewModel { filters.Add(mark); } - OnPropertyChanged(nameof(filters)); + Refresh(); } public async Task GetBooksFromAuthor(string author) @@ -185,6 +186,8 @@ public class ManagerVM : BaseViewModel { OnPropertyChanged(nameof(GroupedBooks)); OnPropertyChanged(nameof(GroupedLaterBooks)); + OnPropertyChanged(nameof(filters)); + OnPropertyChanged(nameof(books)); } public ManagerVM(Manager model) : base(model) @@ -204,6 +207,26 @@ public class ManagerVM : BaseViewModel } } + public async void RemoveSelectedBookToCollection() + { + var result= await Model.GetBookById(SelectedBook.Id); + NbBook--; + await Model.RemoveBookToCollection(result); + books.Remove(SelectedBook); + Refresh(); + } + + public void MoveBook(string action) + { + switch (action) + { + case "Mettre à la poubelle": + RemoveSelectedBookToCollection(); + break; + case "Déplacer en favoris": + break; + } + } public int Index {