Skip to content

Commit

Permalink
feat: use dropdowns instead of textbox entries
Browse files Browse the repository at this point in the history
  • Loading branch information
BrewingWeasel committed Nov 12, 2023
1 parent b98d115 commit 87543df
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 25 deletions.
35 changes: 30 additions & 5 deletions src-tauri/src/ankiconnect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,18 @@ struct FieldInfo {

async fn generic_anki_connect_action(action: &str, data: Value) -> Response {
let client = reqwest::Client::new();
let request = json!({
"action": action,
"version": 6,
"params": data
});
let request = if data == Value::Null {
json!({
"action": action,
"version": 6,
})
} else {
json!({
"action": action,
"version": 6,
"params": data
})
};

client
.post("http://127.0.0.1:8765")
Expand Down Expand Up @@ -156,3 +163,21 @@ fn get_word_from_field(selected_field: String, handler: &NoteToWordHandling) ->
}
parsed
}

#[tauri::command]
pub async fn get_all_deck_names() -> Result<Vec<String>, String> {
let res = generic_anki_connect_action("deckNames", Value::Null).await;
res.json::<AnkiResult<Vec<String>>>().await.unwrap().into()
}

#[tauri::command]
pub async fn get_all_note_names() -> Result<Vec<String>, String> {
let res = generic_anki_connect_action("modelNames", Value::Null).await;
res.json::<AnkiResult<Vec<String>>>().await.unwrap().into()
}

#[tauri::command]
pub async fn get_note_field_names(model: &str) -> Result<Vec<String>, String> {
let res = generic_anki_connect_action("modelFieldNames", json!({ "modelName": model })).await;
res.json::<AnkiResult<Vec<String>>>().await.unwrap().into()
}
10 changes: 9 additions & 1 deletion src-tauri/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
// Prevents additional console window on Windows in release, DO NOT REMOVE!!
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]

use crate::{add_to_anki::add_to_anki, dictionary::get_defs, language_parsing::parse_text};
use crate::{
add_to_anki::add_to_anki,
ankiconnect::{get_all_deck_names, get_all_note_names, get_note_field_names},
dictionary::get_defs,
language_parsing::parse_text,
};
use ankiconnect::get_anki_card_statuses;
use chrono::{DateTime, Utc};
use commands::run_command;
Expand Down Expand Up @@ -89,6 +94,9 @@ fn main() {
get_settings,
add_to_anki,
write_settings,
get_all_deck_names,
get_all_note_names,
get_note_field_names,
])
.on_window_event(handle_window_event)
.run(tauri::generate_context!())
Expand Down
2 changes: 1 addition & 1 deletion src-ui/src/reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ async fn send_sentence(sent: String) -> Vec<Word> {
"parse_text",
&ParsingInfo {
sent: &sent,
model: "lt_core_news_sm",
model: "lt_core_news_sm", // TODO: why is this hardcoded all of a sudden what
},
)
.await
Expand Down
54 changes: 36 additions & 18 deletions src-ui/src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,14 @@ fn save_settings(settings: Settings) {
})
}

async fn get_all_x_names(x: &str) -> Vec<String> {
let note_or_deck = tauri::invoke::<(), Vec<String>>(&format!("get_all_{x}_names"), &()).await;
match note_or_deck {
Err(_) => Vec::new(),
Ok(decks) => decks,
}
}

#[component]
pub fn SettingsChanger(settings: Resource<(), Settings>) -> impl IntoView {
let old_settings = if let Some(s) = settings() {
Expand All @@ -44,6 +52,9 @@ pub fn SettingsChanger(settings: Resource<(), Settings>) -> impl IntoView {
return view! { <p>Unable to load settings</p> }.into_view();
};

let decks = create_resource(|| (), |_| async move { get_all_x_names("deck").await });
let notes = create_resource(|| (), |_| async move { get_all_x_names("note").await });

let (model, set_model) = create_signal(old_settings.model);
let (deck, set_deck) = create_signal(old_settings.deck);
let (note, set_note) = create_signal(old_settings.note_type);
Expand Down Expand Up @@ -93,9 +104,9 @@ pub fn SettingsChanger(settings: Resource<(), Settings>) -> impl IntoView {
</div>
<hr/>
<h2>Anki Settings</h2>
<SimpleTextSetting readsig=deck writesig=set_deck name="deck" desc="Anki Deck"/>
<SimpleDropDown readsig=deck writesig=set_deck name="deck" desc="Anki Deck" options=decks/>
<br/>
<SimpleTextSetting readsig=note writesig=set_note name="note" desc="Note type"/>
<SimpleDropDown readsig=note writesig=set_note name="note" desc="Note type" options=notes/>
<br/>
<SimpleTextAreaSetting
readsig=note_fields
Expand Down Expand Up @@ -189,6 +200,29 @@ fn SimpleTextSetting(
}
}

#[component]
fn SimpleDropDown(
readsig: ReadSignal<String>,
writesig: WriteSignal<String>,
name: &'static str,
desc: &'static str,
options: Resource<(), Vec<String>>,
) -> impl IntoView {
view! {
<div class="dropdown">
<label for=name>{desc}</label>
<select id=name
on:input=move |ev| {
writesig(event_target_value(&ev));
}
prop:value=readsig
>
{ move || options.get().map(|v| v.iter().map(|x| view! { <option value=x selected=readsig() == *x>{x}</option> }.into_view() ).collect_view()) }
</select>
</div>
}
}

#[component]
fn SimpleTextAreaSetting(
readsig: ReadSignal<String>,
Expand Down Expand Up @@ -496,10 +530,6 @@ fn DictionaryRepresentation(
view! {
// TODO: make generic function for this

// TODO: make generic function for this

// TODO: make generic function for this

<div class="labeledinput">
// TODO: make generic function for this
<label for="command">Command</label>
Expand All @@ -526,18 +556,6 @@ fn DictionaryRepresentation(
view! {
// TODO: make generic function for this

// TODO: make generic function for this

// TODO: make generic function for this

// TODO: make generic function for this

// TODO: make generic function for this

// TODO: make generic function for this

// TODO: make generic function for this

<div class="labeledinput">
// TODO: make generic function for this
<label for="filename">File location</label>
Expand Down

0 comments on commit 87543df

Please sign in to comment.