ADD : Page contacts with lend to Contact (page add Contact ToDo) + had to find solution to dont use OnAppearing when open TousView

commands-19-09
Lou BRODA 1 year ago
parent 3f52cb2e78
commit 81ace28547

@ -14,6 +14,7 @@ public partial class AppShell : Shell
Routing.RegisterRoute("library/auteur/", typeof(FiltrageAuteurView));
Routing.RegisterRoute("library/date/", typeof(FiltrageDateView));
Routing.RegisterRoute("library/note/", typeof(FiltrageNoteView));
Routing.RegisterRoute("library/contacts/", typeof(ContactsView));
Routing.RegisterRoute("library/tous/details/", typeof(DetailsLivreView));
InitializeComponent();
}

@ -71,6 +71,9 @@
<MauiXaml Update="View\BibliothequeView.xaml">
<Generator>MSBuild:Compile</Generator>
</MauiXaml>
<MauiXaml Update="View\ContactsView.xaml">
<Generator>MSBuild:Compile</Generator>
</MauiXaml>
<MauiXaml Update="View\ContentViews\AlphabetVerticalMenuView.xaml">
<Generator>MSBuild:Compile</Generator>
</MauiXaml>

@ -33,6 +33,7 @@ public static class MauiProgram
.AddSingleton<StatutLectureView>()
.AddSingleton<EmpruntsPretsView>()
.AddSingleton<FavorisView>()
.AddSingleton<ContactsView>()
.AddSingleton<NavigatorVM>()
@ -50,7 +51,8 @@ public static class MauiProgram
.AddSingleton<ALirePlusTardVM>()
.AddSingleton<StatutLectureVM>()
.AddSingleton<EmpruntsPretsVM>()
.AddSingleton<FavorisVM>();
.AddSingleton<FavorisVM>()
.AddSingleton<ContactsVM>();
#if DEBUG
builder.Logging.AddDebug();

@ -0,0 +1,77 @@
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
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.ContactsView"
Title="ContactsView">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="auto"/>
<RowDefinition Height="10"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<view:HeaderPage HeaderTitle="Contacts"
HeaderBackButtonText="Mes livres"
HeaderPlusButtonVisible="True"
HeaderSwitchButtonVisible="True"
ButtonPlusTappedCommand="{Binding ContactsVM.Navigator.PopupHomePlusNavigationCommand}"
ButtonBackTappedCommand="{Binding ContactsVM.Navigator.PopupBackButtonNavigationCommand}"
Grid.Row="0"/>
<ScrollView Grid.Row="2">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="40"/>
<RowDefinition Height="10"/>
<RowDefinition Height="auto"/>
</Grid.RowDefinitions>
<Grid BackgroundColor="{AppThemeBinding Light={StaticResource HeaderGray}, Dark={StaticResource Gray900}}"
Grid.Row="0">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="10"/>
<ColumnDefinition Width="auto"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Label Text="Contacts"
VerticalOptions="Center"
Style="{StaticResource HeaderCollectionViewText}"
Grid.Column="1"/>
</Grid>
<CollectionView ItemsSource="{Binding ContactsVM.Manager.AllContacts}"
SelectedItem="{Binding ContactsVM.Manager.SelectedContact}"
SelectionChangedCommand="{Binding ContactsVM.Manager.LendBookCommand}"
SelectionChangedCommandParameter="{Binding ContactsVM.Manager.SelectedContact}"
SelectionMode="Single"
Grid.Row="2">
<CollectionView.ItemTemplate>
<DataTemplate x:DataType="viewModel:ContactVM">
<VerticalStackLayout>
<Grid HorizontalOptions="Center">
<Label>
<Label.Text>
<MultiBinding StringFormat="{}{0} {1}">
<Binding Path="FirstName" />
<Binding Path="LastName" />
</MultiBinding>
</Label.Text>
</Label>
</Grid>
<contentView:SeparatorEntireView/>
</VerticalStackLayout>
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>
</Grid>
</ScrollView>
<Grid HorizontalOptions="End"
VerticalOptions="Center"
Grid.RowSpan="3">
<contentView:AlphabetVerticalMenuView/>
</Grid>
</Grid>
</ContentPage>

