keep items in memory
continuous-integration/drone/push Build is passing Details

master
Aurian JAULT 2 years ago
parent 54860c3528
commit 909f0b01e0

@ -7,8 +7,8 @@ using Microsoft.AspNetCore.Components;
using Microsoft.JSInterop; using Microsoft.JSInterop;
using System.Collections.ObjectModel; using System.Collections.ObjectModel;
using System.Collections.Specialized; using System.Collections.Specialized;
using static System.Net.WebRequestMethods; using System.Text.Json;
using Microsoft.AspNetCore.Components.Server.ProtectedBrowserStorage; using Blazor.Data;
namespace Blazor.Components namespace Blazor.Components
{ {
@ -35,7 +35,8 @@ namespace Blazor.Components
public async void update() public async void update()
{ {
this.StateHasChanged(); this.StateHasChanged();
await JavaScriptRuntime.InvokeVoidAsync("localStorage.setItem", "list", inventory.inventoryItems); var serial = JsonSerializer.Serialize(inventory);
await LocalStorage.SetItemAsync<String>("list", serial);
return; return;
} }
@ -53,11 +54,12 @@ namespace Blazor.Components
{ {
return; return;
} }
var response = await JSRuntime.InvokeAsync<List<InventoryItem>>("localStorage.getItemAsync", "list");
var response = await LocalStorage.GetItemAsync<String>("list");
if (!e.CancellationToken.IsCancellationRequested) if (!e.CancellationToken.IsCancellationRequested)
{ {
inventory.inventoryItems = response; inventory = JsonSerializer.Deserialize<Models.InventoryList>(response);
} }
} }

@ -36,7 +36,6 @@ namespace Blazor.Components
{ {
Parent.CurrentDragItem = new InventoryItem(Item); Parent.CurrentDragItem = new InventoryItem(Item);
Parent.Actions.Add(new InventoryAction("On drag start", Item.Id, Item)); Parent.Actions.Add(new InventoryAction("On drag start", Item.Id, Item));
Parent.update();
return; return;
} }
} }

@ -1,7 +1,7 @@
<div class="item" <div class="item"
ondragover="event.preventDefault();" ondragover="event.preventDefault();"
@ondragstart="@OnDragStart" @ondragstart="@OnDragStartAsync"
@ondrop="@OnDrop" @ondrop="@OnDropAsync"
@ondragenter="@OnDragEnter" @ondragenter="@OnDragEnter"
@ondragleave="@OnDragLeave"> @ondragleave="@OnDragLeave">
@if(Items != null) @if(Items != null)

@ -1,4 +1,5 @@
using Blazor.Models; using Blazor.Models;
using Blazored.LocalStorage;
using Blazorise.Extensions; using Blazorise.Extensions;
using Microsoft.AspNetCore.Components; using Microsoft.AspNetCore.Components;
@ -18,67 +19,67 @@ namespace Blazor.Components
[CascadingParameter] [CascadingParameter]
public Inventory Parent { get; set; } 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; Parent.CurrentDragItem = null;
return; return;
} }
Parent.CurrentDragItem = this.Items; 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.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<Models.InventoryItem>("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; return;
} }
if(Parent.CurrentDragItem == null)
if(this.Items == null)
{ {
this.Items = Parent.CurrentDragItem;
Parent.CurrentDragItem = null;
await LocalStorage.SetItemAsync<Models.InventoryItem>("data" + this.Index, this.Items);
Parent.Actions.Add(new InventoryAction("On drag drop",this.Index,Items.item));
return; return;
} }
if (Parent.CurrentDragItem.item.Id != this.Items.item.Id) if (Parent.CurrentDragItem.item.Id != this.Items.item.Id)
{ {
this.Items = Parent.CurrentDragItem; this.Items = Parent.CurrentDragItem;
this.NoDrop= true;
Parent.CurrentDragItem = null; Parent.CurrentDragItem = null;
Parent.inventory.inventoryItems[this.Index] = this.Items; await LocalStorage.SetItemAsync<Models.InventoryItem>("data" + this.Index, this.Items);
Parent.Actions.Add(new InventoryAction("On drag start",this.Index,Items.item)); Parent.Actions.Add(new InventoryAction("On drag drop",this.Index,Items.item));
Parent.update();
return; return;
} }
else else
{ {
int total = Parent.CurrentDragItem.Stack + this.Items.Stack; 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; this.Items.Stack = this.Items.item.StackSize;
Parent.CurrentDragItem = null; Parent.CurrentDragItem = null;
Parent.Actions.Add(new InventoryAction("On drag start",this.Index,Items.item)); await LocalStorage.SetItemAsync<Models.InventoryItem>("data" + this.Index, this.Items);
Parent.inventory.inventoryItems[this.Index] = this.Items; Parent.Actions.Add(new InventoryAction("On drag drop",this.Index,Items.item));
Parent.update();
return; return;
} }
else else
{ {
this.Items.Stack = total; this.Items.Stack = total;
Parent.CurrentDragItem = null; Parent.CurrentDragItem = null;
Parent.inventory.inventoryItems[this.Index] = this.Items; await LocalStorage.SetItemAsync<Models.InventoryItem>("data" + this.Index, this.Items);
Parent.update();
return; return;
} }
} }
@ -94,5 +95,19 @@ namespace Blazor.Components
if (this.Items == null) return; if (this.Items == null) return;
Parent.Actions.Add(new InventoryAction("Drag Leave",this.Index,Items.item)); 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<InventoryItem>("item" + this.Index);
StateHasChanged();
}
} }
} }

Loading…
Cancel
Save