Compare commits

...

1 Commits

Author SHA1 Message Date
Najlae LAMBARAA 365fc74cb7 Add Toolkit
2 years ago

@ -1,21 +1,21 @@
using System;
using CommunityToolkit.Mvvm.Input;
using View.Page;
using ViewModels;
namespace View.ModelViewPage
{
public class ChampionDetailViewM
public partial class ChampionDetailViewM
{
public ChampionDetailViewM(ChampionManagerVM manager, ChampionVm championVm)
{
ChampionVM = championVm;
EditChampionCommand = new Command(EditChampion);
Manager = manager;
}
public ChampionVm ChampionVM { get; }
private ChampionManagerVM Manager;
public Command EditChampionCommand { get; }
[RelayCommand]
private async void EditChampion()
{
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 static StubLib.StubData;
using System.Collections.ObjectModel;
using CommunityToolkit.Mvvm.Input;
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 Command DeleteChampionCommand { get; private set; }
public ChampionsViewM(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)
{
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)));
}
[RelayCommand]
private async void EditChampion(ChampionVm championVM)
{
await Shell.Current.Navigation.PushAsync(new AddChampionPage(new EditChampionViewM(championManagerVm, new EditChampionVm(championVM), championVM)));

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

@ -47,7 +47,7 @@
<SwipeItem Text="Supprimer"
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 .}" />
</SwipeItems>
</SwipeView.RightItems>
@ -82,11 +82,11 @@
</Grid.RowDefinitions>
<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"
Command="{Binding NextPageCommand}" />
Command="{Binding championManagerVm.NextPageCommand}" />
</HorizontalStackLayout>

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

@ -3,27 +3,21 @@ using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Windows.Input;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using Model;
namespace ViewModels
{
public class ChampionManagerVM : INotifyPropertyChanged
public partial class ChampionManagerVM : ObservableObject
{
public ObservableCollection<ChampionVm> Champions { get; }
public event PropertyChangedEventHandler? PropertyChanged;
public ICommand NextPageCommand { get; private set; }
public ICommand DeleteChampionCommand { get; }
public IDataManager DataManager
{
get => _dataManager;
set
{
if (_dataManager == value) return;
_dataManager = value;
OnPropertyChanged();
}
set => _dataManager = value;
}
private IDataManager _dataManager { get; set; }
@ -34,7 +28,6 @@ namespace ViewModels
LoadChampions(index, Count).ConfigureAwait(false);
PropertyChanged += ChampionManagerVM_PropertyChanged;
PropertyChanged += ChampionManager_PropertyChanged;
Total = this.DataManager.ChampionsMgr.GetNbItems().Result;
}
@ -47,16 +40,8 @@ namespace ViewModels
}
}
public int Index
{
get => index;
set
{
if (index == value) return;
index = value;
OnPropertyChanged();
}
}
[ObservableProperty]
[NotifyCanExecuteChangedFor(nameof(PreviousPageCommand),nameof(NextPageCommand))]
private int index;
public int Count
@ -64,31 +49,12 @@ namespace ViewModels
get;
set;
} = 5;
public int PageTotale { get { return this.total / Count + ((this.total % Count) > 0 ? 1 : 0); } }
[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)
{
@ -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)
{
@ -132,8 +90,27 @@ 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()
{
@ -151,6 +128,8 @@ namespace ViewModels
OnPropertyChanged(nameof(PageTotale));
}
[RelayCommand]
public async Task DeleteChampion(ChampionVm champion)
{
if (champion is null) return;

@ -2,10 +2,11 @@
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using CommunityToolkit.Mvvm.ComponentModel;
using Model;
namespace ViewModels
{
public class ChampionVm : INotifyPropertyChanged
public class ChampionVm : ObservableObject
{
public ChampionVm(Champion champion) => Model = champion;
@ -19,10 +20,7 @@ namespace ViewModels
get => model;
set
{
if (value.Equals(model)) return;
if (value == null) return;
model = value;
OnPropertyChanged();
}
}
@ -34,43 +32,23 @@ namespace ViewModels
public string Icon
{
get => Model.Icon;
set
{
if (model == null) return;
Model.Icon = value;
OnPropertyChanged();
}
set => SetProperty(Model.Icon, value, Model, (mod, val) => model.Icon = val);
}
public LargeImage Image
{
get => Model.Image;
set
{
if (model == null) return;
Model.Image = value;
OnPropertyChanged();
}
set=> SetProperty(Model.Image, value, Model, (mod, val) => model.Image = val);
}
public ChampionClass Class
{
get => Model.Class;
set
{
if (model == null) return;
Model.Class = value;
OnPropertyChanged();
}
set=> SetProperty(Model.Class, value, Model, (mod, val) => model.Class = val);
}
public string Bio
{
get => Model.Bio;
set
{
if (model == null) return;
Model.Bio = value;
OnPropertyChanged();
}
set=> SetProperty(Model.Bio, value, Model, (mod, val) => model.Bio = val);
}
private ObservableCollection<Skill> skills;
public HashSet<Skill> Skills
@ -83,13 +61,6 @@ namespace ViewModels
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.ComponentModel;
using System.Runtime.CompilerServices;
using CommunityToolkit.Mvvm.ComponentModel;
using Model;
namespace ViewModels
{
public class EditChampionVm : INotifyPropertyChanged
public partial class EditChampionVm : ObservableObject
{
public ChampionVm Model { get; set; }
public EditChampionVm(ChampionVm vm)
@ -17,7 +18,7 @@ namespace ViewModels
icon = IsNew ? string.Empty : Model.Icon;
_name = IsNew ? string.Empty : Model.Name;
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();
}
@ -25,87 +26,31 @@ namespace ViewModels
public IEnumerable<ChampionClass> ListClasses { get; }
[ObservableProperty]
private string _name;
public string Name
{
get => _name;
set
{
if (_name == value) return;
_name = value;
OnPropertyChanged();
}
}
[ObservableProperty]
private string icon;
public string Icon
{
get => icon;
set
{
if (icon == value) return;
icon = value;
OnPropertyChanged();
}
}
[ObservableProperty]
private string _bio;
public string Bio
{
get => _bio;
set
{
if (_bio == value) return;
_bio = value;
OnPropertyChanged();
}
}
[ObservableProperty]
private string image;
public string Image
{
get => image;
set
{
if (image == value) return;
image = value;
OnPropertyChanged();
}
}
private ChampionClass _classe;
public ChampionClass Class
{
get => _classe;
set
{
if (_classe == null) return;
_classe = value;
OnPropertyChanged();
}
}
[ObservableProperty]
private ChampionClass _class;
[ObservableProperty]
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
{
get => Model.Characteristics;
}
public event PropertyChangedEventHandler PropertyChanged;
public void SaveChampion()
{

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

Loading…
Cancel
Save