Skip to content

Commit

Permalink
feat: load and save from settings file
Browse files Browse the repository at this point in the history
  • Loading branch information
BrewingWeasel committed Jan 29, 2024
1 parent 36e8a27 commit 19ca59c
Show file tree
Hide file tree
Showing 11 changed files with 106 additions and 38 deletions.
10 changes: 10 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions shared/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
28 changes: 26 additions & 2 deletions shared/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -61,6 +77,13 @@ pub struct NoteToWordHandling {
pub tags_wanted: Vec<String>,
}

#[derive(Deserialize, Serialize, Clone)]

pub struct Note(
#[serde(with = "HashMapToArray::<String, NoteToWordHandling, NoteKeyValueLabels>")]
pub HashMap<String, NoteToWordHandling>,
);

#[derive(Deserialize, Serialize, Clone)]
pub struct Settings {
pub deck: String,
Expand All @@ -70,7 +93,8 @@ pub struct Settings {
pub dicts: Vec<Dictionary>,
pub to_remove: Option<usize>,
pub css: Option<String>,
pub anki_parser: Option<HashMap<String, HashMap<String, NoteToWordHandling>>>,
#[serde(with = "HashMapToArray::<String, Note, DeckKeyValueLabels>")]
pub anki_parser: HashMap<String, Note>,
pub to_run: Option<Vec<String>>,
}

Expand All @@ -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,
}
}
Expand Down
2 changes: 1 addition & 1 deletion src-tauri/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand Down
22 changes: 10 additions & 12 deletions src-tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
&note_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 {
Expand Down
22 changes: 20 additions & 2 deletions src/components/Settings.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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<Settings> = ref(await invoke("get_settings"));
async function saveSettings() {
await invoke("write_settings", { settings: settings.value });
}
</script>

<template>
<Button @click="saveSettings">Save</Button>
<div class="w-full flex justify-center">
<Tabs default-value="knowledge" class="object-center">
<TabsList class="grid w-full grid-cols-6">
Expand Down Expand Up @@ -55,7 +73,7 @@ function toggleDarkMode() {
</CardHeader>
<CardContent class="space-y-2">
<Suspense>
<WordKnowledge />
<WordKnowledge :decks="settings.anki_parser" />
</Suspense>
</CardContent>
</Card>
Expand Down
8 changes: 8 additions & 0 deletions src/components/SettingsPage.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<script setup lang="ts">
import { Suspense } from "vue";
import Settings from "./Settings.vue";
</script>

<template>
<Suspense><Settings /></Suspense>
</template>
9 changes: 6 additions & 3 deletions src/components/settings/Deck.vue
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,12 @@ const props = defineProps<{
function addNote() {
props.deck.notes.push({
model: "",
field: "",
removeParens: false,
firstWordOnly: false,
handling: {
field_to_use: "",
only_first_word_or_line: false,
remove_everything_in_parens: false,
tags_wanted: [],
},
});
}
</script>
Expand Down
15 changes: 10 additions & 5 deletions src/components/settings/Notes.vue
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,14 @@ import { computedAsync } from "@vueuse/core";
export interface Note {
model: string;
field: string;
removeParens: boolean;
firstWordOnly: boolean;
handling: NoteToWordHandling;
}
interface NoteToWordHandling {
field_to_use: string;
only_first_word_or_line: boolean;
remove_everything_in_parens: boolean;
tags_wanted: string[];
}
const props = defineProps<{
Expand All @@ -18,7 +23,7 @@ const props = defineProps<{
}>();
const fields = computedAsync(async (): Promise<string[]> => {
props.note.field = "";
props.note.handling.field_to_use = "";
return await invoke("get_note_field_names", {
model: props.note.model,
});
Expand All @@ -35,7 +40,7 @@ const fields = computedAsync(async (): Promise<string[]> => {
/>
<StyledCombobox
:options="fields"
v-model="props.note.field"
v-model="props.note.handling.field_to_use"
item-being-selected="field"
/>
</div>
Expand Down
9 changes: 5 additions & 4 deletions src/components/settings/WordKnowledge.vue
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
<script setup lang="ts">
import { Ref, ref } from "vue";
import {
Accordion,
AccordionContent,
Expand All @@ -13,13 +11,16 @@ import { Button } from "@/components/ui/button";
import IndividualDeck from "@/components/settings/Deck.vue";
import { Deck } from "@/components/settings/Deck.vue";
const decks: Ref<Array<Deck>> = ref([]);
const models: string[] = await invoke("get_all_note_names", {});
const deckNames: string[] = await invoke("get_all_deck_names", {});
const props = defineProps<{
decks: Deck[];
}>();
function addDeck() {
decks.value.push({
props.decks.push({
name: "",
notes: [],
});
Expand Down
18 changes: 9 additions & 9 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,19 @@ import { createRouter, createWebHistory } from "vue-router";
import "./styles.css";
import App from "./App.vue";
import Input from "./components/Input.vue";
import Settings from "./components/Settings.vue";
import Settings from "./components/SettingsPage.vue";
import WordView from "./components/WordView.vue";
import ReaderView from "./components/ReaderView.vue";

const router = createRouter({
history: createWebHistory(),
routes: [
{ path: "/", component: Input },
{ path: "/reader", component: ReaderView },
{ path: "/settings", component: Settings },
{ path: "/words/:word", component: WordView },
// { path: "/reader", component: Reader },
],
history: createWebHistory(),
routes: [
{ path: "/", component: Input },
{ path: "/reader", component: ReaderView },
{ path: "/settings", component: Settings },
{ path: "/words/:word", component: WordView },
// { path: "/reader", component: Reader },
],
});

const app = createApp(App);
Expand Down

0 comments on commit 19ca59c

Please sign in to comment.