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

Chargement en async + indicateur d'activité

This commit is contained in:
yassine benabbas 2018-05-30 16:57:48 +02:00
parent bfe8f0d81f
commit 5d28b9b469
6 changed files with 145 additions and 130 deletions

View file

@ -1,11 +1,11 @@
#pragma warning disable 1591
//------------------------------------------------------------------------------
// <auto-generated>
// Ce code a été généré par un outil.
// Version du runtime :4.0.30319.42000
// This code was generated by a tool.
// Runtime Version:4.0.30319.42000
//
// Les modifications apportées à ce fichier peuvent provoquer un comportement incorrect et seront perdues si
// le code est régénéré.
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

View file

@ -7,8 +7,12 @@
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<DebugType>pdbonly</DebugType>
<DebugSymbols>true</DebugSymbols>
<LangVersion>Latest</LangVersion>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<LangVersion>Latest</LangVersion>
</PropertyGroup>
<ItemGroup>
<EmbeddedResource Remove="Resources\AdJTheme.xaml" />
</ItemGroup>

View file

@ -8,13 +8,15 @@ using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Windows.Input;
using System.Threading;
using System.Threading.Tasks;
namespace AideDeJeu.ViewModels
{
public abstract class FilterViewModel : BaseViewModel
{
public ICommand LoadItemsCommand { get; protected set; }
public abstract IEnumerable<Item> FilterItems(IEnumerable<Item> items);
public abstract Task<IEnumerable<Item>> FilterItems(IEnumerable<Item> items, CancellationToken token = default);
public abstract IEnumerable<Filter> Filters { get; }
private string _SearchText = "";
public string SearchText
@ -111,7 +113,9 @@ namespace AideDeJeu.ViewModels
}
public override IEnumerable<Item> FilterItems(IEnumerable<Item> items)
public override async Task<IEnumerable<Item>> FilterItems(IEnumerable<Item> items, CancellationToken token = default)
{
return await Task.Run(() =>
{
var classe = Filters.SingleOrDefault(filter => filter.Key == FilterKeys.Class).SelectedKey ?? "";
var niveauMin = Filters.SingleOrDefault(filter => filter.Key == FilterKeys.MinLevel).SelectedKey ?? "0";
@ -119,9 +123,8 @@ namespace AideDeJeu.ViewModels
var ecole = Filters.SingleOrDefault(filter => filter.Key == FilterKeys.School).SelectedKey ?? "";
var rituel = Filters.SingleOrDefault(filter => filter.Key == FilterKeys.Ritual).SelectedKey ?? "";
var source = Filters.SingleOrDefault(filter => filter.Key == FilterKeys.Source).SelectedKey ?? "";
return items
.Where(item =>
token.ThrowIfCancellationRequested();
return items.Where(item =>
{
var spell = item as Spell;
return (int.Parse(spell.Level) >= int.Parse(niveauMin)) &&
@ -131,9 +134,10 @@ namespace AideDeJeu.ViewModels
spell.Source.Contains(classe) &&
spell.Type.Contains(rituel) &&
spell.NamePHB.ToLower().Contains(SearchText.ToLower());
})
.OrderBy(spell => spell.NamePHB)
.ToList();
}).OrderBy(spell => spell.NamePHB)
.AsEnumerable();
}, token);
}
public abstract List<KeyValuePair<string, string>> Classes { get; }
@ -343,17 +347,28 @@ namespace AideDeJeu.ViewModels
}
}
public override IEnumerable<Item> FilterItems(IEnumerable<Item> items)
public override async Task<IEnumerable<Item>> FilterItems(IEnumerable<Item> items, CancellationToken token = default)
{
return await Task.Run(() =>
{
var powerComparer = new PowerComparer();
//var category = Filters.SingleOrDefault(filter => filter.Key == FilterKeys.Category).SelectedKey ?? "";
var type = Filters.SingleOrDefault(filter => filter.Key == FilterKeys.Type).SelectedKey ?? "";
token.ThrowIfCancellationRequested();
var minPower = Filters.SingleOrDefault(filter => filter.Key == FilterKeys.MinPower).SelectedKey ?? "0 (0 PX)";
token.ThrowIfCancellationRequested();
var maxPower = Filters.SingleOrDefault(filter => filter.Key == FilterKeys.MaxPower).SelectedKey ?? "30 (155000 PX)";
token.ThrowIfCancellationRequested();
var size = Filters.SingleOrDefault(filter => filter.Key == FilterKeys.Size).SelectedKey ?? "";
token.ThrowIfCancellationRequested();
//var legendary = Filters.SingleOrDefault(filter => filter.Key == FilterKeys.Legendary).SelectedKey ?? "";
var source = Filters.SingleOrDefault(filter => filter.Key == FilterKeys.Source).SelectedKey ?? "";
token.ThrowIfCancellationRequested();
return items.Where(item =>
{
@ -366,7 +381,9 @@ namespace AideDeJeu.ViewModels
monster.NamePHB.ToLower().Contains(SearchText.ToLower());
})
.OrderBy(monster => monster.NamePHB)
.ToList();
.AsEnumerable();
}, token);
}
public abstract List<KeyValuePair<string, string>> Categories { get; }

