From 19ca59c98032906c9c8086ec2698325dd6972db8 Mon Sep 17 00:00:00 2001 From: BrewingWeasel Date: Sun, 28 Jan 2024 17:50:08 -0800 Subject: [PATCH] feat: load and save from settings file --- Cargo.lock | 10 ++++++++ shared/Cargo.toml | 1 + shared/src/lib.rs | 28 +++++++++++++++++++++-- src-tauri/Cargo.toml | 2 +- src-tauri/src/main.rs | 22 ++++++++---------- src/components/Settings.vue | 22 ++++++++++++++++-- src/components/SettingsPage.vue | 8 +++++++ src/components/settings/Deck.vue | 9 +++++--- src/components/settings/Notes.vue | 15 ++++++++---- src/components/settings/WordKnowledge.vue | 9 ++++---- src/main.ts | 18 +++++++-------- 11 files changed, 106 insertions(+), 38 deletions(-) create mode 100644 src/components/SettingsPage.vue diff --git a/Cargo.lock b/Cargo.lock index a5d21a7..6ccb491 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2815,6 +2815,15 @@ dependencies = [ "serde_derive", ] +[[package]] +name = "serde-map-to-array" +version = "1.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c14b52efc56c711e0dbae3f26e0cc233f5dac336c1bf0b07e1b7dc2dca3b2cc7" +dependencies = [ + "serde", +] + [[package]] name = "serde_derive" version = "1.0.189" @@ -2956,6 +2965,7 @@ version = "0.1.0" dependencies = [ "rusty-hook", "serde", + "serde-map-to-array", ] [[package]] diff --git a/shared/Cargo.toml b/shared/Cargo.toml index 6302577..0dfe966 100644 --- a/shared/Cargo.toml +++ b/shared/Cargo.toml @@ -7,6 +7,7 @@ edition = "2021" [dependencies] serde = { version = "1.0.189", features = ["derive"] } +serde-map-to-array = { version = "1.1.1", features = ["std"] } [dev-dependencies] rusty-hook = "^0.11.2" diff --git a/shared/src/lib.rs b/shared/src/lib.rs index fe3c107..9a5e22f 100644 --- a/shared/src/lib.rs +++ b/shared/src/lib.rs @@ -2,6 +2,22 @@ use std::collections::HashMap; use serde::{Deserialize, Serialize}; +use serde_map_to_array::{HashMapToArray, KeyValueLabels}; + +struct DeckKeyValueLabels; + +impl KeyValueLabels for DeckKeyValueLabels { + const KEY: &'static str = "name"; + const VALUE: &'static str = "notes"; +} + +struct NoteKeyValueLabels; + +impl KeyValueLabels for NoteKeyValueLabels { + const KEY: &'static str = "model"; + const VALUE: &'static str = "handling"; +} + #[derive(Clone, Serialize, Deserialize, Debug)] pub struct Word { pub text: String, @@ -61,6 +77,13 @@ pub struct NoteToWordHandling { pub tags_wanted: Vec, } +#[derive(Deserialize, Serialize, Clone)] + +pub struct Note( + #[serde(with = "HashMapToArray::")] + pub HashMap, +); + #[derive(Deserialize, Serialize, Clone)] pub struct Settings { pub deck: String, @@ -70,7 +93,8 @@ pub struct Settings { pub dicts: Vec, pub to_remove: Option, pub css: Option, - pub anki_parser: Option>>, + #[serde(with = "HashMapToArray::")] + pub anki_parser: HashMap, pub to_run: Option>, } @@ -87,7 +111,7 @@ Back:{word}:{def}", dicts: Vec::new(), to_remove: None, css: None, - anki_parser: None, + anki_parser: HashMap::new(), to_run: None, } } diff --git a/src-tauri/Cargo.toml b/src-tauri/Cargo.toml index d6a7a79..e8639ed 100644 --- a/src-tauri/Cargo.toml +++ b/src-tauri/Cargo.toml @@ -17,7 +17,7 @@ tauri-build = { version = "1.5.0", features = [] } [dependencies] serde_json = "1.0" serde = { version = "1.0", features = ["derive"] } -tauri = { version = "1.5.2", features = [ "shell-open"] } +tauri = { version = "1.5.2", features = ["shell-open"] } stardict = "0.2.0" reqwest = { version = "0.11.22", features = ["json"] } shared = { path = "../shared" } diff --git a/src-tauri/src/main.rs b/src-tauri/src/main.rs index 58ee782..6e2760d 100644 --- a/src-tauri/src/main.rs +++ b/src-tauri/src/main.rs @@ -59,18 +59,16 @@ impl Default for SharedInfo { .num_days() + 1; - if let Some(ankiparsers) = &mut settings.anki_parser { - for (deck, note_parser) in ankiparsers { - block_on(get_anki_card_statuses( - deck, - note_parser, - &mut to_save.words, - days_passed, - !to_save.decks_checked.contains(deck), - )) - .unwrap(); - to_save.decks_checked.push(deck.clone()); - } + for (deck, note_parser) in &mut settings.anki_parser { + block_on(get_anki_card_statuses( + deck, + ¬e_parser.0, + &mut to_save.words, + days_passed, + !to_save.decks_checked.contains(deck), + )) + .unwrap(); + to_save.decks_checked.push(deck.clone()); } if let Some(cmds) = &settings.to_run { diff --git a/src/components/Settings.vue b/src/components/Settings.vue index cd35672..abba0ab 100644 --- a/src/components/Settings.vue +++ b/src/components/Settings.vue @@ -7,20 +7,38 @@ import { CardHeader, CardTitle, } from "@/components/ui/card"; -import { Input } from "@/components/ui/input"; import { Tabs, TabsContent, TabsList, TabsTrigger } from "@/components/ui/tabs"; import { Switch } from "@/components/ui/switch"; +import { Button } from "@/components/ui/button"; import WordKnowledge from "@/components/settings/WordKnowledge.vue"; +import { invoke } from "@tauri-apps/api/tauri"; +import { Ref, ref } from "vue"; +import { Deck } from "./settings/Deck.vue"; function toggleDarkMode() { const body = document.querySelector("body"); body!.classList.toggle("dark"); } + +interface Settings { + deck: string; + note_type: string; + note_fields: string; + model: string; + anki_parser: Deck[]; +} + +const settings: Ref = ref(await invoke("get_settings")); + +async function saveSettings() { + await invoke("write_settings", { settings: settings.value }); +}