ADD : Mise à jour apparence bouton ajouter/supprimer des favoris

commands-19-09
Lou BRODA 1 year ago
parent cdd4a4aa86
commit 456b275a84

@ -309,8 +309,8 @@
<contentView:SeparatorCutStartView/> <contentView:SeparatorCutStartView/>
<contentView:DetailsLivreButtonView ButtonIcon="heart.png" <contentView:DetailsLivreButtonView ButtonIcon="heart.png"
ButtonTitle="Ajouter aux favoris" ButtonTitle="{Binding DetailsLivreVM.AddFavorisButtonText}"
ButtonCommand="{Binding DetailsLivreVM.AddBookToFavoritesCommand}" ButtonCommand="{Binding DetailsLivreVM.AddRemoveBookToFavoritesCommand}"
ButtonCommandParameter="{Binding DetailsLivreVM.Book}"/> ButtonCommandParameter="{Binding DetailsLivreVM.Book}"/>
<contentView:SeparatorCutStartView/> <contentView:SeparatorCutStartView/>

@ -1,4 +1,5 @@
using CommunityToolkit.Maui.Alerts; using CommunityToolkit.Maui.Alerts;
using Model;
using PersonalMVVMToolkit; using PersonalMVVMToolkit;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
@ -15,6 +16,7 @@ namespace LivreLand.ViewModel
#region Fields #region Fields
private bool isPickerVisible = false; private bool isPickerVisible = false;
private string addFavorisButtonText;
#endregion #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 ShowPickerCommand { get; private set; }
public ICommand AddBookToFavoritesCommand { get; private set; } public ICommand AddRemoveBookToFavoritesCommand { get; private set; }
public ICommand AddBookToReadListCommand { get; private set; } public ICommand AddBookToReadListCommand { get; private set; }
@ -57,7 +83,7 @@ namespace LivreLand.ViewModel
Navigator = navigatorVM; Navigator = navigatorVM;
Book = bookVM; Book = bookVM;
ShowPickerCommand = new RelayCommand(() => ShowPicker()); ShowPickerCommand = new RelayCommand(() => ShowPicker());
AddBookToFavoritesCommand = new RelayCommand<BookVM>((bookVM) => AddBookToFavorites(bookVM)); AddRemoveBookToFavoritesCommand = new RelayCommand<BookVM>((bookVM) => AddRemoveBookToFavorites(bookVM));
AddBookToReadListCommand = new RelayCommand<BookVM>((bookVM) => AddBookToReadList(bookVM)); AddBookToReadListCommand = new RelayCommand<BookVM>((bookVM) => AddBookToReadList(bookVM));
RemoveBookCommand = new RelayCommand<BookVM>((bookVM) => RemoveBook(bookVM)); RemoveBookCommand = new RelayCommand<BookVM>((bookVM) => RemoveBook(bookVM));
} }
@ -72,14 +98,31 @@ namespace LivreLand.ViewModel
IsPickerVisible = true; 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); var toast = Toast.Make("Livre ajouté aux favoris !", CommunityToolkit.Maui.Core.ToastDuration.Short);
await toast.Show(); 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) private async Task AddBookToReadList(BookVM bookVM)

@ -36,8 +36,11 @@ namespace LivreLand.ViewModel
private void OnSelectionChanged(BookVM bookVM) private void OnSelectionChanged(BookVM bookVM)
{ {
var result = new DetailsLivreVM(Manager, Navigator, bookVM); if (bookVM != null)
App.Current.MainPage.Navigation.PushAsync(new DetailsLivreView(result)); {
var result = new DetailsLivreVM(Manager, Navigator, bookVM);
App.Current.MainPage.Navigation.PushAsync(new DetailsLivreView(result));
}
} }
#endregion #endregion

@ -36,6 +36,8 @@ namespace ViewModels
private string givenFirstName; private string givenFirstName;
private string givenLastName; private string givenLastName;
private bool isFavorite;
#endregion #endregion
#region Properties #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 string SearchTitle { get; private set; }
public int Index public int Index
@ -264,6 +279,8 @@ namespace ViewModels
public ICommand RemoveFromFavoritesCommand { get; private set; } public ICommand RemoveFromFavoritesCommand { get; private set; }
public ICommand CheckBookIsFavoriteCommand { get; private set; }
public ICommand GetCurrentLoansCommand { get; private set; } public ICommand GetCurrentLoansCommand { get; private set; }
public ICommand GetPastLoansCommand { get; private set; } public ICommand GetPastLoansCommand { get; private set; }
@ -302,6 +319,7 @@ namespace ViewModels
GetFavoriteBooksCommand = new RelayCommand(() => GetFavoriteBooks()); GetFavoriteBooksCommand = new RelayCommand(() => GetFavoriteBooks());
AddToFavoritesCommand = new RelayCommand<BookVM>(bookVM => AddToFavorites(bookVM)); AddToFavoritesCommand = new RelayCommand<BookVM>(bookVM => AddToFavorites(bookVM));
RemoveFromFavoritesCommand = new RelayCommand<BookVM>(bookVM => RemoveFromFavorites(bookVM)); RemoveFromFavoritesCommand = new RelayCommand<BookVM>(bookVM => RemoveFromFavorites(bookVM));
CheckBookIsFavoriteCommand = new RelayCommand<BookVM>(bookVM => CheckBookIsFavorite(bookVM));
GetCurrentLoansCommand = new RelayCommand(() => GetCurrentLoans()); GetCurrentLoansCommand = new RelayCommand(() => GetCurrentLoans());
GetPastLoansCommand = new RelayCommand(() => GetPastLoans()); GetPastLoansCommand = new RelayCommand(() => GetPastLoans());
GetCurrentBorrowingsCommand = new RelayCommand(() => GetCurrentBorrowings()); GetCurrentBorrowingsCommand = new RelayCommand(() => GetCurrentBorrowings());
@ -556,6 +574,21 @@ namespace ViewModels
await GetFavoriteBooks(); 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() private async Task GetCurrentLoans()
{ {
var result = await Model.GetCurrentLoans(0, 20); var result = await Model.GetCurrentLoans(0, 20);

Loading…
Cancel
Save