diff --git a/LivreLand/View/DetailsLivreView.xaml b/LivreLand/View/DetailsLivreView.xaml index 386dba6..de63294 100644 --- a/LivreLand/View/DetailsLivreView.xaml +++ b/LivreLand/View/DetailsLivreView.xaml @@ -309,8 +309,8 @@ diff --git a/LivreLand/ViewModel/DetailsLivreVM.cs b/LivreLand/ViewModel/DetailsLivreVM.cs index 9bb2782..bb1fa0c 100644 --- a/LivreLand/ViewModel/DetailsLivreVM.cs +++ b/LivreLand/ViewModel/DetailsLivreVM.cs @@ -1,4 +1,5 @@ using CommunityToolkit.Maui.Alerts; +using Model; using PersonalMVVMToolkit; using System; using System.Collections.Generic; @@ -15,6 +16,7 @@ namespace LivreLand.ViewModel #region Fields private bool isPickerVisible = false; + private string addFavorisButtonText; #endregion @@ -38,10 +40,34 @@ namespace LivreLand.ViewModel } } } + + public string AddFavorisButtonText + { + get + { + Manager.GetFavoriteBooksCommand.Execute(null); + if (Manager.AllFavoriteBooks.Any(favoriteBook => favoriteBook.Id == Book.Id)) + { + return addFavorisButtonText = "Supprimer des favoris"; + } + else + { + return addFavorisButtonText = "Ajouter aux favoris"; + } + } + set + { + if (addFavorisButtonText != value) + { + addFavorisButtonText = value; + OnPropertyChanged(nameof(AddFavorisButtonText)); + } + } + } public ICommand ShowPickerCommand { get; private set; } - public ICommand AddBookToFavoritesCommand { get; private set; } + public ICommand AddRemoveBookToFavoritesCommand { get; private set; } public ICommand AddBookToReadListCommand { get; private set; } @@ -57,7 +83,7 @@ namespace LivreLand.ViewModel Navigator = navigatorVM; Book = bookVM; ShowPickerCommand = new RelayCommand(() => ShowPicker()); - AddBookToFavoritesCommand = new RelayCommand((bookVM) => AddBookToFavorites(bookVM)); + AddRemoveBookToFavoritesCommand = new RelayCommand((bookVM) => AddRemoveBookToFavorites(bookVM)); AddBookToReadListCommand = new RelayCommand((bookVM) => AddBookToReadList(bookVM)); RemoveBookCommand = new RelayCommand((bookVM) => RemoveBook(bookVM)); } @@ -72,14 +98,31 @@ namespace LivreLand.ViewModel IsPickerVisible = true; } - private async Task AddBookToFavorites(BookVM bookVM) + private async Task AddRemoveBookToFavorites(BookVM bookVM) { - Manager.AddToFavoritesCommand.Execute(bookVM); + Manager.CheckBookIsFavoriteCommand.Execute(bookVM); + if (Manager.IsFavorite == false) + { + Manager.AddToFavoritesCommand.Execute(bookVM); + AddFavorisButtonText = "Supprimer des favoris"; + OnPropertyChanged(nameof(AddFavorisButtonText)); - var toast = Toast.Make("Livre ajouté aux favoris !", CommunityToolkit.Maui.Core.ToastDuration.Short); - await toast.Show(); + var toast = Toast.Make("Livre ajouté aux favoris !", CommunityToolkit.Maui.Core.ToastDuration.Short); + await toast.Show(); - Navigator.NavigationCommand.Execute("/favoris"); + Navigator.NavigationCommand.Execute("/favoris"); + } + else + { + Manager.RemoveFromFavoritesCommand.Execute(bookVM); + AddFavorisButtonText = "Ajouter aux favoris"; + OnPropertyChanged(nameof(AddFavorisButtonText)); + + var toast = Toast.Make("Livre supprimé des favoris !", CommunityToolkit.Maui.Core.ToastDuration.Short); + await toast.Show(); + + Navigator.NavigationCommand.Execute("/favoris"); + } } private async Task AddBookToReadList(BookVM bookVM) diff --git a/LivreLand/ViewModel/TousVM.cs b/LivreLand/ViewModel/TousVM.cs index 9e995ab..2aff733 100644 --- a/LivreLand/ViewModel/TousVM.cs +++ b/LivreLand/ViewModel/TousVM.cs @@ -36,8 +36,11 @@ namespace LivreLand.ViewModel private void OnSelectionChanged(BookVM bookVM) { - var result = new DetailsLivreVM(Manager, Navigator, bookVM); - App.Current.MainPage.Navigation.PushAsync(new DetailsLivreView(result)); + if (bookVM != null) + { + var result = new DetailsLivreVM(Manager, Navigator, bookVM); + App.Current.MainPage.Navigation.PushAsync(new DetailsLivreView(result)); + } } #endregion diff --git a/ViewModels/ManagerVM.cs b/ViewModels/ManagerVM.cs index 5c623f6..c9ee959 100644 --- a/ViewModels/ManagerVM.cs +++ b/ViewModels/ManagerVM.cs @@ -36,6 +36,8 @@ namespace ViewModels private string givenFirstName; private string givenLastName; + private bool isFavorite; + #endregion #region Properties @@ -204,6 +206,19 @@ namespace ViewModels } } + public bool IsFavorite + { + get { return isFavorite; } + set + { + if (isFavorite != value) + { + isFavorite = value; + OnPropertyChanged(nameof(IsFavorite)); + } + } + } + public string SearchTitle { get; private set; } public int Index @@ -264,6 +279,8 @@ namespace ViewModels public ICommand RemoveFromFavoritesCommand { get; private set; } + public ICommand CheckBookIsFavoriteCommand { get; private set; } + public ICommand GetCurrentLoansCommand { get; private set; } public ICommand GetPastLoansCommand { get; private set; } @@ -302,6 +319,7 @@ namespace ViewModels GetFavoriteBooksCommand = new RelayCommand(() => GetFavoriteBooks()); AddToFavoritesCommand = new RelayCommand(bookVM => AddToFavorites(bookVM)); RemoveFromFavoritesCommand = new RelayCommand(bookVM => RemoveFromFavorites(bookVM)); + CheckBookIsFavoriteCommand = new RelayCommand(bookVM => CheckBookIsFavorite(bookVM)); GetCurrentLoansCommand = new RelayCommand(() => GetCurrentLoans()); GetPastLoansCommand = new RelayCommand(() => GetPastLoans()); GetCurrentBorrowingsCommand = new RelayCommand(() => GetCurrentBorrowings()); @@ -556,6 +574,21 @@ namespace ViewModels await GetFavoriteBooks(); } + private async Task CheckBookIsFavorite(BookVM bookVM) + { + await GetFavoriteBooks(); + if (AllFavoriteBooks.Any(favoriteBook => favoriteBook.Id == bookVM.Id)) + { + IsFavorite = true; + OnPropertyChanged(nameof(IsFavorite)); + } + else + { + IsFavorite = false; + OnPropertyChanged(nameof(IsFavorite)); + } + } + private async Task GetCurrentLoans() { var result = await Model.GetCurrentLoans(0, 20);