Add Toolkit

Toolkit
Najlae LAMBARAA 2 years ago
parent 6f4122a11a
commit 365fc74cb7

@ -1,21 +1,21 @@
using System; using System;
using CommunityToolkit.Mvvm.Input;
using View.Page; using View.Page;
using ViewModels; using ViewModels;
namespace View.ModelViewPage namespace View.ModelViewPage
{ {
public class ChampionDetailViewM public partial class ChampionDetailViewM
{ {
public ChampionDetailViewM(ChampionManagerVM manager, ChampionVm championVm) public ChampionDetailViewM(ChampionManagerVM manager, ChampionVm championVm)
{ {
ChampionVM = championVm; ChampionVM = championVm;
EditChampionCommand = new Command(EditChampion);
Manager = manager; Manager = manager;
} }
public ChampionVm ChampionVM { get; } public ChampionVm ChampionVM { get; }
private ChampionManagerVM Manager; private ChampionManagerVM Manager;
public Command EditChampionCommand { get; }
[RelayCommand]
private async void EditChampion() private async void EditChampion()
{ {
await Shell.Current.Navigation.PushAsync(new AddChampionPage(new EditChampionViewM(Manager, new EditChampionVm(ChampionVM), ChampionVM))); await Shell.Current.Navigation.PushAsync(new AddChampionPage(new EditChampionViewM(Manager, new EditChampionVm(ChampionVM), ChampionVM)));

@ -5,70 +5,34 @@ using Model;
using System.Windows.Input; using System.Windows.Input;
using static StubLib.StubData; using static StubLib.StubData;
using System.Collections.ObjectModel; using System.Collections.ObjectModel;
using CommunityToolkit.Mvvm.Input;
namespace View.ModelViewPage namespace View.ModelViewPage
{ {
public class ChampionsViewM public partial class ChampionsViewM
{ {
public Command NextPageCommand { get; private set; }
public Command PreviousPageCommand { get; }
public Command EditChampionCommand { get; }
public ChampionManagerVM championManagerVm { get; } public ChampionManagerVM championManagerVm { get; }
public Command DeleteChampionCommand { get; private set; }
public ChampionsViewM(ChampionManagerVM championManager) public ChampionsViewM(ChampionManagerVM championManager)
{ {
championManagerVm = championManager; championManagerVm = championManager;
PushToDetailCommand = new Command<ChampionVm>(PushToDetail);
DeleteChampionCommand = new Command<ChampionVm>(async (ChampionVm obj) => await championManagerVm.DeleteChampion(obj));
NextPageCommand = new Command(NextPage, CanExecuteNext);
PreviousPageCommand = new Command(PreviousPage, CanExecutePrevious);
AddChampionCommand = new Command(Addchampion);
EditChampionCommand = new Command<ChampionVm>(EditChampion);
}
private void NextPage()
{
championManagerVm.Index++;
RefreshCanExecute();
}
private void PreviousPage()
{
championManagerVm.Index--;
RefreshCanExecute();
}
private bool CanExecutePrevious()
{
return championManagerVm.Index > 1;
}
private bool CanExecuteNext()
{
var val = (this.championManagerVm.Index) < this.championManagerVm.PageTotale;
return val;
} }
void RefreshCanExecute()
{
PreviousPageCommand.ChangeCanExecute();
NextPageCommand.ChangeCanExecute();
}
public Command PushToDetailCommand { get; }
public Command AddChampionCommand { get; }
[RelayCommand]
private void PushToDetail(ChampionVm champion) private void PushToDetail(ChampionVm champion)
{ {
Shell.Current.Navigation.PushAsync(new DetailChampion(new ChampionDetailViewM(championManagerVm, champion))); Shell.Current.Navigation.PushAsync(new DetailChampion(new ChampionDetailViewM(championManagerVm, champion)));
} }
private void Addchampion() [RelayCommand]
private void AddChampion()
{ {
Shell.Current.Navigation.PushAsync(page: new AddChampionPage(new EditChampionViewM(championManagerVm, new EditChampionVm(null), null))); Shell.Current.Navigation.PushAsync(page: new AddChampionPage(new EditChampionViewM(championManagerVm, new EditChampionVm(null), null)));
} }
[RelayCommand]
private async void EditChampion(ChampionVm championVM) private async void EditChampion(ChampionVm championVM)
{ {
await Shell.Current.Navigation.PushAsync(new AddChampionPage(new EditChampionViewM(championManagerVm, new EditChampionVm(championVM), championVM))); await Shell.Current.Navigation.PushAsync(new AddChampionPage(new EditChampionViewM(championManagerVm, new EditChampionVm(championVM), championVM)));

@ -1,23 +1,19 @@
using System; using System;
using System.Collections.ObjectModel; using System.Collections.ObjectModel;
using CommunityToolkit.Mvvm.Input;
using Model; using Model;
using ViewModels; using ViewModels;
namespace View.ModelViewPage namespace View.ModelViewPage
{ {
public class EditChampionViewM public partial class EditChampionViewM
{ {
public Command PickIconCommand { get; }
public Command PickImageCommand { get; }
public EditChampionViewM(ChampionManagerVM manager, EditChampionVm aditableChampion,ChampionVm championVM) public EditChampionViewM(ChampionManagerVM manager, EditChampionVm aditableChampion,ChampionVm championVM)
{ {
Manager = manager; Manager = manager;
EditableChampion = aditableChampion; EditableChampion = aditableChampion;
ChampionVM = championVM; ChampionVM = championVM;
SaveChampionCommand = new Command(SaveChampion);
PickIconCommand = new Command(async () => await PickIcon());
PickImageCommand = new Command(async () => await PickImage());
Title = aditableChampion.IsNew ? "Create" : "Update"; Title = aditableChampion.IsNew ? "Create" : "Update";
} }
@ -28,9 +24,8 @@ namespace View.ModelViewPage
private ChampionVm ChampionVM; private ChampionVm ChampionVM;
public Command SaveChampionCommand { get; }
[RelayCommand]
private async void SaveChampion() private async void SaveChampion()
{ {
Manager.SaveChampion(EditableChampion, ChampionVM); Manager.SaveChampion(EditableChampion, ChampionVM);
@ -40,9 +35,9 @@ namespace View.ModelViewPage
{ {
get => Characteristics; get => Characteristics;
} }
[RelayCommand]
private async Task PickIcon() private async Task PickIcon()
{ {
var result = await FilePicker.PickAsync(new PickOptions var result = await FilePicker.PickAsync(new PickOptions
@ -60,6 +55,7 @@ namespace View.ModelViewPage
} }
} }
[RelayCommand]
private async Task PickImage() private async Task PickImage()
{ {
var result = await FilePicker.PickAsync(new PickOptions var result = await FilePicker.PickAsync(new PickOptions

@ -47,7 +47,7 @@
<SwipeItem Text="Supprimer" <SwipeItem Text="Supprimer"
BackgroundColor="Red" BackgroundColor="Red"
Command="{Binding Source={RelativeSource AncestorType={x:Type vm:ChampionsViewM}}, Path=DeleteChampionCommand} " Command="{Binding Source={RelativeSource AncestorType={x:Type vm:ChampionsViewM}}, Path=championManagerVm.DeleteChampionCommand} "
CommandParameter="{Binding .}" /> CommandParameter="{Binding .}" />
</SwipeItems> </SwipeItems>
</SwipeView.RightItems> </SwipeView.RightItems>
@ -82,11 +82,11 @@
</Grid.RowDefinitions> </Grid.RowDefinitions>
<HorizontalStackLayout > <HorizontalStackLayout >
<Button Text="L" HeightRequest="12 " WidthRequest="12" Padding="15" Command="{Binding PreviousPageCommand}"/> <Button Text="L" HeightRequest="12 " WidthRequest="12" Padding="15" Command="{Binding championManagerVm.PreviousPageCommand}"/>
<Button Text="R" HeightRequest="12 " Padding="10" WidthRequest="12" <Button Text="R" HeightRequest="12 " Padding="10" WidthRequest="12"
Command="{Binding NextPageCommand}" /> Command="{Binding championManagerVm.NextPageCommand}" />
</HorizontalStackLayout> </HorizontalStackLayout>

@ -57,6 +57,7 @@
<ItemGroup> <ItemGroup>
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="7.0.0" /> <PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="7.0.0" />
<PackageReference Include="CommunityToolkit.Maui" Version="5.2.0" /> <PackageReference Include="CommunityToolkit.Maui" Version="5.2.0" />
<PackageReference Include="CommunityToolkit.Mvvm" Version="8.2.0" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>

@ -3,27 +3,21 @@ using System.Collections.ObjectModel;
using System.ComponentModel; using System.ComponentModel;
using System.Runtime.CompilerServices; using System.Runtime.CompilerServices;
using System.Windows.Input; using System.Windows.Input;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using Model; using Model;
namespace ViewModels namespace ViewModels
{ {
public class ChampionManagerVM : INotifyPropertyChanged public partial class ChampionManagerVM : ObservableObject
{ {
public ObservableCollection<ChampionVm> Champions { get; } public ObservableCollection<ChampionVm> Champions { get; }
public event PropertyChangedEventHandler? PropertyChanged;
public ICommand NextPageCommand { get; private set; }
public ICommand DeleteChampionCommand { get; }
public IDataManager DataManager public IDataManager DataManager
{ {
get => _dataManager; get => _dataManager;
set set => _dataManager = value;
{
if (_dataManager == value) return;
_dataManager = value;
OnPropertyChanged();
}
} }
private IDataManager _dataManager { get; set; } private IDataManager _dataManager { get; set; }
@ -34,7 +28,6 @@ namespace ViewModels
LoadChampions(index, Count).ConfigureAwait(false); LoadChampions(index, Count).ConfigureAwait(false);
PropertyChanged += ChampionManagerVM_PropertyChanged; PropertyChanged += ChampionManagerVM_PropertyChanged;
PropertyChanged += ChampionManager_PropertyChanged;
Total = this.DataManager.ChampionsMgr.GetNbItems().Result; Total = this.DataManager.ChampionsMgr.GetNbItems().Result;
} }
@ -47,48 +40,21 @@ namespace ViewModels
} }
} }
public int Index [ObservableProperty]
{ [NotifyCanExecuteChangedFor(nameof(PreviousPageCommand),nameof(NextPageCommand))]
get => index; private int index;
set
{
if (index == value) return;
index = value;
OnPropertyChanged();
}
}
private int index;
public int Count public int Count
{ {
get; get;
set; set;
} = 5; } = 5;
public int PageTotale { get { return this.total / Count + ((this.total % Count) > 0 ? 1 : 0); } } public int PageTotale { get { return this.total / Count + ((this.total % Count) > 0 ? 1 : 0); } }
private int total; [ObservableProperty]
private int total;
public int Total
{
get => total;
private set
{
total = value;
OnPropertyChanged();
}
}
public int nombrepage(int GetNbItems, int count)
{
int result = GetNbItems / count;
if (result < 0) return result + 1;
else
return result;
}
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
private async Task LoadChampions(int index, int count) private async Task LoadChampions(int index, int count)
{ {
@ -102,14 +68,6 @@ namespace ViewModels
} }
} }
private async void ChampionManager_PropertyChanged(object? sender, PropertyChangedEventArgs e)
{
if (e.PropertyName == nameof(Index))
{
Champions.Clear();
await LoadChampions(Index, Count);
}
}
public async void SaveChampion(EditChampionVm editableChampionVM, ChampionVm? championVM) public async void SaveChampion(EditChampionVm editableChampionVM, ChampionVm? championVM)
{ {
@ -132,10 +90,29 @@ namespace ViewModels
} }
[RelayCommand(CanExecute =nameof(CanExecuteNext))]
private void NextPage()
{
Index++;
}
[RelayCommand(CanExecute =nameof(CanExecutePrevious))]
private void PreviousPage()
{
Index--;
}
private bool CanExecutePrevious()
{
return Index > 1;
}
private bool CanExecuteNext()
{
var val = (this.Index) < this.PageTotale;
return val;
}
private async void updatePagination() private async void updatePagination()
{ {
@ -151,6 +128,8 @@ namespace ViewModels
OnPropertyChanged(nameof(PageTotale)); OnPropertyChanged(nameof(PageTotale));
} }
[RelayCommand]
public async Task DeleteChampion(ChampionVm champion) public async Task DeleteChampion(ChampionVm champion)
{ {
if (champion is null) return; if (champion is null) return;

@ -2,10 +2,11 @@
using System.Collections.ObjectModel; using System.Collections.ObjectModel;
using System.ComponentModel; using System.ComponentModel;
using System.Runtime.CompilerServices; using System.Runtime.CompilerServices;
using CommunityToolkit.Mvvm.ComponentModel;
using Model; using Model;
namespace ViewModels namespace ViewModels
{ {
public class ChampionVm : INotifyPropertyChanged public class ChampionVm : ObservableObject
{ {
public ChampionVm(Champion champion) => Model = champion; public ChampionVm(Champion champion) => Model = champion;
@ -19,10 +20,7 @@ namespace ViewModels
get => model; get => model;
set set
{ {
if (value.Equals(model)) return;
if (value == null) return;
model = value; model = value;
OnPropertyChanged();
} }
} }
@ -34,43 +32,23 @@ namespace ViewModels
public string Icon public string Icon
{ {
get => Model.Icon; get => Model.Icon;
set set => SetProperty(Model.Icon, value, Model, (mod, val) => model.Icon = val);
{
if (model == null) return;
Model.Icon = value;
OnPropertyChanged();
}
} }
public LargeImage Image public LargeImage Image
{ {
get => Model.Image; get => Model.Image;
set set=> SetProperty(Model.Image, value, Model, (mod, val) => model.Image = val);
{
if (model == null) return;
Model.Image = value;
OnPropertyChanged();
}
} }
public ChampionClass Class public ChampionClass Class
{ {
get => Model.Class; get => Model.Class;
set set=> SetProperty(Model.Class, value, Model, (mod, val) => model.Class = val);
{
if (model == null) return;
Model.Class = value;
OnPropertyChanged();
}
} }
public string Bio public string Bio
{ {
get => Model.Bio; get => Model.Bio;
set set=> SetProperty(Model.Bio, value, Model, (mod, val) => model.Bio = val);
{
if (model == null) return;
Model.Bio = value;
OnPropertyChanged();
}
} }
private ObservableCollection<Skill> skills; private ObservableCollection<Skill> skills;
public HashSet<Skill> Skills public HashSet<Skill> Skills
@ -83,13 +61,6 @@ namespace ViewModels
OnPropertyChanged(); OnPropertyChanged();
} }
} }
public event PropertyChangedEventHandler? PropertyChanged;
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
} }
} }

@ -2,11 +2,12 @@
using System.Collections.ObjectModel; using System.Collections.ObjectModel;
using System.ComponentModel; using System.ComponentModel;
using System.Runtime.CompilerServices; using System.Runtime.CompilerServices;
using CommunityToolkit.Mvvm.ComponentModel;
using Model; using Model;
namespace ViewModels namespace ViewModels
{ {
public class EditChampionVm : INotifyPropertyChanged public partial class EditChampionVm : ObservableObject
{ {
public ChampionVm Model { get; set; } public ChampionVm Model { get; set; }
public EditChampionVm(ChampionVm vm) public EditChampionVm(ChampionVm vm)
@ -17,7 +18,7 @@ namespace ViewModels
icon = IsNew ? string.Empty : Model.Icon; icon = IsNew ? string.Empty : Model.Icon;
_name = IsNew ? string.Empty : Model.Name; _name = IsNew ? string.Empty : Model.Name;
image = IsNew ? string.Empty : Model.Image.Base64; image = IsNew ? string.Empty : Model.Image.Base64;
_classe = IsNew ? ChampionClass.Unknown : Model.Class; _class = IsNew ? ChampionClass.Unknown : Model.Class;
ListClasses = Enum.GetValues<ChampionClass>().Where(c => c != ChampionClass.Unknown).ToArray(); ListClasses = Enum.GetValues<ChampionClass>().Where(c => c != ChampionClass.Unknown).ToArray();
} }
@ -25,87 +26,31 @@ namespace ViewModels
public IEnumerable<ChampionClass> ListClasses { get; } public IEnumerable<ChampionClass> ListClasses { get; }
[ObservableProperty]
private string _name; private string _name;
public string Name
{ [ObservableProperty]
get => _name;
set
{
if (_name == value) return;
_name = value;
OnPropertyChanged();
}
}
private string icon; private string icon;
public string Icon [ObservableProperty]
{
get => icon;
set
{
if (icon == value) return;
icon = value;
OnPropertyChanged();
}
}
private string _bio; private string _bio;
public string Bio
{
get => _bio;
set
{
if (_bio == value) return;
_bio = value;
OnPropertyChanged();
}
}
[ObservableProperty]
private string image; private string image;
public string Image
{
get => image;
set
{
if (image == value) return;
image = value;
OnPropertyChanged();
}
}
private ChampionClass _classe; [ObservableProperty]
public ChampionClass Class private ChampionClass _class;
{
get => _classe;
set
{
if (_classe == null) return;
_classe = value;
OnPropertyChanged();
}
}
[ObservableProperty]
private int index; private int index;
public int Index
{
get => index;
set
{
if (index == value) return;
index = value;
OnPropertyChanged();
}
}
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
=> PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
public ReadOnlyDictionary<string, int> Characteristics public ReadOnlyDictionary<string, int> Characteristics
{ {
get => Model.Characteristics; get => Model.Characteristics;
} }
public event PropertyChangedEventHandler PropertyChanged;
public void SaveChampion() public void SaveChampion()
{ {

@ -29,4 +29,7 @@
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\Model\Model.csproj" /> <ProjectReference Include="..\Model\Model.csproj" />
</ItemGroup> </ItemGroup>
<ItemGroup>
<PackageReference Include="CommunityToolkit.Mvvm" Version="8.2.0" />
</ItemGroup>
</Project> </Project>

Loading…
Cancel
Save