@ -0,0 +1,34 @@
using LivreLand.ViewModel;
namespace LivreLand.View;
public partial class ContactsView : ContentPage
{
#region Properties
public ContactsVM ContactsVM { get; set; }
#endregion
#region Constructor
public ContactsView(ContactsVM contactsVM)
{
ContactsVM = contactsVM;
InitializeComponent();
BindingContext = this;
}
#endregion
#region Methods
protected override void OnAppearing()
{
base.OnAppearing();
ContactsVM.Manager.GetContactsCommand.Execute(null);
}
#endregion
}

@ -26,10 +26,10 @@ public partial class DetailsLivreButtonView : ContentView
set => SetValue(DetailsLivreButtonView.ButtonCommandProperty, value);
}
public static readonly BindableProperty ButtonCommandParameterProperty = BindableProperty.Create(nameof(ButtonCommandParameter), typeof(BookVM), typeof(DetailsLivreButtonView));
public BookVM ButtonCommandParameter
public static readonly BindableProperty ButtonCommandParameterProperty = BindableProperty.Create(nameof(ButtonCommandParameter), typeof(object), typeof(DetailsLivreButtonView));
public object ButtonCommandParameter
{
get => (BookVM)GetValue(DetailsLivreButtonView.ButtonCommandParameterProperty);
get => (object)GetValue(DetailsLivreButtonView.ButtonCommandParameterProperty);
set => SetValue(DetailsLivreButtonView.ButtonCommandParameterProperty, value);
}

@ -334,7 +334,9 @@
<contentView:SeparatorCutStartView/>
<contentView:DetailsLivreButtonView ButtonIcon="person.png"
ButtonTitle="Prêter le livre"/>
ButtonTitle="Prêter le livre"
ButtonCommand="{Binding DetailsLivreVM.Navigator.NavigationCommand}"
ButtonCommandParameter="/contacts"/>
<contentView:SeparatorCutStartView/>

@ -37,6 +37,7 @@
<RowDefinition Height="auto"/>
</Grid.RowDefinitions>
<CollectionView ItemsSource="{Binding TousVM.Manager.AllBooks}"
SelectedItem="{Binding TousVM.Manager.SelectedBook}"
SelectionMode="Single"
SelectionChanged="OnSelectionChanged"
Grid.Row="2">

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

@ -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 ContactsVM : BaseViewModel
{
#region Properties
public NavigatorVM Navigator { get; private set; }
public ManagerVM Manager { get; private set; }
#endregion
#region Constructor
public ContactsVM(NavigatorVM navigatorVM, ManagerVM managerVM)
{
Navigator = navigatorVM;
Manager = managerVM;
}
#endregion
}
}

@ -21,6 +21,8 @@ namespace LivreLand.ViewModel
public ManagerVM Manager { get; private set; }
public NavigatorVM Navigator { get; private set; }
public BookVM Book { get; private set; }
public bool IsPickerVisible
@ -42,9 +44,10 @@ namespace LivreLand.ViewModel
#region Constructor
public DetailsLivreVM(ManagerVM managerVM, BookVM bookVM)
public DetailsLivreVM(ManagerVM managerVM, NavigatorVM navigatorVM, BookVM bookVM)
{
Manager = managerVM;
Navigator = navigatorVM;
Book = bookVM;
ShowPickerCommand = new RelayCommand(() => ShowPicker());
}

