From 059088121e21810228de2f9f9488e17f42be1f4a Mon Sep 17 00:00:00 2001 From: BOT Alex <44818698+MagicBOTAlex@users.noreply.github.com> Date: Thu, 29 Jun 2023 21:27:58 +0200 Subject: [PATCH] Able to download whole data to local --- .../LearningChineseFixed.csproj | 1 + LearningChineseFixed/Pages/Index.razor | 65 ++++++++++++++++++- LearningChineseFixed/Pages/Learn.razor | 13 +++- LearningChineseFixed/Shared/MainLayout.razor | 3 +- LearningChineseFixed/_Imports.razor | 3 +- 5 files changed, 78 insertions(+), 7 deletions(-) diff --git a/LearningChineseFixed/LearningChineseFixed.csproj b/LearningChineseFixed/LearningChineseFixed.csproj index df825b7..038873c 100644 --- a/LearningChineseFixed/LearningChineseFixed.csproj +++ b/LearningChineseFixed/LearningChineseFixed.csproj @@ -12,6 +12,7 @@ + diff --git a/LearningChineseFixed/Pages/Index.razor b/LearningChineseFixed/Pages/Index.razor index b810a7f..b010bbd 100644 --- a/LearningChineseFixed/Pages/Index.razor +++ b/LearningChineseFixed/Pages/Index.razor @@ -21,7 +21,7 @@ @if (Charecters != null) { - + @@ -31,15 +31,47 @@ } + + + + @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; - 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($"Data/Normalized_chunk_{selectedChunk.ToString("000")}.json"); + 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 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(); } } \ No newline at end of file diff --git a/LearningChineseFixed/Pages/Learn.razor b/LearningChineseFixed/Pages/Learn.razor index 3f57a23..65c2232 100644 --- a/LearningChineseFixed/Pages/Learn.razor +++ b/LearningChineseFixed/Pages/Learn.razor @@ -76,6 +76,8 @@ @code { public bool ShowOverlay = false; + bool isSavedLocally = false; + public Answer[] Answers = new Answer[4]; public void SelectButton(int selectedIndex) { @@ -109,10 +111,17 @@ { Program.UpdateUiEvent += OnUiUpdate; + isSavedLocally = await localStorage.ContainKeyAsync("Normalized_chunk_001.json"); + int selectedChunk = await localStorage.GetItemAsync("SelectedChunk"); - //Charecters = await httpClient.GetFromJsonAsync("Data/NormalizedDataset.json"); - Charecters = await httpClient.GetFromJsonAsync($"Data/Normalized_chunk_{selectedChunk.ToString("000")}.json"); + 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); + } DontSkipTheseCChar = Charecters.ToList(); Program.CCharsLeft = DontSkipTheseCChar.Count; diff --git a/LearningChineseFixed/Shared/MainLayout.razor b/LearningChineseFixed/Shared/MainLayout.razor index 226cdfa..545c2ef 100644 --- a/LearningChineseFixed/Shared/MainLayout.razor +++ b/LearningChineseFixed/Shared/MainLayout.razor @@ -7,7 +7,8 @@ @if (Program.CCharsLeft > 0) { - @("CChars left: " + Program.CCharsLeft) + Back + @("CChars left: " + Program.CCharsLeft) } diff --git a/LearningChineseFixed/_Imports.razor b/LearningChineseFixed/_Imports.razor index c1cce23..b17c137 100644 --- a/LearningChineseFixed/_Imports.razor +++ b/LearningChineseFixed/_Imports.razor @@ -9,4 +9,5 @@ @using LearningChineseFixed @using LearningChineseFixed.Shared @using MudBlazor -@using Blazored.LocalStorage \ No newline at end of file +@using Blazored.LocalStorage +@using Newtonsoft.Json \ No newline at end of file