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

@ -4,18 +4,40 @@ using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
using ViewModels;
namespace LivreLand.ViewModel
{
public class ContactsVM : BaseViewModel
{
#region Fields
private bool dataFormIsVisible = false;
#endregion
#region Properties
public NavigatorVM Navigator { 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
#region Constructor
@ -24,6 +46,16 @@ namespace LivreLand.ViewModel
{
Navigator = navigatorVM;
Manager = managerVM;
AddContactDataFormCommand = new RelayCommand(() => AddContactDataForm());
}
#endregion
#region Methods
private void AddContactDataForm()
{
DataFormIsVisible = true;
}
#endregion

@ -2,6 +2,7 @@
using PersonalMVVMToolkit;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text.RegularExpressions;
using System.Windows.Input;
namespace ViewModels
@ -28,6 +29,8 @@ namespace ViewModels
private AuthorVM selectedAuthor;
private ContactVM selectedContact;
private string givenFirstName;
private string givenLastName;
#endregion
#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 int Index
@ -222,7 +251,7 @@ namespace ViewModels
GetPastBorrowingsCommand = new RelayCommand(() => GetPastBorrowings());
LendBookCommand = new RelayCommand<ContactVM>((contactVM) => LendBook(contactVM));
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)));
}
@ -540,22 +569,33 @@ namespace ViewModels
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)
private async Task AddContact()
{
if (c.Id == contactVM.Id)
{
contact = c;
}
}
if (contact != null)
var result = await Model.GetContacts(Index, Count);
IEnumerable<Model.Contact> someContacts = result.contacts;
int lastSequence = someContacts
.Where(c => Regex.IsMatch(c.Id, @"^/contacts/\d+$"))
.Select(c => int.Parse(Regex.Match(c.Id, @"\d+").Value))
.DefaultIfEmpty(0)
.Max();
int newSequence = lastSequence + 1;
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

Loading…
Cancel
Save