@ -97,6 +97,12 @@ namespace Model
return Task.FromResult((result.Item1, result.Item2));
}
public Task<Contact> AddContact(Contact contact)
{
var result = UserLibraryManager.AddContact(contact).Result;
return Task.FromResult(result);
}
public Task<(long count, IEnumerable<Loan> loans)> GetCurrentLoans(int index, int count)
{
var result = UserLibraryManager.GetCurrentLoans(index, count).Result;

@ -21,9 +21,13 @@ namespace ViewModels
private readonly ObservableCollection<LoanVM> pastLoans = new ObservableCollection<LoanVM>();
private readonly ObservableCollection<BorrowingVM> pastBorrowings = new ObservableCollection<BorrowingVM>();
private readonly ObservableCollection<BorrowingVM> currentBorrowings = new ObservableCollection<BorrowingVM>();
private readonly ObservableCollection<ContactVM> contacts= new ObservableCollection<ContactVM>();
private int index;
private long nbBooks;
private AuthorVM selectedAuthor;
private ContactVM selectedContact;
#endregion
#region Properties
@ -78,7 +82,25 @@ namespace ViewModels
get => pastBorrowings;
}
public AuthorVM SelectedAuthor { get; private set; }
public ObservableCollection<ContactVM> AllContacts
{
get => contacts;
}
public BookVM SelectedBook { get; set; }
public AuthorVM SelectedAuthor
{
get { return selectedAuthor; }
set
{
if (selectedAuthor != value)
{
selectedAuthor = value;
OnPropertyChanged(nameof(SelectedAuthor));
}
}
}
public PublishDateVM SelectedDate { get; private set; }
@ -86,6 +108,19 @@ namespace ViewModels
public Status SelectedStatus { get; set; }
public ContactVM SelectedContact
{
get { return selectedContact; }
set
{
if (selectedContact != value)
{
selectedContact = value;
OnPropertyChanged(nameof(SelectedContact));
}
}
}
public string SearchTitle { get; private set; }
public int Index
@ -132,6 +167,8 @@ namespace ViewModels
public ICommand GetAllPublishDatesCommand { get; private set; }
public ICommand GetBooksByRatingCommand { get; private set; }
public ICommand GetAllRatingsCommand { get; private set; }
public ICommand GetToBeReadBooksCommand { get; private set; }
@ -150,7 +187,11 @@ namespace ViewModels
public ICommand GetPastBorrowingsCommand { get; private set; }
public ICommand GetBooksByRatingCommand { get; private set; }
public ICommand LendBookCommand { get; private set; }
public ICommand GetContactsCommand { get; private set; }
public ICommand AddContactCommand { get; private set; }
#endregion
@ -179,6 +220,9 @@ namespace ViewModels
GetPastLoansCommand = new RelayCommand(() => GetPastLoans());
GetCurrentBorrowingsCommand = new RelayCommand(() => GetCurrentBorrowings());
GetPastBorrowingsCommand = new RelayCommand(() => GetPastBorrowings());
LendBookCommand = new RelayCommand<ContactVM>((contactVM) => LendBook(contactVM));
GetContactsCommand = new RelayCommand(() => GetContacts());
AddContactCommand = new RelayCommand<ContactVM>((contactVM) => AddContact(contactVM));
//GetBooksByTitleCommand = new RelayCommand(() => AllBooks = model.GetBooksByTitle(SearchTitle, Index, Count).Result.books.Select(book => new BookVM(book)));
}
@ -464,10 +508,55 @@ namespace ViewModels
OnPropertyChanged(nameof(AllPastBorrowings));
}
//private async Task LendBook()
//{
// await Model.LendBook();
//}
private async Task LendBook(ContactVM contactVM)
{
var book = await Model.GetBookById(SelectedBook.Id);
Model.Contact contact = new Model.Contact();
var resultContacts = await Model.GetContacts(Index, Count);
var allContacts = resultContacts.contacts;
foreach (var c in allContacts)
{
if (c.Id == contactVM.Id)
{
contact = c;
}
}
if (contact != null)
{
await Model.LendBook(book, contact, null);
}
}
private async Task GetContacts()
{
var result = await Model.GetContacts(Index, Count);
IEnumerable<Model.Contact> someContacts = result.contacts;
someContacts = someContacts.OrderBy(c => c.FirstName);
contacts.Clear();
foreach (var c in someContacts.Select(c => new ContactVM(c)))
{
contacts.Add(c);
}
OnPropertyChanged(nameof(AllContacts));
}
private async Task AddContact(ContactVM contactVM)
{
Model.Contact contact = new Model.Contact();
var resultContacts = await Model.GetContacts(Index, Count);
var allContacts = resultContacts.contacts;
foreach (var c in allContacts)
{
if (c.Id == contactVM.Id)
{
contact = c;
}
}
if (contact != null)
{
await Model.AddContact(contact);
}
}
#endregion
}

Loading…
Cancel
Save