Dynamic sync
This commit is contained in:
+28
-12
@@ -96,30 +96,46 @@ pub struct AppState {
|
||||
pub sync_data: AppSettings,
|
||||
}
|
||||
|
||||
impl Default for ScreenState {
|
||||
fn default() -> Self {
|
||||
impl AppState {
|
||||
pub fn new() -> Self {
|
||||
let path = app_data_dir();
|
||||
let db_file = path.join("data.db");
|
||||
let connection = Connection::open(db_file).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 {
|
||||
dictionary: list,
|
||||
card_sets: sets,
|
||||
Self{
|
||||
dictionary: vec![],
|
||||
card_sets: vec![],
|
||||
word_groups: vec![],
|
||||
connection,
|
||||
word_groups: groups,
|
||||
sync_data: setting,
|
||||
}));
|
||||
sync_data: AppSettings { key: None },
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for ScreenState {
|
||||
fn default() -> Self {
|
||||
let mut state = AppState::new();
|
||||
fill_state(&mut state);
|
||||
let state = Arc::new(Mutex::new(state));
|
||||
ScreenState {
|
||||
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 {
|
||||
let key = get_setting("SYNC_KEY".to_string(), connection);
|
||||
AppSettings { key }
|
||||
|
||||
Reference in New Issue
Block a user