Repetition settings base
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
use std::fs;
|
||||
use crate::dictionary::app_data_dir;
|
||||
use crate::lang::WordOpenMode;
|
||||
use chrono::{DateTime, Utc};
|
||||
@@ -7,7 +8,7 @@ use std::io::{BufRead, BufReader};
|
||||
use std::path::PathBuf;
|
||||
|
||||
pub fn get_history_of_set(id: u32) -> Vec<HistoryItem> {
|
||||
let app_dir = app_data_dir();
|
||||
let app_dir = history_dir();
|
||||
let head = app_dir.clone().join(format!("set_{}_history.csv", id));
|
||||
let mut lines = Vec::new();
|
||||
|
||||
@@ -48,7 +49,7 @@ fn parse_history_items(strings: Vec<String>) -> Vec<HistoryItem> {
|
||||
}
|
||||
|
||||
pub fn push_note(set_id: u32, item: HistoryItem) {
|
||||
let app_dir = app_data_dir();
|
||||
let app_dir = history_dir();
|
||||
let path = app_dir.clone().join(format!("set_{}_history.csv", set_id));
|
||||
|
||||
let mut file = OpenOptions::new()
|
||||
@@ -72,6 +73,14 @@ pub fn push_note(set_id: u32, item: HistoryItem) {
|
||||
writeln!(&mut file, "{}", line_str.to_string()).unwrap();
|
||||
}
|
||||
|
||||
pub fn history_dir() -> PathBuf {
|
||||
let directory = app_data_dir().join("history");
|
||||
if !directory.exists() {
|
||||
fs::create_dir(directory.clone()).unwrap();
|
||||
}
|
||||
directory
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct HistoryItem {
|
||||
pub timestamp: DateTime<Utc>,
|
||||
|
||||
@@ -14,7 +14,10 @@ pub mod styling;
|
||||
mod sync;
|
||||
mod word;
|
||||
mod writing;
|
||||
mod repetition_settings;
|
||||
|
||||
use std::collections::HashMap;
|
||||
use chrono::{ NaiveDate};
|
||||
use crate::data_provider::card_sets::load_sets;
|
||||
use crate::data_provider::settings::get_setting;
|
||||
use crate::data_provider::words::{load_word_groups, load_words};
|
||||
@@ -60,6 +63,7 @@ pub struct AppState {
|
||||
pub word_groups: Vec<WordGroup>,
|
||||
pub connection: Connection,
|
||||
pub sync_data: AppSettings,
|
||||
pub activity: HashMap<u32, Vec<(NaiveDate, u32)>>
|
||||
}
|
||||
|
||||
impl AppState {
|
||||
@@ -75,6 +79,7 @@ impl AppState {
|
||||
word_groups: vec![],
|
||||
connection,
|
||||
sync_data: AppSettings { key: None },
|
||||
activity: Default::default(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+54
-7
@@ -1,23 +1,28 @@
|
||||
use crate::data_provider::history::{get_history_of_set, history_dir};
|
||||
use crate::data_provider::sqlite::create_db;
|
||||
use crate::dictionary::{DictionaryMessage, DictionaryState};
|
||||
use crate::dictionary_test::{DictionaryQuizMessage, DictionaryQuizState};
|
||||
use crate::history::{HistoryMessage, HistoryState};
|
||||
use crate::message_navigation;
|
||||
use crate::navigation::Page::*;
|
||||
use crate::navigation::RootMessage::Keyboard;
|
||||
use crate::navigation::RootMessage::{DataLoaded, Keyboard };
|
||||
use crate::quiz::{QuizMessage, QuizState};
|
||||
use crate::randomizer::{RandomizerMessage, RandomizerState};
|
||||
use crate::repetition::{RepetitionMessage, RepetitionState};
|
||||
use crate::repetitions::{RepetitionsMessage, RepetitionsState};
|
||||
use crate::selector::{SelectorMessage, SelectorState};
|
||||
use crate::repetition_settings::{RepetitionSettingsMessage, RepetitionSettingsState};
|
||||
use crate::state_update;
|
||||
use crate::sync::{SyncMessage, SyncState};
|
||||
use crate::view_navigation;
|
||||
use crate::word::{WordMessage, WordState};
|
||||
use crate::writing::{WritingMessage, WritingState};
|
||||
use crate::{fill_state, AppState};
|
||||
use crate::{AppState, fill_state};
|
||||
use chrono::NaiveDate;
|
||||
use iced::keyboard::Event;
|
||||
use iced::{Element, Task};
|
||||
use rusqlite::fallible_iterator::FallibleIterator;
|
||||
use std::collections::HashMap;
|
||||
use std::sync::{Arc, Mutex};
|
||||
|
||||
impl Default for ScreenState {
|
||||
@@ -26,7 +31,8 @@ impl Default for ScreenState {
|
||||
fill_state(&mut state);
|
||||
let state = Arc::new(Mutex::new(state));
|
||||
ScreenState {
|
||||
stack: vec![Page::Selector(SelectorState::new(state.clone()))],
|
||||
stack: vec![Selector(SelectorState::new(state.clone()))],
|
||||
app_state: state.clone(),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -44,7 +50,9 @@ pub enum RootMessage {
|
||||
Word(WordMessage),
|
||||
Sync(SyncMessage),
|
||||
History(HistoryMessage),
|
||||
RepetitionSettings(RepetitionSettingsMessage),
|
||||
Keyboard(Event),
|
||||
DataLoaded(HashMap<u32, Vec<(NaiveDate, u32)>>),
|
||||
}
|
||||
|
||||
pub enum Page {
|
||||
@@ -59,23 +67,59 @@ pub enum Page {
|
||||
Word(WordState),
|
||||
Sync(SyncState),
|
||||
History(HistoryState),
|
||||
RepetitionSettings(RepetitionSettingsState),
|
||||
|
||||
PreviousPage,
|
||||
}
|
||||
|
||||
pub struct ScreenState {
|
||||
stack: Vec<Page>,
|
||||
app_state: Arc<Mutex<AppState>>,
|
||||
}
|
||||
|
||||
impl ScreenState {
|
||||
pub fn boot() -> (ScreenState, Task<RootMessage>) {
|
||||
create_db();
|
||||
(ScreenState::default(), Task::none())
|
||||
(
|
||||
ScreenState::default(),
|
||||
Task::perform(Self::load_additional_data(), |data| data),
|
||||
)
|
||||
}
|
||||
|
||||
async fn load_additional_data() -> RootMessage {
|
||||
let directory = history_dir();
|
||||
let mut map = HashMap::new();
|
||||
for file in directory.read_dir().unwrap() {
|
||||
if let Ok(file) = file {
|
||||
let mut vec = vec![];
|
||||
let history_file_name = file.file_name().into_string().unwrap();
|
||||
let id = history_file_name[4..history_file_name.len() - 12]
|
||||
.parse::<u32>()
|
||||
.unwrap();
|
||||
|
||||
let history = get_history_of_set(id);
|
||||
let by_date = history.chunk_by(|x, x1| {
|
||||
x.timestamp.naive_local().date() == x1.timestamp.naive_local().date()
|
||||
});
|
||||
for group in by_date {
|
||||
vec.push((group[0].timestamp.naive_local().date(), group.len() as u32));
|
||||
}
|
||||
map.insert(id, vec);
|
||||
}
|
||||
}
|
||||
DataLoaded(map)
|
||||
}
|
||||
|
||||
pub fn update(&mut self, message: RootMessage) -> Task<RootMessage> {
|
||||
if let Keyboard(e) = message {
|
||||
return self.handle_keyboard(e);
|
||||
}
|
||||
|
||||
if let DataLoaded(data) = message {
|
||||
self.app_state.lock().unwrap().activity = data;
|
||||
return Task::none();
|
||||
}
|
||||
|
||||
state_update!(
|
||||
message,
|
||||
self.stack,
|
||||
@@ -89,8 +133,10 @@ impl ScreenState {
|
||||
Repetition,
|
||||
Word,
|
||||
Sync,
|
||||
History
|
||||
History,
|
||||
RepetitionSettings
|
||||
);
|
||||
|
||||
Task::none()
|
||||
}
|
||||
pub fn view(&self) -> Element<'_, RootMessage> {
|
||||
@@ -106,14 +152,15 @@ impl ScreenState {
|
||||
Repetition,
|
||||
Word,
|
||||
Sync,
|
||||
History
|
||||
History,
|
||||
RepetitionSettings
|
||||
)
|
||||
}
|
||||
|
||||
fn handle_keyboard(&mut self, message: Event) -> Task<RootMessage> {
|
||||
let page = self.stack.last_mut().unwrap();
|
||||
match page {
|
||||
Page::Repetition(page) => page.press(&message),
|
||||
Repetition(page) => page.press(&message),
|
||||
_ => Task::none(),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,38 @@
|
||||
use crate::navigation::Page::PreviousPage;
|
||||
use crate::navigation::{NavigatedPage, Page, RootMessage};
|
||||
use crate::repetition_settings::RepetitionSettingsMessage::*;
|
||||
use crate::styling::back_overlay;
|
||||
use iced::widget::row;
|
||||
use iced::{Element, Task};
|
||||
|
||||
pub struct RepetitionSettingsState {}
|
||||
impl NavigatedPage<RepetitionSettingsMessage> for RepetitionSettingsState {
|
||||
fn navigate(&self, message: &RepetitionSettingsMessage) -> Option<Page> {
|
||||
if let Back = message {
|
||||
Some(PreviousPage)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl RepetitionSettingsState {
|
||||
pub fn update(&mut self, message: RepetitionSettingsMessage) -> Task<RootMessage> {
|
||||
match message {
|
||||
Back => {}
|
||||
}
|
||||
Task::none()
|
||||
}
|
||||
|
||||
pub fn view(&self) -> Element<'_, RepetitionSettingsMessage> {
|
||||
back_overlay(
|
||||
row![].into(),
|
||||
Back,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub enum RepetitionSettingsMessage {
|
||||
Back
|
||||
}
|
||||
+50
-37
@@ -2,21 +2,24 @@ use crate::data_provider::card_sets::{delete_set, update_card_set};
|
||||
use crate::data_provider::card_stats::load_stats_of_set;
|
||||
use crate::history::HistoryState;
|
||||
use crate::lang::{SetOrderMode, WordData};
|
||||
use crate::navigation::Page::{History, PreviousPage, Repetition};
|
||||
use crate::navigation::Page::{History, PreviousPage, Repetition, RepetitionSettings};
|
||||
use crate::navigation::{NavigatedPage, Page};
|
||||
use crate::repetition::RepetitionState;
|
||||
use crate::styling::*;
|
||||
use crate::{AppState, RootMessage};
|
||||
use iced::widget::button::{danger, Status};
|
||||
pub use iced::widget::button::{Catalog, Style};
|
||||
use iced::widget::button::{Status, danger};
|
||||
use iced::widget::container::bordered_box;
|
||||
use iced::widget::{
|
||||
button, column, container, radio, row, scrollable, space, text, text_input, Column,
|
||||
Column, button, column, container, radio, row, scrollable, space, svg, text, text_input,
|
||||
};
|
||||
use iced::{Background, Border, Center, Color, Element, Fill, Length, Shadow, Task, Theme};
|
||||
use iced_core::Padding;
|
||||
use iced_core::svg::Handle;
|
||||
use rhai::{Engine, Scope};
|
||||
use std::sync::{Arc, Mutex};
|
||||
use crate::repetition_settings::RepetitionSettingsState;
|
||||
use crate::repetitions::RepetitionsMessage::*;
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct RepetitionsState {
|
||||
@@ -28,22 +31,26 @@ pub struct RepetitionsState {
|
||||
|
||||
impl NavigatedPage<RepetitionsMessage> for RepetitionsState {
|
||||
fn navigate(&self, message: &RepetitionsMessage) -> Option<Page> {
|
||||
if let RepetitionsMessage::Back = message {
|
||||
if let Back = message {
|
||||
Some(PreviousPage)
|
||||
} else if let RepetitionsMessage::GoToHistory = message {
|
||||
} else if let GoToHistory = message {
|
||||
let card_set;
|
||||
{
|
||||
card_set = self.state.lock().unwrap().card_sets[self.selected_set.unwrap()].id;
|
||||
}
|
||||
Some(History(HistoryState::new(card_set, self.state.clone())))
|
||||
} else if let RepetitionsMessage::GoToRepetition = message {
|
||||
} else if let GoToRepetition = message {
|
||||
let clone = self.state.clone();
|
||||
let card_set;
|
||||
{
|
||||
card_set = self.state.lock().unwrap().card_sets[self.selected_set.unwrap()].clone();
|
||||
}
|
||||
Some(Repetition(RepetitionState::new(card_set, clone)))
|
||||
} else {
|
||||
}
|
||||
else if let GoToSettings = message {
|
||||
Some(RepetitionSettings(RepetitionSettingsState {}))
|
||||
}
|
||||
else {
|
||||
None
|
||||
}
|
||||
}
|
||||
@@ -64,33 +71,34 @@ impl RepetitionsState {
|
||||
pub fn update(&mut self, message: RepetitionsMessage) -> Task<RootMessage> {
|
||||
let mut state = self.state.lock().unwrap();
|
||||
match message {
|
||||
RepetitionsMessage::Next => {}
|
||||
RepetitionsMessage::Back => {}
|
||||
RepetitionsMessage::GoToRepetition => {}
|
||||
RepetitionsMessage::GoToHistory => {}
|
||||
RepetitionsMessage::CreateSet => {
|
||||
Next => {}
|
||||
Back => {}
|
||||
GoToRepetition => {}
|
||||
GoToHistory => {}
|
||||
GoToSettings => {}
|
||||
CreateSet => {
|
||||
let index = state.card_sets.len() + 1;
|
||||
state
|
||||
.card_sets
|
||||
.push(CardSetSettings::with_name(format!("Card set #{}", index)));
|
||||
self.correct_filters.push(true);
|
||||
}
|
||||
RepetitionsMessage::DeleteSet => {
|
||||
DeleteSet => {
|
||||
let set = state.card_sets.remove(self.selected_set.unwrap());
|
||||
self.correct_filters.remove(self.selected_set.unwrap());
|
||||
self.selected_set = None;
|
||||
delete_set(&set, &state.connection);
|
||||
}
|
||||
RepetitionsMessage::SelectSet(index) => {
|
||||
SelectSet(index) => {
|
||||
self.selected_set = Some(index);
|
||||
let mut set = state.card_sets.get(index).unwrap().clone();
|
||||
set.update_worst_words(&state);
|
||||
state.card_sets[index] = set;
|
||||
}
|
||||
RepetitionsMessage::SetName(new) => {
|
||||
SetName(new) => {
|
||||
state.card_sets[self.selected_set.unwrap()].name = new;
|
||||
}
|
||||
RepetitionsMessage::Save => {
|
||||
Save => {
|
||||
for i in 0..state.card_sets.len() {
|
||||
self.correct_filters[i] = state.card_sets[i].check_filter();
|
||||
}
|
||||
@@ -101,27 +109,27 @@ impl RepetitionsState {
|
||||
|
||||
state.card_sets[self.selected_set.unwrap()] = word.clone();
|
||||
}
|
||||
RepetitionsMessage::SetForward(new) => {
|
||||
SetForward(new) => {
|
||||
state.card_sets[self.selected_set.unwrap()].forward = new;
|
||||
}
|
||||
RepetitionsMessage::SetBackward(new) => {
|
||||
SetBackward(new) => {
|
||||
state.card_sets[self.selected_set.unwrap()].backward = new;
|
||||
}
|
||||
RepetitionsMessage::SetFilter(new) => {
|
||||
SetFilter(new) => {
|
||||
state.card_sets[self.selected_set.unwrap()].filter = new;
|
||||
}
|
||||
RepetitionsMessage::TryFilter => {
|
||||
TryFilter => {
|
||||
let set = state.card_sets.get(self.selected_set.unwrap()).unwrap();
|
||||
let count = set.get_word_list(&state).len();
|
||||
state.card_sets[self.selected_set.unwrap()].count = Some(count);
|
||||
}
|
||||
RepetitionsMessage::SetOpenMode(mode) => {
|
||||
SetOpenMode(mode) => {
|
||||
state
|
||||
.card_sets
|
||||
.get_mut(self.selected_set.unwrap())
|
||||
.unwrap()
|
||||
.open_mode = mode;
|
||||
}
|
||||
},
|
||||
}
|
||||
Task::none()
|
||||
}
|
||||
@@ -134,7 +142,7 @@ impl RepetitionsState {
|
||||
button("Добавить")
|
||||
.style(jl_button)
|
||||
.width(Fill)
|
||||
.on_press(RepetitionsMessage::CreateSet),
|
||||
.on_press(CreateSet),
|
||||
]
|
||||
.spacing(DEFAULT_SPACING)
|
||||
.width(Length::FillPortion(1)),
|
||||
@@ -145,7 +153,7 @@ impl RepetitionsState {
|
||||
.width(Fill)
|
||||
.height(Fill)
|
||||
.into(),
|
||||
RepetitionsMessage::Back,
|
||||
Back,
|
||||
)
|
||||
}
|
||||
|
||||
@@ -155,7 +163,7 @@ impl RepetitionsState {
|
||||
{
|
||||
return button(text!("Начать повторение").width(200).center())
|
||||
.style(cta_button)
|
||||
.on_press(RepetitionsMessage::GoToRepetition)
|
||||
.on_press(GoToRepetition)
|
||||
.into();
|
||||
}
|
||||
space().into()
|
||||
@@ -172,25 +180,29 @@ impl RepetitionsState {
|
||||
column![
|
||||
column![
|
||||
text!("Название набора"),
|
||||
text_input("", &set.name).on_input(RepetitionsMessage::SetName)
|
||||
row![
|
||||
text_input("", &set.name).on_input(SetName),
|
||||
button(svg(Handle::from_memory(SETTINGS_ICON))).width(43).on_press(GoToSettings),
|
||||
]
|
||||
.spacing(HALF_SPACING)
|
||||
]
|
||||
.spacing(QUARTER_SPACING),
|
||||
column![
|
||||
text!("Передняя сторона"),
|
||||
text_input("", &set.forward).on_input(RepetitionsMessage::SetForward),
|
||||
text_input("", &set.forward).on_input(SetForward),
|
||||
]
|
||||
.spacing(QUARTER_SPACING),
|
||||
column![
|
||||
text!("Задняя сторона"),
|
||||
text_input("", &set.backward).on_input(RepetitionsMessage::SetBackward),
|
||||
text_input("", &set.backward).on_input(SetBackward),
|
||||
]
|
||||
.spacing(QUARTER_SPACING),
|
||||
column![
|
||||
text!("Фильтр"),
|
||||
text_input("", &set.filter).on_input(RepetitionsMessage::SetFilter),
|
||||
text_input("", &set.filter).on_input(SetFilter),
|
||||
button("Проверить фильтр")
|
||||
.style(jl_button)
|
||||
.on_press(RepetitionsMessage::TryFilter),
|
||||
.on_press(TryFilter),
|
||||
self.count_view(&set),
|
||||
]
|
||||
.spacing(QUARTER_SPACING),
|
||||
@@ -198,12 +210,12 @@ impl RepetitionsState {
|
||||
"Обычный режим",
|
||||
SetOrderMode::Default,
|
||||
Some(set.open_mode),
|
||||
RepetitionsMessage::SetOpenMode
|
||||
SetOpenMode
|
||||
),
|
||||
self.words_words_view(&set),
|
||||
button("История")
|
||||
.style(jl_button)
|
||||
.on_press(RepetitionsMessage::GoToHistory),
|
||||
.on_press(GoToHistory),
|
||||
]
|
||||
.spacing(DEFAULT_SPACING)
|
||||
.padding(Padding {
|
||||
@@ -217,10 +229,10 @@ impl RepetitionsState {
|
||||
row![
|
||||
button("Сохранить")
|
||||
.style(jl_button)
|
||||
.on_press(RepetitionsMessage::Save),
|
||||
.on_press(Save),
|
||||
button("Удалить")
|
||||
.style(danger)
|
||||
.on_press(RepetitionsMessage::DeleteSet),
|
||||
.on_press(DeleteSet),
|
||||
space().width(Fill),
|
||||
self.launch_button()
|
||||
]
|
||||
@@ -241,13 +253,13 @@ impl RepetitionsState {
|
||||
"Начать с плохих слов",
|
||||
SetOrderMode::TrainWorstFirst,
|
||||
Some(set.open_mode),
|
||||
RepetitionsMessage::SetOpenMode
|
||||
SetOpenMode
|
||||
),
|
||||
radio(
|
||||
"Полностью случайно",
|
||||
SetOrderMode::FullRandom,
|
||||
Some(set.open_mode),
|
||||
RepetitionsMessage::SetOpenMode
|
||||
SetOpenMode
|
||||
)
|
||||
]
|
||||
.spacing(DEFAULT_SPACING)
|
||||
@@ -277,7 +289,7 @@ impl RepetitionsState {
|
||||
for set in sets {
|
||||
column = column.push(
|
||||
button(text!("{}", set.name.clone()))
|
||||
.on_press_with(move || RepetitionsMessage::SelectSet(i.clone()))
|
||||
.on_press_with(move || SelectSet(i.clone()))
|
||||
.style(move |_x: &Theme, status: Status| Style {
|
||||
background: if status == Status::Hovered {
|
||||
Some(Background::Color(Color::WHITE.scale_alpha(0.2)))
|
||||
@@ -321,6 +333,7 @@ pub enum RepetitionsMessage {
|
||||
TryFilter,
|
||||
SetOpenMode(SetOrderMode),
|
||||
GoToHistory,
|
||||
GoToSettings,
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
|
||||
+4
-4
@@ -11,7 +11,7 @@ use crate::writing::WritingState;
|
||||
use crate::{AppState, QuizState, RootMessage};
|
||||
use iced::widget::button::{Status, Style};
|
||||
use iced::widget::*;
|
||||
use iced::{alignment, Element, Task};
|
||||
use iced::{Element, Task, alignment};
|
||||
use iced_core::Alignment::Center;
|
||||
use iced_core::Background;
|
||||
use iced_core::Color;
|
||||
@@ -105,12 +105,12 @@ impl SelectorState {
|
||||
button("Словарь")
|
||||
.on_press(SelectorMessage::ToDictionary)
|
||||
.style(Self::nav_style),
|
||||
button("Рандомайзер")
|
||||
.on_press(SelectorMessage::ToRandomize)
|
||||
.style(Self::nav_style),
|
||||
button("Повторение")
|
||||
.on_press(SelectorMessage::ToRepetitions)
|
||||
.style(Self::nav_style),
|
||||
button("Рандомайзер")
|
||||
.on_press(SelectorMessage::ToRandomize)
|
||||
.style(Self::nav_style),
|
||||
button("Синхронизация")
|
||||
.on_press(SelectorMessage::ToSync)
|
||||
.style(Self::nav_style)
|
||||
|
||||
@@ -13,6 +13,8 @@ pub const DEFAULT_SPACING: f32 = 16.0;
|
||||
pub const ACCENT_FONT_SIZE: f32 = 22.0;
|
||||
pub const BUTTON_RADIUS: f32 = 8.0;
|
||||
|
||||
pub const SETTINGS_ICON: &[u8] = include_bytes!("../settings_icon.svg");
|
||||
|
||||
pub fn jl_button(theme: &Theme, status: Status) -> Style {
|
||||
let mut style = primary(theme, status);
|
||||
style.border = Border {
|
||||
|
||||
Reference in New Issue
Block a user