ADD : page Favorite and AddToFavorites Method

commands-19-09
Lou BRODA 1 year ago
parent 3b2d64ed04
commit 5ef7f9f9d8

@ -32,6 +32,7 @@ public static class MauiProgram
.AddSingleton<ALirePlusTardView>()
.AddSingleton<StatutLectureView>()
.AddSingleton<EmpruntsPretsView>()
.AddSingleton<FavorisView>()
.AddSingleton<NavigatorVM>()
@ -48,7 +49,8 @@ public static class MauiProgram
.AddSingleton<FiltrageNoteVM>()
.AddSingleton<ALirePlusTardVM>()
.AddSingleton<StatutLectureVM>()
.AddSingleton<EmpruntsPretsVM>();
.AddSingleton<EmpruntsPretsVM>()
.AddSingleton<FavorisVM>();
#if DEBUG
builder.Logging.AddDebug();

@ -26,6 +26,9 @@
TextColor="Red"
VerticalOptions="Center"
Grid.Column="2"/>
<Grid.GestureRecognizers>
<TapGestureRecognizer Command="{Binding ButtonCommand}" CommandParameter="{Binding ButtonCommandParameter}"/>
</Grid.GestureRecognizers>
</Grid>
</ContentView>

@ -1,3 +1,6 @@
using System.Windows.Input;
using ViewModels;
namespace LivreLand.View.ContentViews;
public partial class DetailsLivreButtonView : ContentView
@ -16,6 +19,20 @@ public partial class DetailsLivreButtonView : ContentView
set => SetValue(DetailsLivreButtonView.ButtonIconProperty, value);
}
public static readonly BindableProperty ButtonCommandProperty = BindableProperty.Create(nameof(ButtonCommand), typeof(ICommand), typeof(DetailsLivreButtonView));
public ICommand ButtonCommand
{
get => (ICommand)GetValue(DetailsLivreButtonView.ButtonCommandProperty);
set => SetValue(DetailsLivreButtonView.ButtonCommandProperty, value);
}
public static readonly BindableProperty ButtonCommandParameterProperty = BindableProperty.Create(nameof(ButtonCommandParameter), typeof(BookVM), typeof(DetailsLivreButtonView));
public BookVM ButtonCommandParameter
{
get => (BookVM)GetValue(DetailsLivreButtonView.ButtonCommandParameterProperty);
set => SetValue(DetailsLivreButtonView.ButtonCommandParameterProperty, value);
}
public DetailsLivreButtonView()
{
InitializeComponent();

@ -278,6 +278,13 @@
<contentView:SeparatorCutStartView/>
<contentView:DetailsLivreButtonView ButtonIcon="heart.png"
ButtonTitle="Ajouter aux favoris"
ButtonCommand="{Binding DetailsLivreVM.Manager.AddToFavoritesCommand}"
ButtonCommandParameter="{Binding DetailsLivreVM.Book}"/>
<contentView:SeparatorCutStartView/>
<contentView:DetailsLivreButtonView ButtonIcon="folder.png"
ButtonTitle="Déplacer le livre"/>

@ -3,6 +3,7 @@
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:view="clr-namespace:LivreLand.View"
xmlns:contentView="clr-namespace:LivreLand.View.ContentViews"
xmlns:viewModel="clr-namespace:ViewModels;assembly=ViewModels"
x:Class="LivreLand.View.FavorisView"
Title="FavorisView">
@ -38,12 +39,12 @@
Style="{StaticResource HeaderCollectionViewText}"
Grid.Column="1"/>
</Grid>
<CollectionView ItemsSource="{Binding FavorisBooks}"
<CollectionView ItemsSource="{Binding FavorisVM.Manager.AllFavoriteBooks}"
SelectionMode="Single"
SelectionChanged="OnSelectionChanged"
Grid.Row="2">
<CollectionView.ItemTemplate>
<DataTemplate>
<DataTemplate x:DataType="viewModel:BookVM">
<VerticalStackLayout Margin="10"
Spacing="20">
<VisualStateManager.VisualStateGroups x:Name="SelectionStates">
@ -82,7 +83,7 @@
Stroke="{StaticResource Gray}"
StrokeShape="RoundRectangle 3"
StrokeThickness="3">
<Image Source="couverture_la_horde_du_contrevent.png"
<Image Source="{Binding ImageSmall}"
Aspect="AspectFill"
Grid.Column="0"
Grid.RowSpan="5"/>
@ -98,11 +99,11 @@
Style="{StaticResource MasterTitleBookText}"
Grid.Column="2"
Grid.Row="0"/>
<Label Text="{Binding Author}"
<Label Text="Author"
Style="{StaticResource MasterAuthorBookText}"
Grid.Column="2"
Grid.Row="1"/>
<Label Text="{Binding State}"
<Label Text="{Binding Status}"
Style="{StaticResource MasterStateBookText}"
Grid.Column="2"
Grid.Row="2"/>

@ -1,23 +1,40 @@
using LivreLand.Model;
using LivreLand.ViewModel;
namespace LivreLand.View;
public partial class FavorisView : ContentPage
{
public List<BookModel> FavorisBooks { get; set; } = new List<BookModel>()
{
new BookModel("La zone du dehors","Alain Damasio","Terminé", 0),
new BookModel("Le problème à trois corps","Cixin Liu","Terminé", 0)
};
#region Properties
public FavorisVM FavorisVM { get; set; }
#endregion
#region Constructor
public FavorisView()
public FavorisView(FavorisVM favorisVM)
{
BindingContext = this;
FavorisVM = favorisVM;
InitializeComponent();
BindingContext = this;
}
#endregion
#region Methods
void OnSelectionChanged(object sender, SelectionChangedEventArgs e)
{
//App.Current.MainPage.Navigation.PushAsync(new DetailsLivreView());
}
protected override void OnAppearing()
{
base.OnAppearing();
FavorisVM.Manager.GetFavoriteBooksCommand.Execute(null);
}
#endregion
}

@ -30,7 +30,7 @@ public partial class TousView : ContentPage
{
if (e.CurrentSelection.FirstOrDefault() is BookVM)
{
var result = new DetailsLivreVM(e.CurrentSelection.FirstOrDefault() as BookVM);
var result = new DetailsLivreVM(this.TousVM.Manager, e.CurrentSelection.FirstOrDefault() as BookVM);
App.Current.MainPage.Navigation.PushAsync(new DetailsLivreView(result));
}
}

@ -12,14 +12,17 @@ namespace LivreLand.ViewModel
{
#region Properties
public ManagerVM Manager { get; private set; }
public BookVM Book { get; private set; }
#endregion
#region Constructor
public DetailsLivreVM(BookVM bookVM)
public DetailsLivreVM(ManagerVM managerVM, BookVM bookVM)
{
Manager = managerVM;
Book = bookVM;
}

@ -0,0 +1,31 @@
using PersonalMVVMToolkit;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ViewModels;
namespace LivreLand.ViewModel
{
public class FavorisVM : BaseViewModel
{
#region Properties
public NavigatorVM Navigator { get; private set; }
public ManagerVM Manager { get; private set; }
#endregion
#region Constructor
public FavorisVM(NavigatorVM navigatorVM, ManagerVM managerVM)
{
Navigator = navigatorVM;
Manager = managerVM;
}
#endregion
}
}

@ -70,6 +70,22 @@ namespace Model
return Task.FromResult((result.Item1, result.Item2));
}
public Task<(long count, IEnumerable<Book> books)> GetFavoritesBooks(int index, int count)
{
var result = UserLibraryManager.GetFavoritesBooks(index, count).Result;
return Task.FromResult((result.Item1, result.Item2));
}
public Task<bool> AddToFavorites(string id)
{
return UserLibraryManager.AddToFavorites(id);
}
public Task<bool> RemoveFromFavorites(string id)
{
return UserLibraryManager.RemoveFromFavorites(id);
}
public Task<(long count, IEnumerable<Contact> contacts)> GetContacts(int index, int count)
{
var result = UserLibraryManager.GetContacts(index, count).Result;

@ -17,6 +17,12 @@ namespace ViewModels
#region Properties
public string Id
{
get => Model.Id;
set => SetProperty(Model.Id, value, v => Model.Id = value);
}
public string ISBN13
{
get => Model.ISBN13;

@ -16,6 +16,7 @@ namespace ViewModels
private readonly ObservableCollection<PublishDateVM> publishDates = new ObservableCollection<PublishDateVM>();
private readonly ObservableCollection<RatingsVM> ratings = new ObservableCollection<RatingsVM>();
private readonly ObservableCollection<BookVM> toBeReadBooks = new ObservableCollection<BookVM>();
private readonly ObservableCollection<BookVM> favoriteBooks = new ObservableCollection<BookVM>();
private readonly ObservableCollection<LoanVM> currentLoans = new ObservableCollection<LoanVM>();
private readonly ObservableCollection<LoanVM> pastLoans = new ObservableCollection<LoanVM>();
private readonly ObservableCollection<BorrowingVM> pastBorrowings = new ObservableCollection<BorrowingVM>();
@ -52,6 +53,11 @@ namespace ViewModels
get => toBeReadBooks;
}
public ObservableCollection<BookVM> AllFavoriteBooks
{
get => favoriteBooks;
}
public ObservableCollection<LoanVM> AllCurrentLoans
{
get => currentLoans;
@ -119,6 +125,10 @@ namespace ViewModels
public ICommand GetToBeReadBooksCommand { get; private set; }
public ICommand GetFavoriteBooksCommand { get; private set; }
public ICommand AddToFavoritesCommand { get; private set; }
public ICommand GetCurrentLoansCommand { get; private set; }
public ICommand GetPastLoansCommand { get; private set; }
@ -145,6 +155,8 @@ namespace ViewModels
GetBooksByRatingCommand = new RelayCommand(() => GetBooksByRating());
GetAllRatingsCommand = new RelayCommand(() => GetAllRatings());
GetToBeReadBooksCommand = new RelayCommand(() => GetToBeReadBooks());
GetFavoriteBooksCommand = new RelayCommand(() => GetFavoriteBooks());
AddToFavoritesCommand = new RelayCommand<BookVM>(bookVM => AddToFavorites(bookVM));
GetCurrentLoansCommand = new RelayCommand(() => GetCurrentLoans());
GetPastLoansCommand = new RelayCommand(() => GetPastLoans());
GetCurrentBorrowingsCommand = new RelayCommand(() => GetCurrentBorrowings());
@ -328,6 +340,27 @@ namespace ViewModels
toBeReadBooks.Add(b);
}
}
OnPropertyChanged(nameof(ToBeReadBooks));
}
private async Task GetFavoriteBooks()
{
var result = await Model.GetFavoritesBooks(Index, Count);
IEnumerable<Book> someBooks = result.books;
books.Clear();
favoriteBooks.Clear();
foreach (var b in someBooks.Select(b => new BookVM(b)))
{
favoriteBooks.Add(b);
}
OnPropertyChanged(nameof(AllFavoriteBooks));
}
private async Task AddToFavorites(BookVM bookVM)
{
var book = await Model.GetBookById(bookVM.Id);
await Model.AddToFavorites(book.Id);
await GetFavoriteBooks();
}
private async Task GetCurrentLoans()

Loading…
Cancel
Save