Data provider refactor #11
No files matched your search
@@ -1,3 +1,5 @@
|
||||
use crate::notification::NotificationData;
|
||||
use crate::notification::NotificationData::{DbLoadError, DbSaveError};
|
||||
use rusqlite::Connection;
|
||||
|
||||
pub fn get_setting(key: String, connection: &Connection) -> Option<String> {
|
||||
@@ -15,13 +17,19 @@ pub fn get_setting(key: String, connection: &Connection) -> Option<String> {
|
||||
None
|
||||
}
|
||||
|
||||
pub fn set_setting(key: String, value: String, connection: &Connection) {
|
||||
let current = get_settings_list(connection);
|
||||
pub fn set_setting(
|
||||
key: String,
|
||||
value: String,
|
||||
connection: &Connection,
|
||||
) -> Result<(), NotificationData> {
|
||||
let current = get_settings_list(connection)?;
|
||||
if current.contains(&key) {
|
||||
update_settings(key, value, connection);
|
||||
update_settings(key, value, connection)?;
|
||||
} else {
|
||||
create_settings(key, value, connection);
|
||||
create_settings(key, value, connection)?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn delete_settings(key: String, connection: &Connection) {
|
||||
@@ -30,16 +38,21 @@ pub fn delete_settings(key: String, connection: &Connection) {
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
fn create_settings(key: String, value: String, connection: &Connection) {
|
||||
fn create_settings(key: String, value: String, connection: &Connection) -> Result<(), NotificationData>{
|
||||
connection
|
||||
.execute(
|
||||
"INSERT into settings (id, value) VALUES (?1, ?2)",
|
||||
(key, value),
|
||||
)
|
||||
.unwrap();
|
||||
.map_err(|_| DbSaveError)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn update_settings(key: String, value: String, connection: &Connection) {
|
||||
fn update_settings(
|
||||
key: String,
|
||||
value: String,
|
||||
connection: &Connection,
|
||||
) -> Result<(), NotificationData> {
|
||||
connection
|
||||
.execute(
|
||||
"update settings
|
||||
@@ -47,11 +60,12 @@ set value = ?2
|
||||
where id = ?1",
|
||||
(key, value),
|
||||
)
|
||||
.unwrap();
|
||||
.map_err(|_| DbSaveError)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn get_settings_list(connection: &Connection) -> Vec<String> {
|
||||
let mut stmt = connection.prepare("SELECT id FROM settings").unwrap();
|
||||
let iter = stmt.query_map((), |row| row.get(0)).unwrap();
|
||||
iter.map(|row| row.unwrap()).collect()
|
||||
fn get_settings_list(connection: &Connection) -> Result<Vec<String>, NotificationData> {
|
||||
let mut stmt = connection.prepare("SELECT id FROM settings").expect("prepare stmt failed");
|
||||
let iter = stmt.query_map((), |row| row.get(0)).map_err(|_| DbLoadError)?;
|
||||
Ok(iter.map(|row| row.unwrap()).collect())
|
||||
}
|
||||
+17
-13
@@ -1,20 +1,20 @@
|
||||
use crate::app::{AppState, fill_state};
|
||||
use crate::data_provider::settings::{delete_settings, set_setting};
|
||||
use crate::data_provider::web_api::*;
|
||||
use crate::navigation::Page::PreviousPage;
|
||||
use crate::navigation::{NavigatedPage, Page, RootMessage};
|
||||
use crate::styling::*;
|
||||
use crate::notification::{NotificationData, NotificationRequest};
|
||||
use crate::pages::sync::SyncMessage::*;
|
||||
use crate::styling::*;
|
||||
use crate::{push_error, push_error_task};
|
||||
use iced::widget::button::danger;
|
||||
use iced::widget::container::rounded_box;
|
||||
use iced::widget::{button, column, container, progress_bar, row, space, text, toggler};
|
||||
use iced::{Center, Element, Fill, Font, Length, Task};
|
||||
use log::error;
|
||||
use parking_lot::Mutex;
|
||||
use std::sync::Arc;
|
||||
use std::time::Duration;
|
||||
use log::error;
|
||||
use crate::app::{fill_state, AppState};
|
||||
use crate::notification::{NotificationRequest, NotificationData};
|
||||
use crate::push_error;
|
||||
|
||||
#[derive(Clone)]
|
||||
pub enum SyncMessage {
|
||||
@@ -62,11 +62,9 @@ impl NavigatedPage<SyncMessage> for SyncState {
|
||||
.map(|_val: String| RootMessage::Sync(KeyCopied));
|
||||
}
|
||||
InitSync => {
|
||||
return Task::perform(first_sync(), |id| {
|
||||
match id {
|
||||
return Task::perform(first_sync(), |id| match id {
|
||||
Ok(id) => RootMessage::Sync(IdReceived(id)),
|
||||
Err(err) => push_error!(err)
|
||||
}
|
||||
Err(err) => push_error!(err),
|
||||
});
|
||||
}
|
||||
GetKey => {
|
||||
@@ -80,7 +78,9 @@ impl NavigatedPage<SyncMessage> for SyncState {
|
||||
}
|
||||
let mut state = self.state.lock();
|
||||
state.sync_data.key = Some(new_id.clone());
|
||||
set_setting("SYNC_KEY".to_string(), new_id, &state.connection);
|
||||
if let Err(err) = set_setting("SYNC_KEY".to_string(), new_id, &state.connection) {
|
||||
return push_error_task!(err);
|
||||
}
|
||||
}
|
||||
Send => {
|
||||
self.prepare_to_db_interaction();
|
||||
@@ -131,7 +131,9 @@ impl NavigatedPage<SyncMessage> for SyncState {
|
||||
state.decks = updated_state.decks;
|
||||
state.dictionary = updated_state.dictionary;
|
||||
state.word_groups = updated_state.word_groups;
|
||||
return Task::done(RootMessage::PushNotification(NotificationRequest::Info(NotificationData::DataLoaded)))
|
||||
return Task::done(RootMessage::PushNotification(NotificationRequest::Info(
|
||||
NotificationData::DataLoaded,
|
||||
)));
|
||||
}
|
||||
Disable => {}
|
||||
DisableSync => {
|
||||
@@ -144,11 +146,13 @@ impl NavigatedPage<SyncMessage> for SyncState {
|
||||
self.auto_fetch = !self.auto_fetch;
|
||||
let mut state = self.state.lock();
|
||||
state.sync_data.auto_web_fetch = self.auto_fetch;
|
||||
set_setting(
|
||||
if let Err(err) = set_setting(
|
||||
"AUTO_WEB_FETCH".to_string(),
|
||||
self.auto_fetch.to_string(),
|
||||
&state.connection,
|
||||
);
|
||||
) {
|
||||
return push_error_task!(err);
|
||||
}
|
||||
}
|
||||
}
|
||||
Task::none()
|
||||
|
||||
Reference in new issue
Block a user