diff --git a/AideDeJeu/AideDeJeu/Tools/NotifyTaskCompletion.cs b/AideDeJeu/AideDeJeu/Tools/NotifyTaskCompletion.cs new file mode 100644 index 00000000..02a75316 --- /dev/null +++ b/AideDeJeu/AideDeJeu/Tools/NotifyTaskCompletion.cs @@ -0,0 +1,86 @@ +using System; +using System.ComponentModel; +using System.Threading.Tasks; + +namespace AideDeJeu.Tools +{ + public sealed class NotifyTaskCompletion : INotifyPropertyChanged + { + public NotifyTaskCompletion(Task 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 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; + } +} diff --git a/AideDeJeu/AideDeJeu/ViewModels/PlayerCharacterEditorViewModel.cs b/AideDeJeu/AideDeJeu/ViewModels/PlayerCharacterEditorViewModel.cs index 82e2e319..e9ff9b63 100644 --- a/AideDeJeu/AideDeJeu/ViewModels/PlayerCharacterEditorViewModel.cs +++ b/AideDeJeu/AideDeJeu/ViewModels/PlayerCharacterEditorViewModel.cs @@ -1,4 +1,5 @@ -using AideDeJeuLib; +using AideDeJeu.Tools; +using AideDeJeuLib; using Microsoft.EntityFrameworkCore; using System; using System.Collections.Generic; @@ -10,22 +11,14 @@ namespace AideDeJeu.ViewModels { public class PlayerCharacterEditorViewModel : BaseViewModel { - private List _Races = new List(); - public List Races + public PlayerCharacterEditorViewModel() { - get - { - if(_Races == null || _Races.Count == 0) - { - Task.Run(() => LoadRacesAsync().ConfigureAwait(false)); - } - return _Races; - } - set - { - SetProperty(ref _Races, value); - } + Races = new NotifyTaskCompletion>(Task.Run(() => LoadRacesAsync())); + Classes = new NotifyTaskCompletion>(Task.Run(() => LoadClassesAsync())); + Backgrounds = new NotifyTaskCompletion>(Task.Run(() => LoadBackgroundsAsync())); } + + public NotifyTaskCompletion> Races { get; private set; } private int _RaceSelectedIndex = 0; public int RaceSelectedIndex { @@ -36,28 +29,14 @@ namespace AideDeJeu.ViewModels set { 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 _Classes = new List(); - public List Classes - { - get - { - if (_Classes == null || _Classes.Count == 0) - { - Task.Run(() => LoadClassesAsync().ConfigureAwait(false)); - } - return _Classes; - } - set - { - SetProperty(ref _Classes, value); - } - } + public NotifyTaskCompletion> Classes { get; private set; } + private int _ClassSelectedIndex = 0; public int ClassSelectedIndex { @@ -68,25 +47,12 @@ namespace AideDeJeu.ViewModels set { SetProperty(ref _ClassSelectedIndex, value); - SelectedPlayerCharacter.Class = Classes[_ClassSelectedIndex]; - } - } - private List _Backgrounds = new List(); - public List Backgrounds - { - get - { - if (_Backgrounds == null || _Backgrounds.Count == 0) - { - Task.Run(() => LoadBackgroundsAsync().ConfigureAwait(false)); - } - return _Backgrounds; - } - set - { - SetProperty(ref _Backgrounds, value); + SelectedPlayerCharacter.Class = Classes.Result[_ClassSelectedIndex]; } } + + public NotifyTaskCompletion> Backgrounds { get; private set; } + private int _BackgroundSelectedIndex = 0; public int BackgroundSelectedIndex { @@ -97,7 +63,7 @@ namespace AideDeJeu.ViewModels set { SetProperty(ref _BackgroundSelectedIndex, value); - SelectedPlayerCharacter.Background = Backgrounds[_BackgroundSelectedIndex]; + SelectedPlayerCharacter.Background = Backgrounds.Result[_BackgroundSelectedIndex]; } } 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" }; - public async Task LoadRacesAsync() + public async Task> LoadRacesAsync() { 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> LoadClassesAsync() { 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> LoadBackgroundsAsync() { 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() diff --git a/AideDeJeu/AideDeJeu/Views/PlayerCharacterEditorPage.xaml b/AideDeJeu/AideDeJeu/Views/PlayerCharacterEditorPage.xaml index da9917de..bc56047b 100644 --- a/AideDeJeu/AideDeJeu/Views/PlayerCharacterEditorPage.xaml +++ b/AideDeJeu/AideDeJeu/Views/PlayerCharacterEditorPage.xaml @@ -21,13 +21,13 @@ - + - + - +