ADD : Ajout d'un contact (update view after created contact ToDo)

commands-19-09
Lou BRODA 1 year ago
parent 346256b1fc
commit 569d16e571

@ -26,7 +26,7 @@
<Grid.RowDefinitions> <Grid.RowDefinitions>
<RowDefinition Height="40"/> <RowDefinition Height="40"/>
<RowDefinition Height="10"/> <RowDefinition Height="10"/>
<RowDefinition Height="auto"/> <RowDefinition Height="*"/>
<RowDefinition Height="10"/> <RowDefinition Height="10"/>
<RowDefinition Height="auto"/> <RowDefinition Height="auto"/>
</Grid.RowDefinitions> </Grid.RowDefinitions>
@ -44,33 +44,30 @@
Grid.Column="1"/> Grid.Column="1"/>
</Grid> </Grid>
<Grid HorizontalOptions="Center" <Grid HorizontalOptions="Center"
Grid.Column="2"> Grid.Row="2">
<Grid.ColumnDefinitions> <Grid.RowDefinitions>
<ColumnDefinition Width="*"/> <RowDefinition Height="*"/>
<ColumnDefinition Width="5"/> <RowDefinition Height="5"/>
<ColumnDefinition Width="*"/> <RowDefinition Height="*"/>
</Grid.ColumnDefinitions> </Grid.RowDefinitions>
<HorizontalStackLayout Spacing="10" <Label Text="+ Add new contact"
Grid.Column="0"> Grid.Row="0"/>
<Image Source="plus.png"/> <VerticalStackLayout IsVisible="{Binding ContactsVM.DataFormIsVisible}"
<Label Text="Add new contact"/> Grid.Row="2">
</HorizontalStackLayout>
<!--<VerticalStackLayout IsVisible=""
Grid.Column="2">
<Grid FlowDirection="LeftToRight">
<Label Text="Prénom :" /> <Label Text="Prénom :" />
<Entry Placeholder="Saisissez le prénom"/> <Entry Placeholder="Saisissez le prénom"
Text="{Binding ContactsVM.Manager.GivenFirstName}"/>
<Label Text="Nom :"/> <Label Text="Nom :"/>
<Entry Placeholder="Saisissez le nom"/> <Entry Placeholder="Saisissez le nom"
Text="{Binding ContactsVM.Manager.GivenLastName}"/>
<Button Text="Enregistrer" <Button Text="Enregistrer"
Command="{Binding ContactsVM.Navigator.AddContactCommand}"/> BackgroundColor="Transparent"
</Grid> BorderColor="Black"
Command="{Binding ContactsVM.Manager.AddContactCommand}"/>
</VerticalStackLayout> </VerticalStackLayout>
<Grid.GestureRecognizers> <Grid.GestureRecognizers>
<TapGestureRecognizer Command="{Binding ContactsVM.}"/> <TapGestureRecognizer Command="{Binding ContactsVM.AddContactDataFormCommand}"/>
</Grid.GestureRecognizers>--> </Grid.GestureRecognizers>
</Grid> </Grid>
<CollectionView ItemsSource="{Binding ContactsVM.Manager.AllContacts}" <CollectionView ItemsSource="{Binding ContactsVM.Manager.AllContacts}"
SelectedItem="{Binding ContactsVM.Manager.SelectedContact}" SelectedItem="{Binding ContactsVM.Manager.SelectedContact}"

