Able to download whole data to local

This commit is contained in:
BOT Alex 2023-06-29 21:27:58 +02:00
parent d2ab28372f
commit 059088121e
5 changed files with 78 additions and 7 deletions

View file

@ -21,7 +21,7 @@
<MudContainer Class="justify-center d-flex">
@if (Charecters != null)
{
<MudPaper Class="overflow-scroll rounded-0" Style="height: 60vh;" Elevation="1">
<MudPaper Class="overflow-scroll rounded-0" Style="height: 50vh;" Elevation="1">
<MudDataGrid FixedHeader=true Items="@Charecters">
<Columns>
<PropertyColumn Property="x => x.charcter" Title="CChar" />
@ -31,15 +31,47 @@
</MudPaper>
}
</MudContainer>
<MudContainer Class="justify-center d-flex pt-4">
<MudButton Class="mt-1 mb-7" Variant="Variant.Filled" Color="Color.Secondary" Disabled="@(isLoading || isSavedLocally)" OnClick="LoadAllChunksIntoLocalStorage">
@if (!isSavedLocally)
{
@if (isLoading)
{
<MudText>@($"{savedChunks}/{numOfChunks}")</MudText>
<MudProgressCircular Class="pl-8 ms-n1 mr-2" Size="Size.Small" Indeterminate="true" />
}
else
{
<MudText>Load chunks to local storage</MudText>
<MudIcon Icon="@Icons.Material.Rounded.Send" Class="mr-1" />
}
}
else
{
<MudText>Chunks are saved locally</MudText>
}
</MudButton>
</MudContainer>
<MudContainer Class="justify-center d-flex pt-4">
<MudButton OnClick="async ()=>{await localStorage.ClearAsync(); navigator.NavigateTo(string.Empty, true);}" Variant="Variant.Outlined" Color="Color.Tertiary">Delete local storage</MudButton>
</MudContainer>
</MudContainer>
</MudContainer>
@code{
int numOfChunks = 197; //Exclusive index 0
int selectedChunk = 0;
int savedChunks = 0;
protected override void OnInitialized()
bool isLoading = false;
bool isSavedLocally = false;
protected async override Task OnInitializedAsync()
{
if (await localStorage.ContainKeyAsync("Normalized_chunk_001.json"))
isSavedLocally = true;
SelectedChunk();
StateHasChanged();
}
@ -53,7 +85,34 @@
CChar[]? Charecters;
async void SelectedChunk()
{
Charecters = await httpClient.GetFromJsonAsync<CChar[]>($"Data/Normalized_chunk_{selectedChunk.ToString("000")}.json");
if (await localStorage.ContainKeyAsync("Normalized_chunk_001.json"))
isSavedLocally = true;
if (!isSavedLocally)
Charecters = await httpClient.GetFromJsonAsync<CChar[]>($"Data/Normalized_chunk_{selectedChunk.ToString("000")}.json");
else
{
string json = await localStorage.GetItemAsync<string>($"Normalized_chunk_{selectedChunk.ToString("000")}.json");
Charecters = JsonConvert.DeserializeObject<CChar[]>(json);
}
StateHasChanged();
}
async void LoadAllChunksIntoLocalStorage()
{
isLoading = true;
for (int i = 0; i < numOfChunks; i++)
{
string? jsonChunk = await httpClient.GetStringAsync($"Data/Normalized_chunk_{i.ToString("000")}.json");
if (string.IsNullOrEmpty(jsonChunk)) continue;
await localStorage.SetItemAsync($"Normalized_chunk_{i.ToString("000")}.json", jsonChunk);
savedChunks = i + 1;
StateHasChanged();
}
isLoading = false;
isSavedLocally = true;
StateHasChanged();
}
}