Data provider refactor #11

Merged
micialware merged 11 commits from data-provider-refactor into master 2026-08-25 12:21:54 +03:00
2 changed files with 48 additions and 30 deletions
Showing only changes of commit 6f8adde06b - Show all commits

No files matched your search

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