From 909f0b01e09e488c928c18249223e6cc6dcb2220 Mon Sep 17 00:00:00 2001 From: aujault Date: Tue, 20 Dec 2022 17:52:10 +0100 Subject: [PATCH] keep items in memory --- Blazor/Blazor/Components/Inventory.razor.cs | 12 ++-- .../Components/InventoryListImage.razor.cs | 1 - Blazor/Blazor/Components/ItemInventory.razor | 4 +- .../Blazor/Components/ItemInventory.razor.cs | 67 ++++++++++++------- 4 files changed, 50 insertions(+), 34 deletions(-) diff --git a/Blazor/Blazor/Components/Inventory.razor.cs b/Blazor/Blazor/Components/Inventory.razor.cs index 71240a3..9d8689c 100644 --- a/Blazor/Blazor/Components/Inventory.razor.cs +++ b/Blazor/Blazor/Components/Inventory.razor.cs @@ -7,8 +7,8 @@ using Microsoft.AspNetCore.Components; using Microsoft.JSInterop; using System.Collections.ObjectModel; using System.Collections.Specialized; -using static System.Net.WebRequestMethods; -using Microsoft.AspNetCore.Components.Server.ProtectedBrowserStorage; +using System.Text.Json; +using Blazor.Data; namespace Blazor.Components { @@ -35,7 +35,8 @@ namespace Blazor.Components public async void update() { this.StateHasChanged(); - await JavaScriptRuntime.InvokeVoidAsync("localStorage.setItem", "list", inventory.inventoryItems); + var serial = JsonSerializer.Serialize(inventory); + await LocalStorage.SetItemAsync("list", serial); return; } @@ -53,11 +54,12 @@ namespace Blazor.Components { return; } - var response = await JSRuntime.InvokeAsync>("localStorage.getItemAsync", "list"); + + var response = await LocalStorage.GetItemAsync("list"); if (!e.CancellationToken.IsCancellationRequested) { - inventory.inventoryItems = response; + inventory = JsonSerializer.Deserialize(response); } } diff --git a/Blazor/Blazor/Components/InventoryListImage.razor.cs b/Blazor/Blazor/Components/InventoryListImage.razor.cs index a492163..a6b258c 100644 --- a/Blazor/Blazor/Components/InventoryListImage.razor.cs +++ b/Blazor/Blazor/Components/InventoryListImage.razor.cs @@ -36,7 +36,6 @@ namespace Blazor.Components { Parent.CurrentDragItem = new InventoryItem(Item); Parent.Actions.Add(new InventoryAction("On drag start", Item.Id, Item)); - Parent.update(); return; } } diff --git a/Blazor/Blazor/Components/ItemInventory.razor b/Blazor/Blazor/Components/ItemInventory.razor index 76de4c1..10f3e50 100644 --- a/Blazor/Blazor/Components/ItemInventory.razor +++ b/Blazor/Blazor/Components/ItemInventory.razor @@ -1,7 +1,7 @@ 
@if(Items != null) diff --git a/Blazor/Blazor/Components/ItemInventory.razor.cs b/Blazor/Blazor/Components/ItemInventory.razor.cs index 7a927b0..5de4720 100644 --- a/Blazor/Blazor/Components/ItemInventory.razor.cs +++ b/Blazor/Blazor/Components/ItemInventory.razor.cs @@ -1,4 +1,5 @@ using Blazor.Models; +using Blazored.LocalStorage; using Blazorise.Extensions; using Microsoft.AspNetCore.Components; @@ -18,67 +19,67 @@ namespace Blazor.Components [CascadingParameter] public Inventory Parent { get; set; } - public void OnDragStart() + [Inject] + public ILocalStorageService LocalStorage { get; set; } + + public async Task OnDragStartAsync() { - if(!this.NoDrop) + + if (this.Items == null) { Parent.CurrentDragItem = null; return; } + Parent.CurrentDragItem = this.Items; - Parent.inventory.inventoryItems[this.Index] = null; - this.NoDrop = false; - if (this.Items == null) return; Parent.Actions.Add(new InventoryAction("On drag start",this.Index,Items.item)); - Parent.update(); + this.Items = null; + Parent.inventory.inventoryItems[this.Index] = null ; + await LocalStorage.SetItemAsync("data" + this.Index, this.Items); } - public void OnDrop() + public async Task OnDropAsync() { - if (!this.NoDrop) + + if (Parent.CurrentDragItem == null) { - this.Items = Parent.CurrentDragItem; - this.NoDrop = true; - Parent.CurrentDragItem = null; - Parent.inventory.inventoryItems[this.Index] = this.Items; - if (this.Items == null) return; - Parent.Actions.Add(new InventoryAction("On Drop",this.Index,Items.item)); - Parent.update(); return; } - if(Parent.CurrentDragItem == null) + + if(this.Items == null) { + this.Items = Parent.CurrentDragItem; + Parent.CurrentDragItem = null; + await LocalStorage.SetItemAsync("data" + this.Index, this.Items); + Parent.Actions.Add(new InventoryAction("On drag drop",this.Index,Items.item)); return; } + if (Parent.CurrentDragItem.item.Id != this.Items.item.Id) { this.Items = Parent.CurrentDragItem; - this.NoDrop= true; Parent.CurrentDragItem = null; - Parent.inventory.inventoryItems[this.Index] = this.Items; - Parent.Actions.Add(new InventoryAction("On drag start",this.Index,Items.item)); - Parent.update(); + await LocalStorage.SetItemAsync("data" + this.Index, this.Items); + Parent.Actions.Add(new InventoryAction("On drag drop",this.Index,Items.item)); return; } else { int total = Parent.CurrentDragItem.Stack + this.Items.Stack; - if (total >this.Items.item.StackSize) + if (total > this.Items.item.StackSize) { this.Items.Stack = this.Items.item.StackSize; Parent.CurrentDragItem = null; - Parent.Actions.Add(new InventoryAction("On drag start",this.Index,Items.item)); - Parent.inventory.inventoryItems[this.Index] = this.Items; - Parent.update(); + await LocalStorage.SetItemAsync("data" + this.Index, this.Items); + Parent.Actions.Add(new InventoryAction("On drag drop",this.Index,Items.item)); return; } else { this.Items.Stack = total; Parent.CurrentDragItem = null; - Parent.inventory.inventoryItems[this.Index] = this.Items; - Parent.update(); + await LocalStorage.SetItemAsync("data" + this.Index, this.Items); return; } } @@ -94,5 +95,19 @@ namespace Blazor.Components if (this.Items == null) return; Parent.Actions.Add(new InventoryAction("Drag Leave",this.Index,Items.item)); } + + + protected override async Task OnAfterRenderAsync(bool firstRender) + { + base.OnAfterRenderAsync(firstRender); + + if (!firstRender) + { + return; + } + Items = await LocalStorage.GetItemAsync("item" + this.Index); + + StateHasChanged(); + } } }