diff --git a/BlazorApp/BlazorApp/Components/InventoryComp.razor b/BlazorApp/BlazorApp/Components/InventoryComp.razor index 5ed6408..dc7879f 100644 --- a/BlazorApp/BlazorApp/Components/InventoryComp.razor +++ b/BlazorApp/BlazorApp/Components/InventoryComp.razor @@ -12,24 +12,30 @@
- - - - - - - - - - - - - - - - - - + + @for(int i=0; i + }
@@ -43,7 +49,7 @@
- +
(new Item[18]); + + + } + + protected override void OnInitialized() + { + this.InventoryItems = new List(new Item[InventorySize]); } + + private void OnActionsCollectionChanged(object? sender, NotifyCollectionChangedEventArgs e) { JavaScriptRuntime.InvokeVoidAsync("Crafting.AddActions", e.NewItems); @@ -58,17 +74,23 @@ namespace BlazorApp.Components { return; } - if (!e.CancellationToken.IsCancellationRequested && !IsSorted) + + CurrentPage = e.Page; + PageSize = e.PageSize; + + if (!e.CancellationToken.IsCancellationRequested && !IsSorted && string.IsNullOrWhiteSpace(Recherche)) { Items = await DataService.List(e.Page, e.PageSize); totalItem = await DataService.Count(); - CurrentPage = e.Page; - PageSize = e.PageSize; + + } + else if (!string.IsNullOrWhiteSpace(Recherche)) + { + (Items, totalItem) = await DataService.SearchList(CurrentPage, PageSize, Recherche); + } else { - CurrentPage = e.Page; - PageSize = e.PageSize; Items = await DataService.SortedList(CurrentPage, PageSize); } } @@ -82,8 +104,26 @@ namespace BlazorApp.Components } } - void refresh() + private async void OnInput(Microsoft.AspNetCore.Components.ChangeEventArgs args) { + Recherche = (string)args.Value; + + if (string.IsNullOrWhiteSpace(Recherche)) + { + if (!IsSorted) + { + Items = await DataService.List(CurrentPage, PageSize); + } + else + { + Items = await DataService.SortedList(CurrentPage, PageSize); + } + totalItem = await DataService.Count(); + } + else + { + (Items, totalItem) = await DataService.SearchList(CurrentPage, PageSize, Recherche); + } StateHasChanged(); } diff --git a/BlazorApp/BlazorApp/Components/InventoryItem.razor b/BlazorApp/BlazorApp/Components/InventoryItem.razor index 7c66705..6531023 100644 --- a/BlazorApp/BlazorApp/Components/InventoryItem.razor +++ b/BlazorApp/BlazorApp/Components/InventoryItem.razor @@ -22,7 +22,8 @@ } else { - @Item.DisplayName + @Item.DisplayName; + @NbItem } } diff --git a/BlazorApp/BlazorApp/Components/InventoryItem.razor.cs b/BlazorApp/BlazorApp/Components/InventoryItem.razor.cs index 9ccfd8e..9ef1d2d 100644 --- a/BlazorApp/BlazorApp/Components/InventoryItem.razor.cs +++ b/BlazorApp/BlazorApp/Components/InventoryItem.razor.cs @@ -13,9 +13,12 @@ namespace BlazorApp.Components [Parameter] public Item Item { get; set; } + public int NbItem { get; set; } + [Parameter] public bool NoDrop { get; set; } + [CascadingParameter] public InventoryComp Parent { get; set; } @@ -46,6 +49,26 @@ namespace BlazorApp.Components if (Parent.CurrentDragItem != null) { + if (Parent.CurrentDragItem.Equals(Item)) + { + if (NbItem + Parent.CurrentDragNbItem > Item.StackSize) + { + int nbTrop = NbItem + Parent.CurrentDragNbItem - Item.StackSize; + NbItem = Item.StackSize; + Parent.CurrentDragNbItem = nbTrop; + } + else + { + NbItem = NbItem + Parent.CurrentDragNbItem; + Parent.CurrentDragNbItem = -1; + } + } + else + { + int changement = NbItem; + NbItem = Parent.CurrentDragNbItem; + Parent.CurrentDragNbItem = changement; + } Item tmp = this.Item; this.Item = Parent.CurrentDragItem; Parent.InventoryItems[this.Index] = this.Item; @@ -59,18 +82,21 @@ namespace BlazorApp.Components private void OnDragStart() { - Parent.CurrentDragItem = this.Item; if (this.Item != null) { + Parent.CurrentDragItem = this.Item; if (!NoDrop) { Parent.CurrentDragIndex = this.Index; + Parent.CurrentDragNbItem = NbItem; this.Item = null; + NbItem = -1; Parent.InventoryItems[Index] = null; } else { Parent.CurrentDragIndex = -1; + Parent.CurrentDragNbItem = 1; } } @@ -84,10 +110,11 @@ namespace BlazorApp.Components { return; } - if (Parent.CurrentDragIndex != -1 && Parent.CurrentDragIndex!=this.Index) + if (Parent.CurrentDragIndex != -1 && Parent.CurrentDragIndex!=this.Index && Parent.CurrentDragNbItem!=-1) { Parent.InventoryItems[this.Index] = this.Item; this.Item = Parent.CurrentDragItem; + NbItem = Parent.CurrentDragNbItem; } Parent.Actions.Add(new CraftingAction { Action = "Drag End", Item = this.Item, Index = this.Index }); diff --git a/BlazorApp/BlazorApp/Pages/Inventory.razor b/BlazorApp/BlazorApp/Pages/Inventory.razor index 3f9c38d..3b4b541 100644 --- a/BlazorApp/BlazorApp/Pages/Inventory.razor +++ b/BlazorApp/BlazorApp/Pages/Inventory.razor @@ -5,6 +5,6 @@
- +
diff --git a/BlazorApp/BlazorApp/Services/DataApiService.cs b/BlazorApp/BlazorApp/Services/DataApiService.cs index 5cb5fc3..06a95cb 100644 --- a/BlazorApp/BlazorApp/Services/DataApiService.cs +++ b/BlazorApp/BlazorApp/Services/DataApiService.cs @@ -1,7 +1,9 @@ using System; +using System.Collections.Generic; using BlazorApp.Components; using BlazorApp.Factories; using BlazorApp.Models; +using static System.Formats.Asn1.AsnWriter; namespace BlazorApp.Services { @@ -61,14 +63,47 @@ namespace BlazorApp.Services { List it = await _http.GetFromJsonAsync>($"https://localhost:7234/api/Crafting/all/"); it = it.OrderBy(i => i.DisplayName).ToList(); - if ((currentPage - 1) * 10+ pageSize > it.Count) + + int indexDeb = (currentPage - 1) * pageSize; + + if (indexDeb + pageSize > it.Count) { - int tmp = ((currentPage - 1) * 10 + pageSize) - it.Count; - return it.GetRange((currentPage - 1) * 10, pageSize - tmp); + int tmp = indexDeb + pageSize - it.Count; + return it.GetRange(indexDeb, pageSize - tmp); } - return it.GetRange((currentPage - 1) * 10, pageSize); + return it.GetRange(indexDeb, pageSize); } - + + public async Task<(List, int)> SearchList(int currentPage, int pageSize, string recherche) + { + IEnumerable it = await _http.GetFromJsonAsync>($"https://localhost:7234/api/Crafting/all/"); + it = it.OrderBy(i => i.DisplayName).ToList(); + it = from item in it + where item.DisplayName.StartsWith(recherche) + select item; + + + if (currentPage*10 > it.Count()) + { + currentPage = it.Count() / pageSize + 1; + } + + int indexDeb = (currentPage - 1) * pageSize; + + if (it.Count() == 0) + { + return (it.ToList(), it.Count()); + } + + if (indexDeb + pageSize > it.Count()) + { + int tmp = (indexDeb + pageSize) - it.Count(); + return( it.ToList().GetRange(indexDeb, pageSize - tmp), it.Count()); + } + return (it.ToList().GetRange(indexDeb, pageSize), it.Count()); + + } + } } \ No newline at end of file diff --git a/BlazorApp/BlazorApp/Services/DataLocalService.cs b/BlazorApp/BlazorApp/Services/DataLocalService.cs index d599597..4950ef9 100644 --- a/BlazorApp/BlazorApp/Services/DataLocalService.cs +++ b/BlazorApp/BlazorApp/Services/DataLocalService.cs @@ -189,6 +189,12 @@ namespace BlazorApp.Services { throw new NotImplementedException(); } + + public async Task<(List, int)> SearchList(int currentPage, int pageSize, string recherche) + { + throw new NotImplementedException(); + + } } } diff --git a/BlazorApp/BlazorApp/Services/IDataService.cs b/BlazorApp/BlazorApp/Services/IDataService.cs index 12d2cdb..a82d6d7 100644 --- a/BlazorApp/BlazorApp/Services/IDataService.cs +++ b/BlazorApp/BlazorApp/Services/IDataService.cs @@ -15,6 +15,7 @@ namespace BlazorApp.Services Task Delete(int id); Task> GetRecipes(); Task> SortedList(int currentPage, int pageSize); + Task<(List, int)> SearchList(int currentPage, int pageSize, string recherche); } }