1
0
Fork 0
mirror of https://github.com/Nioux/AideDeJeu.git synced 2025-10-30 15:06:06 +00:00

NotifyTaskCompletion

This commit is contained in:
Yan Maniez 2019-04-05 13:26:00 +02:00
parent 5c5314bb8d
commit 34f8713403
3 changed files with 112 additions and 60 deletions

View file

@ -0,0 +1,86 @@
using System;
using System.ComponentModel;
using System.Threading.Tasks;
namespace AideDeJeu.Tools
{
public sealed class NotifyTaskCompletion<TResult> : INotifyPropertyChanged
{
public NotifyTaskCompletion(Task<TResult> task)
{
Task = task;
if (!task.IsCompleted)
{
var _ = WatchTaskAsync(task);
}
}
private async Task WatchTaskAsync(Task task)
{
try
{
await task.ConfigureAwait(false);
}
catch
{
}
var propertyChanged = PropertyChanged;
if (propertyChanged == null)
return;
propertyChanged(this, new PropertyChangedEventArgs("Status"));
propertyChanged(this, new PropertyChangedEventArgs("IsCompleted"));
propertyChanged(this, new PropertyChangedEventArgs("IsNotCompleted"));
if (task.IsCanceled)
{
propertyChanged(this, new PropertyChangedEventArgs("IsCanceled"));
}
else if (task.IsFaulted)
{
propertyChanged(this, new PropertyChangedEventArgs("IsFaulted"));
propertyChanged(this, new PropertyChangedEventArgs("Exception"));
propertyChanged(this, new PropertyChangedEventArgs("InnerException"));
propertyChanged(this, new PropertyChangedEventArgs("ErrorMessage"));
}
else
{
propertyChanged(this, new PropertyChangedEventArgs("IsSuccessfullyCompleted"));
propertyChanged(this, new PropertyChangedEventArgs("Result"));
}
}
public Task<TResult> Task { get; private set; }
public TResult Result
{
get
{
return (Task.Status == TaskStatus.RanToCompletion) ? Task.Result : default(TResult);
}
}
public TaskStatus Status { get { return Task.Status; } }
public bool IsCompleted { get { return Task.IsCompleted; } }
public bool IsNotCompleted { get { return !Task.IsCompleted; } }
public bool IsSuccessfullyCompleted
{
get
{
return Task.Status == TaskStatus.RanToCompletion;
}
}
public bool IsCanceled { get { return Task.IsCanceled; } }
public bool IsFaulted { get { return Task.IsFaulted; } }
public AggregateException Exception { get { return Task.Exception; } }
public Exception InnerException
{
get
{
return (Exception == null) ? null : Exception.InnerException;
}
}
public string ErrorMessage
{
get
{
return (InnerException == null) ? null : InnerException.Message;
}
}
public event PropertyChangedEventHandler PropertyChanged;
}
}

View file

@ -1,4 +1,5 @@
using AideDeJeuLib; using AideDeJeu.Tools;
using AideDeJeuLib;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
@ -10,22 +11,14 @@ namespace AideDeJeu.ViewModels
{ {
public class PlayerCharacterEditorViewModel : BaseViewModel public class PlayerCharacterEditorViewModel : BaseViewModel
{ {
private List<RaceItem> _Races = new List<RaceItem>(); public PlayerCharacterEditorViewModel()
public List<RaceItem> Races
{ {
get Races = new NotifyTaskCompletion<List<RaceItem>>(Task.Run(() => LoadRacesAsync()));
{ Classes = new NotifyTaskCompletion<List<ClassItem>>(Task.Run(() => LoadClassesAsync()));
if(_Races == null || _Races.Count == 0) Backgrounds = new NotifyTaskCompletion<List<BackgroundItem>>(Task.Run(() => LoadBackgroundsAsync()));
{
Task.Run(() => LoadRacesAsync().ConfigureAwait(false));
}
return _Races;
}
set
{
SetProperty(ref _Races, value);
}
} }
public NotifyTaskCompletion<List<RaceItem>> Races { get; private set; }
private int _RaceSelectedIndex = 0; private int _RaceSelectedIndex = 0;
public int RaceSelectedIndex public int RaceSelectedIndex
{ {
@ -36,28 +29,14 @@ namespace AideDeJeu.ViewModels
set set
{ {
SetProperty(ref _RaceSelectedIndex, value); SetProperty(ref _RaceSelectedIndex, value);
if (Races.Count > _RaceSelectedIndex && _RaceSelectedIndex >= 0) if (Races.Result.Count > _RaceSelectedIndex && _RaceSelectedIndex >= 0)
{ {
SelectedPlayerCharacter.Race = Races[_RaceSelectedIndex]; SelectedPlayerCharacter.Race = Races.Result[_RaceSelectedIndex];
} }
} }
} }
private List<ClassItem> _Classes = new List<ClassItem>(); public NotifyTaskCompletion<List<ClassItem>> Classes { get; private set; }
public List<ClassItem> Classes
{
get
{
if (_Classes == null || _Classes.Count == 0)
{
Task.Run(() => LoadClassesAsync().ConfigureAwait(false));
}
return _Classes;
}
set
{
SetProperty(ref _Classes, value);
}
}
private int _ClassSelectedIndex = 0; private int _ClassSelectedIndex = 0;
public int ClassSelectedIndex public int ClassSelectedIndex
{ {
@ -68,25 +47,12 @@ namespace AideDeJeu.ViewModels
set set
{ {
SetProperty(ref _ClassSelectedIndex, value); SetProperty(ref _ClassSelectedIndex, value);
SelectedPlayerCharacter.Class = Classes[_ClassSelectedIndex]; SelectedPlayerCharacter.Class = Classes.Result[_ClassSelectedIndex];
}
}
private List<BackgroundItem> _Backgrounds = new List<BackgroundItem>();
public List<BackgroundItem> Backgrounds
{
get
{
if (_Backgrounds == null || _Backgrounds.Count == 0)
{
Task.Run(() => LoadBackgroundsAsync().ConfigureAwait(false));
}
return _Backgrounds;
}
set
{
SetProperty(ref _Backgrounds, value);
} }
} }
public NotifyTaskCompletion<List<BackgroundItem>> Backgrounds { get; private set; }
private int _BackgroundSelectedIndex = 0; private int _BackgroundSelectedIndex = 0;
public int BackgroundSelectedIndex public int BackgroundSelectedIndex
{ {
@ -97,7 +63,7 @@ namespace AideDeJeu.ViewModels
set set
{ {
SetProperty(ref _BackgroundSelectedIndex, value); SetProperty(ref _BackgroundSelectedIndex, value);
SelectedPlayerCharacter.Background = Backgrounds[_BackgroundSelectedIndex]; SelectedPlayerCharacter.Background = Backgrounds.Result[_BackgroundSelectedIndex];
} }
} }
private PlayerCharacterViewModel _SelectedPlayerCharacter = new PlayerCharacterViewModel(); private PlayerCharacterViewModel _SelectedPlayerCharacter = new PlayerCharacterViewModel();
@ -122,25 +88,25 @@ namespace AideDeJeu.ViewModels
"1", //"2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20" "1", //"2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20"
}; };
public async Task LoadRacesAsync() public async Task<List<RaceItem>> LoadRacesAsync()
{ {
using (var context = await StoreViewModel.GetLibraryContextAsync()) using (var context = await StoreViewModel.GetLibraryContextAsync())
{ {
Races = await context.Races.Where(r => !r.HasSubRaces).OrderBy(r => Tools.Helpers.RemoveDiacritics(r.Name)).ToListAsync(); return await context.Races.Where(r => !r.HasSubRaces).OrderBy(r => Tools.Helpers.RemoveDiacritics(r.Name)).ToListAsync().ConfigureAwait(false);
} }
} }
public async Task LoadClassesAsync() public async Task<List<ClassItem>> LoadClassesAsync()
{ {
using (var context = await StoreViewModel.GetLibraryContextAsync()) using (var context = await StoreViewModel.GetLibraryContextAsync())
{ {
Classes = await context.Classes.Where(c => !(c is SubClassItem)).OrderBy(c => Tools.Helpers.RemoveDiacritics(c.Name)).ToListAsync(); return await context.Classes.Where(c => !(c is SubClassItem)).OrderBy(c => Tools.Helpers.RemoveDiacritics(c.Name)).ToListAsync().ConfigureAwait(false);
} }
} }
public async Task LoadBackgroundsAsync() public async Task<List<BackgroundItem>> LoadBackgroundsAsync()
{ {
using (var context = await StoreViewModel.GetLibraryContextAsync()) using (var context = await StoreViewModel.GetLibraryContextAsync())
{ {
Backgrounds = await context.Backgrounds.Where(b => b.GetType() == typeof(BackgroundItem)).OrderBy(b => Tools.Helpers.RemoveDiacritics(b.Name)).ToListAsync(); return await context.Backgrounds.Where(b => b.GetType() == typeof(BackgroundItem)).OrderBy(b => Tools.Helpers.RemoveDiacritics(b.Name)).ToListAsync().ConfigureAwait(false);
} }
} }
public async Task InitAsync() public async Task InitAsync()

View file

@ -21,13 +21,13 @@
<Picker Title="Niveau" HorizontalOptions="FillAndExpand" ItemsSource="{Binding Levels}" ItemDisplayBinding="{Binding StringFormat='Niveau : {0}'}" /> <Picker Title="Niveau" HorizontalOptions="FillAndExpand" ItemsSource="{Binding Levels}" ItemDisplayBinding="{Binding StringFormat='Niveau : {0}'}" />
</ViewCell> </ViewCell>
<ViewCell> <ViewCell>
<Picker Title="Race" HorizontalOptions="FillAndExpand" ItemsSource="{Binding Races}" ItemDisplayBinding="{Binding Name}" SelectedIndex="{Binding RaceSelectedIndex, Mode=TwoWay}" /> <Picker Title="Race" HorizontalOptions="FillAndExpand" IsEnabled="{Binding Races.IsSuccessfullyCompleted}" ItemsSource="{Binding Races.Result}" ItemDisplayBinding="{Binding Name}" SelectedIndex="{Binding RaceSelectedIndex, Mode=TwoWay}" />
</ViewCell> </ViewCell>
<ViewCell> <ViewCell>
<Picker Title="Classe" HorizontalOptions="FillAndExpand" ItemsSource="{Binding Classes}" ItemDisplayBinding="{Binding Name}" SelectedIndex="{Binding ClassSelectedIndex, Mode=TwoWay}" /> <Picker Title="Classe" HorizontalOptions="FillAndExpand" IsEnabled="{Binding Classes.IsSuccessfullyCompleted}" ItemsSource="{Binding Classes.Result}" ItemDisplayBinding="{Binding Name}" SelectedIndex="{Binding ClassSelectedIndex, Mode=TwoWay}" />
</ViewCell> </ViewCell>
<ViewCell> <ViewCell>
<Picker Title="Historique" HorizontalOptions="FillAndExpand" ItemsSource="{Binding Backgrounds}" ItemDisplayBinding="{Binding Name}" SelectedIndex="{Binding BackgroundSelectedIndex, Mode=TwoWay}" /> <Picker Title="Historique" HorizontalOptions="FillAndExpand" IsEnabled="{Binding Backgrounds.IsSuccessfullyCompleted}" ItemsSource="{Binding Backgrounds.Result}" ItemDisplayBinding="{Binding Name}" SelectedIndex="{Binding BackgroundSelectedIndex, Mode=TwoWay}" />
</ViewCell> </ViewCell>
</TableSection> </TableSection>
<TableSection Title="Caractéristiques"> <TableSection Title="Caractéristiques">