From a1c00cc7ea096c996333a2c1ea6668d94f81423a Mon Sep 17 00:00:00 2001 From: Finn Brewer Date: Sun, 4 Aug 2024 12:52:26 -0700 Subject: [PATCH] fix: problems when renaming languages --- src-tauri/src/main.rs | 21 +++++++++++++++++++++ src-tauri/src/stats.rs | 1 + src/App.vue | 5 +++++ src/pages/settings/Index.vue | 6 +++++- src/pages/settings/Settings.vue | 12 ++++++++++-- 5 files changed, 42 insertions(+), 3 deletions(-) diff --git a/src-tauri/src/main.rs b/src-tauri/src/main.rs index e6ff546..e7ceaa3 100644 --- a/src-tauri/src/main.rs +++ b/src-tauri/src/main.rs @@ -264,6 +264,7 @@ fn main() { setup_stanza, check_stanza_installed, get_export_variables, + rename_language, ]) .on_window_event(handle_window_event) .run(tauri::generate_context!()) @@ -470,6 +471,26 @@ async fn set_language(state: State<'_, KalbaState>, language: String) -> Result< Ok(()) } +#[tauri::command] +async fn rename_language( + state: State<'_, KalbaState>, + original_name: String, + new_name: String, +) -> Result<(), String> { + let mut state = state.0.lock().await; + if let Some(old) = state.to_save.language_specific.remove(&original_name) { + state + .to_save + .language_specific + .insert(new_name.clone(), old); + } + if let Some(old) = state.language_cached_data.remove(&original_name) { + state.language_cached_data.insert(new_name.clone(), old); + } + state.current_language = Some(new_name); + Ok(()) +} + #[tauri::command] async fn write_settings( state: State<'_, KalbaState>, diff --git a/src-tauri/src/stats.rs b/src-tauri/src/stats.rs index da5e07f..29f39da 100644 --- a/src-tauri/src/stats.rs +++ b/src-tauri/src/stats.rs @@ -72,6 +72,7 @@ pub async fn get_words_known_at_levels( ) -> Result, String> { let state = state.0.lock().await; let current_language = state.current_language.as_ref().expect("language to be set"); + log::info!("loading stats for profile {current_language}"); let words = &state .to_save .language_specific diff --git a/src/App.vue b/src/App.vue index 499a17b..69d1f73 100644 --- a/src/App.vue +++ b/src/App.vue @@ -252,6 +252,11 @@ onMounted(async () => { diff --git a/src/pages/settings/Index.vue b/src/pages/settings/Index.vue index d60bc6b..af40d99 100644 --- a/src/pages/settings/Index.vue +++ b/src/pages/settings/Index.vue @@ -8,5 +8,9 @@ const props = defineProps<{ diff --git a/src/pages/settings/Settings.vue b/src/pages/settings/Settings.vue index a1cb959..8d311ba 100644 --- a/src/pages/settings/Settings.vue +++ b/src/pages/settings/Settings.vue @@ -90,7 +90,7 @@ watch( }, ); -const emit = defineEmits(["settingsChanged"]); +const emit = defineEmits(["settingsChanged", "newCurrentLanguage"]); console.log(settings); @@ -294,12 +294,20 @@ async function newLanguage(language: string) { class="w-64" id="language-name" @change=" - () => { + async () => { if (selectedLang === null) return; const newName = languageNameChanges[selectedLang]; settings.languages[newName] = settings.languages[selectedLang]; delete settings.languages[selectedLang]; languageNameChanges[newName] = newName; + if (selectedLang === props.currentLanguage) { + await invoke('rename_language', { + originalName: selectedLang, + newName, + }); + } + $emit('newCurrentLanguage', newName); + selectedLang = newName; } " />