View file

@ -8,15 +8,18 @@ using System.Diagnostics;
using System.Threading.Tasks;
using System.Windows.Input;
using Xamarin.Forms;
using System.Threading;
namespace AideDeJeu.ViewModels
{
public abstract class ItemsViewModel : BaseViewModel
{
CancellationTokenSource cancellationTokenSource;
public ItemsViewModel(ItemSourceType itemSourceType)
{
this.ItemSourceType = itemSourceType;
LoadItemsCommand = new Command(() => ExecuteLoadItemsCommand());
LoadItemsCommand = new Command(async () => await ExecuteLoadItemsCommandAsync());
}
public ICommand LoadItemsCommand { get; protected set; }
//public abstract void ExecuteLoadItemsCommand();
@ -66,26 +69,28 @@ namespace AideDeJeu.ViewModels
}
}
public void ExecuteLoadItemsCommand()
async Task LoadItemsAsync(CancellationToken token = default)
{
if (IsBusy)
return;
IsBusy = true;
Main.IsLoading = true;
try
{
Main.Items.Clear();
var filterViewModel = Main.GetFilterViewModel(ItemSourceType);
var items = filterViewModel.FilterItems(AllItems);
var items = await filterViewModel.FilterItems(AllItems, token);
await Task.Run(() =>
{
Main.Items.Clear();
foreach (var item in items)
{
token.ThrowIfCancellationRequested();
Main.Items.Add(item);
}
});
//On arrete le loading ici car on annule toujours avant de lancer une nouvelle opération
Main.IsLoading = false;
}
catch (Exception ex)
catch (OperationCanceledException ex)
{
Debug.WriteLine(ex);
}
@ -95,6 +100,14 @@ namespace AideDeJeu.ViewModels
}
}
public async Task ExecuteLoadItemsCommandAsync()
{
if (cancellationTokenSource != null)
{
cancellationTokenSource.Cancel();
}
cancellationTokenSource = new CancellationTokenSource();
await LoadItemsAsync(cancellationTokenSource.Token);
}
}
}

View file

