Skip to content

Commit

Permalink
feat: add a menu for when the user opens the app for the first time
Browse files Browse the repository at this point in the history
  • Loading branch information
BrewingWeasel committed Aug 15, 2024
1 parent 29a5212 commit 3bcedd0
Show file tree
Hide file tree
Showing 19 changed files with 875 additions and 379 deletions.
8 changes: 8 additions & 0 deletions shared/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,14 @@ impl KeyValueLabels for NoteKeyValueLabels {
const VALUE: &'static str = "handling";
}

#[derive(Deserialize)]
pub struct StartingSettings {
pub template: String,
#[serde(with = "HashMapToArray::<String, Note, DeckKeyValueLabels>")]
pub decks: HashMap<String, Note>,
pub stanza_enabled: bool,
}

#[derive(Clone, serde::Serialize)]
pub struct ToasterPayload<'a> {
pub message: Option<&'a str>,
Expand Down
42 changes: 34 additions & 8 deletions src-tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ use crate::{
use ankiconnect::get_anki_card_statuses;
use chrono::{DateTime, TimeDelta, Utc};
use commands::run_command;
use new_language_template::use_language_template;
use serde::{Deserialize, Serialize};
use shared::{Definition, LanguageSettings, Settings, ToasterPayload};
use shared::{Definition, LanguageSettings, Settings, StartingSettings, ToasterPayload};
use simple_logger::SimpleLogger;
use spyglys_integration::{format_spyglys, get_spyglys_functions};
use stats::{get_words_added, get_words_known_at_levels, time_spent};
Expand Down Expand Up @@ -258,7 +259,7 @@ fn main() {
get_spyglys_functions,
time_spent,
get_words_known_at_levels,
check_startup_errors,
get_startup_state,
parse_url,
get_definition_on_demand,
always_change_lemma,
Expand All @@ -267,6 +268,7 @@ fn main() {
get_export_variables,
rename_language,
get_words_added,
get_started,
])
.on_window_event(handle_window_event)
.run(tauri::generate_context!())
Expand Down Expand Up @@ -296,15 +298,39 @@ async fn refresh_anki(
Ok(())
}

#[derive(Serialize, Debug)]
struct StartupState {
errors: Vec<KalbaError>,
first_time: bool,
}

#[tauri::command]
async fn check_startup_errors(state: State<'_, KalbaState>) -> Result<(), Vec<KalbaError>> {
async fn get_startup_state(state: State<'_, KalbaState>) -> Result<StartupState, String> {
let mut state = state.0.lock().await;
let errs = std::mem::take(&mut state.errors);
if errs.is_empty() {
Ok(())
} else {
Err(errs)
}
Ok(StartupState {
errors: errs,
first_time: state.to_save.last_language.is_none(),
})
}

#[tauri::command]
async fn get_started(
state: State<'_, KalbaState>,
starting: StartingSettings,
) -> Result<(), KalbaError> {
let mut state = state.0.lock().await;
let name = use_language_template(&mut state, &starting.template).await?;
state
.settings
.languages
.get_mut(&name)
.expect("language to be added")
.anki_parser = starting.decks;
state.settings.stanza_enabled = starting.stanza_enabled;
state.to_save.last_language = Some(name.clone());
state.current_language = Some(name);
Ok(())
}

async fn set_word_knowledge_from_anki(
Expand Down
43 changes: 30 additions & 13 deletions src-tauri/src/new_language_template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ use reqwest::Client;
use serde::Deserialize;
use shared::{Dictionary, LanguageSettings};
use tauri::State;
use tokio::sync::MutexGuard;

use crate::{KalbaError, KalbaState};
use crate::{KalbaError, KalbaState, SharedInfo};

#[derive(Debug, Deserialize, Clone)]
struct TemplateDetails {
Expand All @@ -21,10 +22,17 @@ struct TemplateDetails {
#[tauri::command]
pub async fn new_language_from_template(
state: State<'_, KalbaState>,
language: String,
) -> Result<(), KalbaError> {
let language = language.to_lowercase();
language: &str,
) -> Result<String, KalbaError> {
let mut state = state.0.lock().await;
use_language_template(&mut state, language).await
}

pub async fn use_language_template(
state: &mut MutexGuard<'_, SharedInfo>,
language: &str,
) -> Result<String, KalbaError> {
let language = language.to_lowercase();

let mut language_name = language.clone();
if state.settings.languages.contains_key(&language_name) {
Expand All @@ -44,11 +52,11 @@ pub async fn new_language_from_template(
.settings
.languages
.insert(language_name.clone(), LanguageSettings::default());
state
.to_save
.language_specific
.insert(language_name, crate::LanguageSpecficToSave::default());
return Ok(());
state.to_save.language_specific.insert(
language_name.clone(),
crate::LanguageSpecficToSave::default(),
);
return Ok(language_name);
}

let client = Client::new();
Expand All @@ -61,11 +69,20 @@ pub async fn new_language_from_template(
.expect("githubusercontent to return valid text");
let details: TemplateDetails = toml::from_str(&template).unwrap();
let frequency_list = if details.frequency_list {
let path = dirs::data_dir()
.ok_or_else(|| KalbaError::MissingDir("data".to_owned()))?
let data_dir = dirs::data_dir().ok_or_else(|| KalbaError::MissingDir("data".to_owned()))?;
let path = data_dir
.join("kalba")
.join("language_data")
.join(format!("{language}_frequency"));

if data_dir.exists() {
if let Some(parent) = path.parent() {
if !parent.exists() {
fs::create_dir_all(parent)?;
}
}
}

if !path.exists() {
let contents = client.get(format!(
"https://raw.githubusercontent.com/brewingweasel/kalba/main/data/frequency_lists/{language}",))
Expand Down Expand Up @@ -106,11 +123,11 @@ pub async fn new_language_from_template(
.languages
.insert(language_name.clone(), lang_settings);
state.to_save.language_specific.insert(
language_name,
language_name.clone(),
crate::LanguageSpecficToSave {
lemmas_to_replace: details.replace_lemmas,
..Default::default()
},
);
Ok(())
Ok(language_name)
}
Loading

0 comments on commit 3bcedd0

Please sign in to comment.