Autosync first edition
This commit is contained in:
+73
-24
@@ -1,27 +1,29 @@
|
||||
use crate::state_navigate_handle;
|
||||
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::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_test::{DictionaryQuizMessage, DictionaryQuizState};
|
||||
use crate::history::{HistoryMessage, HistoryState};
|
||||
use crate::message_navigation;
|
||||
use crate::navigation::Page::*;
|
||||
use crate::navigation::RootMessage::{DataLoaded, Keyboard};
|
||||
use crate::navigation::RootMessage::{DataLoaded, Keyboard, UpdateData};
|
||||
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::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 crate::{fill_state, AppState};
|
||||
use crate::{AppState, fill_state};
|
||||
use chrono::NaiveDate;
|
||||
use iced::keyboard::Event;
|
||||
use iced::{Element, Task};
|
||||
use reqwest::Error;
|
||||
use std::collections::HashMap;
|
||||
use std::sync::{Arc, Mutex};
|
||||
use std::time::Instant;
|
||||
@@ -54,6 +56,8 @@ pub enum RootMessage {
|
||||
RepetitionSettings(RepetitionSettingsMessage),
|
||||
Keyboard(Event),
|
||||
DataLoaded(HashMap<u32, Vec<(NaiveDate, u32)>>),
|
||||
None,
|
||||
UpdateData,
|
||||
}
|
||||
|
||||
pub enum Page {
|
||||
@@ -81,10 +85,29 @@ pub struct ScreenState {
|
||||
impl ScreenState {
|
||||
pub fn boot() -> (ScreenState, Task<RootMessage>) {
|
||||
create_db();
|
||||
(
|
||||
ScreenState::default(),
|
||||
Task::perform(Self::load_additional_data(), |data| data),
|
||||
)
|
||||
let state = ScreenState::default();
|
||||
let reading_additional_task = Task::perform(Self::load_additional_data(), |data| data);
|
||||
|
||||
let final_task;
|
||||
let state_guard = state.app_state.lock().unwrap();
|
||||
if state_guard.sync_data.auto_web_fetch {
|
||||
let key = state_guard.sync_data.key.clone().unwrap();
|
||||
final_task = Task::batch([
|
||||
reading_additional_task,
|
||||
Task::perform(Self::load_web_backup(key), |result| {
|
||||
if let Ok(update) = result && update {
|
||||
UpdateData
|
||||
} else {
|
||||
RootMessage::None
|
||||
}
|
||||
}),
|
||||
]);
|
||||
} else {
|
||||
final_task = reading_additional_task;
|
||||
}
|
||||
|
||||
drop(state_guard);
|
||||
(state, final_task)
|
||||
}
|
||||
|
||||
async fn load_additional_data() -> RootMessage {
|
||||
@@ -111,6 +134,19 @@ impl ScreenState {
|
||||
DataLoaded(map)
|
||||
}
|
||||
|
||||
async fn load_web_backup(string: String) -> Result<bool, Error> {
|
||||
let local = get_local_version().await;
|
||||
let web = get_web_version(string.as_str()).await?;
|
||||
if web > local {
|
||||
println!("Web version is newer than local version");
|
||||
load_data(string, true).await;
|
||||
set_local_version(web).await;
|
||||
}else {
|
||||
return Ok(false);
|
||||
}
|
||||
Ok(true)
|
||||
}
|
||||
|
||||
pub fn update(&mut self, message: RootMessage) -> Task<RootMessage> {
|
||||
let time = Instant::now();
|
||||
if let Keyboard(e) = message {
|
||||
@@ -122,6 +158,18 @@ impl ScreenState {
|
||||
return Task::none();
|
||||
}
|
||||
|
||||
if let UpdateData = message{
|
||||
println!("Loading data");
|
||||
let mut state = self.app_state.lock().unwrap();
|
||||
let path = app_data_dir();
|
||||
let db_file = path.join("data.db");
|
||||
let temp_db_file = path.join("data.db.tmp");
|
||||
std::fs::rename(temp_db_file, &db_file).unwrap();
|
||||
|
||||
state.connection = default_connection();
|
||||
fill_state(&mut state);
|
||||
return Task::none();
|
||||
}
|
||||
let task: (Task<RootMessage>, bool) = self.navigate_or_update(message);
|
||||
|
||||
if task.1 {
|
||||
@@ -148,21 +196,21 @@ impl ScreenState {
|
||||
|
||||
fn navigate_or_update(&mut self, message: RootMessage) -> (Task<RootMessage>, bool) {
|
||||
state_update!(
|
||||
message,
|
||||
self.stack,
|
||||
Selector,
|
||||
Quiz,
|
||||
Writing,
|
||||
Dictionary,
|
||||
DictionaryQuiz,
|
||||
Randomizer,
|
||||
Repetitions,
|
||||
Repetition,
|
||||
Word,
|
||||
Sync,
|
||||
History,
|
||||
RepetitionSettings
|
||||
)
|
||||
message,
|
||||
self.stack,
|
||||
Selector,
|
||||
Quiz,
|
||||
Writing,
|
||||
Dictionary,
|
||||
DictionaryQuiz,
|
||||
Randomizer,
|
||||
Repetitions,
|
||||
Repetition,
|
||||
Word,
|
||||
Sync,
|
||||
History,
|
||||
RepetitionSettings
|
||||
)
|
||||
}
|
||||
|
||||
pub fn view(&self) -> Element<'_, RootMessage> {
|
||||
@@ -198,6 +246,7 @@ impl ScreenState {
|
||||
|
||||
pub struct AppSettings {
|
||||
pub(crate) key: Option<String>,
|
||||
pub(crate) auto_web_fetch: bool,
|
||||
}
|
||||
|
||||
#[macro_export]
|
||||
|
||||
Reference in New Issue
Block a user