mirror of
https://github.com/Nioux/AideDeJeu.git
synced 2025-12-15 22:59:39 +00:00
Deeeeeeep search ;)
This commit is contained in:
parent
dc4b6fe364
commit
c1f0c66283
7 changed files with 181 additions and 6 deletions
|
|
@ -332,6 +332,9 @@
|
|||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<EmbeddedResource Update="Views\DeepSearchPage.xaml">
|
||||
<Generator>MSBuild:UpdateDesignTimeXaml</Generator>
|
||||
</EmbeddedResource>
|
||||
<EmbeddedResource Update="Views\MainNavigationPage.xaml">
|
||||
<Generator>MSBuild:UpdateDesignTimeXaml</Generator>
|
||||
</EmbeddedResource>
|
||||
|
|
|
|||
|
|
@ -15,6 +15,11 @@ namespace AideDeJeuLib
|
|||
_Items = items;
|
||||
}
|
||||
|
||||
public Items(IEnumerable<Item> items)
|
||||
{
|
||||
_Items = items.ToList();
|
||||
}
|
||||
|
||||
public Items()
|
||||
{
|
||||
_Items = new List<Item>();
|
||||
|
|
|
|||
43
AideDeJeu/AideDeJeu/ViewModels/DeepSearchViewModel.cs
Normal file
43
AideDeJeu/AideDeJeu/ViewModels/DeepSearchViewModel.cs
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
using AideDeJeuLib;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows.Input;
|
||||
using Xamarin.Forms;
|
||||
|
||||
namespace AideDeJeu.ViewModels
|
||||
{
|
||||
public class DeepSearchViewModel : BaseViewModel
|
||||
{
|
||||
private Command<string> _SearchCommand = null;
|
||||
public ICommand SearchCommand
|
||||
{
|
||||
get
|
||||
{
|
||||
return _SearchCommand ?? (_SearchCommand = new Command<string>(async (searchText) => await ExecuteSearchCommandAsync(searchText)));
|
||||
}
|
||||
}
|
||||
|
||||
public async Task ExecuteSearchCommandAsync(string searchText)
|
||||
{
|
||||
Main.IsLoading = true;
|
||||
await Task.Run(async () => await Main.PreloadAllItemsAsync());
|
||||
Items = await Task.Run(async () => await Main.DeepSearchAllItemsAsync(searchText));
|
||||
Main.IsLoading = false;
|
||||
}
|
||||
|
||||
public IEnumerable<MainViewModel.SearchedItem> _Items = null;
|
||||
public IEnumerable<MainViewModel.SearchedItem> Items
|
||||
{
|
||||
get
|
||||
{
|
||||
return _Items;
|
||||
}
|
||||
set
|
||||
{
|
||||
SetProperty(ref _Items, value);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -85,6 +85,46 @@ namespace AideDeJeu.ViewModels
|
|||
}
|
||||
}
|
||||
|
||||
public class SearchedItem
|
||||
{
|
||||
public string Preview { get; set; }
|
||||
public Item Item { get; set; }
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<SearchedItem>> DeepSearchAllItemsAsync(string searchText)
|
||||
{
|
||||
List<SearchedItem> primaryItems = new List<SearchedItem>();
|
||||
List<SearchedItem> secondaryItems = new List<SearchedItem>();
|
||||
var cleanSearchText = Tools.Helpers.RemoveDiacritics(searchText).ToLower();
|
||||
foreach (var allItem in _AllItems)
|
||||
{
|
||||
foreach(var item in allItem.Value.Anchors)
|
||||
{
|
||||
var name = item.Value.Name;
|
||||
var cleanName = Tools.Helpers.RemoveDiacritics(name).ToLower();
|
||||
if (cleanName.Contains(cleanSearchText))
|
||||
{
|
||||
primaryItems.Add(new SearchedItem() { Item = item.Value, Preview = name });
|
||||
}
|
||||
else
|
||||
{
|
||||
var markdown = item.Value.Markdown;
|
||||
var cleanMarkdown = Tools.Helpers.RemoveDiacritics(markdown).ToLower();
|
||||
if (cleanMarkdown.Contains(cleanSearchText))
|
||||
{
|
||||
int position = cleanMarkdown.IndexOf(cleanSearchText);
|
||||
int startPosition = Math.Max(0, position - 30);
|
||||
int endPosition = Math.Min(markdown.Length, position + searchText.Length + 30);
|
||||
var preview = markdown.Substring(startPosition, endPosition - startPosition - 1);
|
||||
secondaryItems.Add(new SearchedItem() { Item = item.Value, Preview = preview });
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
primaryItems.AddRange(secondaryItems);
|
||||
return primaryItems;
|
||||
}
|
||||
|
||||
public async Task<Item> GetItemFromDataAsync(string source, string anchor)
|
||||
{
|
||||
//await Task.Delay(3000);
|
||||
|
|
|
|||
|
|
@ -48,12 +48,7 @@ namespace AideDeJeu.ViewModels
|
|||
|
||||
public async Task GotoDeepSearchPageAsync()
|
||||
{
|
||||
Main.IsLoading = true;
|
||||
// à remplacer par gros chargement
|
||||
await Task.Run(async () => await Main.PreloadAllItemsAsync());
|
||||
//await Task.Run(async () => await Task.Delay(5000));
|
||||
Main.IsLoading = false;
|
||||
//await Navigation.PushAsync(new Views.DeepSearchPage());
|
||||
await Navigation.PushAsync(new Views.DeepSearchPage());
|
||||
}
|
||||
|
||||
public async Task GotoItemDetailPageAsync(Item item)
|
||||
|
|
|
|||
52
AideDeJeu/AideDeJeu/Views/DeepSearchPage.xaml
Normal file
52
AideDeJeu/AideDeJeu/Views/DeepSearchPage.xaml
Normal file
|
|
@ -0,0 +1,52 @@
|
|||
<?xml version="1.0" encoding="utf-8" ?>
|
||||
<ContentPage
|
||||
xmlns="http://xamarin.com/schemas/2014/forms"
|
||||
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
|
||||
x:Class="AideDeJeu.Views.DeepSearchPage"
|
||||
xmlns:tools="clr-namespace:AideDeJeu.Tools"
|
||||
x:Name="This"
|
||||
BackgroundColor="{StaticResource bgtan}" >
|
||||
<ContentPage.ToolbarItems>
|
||||
<ToolbarItem Name="DeepSearch" Text="Rechercher..." Order="Primary" Icon="crystal_ball.png" Command="{Binding Main.Navigator.DeepSearchCommand}" />
|
||||
<ToolbarItem Name="About" Text="À propos de..." Order="Secondary" Icon="wooden_sign.png" Command="{Binding Main.Navigator.AboutCommand}" />
|
||||
</ContentPage.ToolbarItems>
|
||||
<ContentPage.Content>
|
||||
<Grid>
|
||||
<Grid.RowDefinitions>
|
||||
<RowDefinition Height="Auto" />
|
||||
<RowDefinition Height="*" />
|
||||
</Grid.RowDefinitions>
|
||||
<SearchBar Grid.Column="0" Grid.Row="0" x:Name="SearchBar" HeightRequest="42" SearchCommand="{Binding SearchCommand}" SearchCommandParameter="{Binding Text, Source={x:Reference SearchBar}}">
|
||||
<!--<SearchBar.Behaviors>
|
||||
<tools:TextChangedBehavior />
|
||||
</SearchBar.Behaviors>-->
|
||||
</SearchBar>
|
||||
<ListView Grid.Column="0" Grid.Row="1" x:Name="ItemsListView" ItemsSource="{Binding Items}" VerticalOptions="FillAndExpand" HasUnevenRows="true" CachingStrategy="RecycleElement" SelectedItem="{Binding SelectedItem}" ItemTapped="ItemsListView_ItemTapped">
|
||||
<ListView.ItemTemplate>
|
||||
<DataTemplate>
|
||||
<ViewCell AutomationProperties.IsInAccessibleTree="True" AutomationId="machin" AutomationProperties.Name="hop">
|
||||
<StackLayout Padding="10" Orientation="Vertical">
|
||||
<Label Text="{Binding Preview}" LineBreakMode="WordWrap" Style="{DynamicResource subsubsection}" FontSize="16" />
|
||||
<Label Text="{Binding Item.Name}" LineBreakMode="WordWrap" Style="{DynamicResource subsubsection}" FontSize="12" />
|
||||
</StackLayout>
|
||||
</ViewCell>
|
||||
</DataTemplate>
|
||||
</ListView.ItemTemplate>
|
||||
</ListView>
|
||||
<ActivityIndicator
|
||||
Grid.Column="0" Grid.Row="0"
|
||||
VerticalOptions="StartAndExpand"
|
||||
HorizontalOptions="End"
|
||||
Color="{StaticResource titlered}"
|
||||
IsRunning="{Binding BindingContext.Main.IsLoading, Source={x:Reference This}}"
|
||||
IsVisible="{Binding BindingContext.Main.IsLoading, Source={x:Reference This}}">
|
||||
<ActivityIndicator.WidthRequest>
|
||||
<OnPlatform x:TypeArguments="x:Double" iOS="50" WinPhone="400" Android="50" />
|
||||
</ActivityIndicator.WidthRequest>
|
||||
<ActivityIndicator.HeightRequest>
|
||||
<OnPlatform x:TypeArguments="x:Double" iOS="50" WinPhone="10" Android="50" />
|
||||
</ActivityIndicator.HeightRequest>
|
||||
</ActivityIndicator>
|
||||
</Grid>
|
||||
</ContentPage.Content>
|
||||
</ContentPage>
|
||||
37
AideDeJeu/AideDeJeu/Views/DeepSearchPage.xaml.cs
Normal file
37
AideDeJeu/AideDeJeu/Views/DeepSearchPage.xaml.cs
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
using AideDeJeu.ViewModels;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
using Xamarin.Forms;
|
||||
using Xamarin.Forms.Xaml;
|
||||
|
||||
namespace AideDeJeu.Views
|
||||
{
|
||||
[XamlCompilation(XamlCompilationOptions.Compile)]
|
||||
public partial class DeepSearchPage : ContentPage
|
||||
{
|
||||
MainViewModel Main
|
||||
{
|
||||
get
|
||||
{
|
||||
return DependencyService.Get<MainViewModel>();
|
||||
}
|
||||
}
|
||||
|
||||
public DeepSearchPage ()
|
||||
{
|
||||
InitializeComponent ();
|
||||
|
||||
BindingContext = new DeepSearchViewModel();
|
||||
}
|
||||
|
||||
private async void ItemsListView_ItemTapped(object sender, ItemTappedEventArgs e)
|
||||
{
|
||||
var searchedItem = e.Item as MainViewModel.SearchedItem;
|
||||
await Main.Navigator.GotoItemDetailPageAsync(searchedItem.Item);
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue