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 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<String>("list", serial);
return;
}
@ -53,11 +54,12 @@ namespace Blazor.Components
{
return;
}
var response = await JSRuntime.InvokeAsync<List<InventoryItem>>("localStorage.getItemAsync", "list");
var response = await LocalStorage.GetItemAsync<String>("list");
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.Actions.Add(new InventoryAction("On drag start", Item.Id, Item));
Parent.update();
return;
}
}

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

@ -1,4 +1,5 @@
using Blazor.Models;
using Blazored.LocalStorage;
using Blazorise.Extensions;
using Microsoft.AspNetCore.Components;
@ -18,47 +19,49 @@ 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<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;
}
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;
}
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<Models.InventoryItem>("data" + this.Index, this.Items);
Parent.Actions.Add(new InventoryAction("On drag drop",this.Index,Items.item));
return;
}
else
@ -68,17 +71,15 @@ namespace Blazor.Components
{
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<Models.InventoryItem>("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<Models.InventoryItem>("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<InventoryItem>("item" + this.Index);
StateHasChanged();
}
}
}

Loading…
Cancel
Save