Dynamic sync
This commit is contained in:
+28
-12
@@ -96,30 +96,46 @@ pub struct AppState {
|
|||||||
pub sync_data: AppSettings,
|
pub sync_data: AppSettings,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for ScreenState {
|
impl AppState {
|
||||||
fn default() -> Self {
|
pub fn new() -> Self {
|
||||||
let path = app_data_dir();
|
let path = app_data_dir();
|
||||||
let db_file = path.join("data.db");
|
let db_file = path.join("data.db");
|
||||||
let connection = Connection::open(db_file).unwrap();
|
let connection = Connection::open(db_file).unwrap();
|
||||||
connection.execute("PRAGMA foreign_keys = ON;", []).unwrap();
|
connection.execute("PRAGMA foreign_keys = ON;", []).unwrap();
|
||||||
let list = load_words(&connection);
|
|
||||||
let sets = load_sets(&connection);
|
|
||||||
let groups = load_word_groups(&connection);
|
|
||||||
let setting = load_settings(&connection);
|
|
||||||
|
|
||||||
let state = Arc::new(Mutex::new(AppState {
|
Self{
|
||||||
dictionary: list,
|
dictionary: vec![],
|
||||||
card_sets: sets,
|
card_sets: vec![],
|
||||||
|
word_groups: vec![],
|
||||||
connection,
|
connection,
|
||||||
word_groups: groups,
|
sync_data: AppSettings { key: None },
|
||||||
sync_data: setting,
|
}
|
||||||
}));
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Default for ScreenState {
|
||||||
|
fn default() -> Self {
|
||||||
|
let mut state = AppState::new();
|
||||||
|
fill_state(&mut state);
|
||||||
|
let state = Arc::new(Mutex::new(state));
|
||||||
ScreenState {
|
ScreenState {
|
||||||
stack: vec![Selector(SelectorState::new(state.clone()))],
|
stack: vec![Selector(SelectorState::new(state.clone()))],
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn fill_state(mut state: &mut AppState) {
|
||||||
|
let list = load_words(&state.connection);
|
||||||
|
let sets = load_sets(&state.connection);
|
||||||
|
let groups = load_word_groups(&state.connection);
|
||||||
|
let setting = load_settings(&state.connection);
|
||||||
|
|
||||||
|
state.dictionary = list;
|
||||||
|
state.card_sets = sets;
|
||||||
|
state.word_groups = groups;
|
||||||
|
state.sync_data = setting
|
||||||
|
}
|
||||||
|
|
||||||
fn load_settings(connection: &Connection) -> AppSettings {
|
fn load_settings(connection: &Connection) -> AppSettings {
|
||||||
let key = get_setting("SYNC_KEY".to_string(), connection);
|
let key = get_setting("SYNC_KEY".to_string(), connection);
|
||||||
AppSettings { key }
|
AppSettings { key }
|
||||||
|
|||||||
+11
-1
@@ -2,7 +2,7 @@ use crate::data_provider::settings::{delete_settings, set_setting};
|
|||||||
use crate::dictionary::app_data_dir;
|
use crate::dictionary::app_data_dir;
|
||||||
use crate::sync::SyncMessage::NextAnimation;
|
use crate::sync::SyncMessage::NextAnimation;
|
||||||
use crate::Page::PreviousPage;
|
use crate::Page::PreviousPage;
|
||||||
use crate::{AppState, NavigatedPage, Page, RootMessage, DEFAULT_SPACING};
|
use crate::{fill_state, AppState, NavigatedPage, Page, RootMessage, DEFAULT_SPACING};
|
||||||
use iced::widget::button::danger;
|
use iced::widget::button::danger;
|
||||||
use iced::widget::container::rounded_box;
|
use iced::widget::container::rounded_box;
|
||||||
use iced::widget::{button, column, container, progress_bar, row, space, text};
|
use iced::widget::{button, column, container, progress_bar, row, space, text};
|
||||||
@@ -130,6 +130,16 @@ impl SyncState {
|
|||||||
}
|
}
|
||||||
SyncMessage::NetworkFinished => {
|
SyncMessage::NetworkFinished => {
|
||||||
self.frozen = false;
|
self.frozen = false;
|
||||||
|
let mut updated_state = AppState::new();
|
||||||
|
fill_state(&mut updated_state);
|
||||||
|
|
||||||
|
let mut state = self.state.lock().unwrap();
|
||||||
|
state.sync_data = updated_state.sync_data;
|
||||||
|
state.connection = updated_state.connection;
|
||||||
|
state.card_sets = updated_state.card_sets;
|
||||||
|
state.dictionary = updated_state.dictionary;
|
||||||
|
state.word_groups = updated_state.word_groups;
|
||||||
|
|
||||||
}
|
}
|
||||||
SyncMessage::Disable => {}
|
SyncMessage::Disable => {}
|
||||||
SyncMessage::DisableSync => {
|
SyncMessage::DisableSync => {
|
||||||
|
|||||||
Reference in New Issue
Block a user