@ -52,6 +52,13 @@ namespace AideDeJeu.ViewModels
}
}
private bool _isLoading;
public bool IsLoading
{
get => _isLoading;
set => SetProperty(ref _isLoading, value);
}
public Dictionary<ItemSourceType, Lazy<ItemsViewModel>> AllItemsViewModel = new Dictionary<ItemSourceType, Lazy<ItemsViewModel>>()
{
{ ItemSourceType.SpellVF, new Lazy<ItemsViewModel>(() => new SpellsViewModel(ItemSourceType.SpellVF)) },
@ -116,9 +123,9 @@ namespace AideDeJeu.ViewModels
public MainViewModel()
{
LoadItemsCommand = new Command(() =>
LoadItemsCommand = new Command(async () =>
{
GetItemsViewModel(ItemSourceType).ExecuteLoadItemsCommand();
await GetItemsViewModel(ItemSourceType).ExecuteLoadItemsCommandAsync();
});
GotoItemCommand = new Command<Item>(async (item) => await GetItemsViewModel(ItemSourceType).ExecuteGotoItemCommandAsync(item));
SwitchToSpells = new Command(() => ItemSourceType = (ItemSourceType & ~ItemSourceType.Monster) | ItemSourceType.Spell);
@ -127,10 +134,10 @@ namespace AideDeJeu.ViewModels
SwitchToVO = new Command(() => ItemSourceType = (ItemSourceType & ~ItemSourceType.VF & ~ItemSourceType.HD) | ItemSourceType.VO);
SwitchToHD = new Command(() => ItemSourceType = (ItemSourceType & ~ItemSourceType.VF & ~ItemSourceType.VO) | ItemSourceType.HD);
AboutCommand = new Command(async () => await Main.Navigator.GotoAboutPageAsync());
SearchCommand = new Command<string>((text) =>
SearchCommand = new Command<string>(async (text) =>
{
GetFilterViewModel(ItemSourceType).SearchText = text;
GetItemsViewModel(ItemSourceType).ExecuteLoadItemsCommand();
await GetItemsViewModel(ItemSourceType).ExecuteLoadItemsCommandAsync();
});
}
}

View file

@ -1,31 +1,15 @@
<?xml version="1.0" encoding="utf-8" ?>
<MasterDetailPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:AideDeJeu.Views"
xmlns:tools="clr-namespace:AideDeJeu.Tools"
x:Class="AideDeJeu.Views.MainPage"
x:Name="This"
IsPresented="False"
Title="">
<?xml version="1.0" encoding="utf-8"?>
<MasterDetailPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:local="clr-namespace:AideDeJeu.Views" xmlns:tools="clr-namespace:AideDeJeu.Tools" x:Class="AideDeJeu.Views.MainPage" x:Name="This" IsPresented="False" Title="">
<MasterDetailPage.Resources>
<ResourceDictionary>
<tools:ItemSourceTypeToStringConverter
x:Key="ItemSourceTypeToTitleConverter"
SpellVF="Sorts"
SpellVO="Spells"
SpellHD="Sorts (H&amp;D)"
MonsterVF="Monstres"
MonsterVO="Monsters"
MonsterHD="Créatures (H&amp;D)"
/>
<tools:ItemSourceTypeToItemsConverter
x:Key="ItemSourceTypeToItemsConverter" />
<tools:ItemSourceTypeToFilterConverter
x:Key="ItemSourceTypeToFilterConverter" />
<tools:ItemSourceTypeToStringConverter x:Key="ItemSourceTypeToTitleConverter" SpellVF="Sorts" SpellVO="Spells" SpellHD="Sorts (H&amp;D)" MonsterVF="Monstres" MonsterVO="Monsters" MonsterHD="Créatures (H&amp;D)" />
<tools:ItemSourceTypeToItemsConverter x:Key="ItemSourceTypeToItemsConverter" />
<tools:ItemSourceTypeToFilterConverter x:Key="ItemSourceTypeToFilterConverter" />
</ResourceDictionary>
</MasterDetailPage.Resources>
<MasterDetailPage.Master>
<ContentPage Title=" ">
<AbsoluteLayout>
<ListView ItemsSource="{Binding ItemSourceType, Converter={StaticResource ItemSourceTypeToFilterConverter}}" HasUnevenRows="True" RowHeight="-1" SeparatorVisibility="None" IsPullToRefreshEnabled="False">
<ListView.ItemTemplate>
<DataTemplate>
@ -40,6 +24,7 @@
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</AbsoluteLayout>
</ContentPage>
</MasterDetailPage.Master>
<MasterDetailPage.Detail>
@ -54,38 +39,27 @@
<ToolbarItem Name="VO" Text="VO AideDD/SRD" Order="Secondary" Command="{Binding SwitchToVO}" />
<ToolbarItem Name="HD" Text="VF H&amp;D" Order="Secondary" Command="{Binding SwitchToHD}" />
</ContentPage.ToolbarItems>
<AbsoluteLayout>
<StackLayout Orientation="Vertical">
<SearchBar x:Name="SearchBar" SearchCommand="{Binding SearchCommand}" SearchCommandParameter="{Binding Text, Source={x:Reference SearchBar}}">
<SearchBar.Behaviors>
<tools:TextChangedBehavior />
</SearchBar.Behaviors>
</SearchBar>
<ListView
x:Name="ItemsListView"
ItemsSource="{Binding Items}"
VerticalOptions="FillAndExpand"
HasUnevenRows="true"
IsRefreshing="{Binding IsBusy, Mode=OneWay}"
CachingStrategy="RecycleElement"
SelectedItem="{Binding SelectedItem}"
ItemTapped="ItemsListView_ItemTapped">
<ListView x:Name="ItemsListView" ItemsSource="{Binding Items}" VerticalOptions="FillAndExpand" HasUnevenRows="true" IsRefreshing="{Binding IsBusy, Mode=OneWay}" CachingStrategy="RecycleElement" SelectedItem="{Binding SelectedItem}" ItemTapped="ItemsListView_ItemTapped">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<StackLayout Padding="10">
<Label
Text="{Binding NamePHB}"
LineBreakMode="NoWrap"
Style="{DynamicResource subsubsection}"
FontSize="16" />
<Label Text="{Binding NamePHB}" LineBreakMode="NoWrap" Style="{DynamicResource subsubsection}" FontSize="16" />
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackLayout>
<ActivityIndicator AbsoluteLayout.LayoutBounds="0.5,0.5" AbsoluteLayout.LayoutFlags="XProportional, YProportional" IsRunning="{x:Binding IsLoading}" Color="Olive" />
</AbsoluteLayout>
</ContentPage>
</x:Arguments>
</NavigationPage>