1
0
Fork 0
mirror of https://github.com/Nioux/AideDeJeu.git synced 2025-10-29 14:35:45 +00:00
This commit is contained in:
Yan Maniez 2019-09-30 18:28:46 +02:00
parent ad9344567c
commit bf01af25ee
3 changed files with 205 additions and 5 deletions

View file

@ -0,0 +1,162 @@
using AideDeJeuLib;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Input;
using Xamarin.Forms;
namespace AideDeJeu.ViewModels.Library
{
public class ItemViewModel : BaseViewModel
{
Item _Item = null;
public Item Item
{
get { return _Item; }
set
{
SetProperty(ref _Item, value);
}
}
CancellationTokenSource cancellationTokenSource;
public ItemViewModel(Item item = null)
{
Title = item?.Name;
Item = item;
LoadItemsCommand = new Command(async () => await ExecuteLoadItemsCommandAsync().ConfigureAwait(false));
SearchCommand = new Command<string>((text) =>
{
if (Filter != null)
{
Filter.SearchText = text;
}
});
}
public Command<string> SearchCommand { get; private set; }
public ICommand LoadItemsCommand { get; protected set; }
private FilterViewModel _Filter;
public FilterViewModel Filter
{
get
{
return _Filter;
}
set
{
SetProperty(ref _Filter, value);
if (_Filter != null)
{
_Filter.LoadItemsCommand = LoadItemsCommand;
}
}
}
public Item _Items = new Item();
public Item Items
{
get
{
return _Items;
}
set
{
SetProperty(ref _Items, value);
}
}
public IEnumerable<Item> _Children = new List<Item>();
public IEnumerable<Item> Children
{
get
{
return _Children;
}
set
{
SetProperty(ref _Children, value);
}
}
private Item _SelectedItem;
public Item SelectedItem
{
get
{
return _SelectedItem;
}
set
{
SetProperty(ref _SelectedItem, value);
if (_SelectedItem != null)
{
if (_SelectedItem is LinkItem)
{
Main.Navigator.NavigateToLinkAsync("/" + (_SelectedItem as LinkItem).Link).ConfigureAwait(true);
}
else
{
Main.Navigator.GotoItemDetailPageAsync(_SelectedItem).ConfigureAwait(true);
}
}
}
}
private Item _AllItems;
public Item AllItems
{
get
{
return _AllItems;
}
set
{
_AllItems = value;
if (_AllItems != null)
{
Title = _AllItems.Name;
Filter = _AllItems.GetNewFilterViewModel();
}
}
}
async Task LoadItemsAsync(CancellationToken cancellationToken = default)
{
try
{
if (Filter != null)
{
var start = DateTime.Now;
var items = await Task.Run(async () => await Filter.GetFilteredItemsAsync(cancellationToken: cancellationToken));
var end = DateTime.Now;
Debug.WriteLine((end - start).TotalMilliseconds);
Items = new Item(items.ToList());
Children = items;
}
else
{
Items = AllItems;
//Children = await AllItems.GetChildrenAsync();
}
}
catch (OperationCanceledException ex)
{
Debug.WriteLine(ex);
}
}
public async Task ExecuteLoadItemsCommandAsync()
{
cancellationTokenSource?.Cancel();
cancellationTokenSource = new CancellationTokenSource();
await LoadItemsAsync(cancellationTokenSource.Token);
}
}
}

View file

@ -11,6 +11,7 @@
<ResourceDictionary>
<tools:MonsterMarkdownTheme x:Key="MonsterMarkdownTheme" />
<tools:NullToFalseConverter x:Key="NullToFalseConverter" />
<tools:HeaderLevelToStyleConverter x:Key="HeaderLevelToStyleConverter" />
</ResourceDictionary>
</ContentPage.Resources>
<ContentPage.ToolbarItems>
@ -18,13 +19,32 @@
<ToolbarItem Name="Print" Text="Générer un PDF" Order="Primary" Icon="scroll_unfurled.png" Command="{Binding Main.Navigator.GeneratePDFCommand}" CommandParameter="{Binding Item.Markdown}" />
</ContentPage.ToolbarItems>
<Grid>
<ScrollView Orientation="Vertical" BackgroundColor="{StaticResource HDWhite}">
<!--<ScrollView Orientation="Vertical" BackgroundColor="{StaticResource HDWhite}">
<mdview:MarkdownView
Theme="{StaticResource MonsterMarkdownTheme}"
Markdown="{Binding Item.Item.Markdown}"
NavigateToLinkCommand="{Binding Main.Navigator.NavigateToLinkCommand}"
/>
</ScrollView>
</ScrollView>-->
<ListView Grid.Column="0" Grid.Row="0" x:Name="ItemsListView" ItemsSource="{Binding Item.Children}" VerticalOptions="FillAndExpand" HasUnevenRows="true" CachingStrategy="RecycleElement" SelectedItem="{Binding Item.SelectedItem}" ItemTapped="ItemsListView_ItemTapped">
<ListView.Header>
<mdview:MarkdownView
Theme="{StaticResource MonsterMarkdownTheme}"
Markdown="{Binding Item.Item.Markdown}"
NavigateToLinkCommand="{Binding Main.Navigator.NavigateToLinkCommand}"
/>
</ListView.Header>
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<StackLayout Padding="10" Orientation="Vertical">
<Label Text="{Binding Name}" LineBreakMode="WordWrap" TextColor="{StaticResource HDBlue}" Style="{Binding NameLevel,Converter={StaticResource HeaderLevelToStyleConverter}, ConverterParameter=2}" />
<Label Text="{Binding AltNameText}" LineBreakMode="WordWrap" TextColor="{StaticResource HDLightGrey}" Style="{Binding NameLevel,Converter={StaticResource HeaderLevelToStyleConverter}, ConverterParameter=4}" />
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
<ActivityIndicator
VerticalOptions="StartAndExpand"
HorizontalOptions="End"

View file

@ -28,8 +28,8 @@ namespace AideDeJeu.Views.Library
}
}
private ItemDetailViewModel _Item = null;
public ItemDetailViewModel Item
private ItemViewModel _Item = null;
public ItemViewModel Item
{
get
{
@ -99,8 +99,19 @@ namespace AideDeJeu.Views.Library
{
var items = item; // as Items;
var filterViewModel = items.GetNewFilterViewModel();
var itemsViewModel = new ItemDetailViewModel() { Item = items };
var itemsViewModel = new ItemViewModel() { Item = items, AllItems = items, Filter = filterViewModel };
this.Item = itemsViewModel;
itemsViewModel.LoadItemsCommand.Execute(null);
if (!string.IsNullOrEmpty(with))
{
var swith = with.Split('_');
for (int i = 0; i < swith.Length / 2; i++)
{
var key = swith[i * 2 + 0];
var val = swith[i * 2 + 1];
filterViewModel.FilterWith(key, val);
}
}
//SwitchToMainTab();
//if (filterViewModel == null)
//{
@ -118,6 +129,13 @@ namespace AideDeJeu.Views.Library
}
}
private void ItemsListView_ItemTapped(object sender, ItemTappedEventArgs e)
{
if (e.Item == null) return;
((ListView)sender).SelectedItem = null;
}
protected bool SetProperty<T>(ref T backingStore, T value,
[CallerMemberName]string propertyName = "",
Action onChanged = null)