Splitting kana set
This commit is contained in:
1 parent
36e5693564
commit
c74a0eb311
25 files changed
+389
-354
No files matched your search
+63
-56
@@ -1,39 +1,46 @@
|
||||
use crate::app::{fill_state, AppState};
|
||||
use crate::data_provider::history::{get_history_of_set, history_dir};
|
||||
use crate::data_provider::history::get_history_of_set;
|
||||
use crate::data_provider::sqlite::{create_db, default_connection};
|
||||
use crate::data_provider::web_api::{get_local_version, get_web_version, load_data, set_local_version};
|
||||
use crate::dictionary::{app_data_dir, DictionaryMessage, DictionaryState};
|
||||
use crate::dictionary_quiz::{DictionaryQuizMessage, DictionaryQuizState};
|
||||
use crate::history::{HistoryMessage, HistoryState};
|
||||
use crate::import::{ImportMessage, ImportState};
|
||||
use crate::data_provider::web_api::{
|
||||
get_local_version, get_web_version, load_data, set_local_version,
|
||||
};
|
||||
use crate::fs::*;
|
||||
use crate::lang::Id;
|
||||
use crate::message_navigation;
|
||||
use crate::navigation::Page::*;
|
||||
use crate::navigation::RootMessage::{DataLoaded, Keyboard, Notification, PushNotification, UpdateData};
|
||||
use crate::notification::{NotificationData, NotificationManager, NotificationMessage, NotificationRequest};
|
||||
use crate::quiz::{QuizMessage, QuizState};
|
||||
use crate::randomizer::{RandomizerMessage, RandomizerState};
|
||||
use crate::repetition::{RepetitionMessage, RepetitionState};
|
||||
use crate::repetition_settings::{RepetitionSettingsMessage, RepetitionSettingsState};
|
||||
use crate::repetitions::{RepetitionsMessage, RepetitionsState};
|
||||
use crate::selector::{SelectorMessage, SelectorState};
|
||||
use crate::navigation::RootMessage::{
|
||||
DataLoaded, Keyboard, Notification, PushNotification, UpdateData,
|
||||
};
|
||||
use crate::notification::{
|
||||
NotificationData, NotificationManager, NotificationMessage, NotificationRequest,
|
||||
};
|
||||
use crate::pages::dictionary::{DictionaryMessage, DictionaryState};
|
||||
use crate::pages::dictionary_quiz::{DictionaryQuizMessage, DictionaryQuizState};
|
||||
use crate::pages::history::{HistoryMessage, HistoryState};
|
||||
use crate::pages::import::{ImportMessage, ImportState};
|
||||
use crate::pages::quiz::{QuizMessage, QuizState};
|
||||
use crate::pages::randomizer::{RandomizerMessage, RandomizerState};
|
||||
use crate::pages::repetition::{RepetitionMessage, RepetitionState};
|
||||
use crate::pages::repetition_settings::{RepetitionSettingsMessage, RepetitionSettingsState};
|
||||
use crate::pages::repetitions::{RepetitionsMessage, RepetitionsState};
|
||||
use crate::pages::selector::{SelectorMessage, SelectorState};
|
||||
use crate::pages::sync::{SyncMessage, SyncState};
|
||||
use crate::pages::word::{WordMessage, WordState};
|
||||
use crate::pages::writing::{WritingMessage, WritingState};
|
||||
use crate::state_navigate_handle;
|
||||
use crate::state_update;
|
||||
use crate::sync::{SyncMessage, SyncState};
|
||||
use crate::view_navigation;
|
||||
use crate::word::{WordMessage, WordState};
|
||||
use crate::writing::{WritingMessage, WritingState};
|
||||
use chrono::NaiveDate;
|
||||
use hashbrown::HashMap;
|
||||
use iced::keyboard::Event;
|
||||
use iced::widget::stack;
|
||||
use iced::{Element, Task};
|
||||
use iced_core::Length::Fill;
|
||||
use log::info;
|
||||
use parking_lot::Mutex;
|
||||
use rusqlite::Connection;
|
||||
use std::sync::Arc;
|
||||
use std::time::Instant;
|
||||
use log::info;
|
||||
|
||||
impl Default for ScreenState {
|
||||
fn default() -> Self {
|
||||
@@ -91,7 +98,7 @@ pub enum Page {
|
||||
pub struct ScreenState {
|
||||
stack: Vec<Page>,
|
||||
app_state: Arc<Mutex<AppState>>,
|
||||
notification_manager: NotificationManager
|
||||
notification_manager: NotificationManager,
|
||||
}
|
||||
|
||||
impl ScreenState {
|
||||
@@ -108,21 +115,16 @@ impl ScreenState {
|
||||
let key = state_guard.sync_data.key.clone().unwrap();
|
||||
final_task = Task::batch([
|
||||
reading_additional_task,
|
||||
Task::perform(
|
||||
Self::load_web_backup(key),
|
||||
|result| {
|
||||
match result {
|
||||
Ok(update) => {
|
||||
if update {
|
||||
UpdateData
|
||||
}else {
|
||||
RootMessage::None
|
||||
}
|
||||
}
|
||||
Err(err) => PushNotification(NotificationRequest::Warning(err))
|
||||
Task::perform(Self::load_web_backup(key), |result| match result {
|
||||
Ok(update) => {
|
||||
if update {
|
||||
UpdateData
|
||||
} else {
|
||||
RootMessage::None
|
||||
}
|
||||
}
|
||||
),
|
||||
Err(err) => PushNotification(NotificationRequest::Warning(err)),
|
||||
}),
|
||||
]);
|
||||
} else {
|
||||
final_task = reading_additional_task;
|
||||
@@ -135,28 +137,35 @@ impl ScreenState {
|
||||
|
||||
async fn load_additional_data() -> RootMessage {
|
||||
let directory = history_dir();
|
||||
let mut map = HashMap::new();
|
||||
for file in directory.read_dir().unwrap().flatten() {
|
||||
let mut vec = vec![];
|
||||
let history_file_name = match file.file_name().into_string() {
|
||||
Err(_) => continue,
|
||||
Ok(v) => v,
|
||||
};
|
||||
if let Ok(directory) = directory {
|
||||
let mut map = HashMap::new();
|
||||
for file in directory.read_dir().unwrap().flatten() {
|
||||
let mut vec = vec![];
|
||||
let history_file_name = match file.file_name().into_string() {
|
||||
Err(_) => continue,
|
||||
Ok(v) => v,
|
||||
};
|
||||
|
||||
let id = history_file_name[4..history_file_name.len() - 12]
|
||||
.parse::<Id>()
|
||||
.unwrap();
|
||||
let id = history_file_name[4..history_file_name.len() - 12]
|
||||
.parse::<Id>()
|
||||
.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));
|
||||
let history = get_history_of_set(id);
|
||||
if let Ok(history) = history {
|
||||
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);
|
||||
} else {
|
||||
return PushNotification(NotificationRequest::Warning(history.err().unwrap()));
|
||||
}
|
||||
}
|
||||
map.insert(id, vec);
|
||||
return DataLoaded(map);
|
||||
}
|
||||
DataLoaded(map)
|
||||
PushNotification(NotificationRequest::Warning(directory.err().unwrap()))
|
||||
}
|
||||
|
||||
async fn load_web_backup(string: String) -> Result<bool, NotificationData> {
|
||||
@@ -191,9 +200,8 @@ impl ScreenState {
|
||||
if cfg!(windows) {
|
||||
state.connection = Connection::open_in_memory().unwrap();
|
||||
}
|
||||
let path = app_data_dir();
|
||||
let db_file = path.join("data.db");
|
||||
let temp_db_file = path.join("data.db.tmp");
|
||||
let db_file = db_path();
|
||||
let temp_db_file = temp_db_path();
|
||||
std::fs::rename(temp_db_file, &db_file).unwrap();
|
||||
|
||||
state.connection = default_connection();
|
||||
@@ -206,7 +214,7 @@ impl ScreenState {
|
||||
}
|
||||
|
||||
if let PushNotification(notification) = &message {
|
||||
return self.notification_manager.push(notification.clone())
|
||||
return self.notification_manager.push(notification.clone());
|
||||
}
|
||||
|
||||
let task: (Task<RootMessage>, bool) = self.navigate_or_update(message);
|
||||
@@ -255,7 +263,6 @@ impl ScreenState {
|
||||
}
|
||||
|
||||
pub fn view(&self) -> Element<'_, RootMessage> {
|
||||
|
||||
info!("View update");
|
||||
let time = Instant::now();
|
||||
let view = view_navigation!(
|
||||
@@ -275,7 +282,7 @@ impl ScreenState {
|
||||
Import
|
||||
);
|
||||
|
||||
let notifications = self.notification_manager.draw().map(RootMessage::Notification);
|
||||
let notifications = self.notification_manager.draw().map(Notification);
|
||||
|
||||
info!("View took {:?}", time.elapsed());
|
||||
stack![view, notifications].width(Fill).height(Fill).into()
|
||||
|
||||
Reference in new issue
Block a user