|
|
|
@ -1,9 +1,14 @@
|
|
|
|
|
using Blazor.Models;
|
|
|
|
|
using Blazor.Pages;
|
|
|
|
|
using Blazored.LocalStorage;
|
|
|
|
|
using Blazorise.DataGrid;
|
|
|
|
|
using Blazorise;
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
|
|
namespace Blazor.Components
|
|
|
|
|
{
|
|
|
|
@ -16,20 +21,60 @@ namespace Blazor.Components
|
|
|
|
|
[Inject]
|
|
|
|
|
internal IJSRuntime JavaScriptRuntime { get; set; }
|
|
|
|
|
|
|
|
|
|
[Inject]
|
|
|
|
|
public ILocalStorageService LocalStorage { get; set; }
|
|
|
|
|
|
|
|
|
|
public Inventory()
|
|
|
|
|
{
|
|
|
|
|
Actions = new ObservableCollection<InventoryAction>();
|
|
|
|
|
Actions.CollectionChanged += OnActionsCollectionChanged;
|
|
|
|
|
}
|
|
|
|
|
public void update()
|
|
|
|
|
public async void update()
|
|
|
|
|
{
|
|
|
|
|
await LocalStorage.SetItemAsync("list", inventory);
|
|
|
|
|
this.StateHasChanged();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private void OnActionsCollectionChanged(object? sender, NotifyCollectionChangedEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
JavaScriptRuntime.InvokeVoidAsync("Crafting.AddActions", e.NewItems);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
protected override async Task OnAfterRenderAsync(bool firstRender)
|
|
|
|
|
{
|
|
|
|
|
// Do not treat this action if is not the first render
|
|
|
|
|
if (!firstRender)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var currentData = await LocalStorage.GetItemAsync<Item[]>("data");
|
|
|
|
|
|
|
|
|
|
// Check if data exist in the local storage
|
|
|
|
|
if (currentData == null)
|
|
|
|
|
{
|
|
|
|
|
// this code add in the local storage the fake data (we load the data sync for initialize the data before load the OnReadData method)
|
|
|
|
|
await LocalStorage.SetItemAsync("list", inventory);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async Task OnReadData(DataGridReadDataEventArgs<Item> e)
|
|
|
|
|
{
|
|
|
|
|
if (e.CancellationToken.IsCancellationRequested)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var response = (await LocalStorage.GetItemAsync<Models.InventoryList>("list"));
|
|
|
|
|
|
|
|
|
|
if (!e.CancellationToken.IsCancellationRequested)
|
|
|
|
|
{
|
|
|
|
|
inventory = response;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|