1
0
Fork 0
mirror of https://github.com/Nioux/AideDeJeu.git synced 2025-10-30 15:06:06 +00:00
This commit is contained in:
Yan Maniez 2018-08-22 13:13:51 +02:00
parent 649c1acd14
commit d738fc87b4
2 changed files with 68 additions and 32 deletions

View file

@ -1,4 +1,5 @@
using AideDeJeuLib;
using AideDeJeu.Tools;
using AideDeJeuLib;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
@ -18,20 +19,58 @@ namespace AideDeJeu.ViewModels
set => SetProperty(ref _isLoading, value);
}
private Dictionary<string, Item> _AllItems = new Dictionary<string, Item>();
public async Task<Item> GetItemFromDataAsync(string source)
void AddAnchor(Dictionary<string, Item> anchors, Item item)
{
if (item != null)
{
var basename = Helpers.IdFromName(item.Name);
var name = basename;
int index = 0;
while (true)
{
if (!anchors.ContainsKey(name))
{
anchors.Add(name, item);
return;
}
index++;
name = $"{basename}{index}";
}
}
}
void MakeAnchors(Dictionary<string, Item> anchors, Item baseItem)
{
AddAnchor(anchors, baseItem);
if(baseItem is Items)
{
foreach(var item in (baseItem as Items))
{
MakeAnchors(anchors, item);
}
}
}
public class ItemWithAnchors
{
public Item Item { get; set; }
public Dictionary<string, Item> Anchors { get; set; } = new Dictionary<string, Item>();
}
private Dictionary<string, ItemWithAnchors> _AllItems = new Dictionary<string, ItemWithAnchors>();
public async Task<Item> GetItemFromDataAsync(string source, string anchor)
{
if (!_AllItems.ContainsKey(source))
{
//var md = await Tools.Helpers.GetStringFromUrl($"https://raw.githubusercontent.com/Nioux/AideDeJeu/master/Data/{source}.md");
var md = await Tools.Helpers.GetResourceStringAsync($"AideDeJeu.Data.{source}.md");
//return Tools.MarkdownExtensions.ToItem(md);
if (md != null)
{
var item = Tools.MarkdownExtensions.ToItem(md);
if (item != null)
{
_AllItems[source] = item;
var anchors = new Dictionary<string, Item>();
MakeAnchors(anchors, item);
_AllItems[source] = new ItemWithAnchors() { Item = item, Anchors = anchors };
}
else
{
@ -43,7 +82,15 @@ namespace AideDeJeu.ViewModels
return null;
}
}
return _AllItems[source];
var itemWithAnchors = _AllItems[source];
if (!string.IsNullOrEmpty(anchor))
{
if (itemWithAnchors.Anchors.ContainsKey(anchor))
{
return itemWithAnchors.Anchors[anchor];
}
}
return itemWithAnchors.Item;
}
public Command LoadItemsCommand { get; private set; }

View file

@ -73,7 +73,7 @@ namespace AideDeJeu.ViewModels
var with = match.Groups["with"].Value;
Main.IsBusy = true;
Main.IsLoading = true;
var item = await Main.GetItemFromDataAsync(file);
var item = await Main.GetItemFromDataAsync(file, anchor);
Main.IsBusy = false;
Main.IsLoading = false;
if (item != null)
@ -81,16 +81,6 @@ namespace AideDeJeu.ViewModels
if (item is Items)
{
var items = item as Items;
if (!string.IsNullOrEmpty(anchor))
{
var subitem = items.Where(i => Tools.Helpers.IdFromName(i.Name) == anchor).FirstOrDefault();
if (subitem != null)
{
await GotoItemDetailPageAsync(subitem);
}
}
else
{
var filterViewModel = items.GetNewFilterViewModel();
var itemsViewModel = new ItemsViewModel() { AllItems = items, Filter = filterViewModel };
itemsViewModel.LoadItemsCommand.Execute(null);
@ -113,7 +103,6 @@ namespace AideDeJeu.ViewModels
await GotoFilteredItemsPageAsync(itemsViewModel);
}
}
}
else
{
await GotoItemDetailPageAsync(item);