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

@ -12,6 +12,7 @@
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="6.0.19" /> <PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="6.0.19" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.DevServer" Version="6.0.19" PrivateAssets="all" /> <PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.DevServer" Version="6.0.19" PrivateAssets="all" />
<PackageReference Include="MudBlazor" Version="6.5.0" /> <PackageReference Include="MudBlazor" Version="6.5.0" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>

View File

@ -21,7 +21,7 @@
<MudContainer Class="justify-center d-flex"> <MudContainer Class="justify-center d-flex">
@if (Charecters != null) @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"> <MudDataGrid FixedHeader=true Items="@Charecters">
<Columns> <Columns>
<PropertyColumn Property="x => x.charcter" Title="CChar" /> <PropertyColumn Property="x => x.charcter" Title="CChar" />
@ -31,15 +31,47 @@
</MudPaper> </MudPaper>
} }
</MudContainer> </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>
</MudContainer> </MudContainer>
@code{ @code{
int numOfChunks = 197; //Exclusive index 0 int numOfChunks = 197; //Exclusive index 0
int selectedChunk = 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(); SelectedChunk();
StateHasChanged(); StateHasChanged();
} }
@ -53,7 +85,34 @@
CChar[]? Charecters; CChar[]? Charecters;
async void SelectedChunk() async void SelectedChunk()
{ {
if (await localStorage.ContainKeyAsync("Normalized_chunk_001.json"))
isSavedLocally = true;
if (!isSavedLocally)
Charecters = await httpClient.GetFromJsonAsync<CChar[]>($"Data/Normalized_chunk_{selectedChunk.ToString("000")}.json"); 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(); StateHasChanged();
} }
} }

View File

@ -76,6 +76,8 @@
@code { @code {
public bool ShowOverlay = false; public bool ShowOverlay = false;
bool isSavedLocally = false;
public Answer[] Answers = new Answer[4]; public Answer[] Answers = new Answer[4];
public void SelectButton(int selectedIndex) public void SelectButton(int selectedIndex)
{ {
@ -109,10 +111,17 @@
{ {
Program.UpdateUiEvent += OnUiUpdate; Program.UpdateUiEvent += OnUiUpdate;
isSavedLocally = await localStorage.ContainKeyAsync("Normalized_chunk_001.json");
int selectedChunk = await localStorage.GetItemAsync<int>("SelectedChunk"); int selectedChunk = await localStorage.GetItemAsync<int>("SelectedChunk");
//Charecters = await httpClient.GetFromJsonAsync<CChar[]>("Data/NormalizedDataset.json"); if (!isSavedLocally)
Charecters = await httpClient.GetFromJsonAsync<CChar[]>($"Data/Normalized_chunk_{selectedChunk.ToString("000")}.json"); 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);
}
DontSkipTheseCChar = Charecters.ToList(); DontSkipTheseCChar = Charecters.ToList();
Program.CCharsLeft = DontSkipTheseCChar.Count; Program.CCharsLeft = DontSkipTheseCChar.Count;

View File

@ -7,7 +7,8 @@
<MudAppBar Elevation="0"> <MudAppBar Elevation="0">
@if (Program.CCharsLeft > 0) @if (Program.CCharsLeft > 0)
{ {
<MudText>@("CChars left: " + Program.CCharsLeft)</MudText> <MudButton OnClick="()=>navigator.NavigateTo(string.Empty)" Variant="Variant.Outlined" Color="Color.Primary">Back</MudButton>
<MudText Class="pl-8">@("CChars left: " + Program.CCharsLeft)</MudText>
} }
<MudSpacer /> <MudSpacer />
<MudIconButton Icon="@Icons.Custom.Brands.MudBlazor" Color="Color.Inherit" Link="https://mudblazor.com/" Target="_blank" /> <MudIconButton Icon="@Icons.Custom.Brands.MudBlazor" Color="Color.Inherit" Link="https://mudblazor.com/" Target="_blank" />

View File

@ -10,3 +10,4 @@
@using LearningChineseFixed.Shared @using LearningChineseFixed.Shared
@using MudBlazor @using MudBlazor
@using Blazored.LocalStorage @using Blazored.LocalStorage
@using Newtonsoft.Json