@page "/"
@inject NavigationManager navigator
@inject HttpClient httpClient
@inject Blazored.LocalStorage.ILocalStorageService localStorage
@inject SpeechSynthesis SpeechSynthesis
Index
@for (int i = 0; i < numOfChunks; i++)
{
}
Start learning!
@if (Charecters != null)
{
}
@if (!isSavedLocally)
{
@if (isLoading)
{
@($"{savedChunks}/{numOfChunks}")
}
else
{
Load chunks to local storage
}
}
else
{
Chunks are saved locally
}
Delete local storage
@code{
int numOfChunks = 197; //Exclusive index 0
int selectedChunk = 0;
int savedChunks = 0;
bool isLoading = false;
bool isSavedLocally = false;
protected async override Task OnInitializedAsync()
{
if (await localStorage.ContainKeyAsync("Normalized_chunk_001.json"))
isSavedLocally = true;
SelectedChunk();
StateHasChanged();
}
async void StartLearning()
{
await localStorage.SetItemAsync("SelectedChunk", selectedChunk);
navigator.NavigateTo("/Learn");
}
CChar[]? Charecters;
async void SelectedChunk()
{
if (await localStorage.ContainKeyAsync("Normalized_chunk_001.json"))
isSavedLocally = true;
if (!isSavedLocally)
Charecters = await httpClient.GetFromJsonAsync($"Data/Normalized_chunk_{selectedChunk.ToString("000")}.json");
else
{
string json = await localStorage.GetItemAsync($"Normalized_chunk_{selectedChunk.ToString("000")}.json");
Charecters = JsonConvert.DeserializeObject(json);
}
StateHasChanged();
}
async Task LoadAllChunksIntoLocalStorage()
{
isLoading = true;
string[] jsonChunks = new string[numOfChunks];
Parallel.For(0, numOfChunks, async (i)=>
{
await Task.Delay(10*i);
try
{
jsonChunks[i] = await httpClient.GetStringAsync($"Data/Normalized_chunk_{i.ToString("000")}.json");
}
catch (Exception)
{
await Task.Delay(100);
jsonChunks[i] = await httpClient.GetStringAsync($"Data/Normalized_chunk_{i.ToString("000")}.json");
}
if (string.IsNullOrEmpty(jsonChunks[i])) throw new Exception("Couldn't load chunk: " + i);
try
{
await localStorage.SetItemAsync($"Normalized_chunk_{i.ToString("000")}.json", jsonChunks[i]);
}
catch (Exception)
{
await Task.Delay(100);
await localStorage.SetItemAsync($"Normalized_chunk_{i.ToString("000")}.json", jsonChunks[i]);
}
if (!await localStorage.ContainKeyAsync($"Normalized_chunk_{i.ToString("000")}.json")) throw new Exception("Couldn't save chunk: " + i);
});
while (jsonChunks.Any(x => x == null))
{
await Task.Delay(1);
savedChunks = jsonChunks.Count(x=>x != null);
StateHasChanged();
}
isLoading = false;
isSavedLocally = true;
StateHasChanged();
}
}