@ -4,18 +4,40 @@ using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Windows.Input;
using ViewModels; using ViewModels;
namespace LivreLand.ViewModel namespace LivreLand.ViewModel
{ {
public class ContactsVM : BaseViewModel public class ContactsVM : BaseViewModel
{ {
#region Fields
private bool dataFormIsVisible = false;
#endregion
#region Properties #region Properties
public NavigatorVM Navigator { get; private set; } public NavigatorVM Navigator { get; private set; }
public ManagerVM Manager { get; private set; } public ManagerVM Manager { get; private set; }
public bool DataFormIsVisible
{
get { return dataFormIsVisible; }
set
{
if (dataFormIsVisible != value)
{
dataFormIsVisible = value;
OnPropertyChanged(nameof(DataFormIsVisible));
}
}
}
public ICommand AddContactDataFormCommand { get; private set; }
#endregion #endregion
#region Constructor #region Constructor
@ -24,6 +46,16 @@ namespace LivreLand.ViewModel
{ {
Navigator = navigatorVM; Navigator = navigatorVM;
Manager = managerVM; Manager = managerVM;
AddContactDataFormCommand = new RelayCommand(() => AddContactDataForm());
}
#endregion
#region Methods
private void AddContactDataForm()
{
DataFormIsVisible = true;
} }
#endregion #endregion

@ -2,6 +2,7 @@
using PersonalMVVMToolkit; using PersonalMVVMToolkit;
using System.Collections.ObjectModel; using System.Collections.ObjectModel;
using System.Linq; using System.Linq;
using System.Text.RegularExpressions;
using System.Windows.Input; using System.Windows.Input;
namespace ViewModels namespace ViewModels
@ -28,6 +29,8 @@ namespace ViewModels
private AuthorVM selectedAuthor; private AuthorVM selectedAuthor;
private ContactVM selectedContact; private ContactVM selectedContact;
private string givenFirstName;
private string givenLastName;
#endregion #endregion
#region Properties #region Properties
@ -121,6 +124,32 @@ namespace ViewModels
} }
} }
public string GivenFirstName
{
get { return givenFirstName; }
set
{
if (givenFirstName != value)
{
givenFirstName = value;
OnPropertyChanged(nameof(GivenFirstName));
}
}
}
public string GivenLastName
{
get { return givenLastName; }
set
{
if (givenLastName != value)
{
givenLastName = value;
OnPropertyChanged(nameof(GivenLastName));
}
}
}
public string SearchTitle { get; private set; } public string SearchTitle { get; private set; }
public int Index public int Index
@ -222,7 +251,7 @@ namespace ViewModels
GetPastBorrowingsCommand = new RelayCommand(() => GetPastBorrowings()); GetPastBorrowingsCommand = new RelayCommand(() => GetPastBorrowings());
LendBookCommand = new RelayCommand<ContactVM>((contactVM) => LendBook(contactVM)); LendBookCommand = new RelayCommand<ContactVM>((contactVM) => LendBook(contactVM));
GetContactsCommand = new RelayCommand(() => GetContacts()); GetContactsCommand = new RelayCommand(() => GetContacts());
AddContactCommand = new RelayCommand<ContactVM>((contactVM) => AddContact(contactVM)); AddContactCommand = new RelayCommand(() => AddContact());
//GetBooksByTitleCommand = new RelayCommand(() => AllBooks = model.GetBooksByTitle(SearchTitle, Index, Count).Result.books.Select(book => new BookVM(book))); //GetBooksByTitleCommand = new RelayCommand(() => AllBooks = model.GetBooksByTitle(SearchTitle, Index, Count).Result.books.Select(book => new BookVM(book)));
} }
@ -540,22 +569,33 @@ namespace ViewModels
OnPropertyChanged(nameof(AllContacts)); OnPropertyChanged(nameof(AllContacts));
} }
private async Task AddContact(ContactVM contactVM) private async Task AddContact()
{ {
Model.Contact contact = new Model.Contact(); var result = await Model.GetContacts(Index, Count);
var resultContacts = await Model.GetContacts(Index, Count); IEnumerable<Model.Contact> someContacts = result.contacts;
var allContacts = resultContacts.contacts;
foreach (var c in allContacts) int lastSequence = someContacts
{ .Where(c => Regex.IsMatch(c.Id, @"^/contacts/\d+$"))
if (c.Id == contactVM.Id) .Select(c => int.Parse(Regex.Match(c.Id, @"\d+").Value))
{ .DefaultIfEmpty(0)
contact = c; .Max();
}
} int newSequence = lastSequence + 1;
if (contact != null)
string newId = $"/contacts/{newSequence:D2}";
var newContact = new Model.Contact
{ {
await Model.AddContact(contact); Id = newId,
} FirstName = GivenFirstName,
LastName = GivenLastName
};
GivenFirstName = null;
GivenLastName = null;
await Model.AddContact(newContact);
OnPropertyChanged(nameof(AllContacts));
} }
#endregion #endregion

Loading…
Cancel
Save