Autosync first edition
This commit is contained in:
+18
-75
@@ -1,5 +1,5 @@
|
||||
use crate::data_provider::settings::{delete_settings, set_setting};
|
||||
use crate::dictionary::app_data_dir;
|
||||
use crate::data_provider::web_api::*;
|
||||
use crate::navigation::Page::PreviousPage;
|
||||
use crate::navigation::{NavigatedPage, Page};
|
||||
use crate::styling::*;
|
||||
@@ -7,14 +7,11 @@ use crate::sync::SyncMessage::*;
|
||||
use crate::{fill_state, AppState, RootMessage};
|
||||
use iced::widget::button::danger;
|
||||
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, toggler};
|
||||
use iced::{Center, Element, Fill, Font, Length, Task};
|
||||
use std::io;
|
||||
use std::sync::{Arc, Mutex};
|
||||
use std::time::Duration;
|
||||
use zstd::{Decoder, Encoder, DEFAULT_COMPRESSION_LEVEL};
|
||||
|
||||
const API_URL: &str = "https://learning.micialware.ru/";
|
||||
|
||||
#[derive(Clone)]
|
||||
pub enum SyncMessage {
|
||||
@@ -30,10 +27,12 @@ pub enum SyncMessage {
|
||||
NetworkFinished,
|
||||
Disable,
|
||||
DisableSync,
|
||||
SwitchAutoSync,
|
||||
}
|
||||
#[derive(Clone)]
|
||||
pub struct SyncState {
|
||||
state: Arc<Mutex<AppState>>,
|
||||
auto_fetch: bool,
|
||||
frozen: bool,
|
||||
progress: f32,
|
||||
}
|
||||
@@ -49,13 +48,17 @@ impl NavigatedPage<SyncMessage> for SyncState {
|
||||
}
|
||||
}
|
||||
|
||||
fn navigated(&mut self) {
|
||||
}
|
||||
fn navigated(&mut self) {}
|
||||
}
|
||||
|
||||
impl SyncState {
|
||||
pub fn new(state: Arc<Mutex<AppState>>) -> SyncState {
|
||||
let auto_fetch;
|
||||
{
|
||||
auto_fetch = state.lock().unwrap().sync_data.auto_web_fetch;
|
||||
}
|
||||
Self {
|
||||
auto_fetch,
|
||||
state,
|
||||
frozen: false,
|
||||
progress: 0.0,
|
||||
@@ -108,7 +111,7 @@ impl SyncState {
|
||||
async { tokio::time::sleep(Duration::from_millis(200)).await },
|
||||
|_| RootMessage::Sync(NextAnimation),
|
||||
),
|
||||
Task::perform(load_data(id), |_| RootMessage::Sync(NetworkFinished)),
|
||||
Task::perform(load_data(id, false), |_| RootMessage::Sync(NetworkFinished)),
|
||||
]);
|
||||
|
||||
return tasks;
|
||||
@@ -144,6 +147,12 @@ impl SyncState {
|
||||
|
||||
delete_settings("SYNC_KEY".to_string(), &state.connection);
|
||||
}
|
||||
SwitchAutoSync => {
|
||||
self.auto_fetch = !self.auto_fetch;
|
||||
let mut state = self.state.lock().unwrap();
|
||||
state.sync_data.auto_web_fetch = self.auto_fetch;
|
||||
set_setting("AUTO_WEB_FETCH".to_string(), self.auto_fetch.to_string(), &state.connection);
|
||||
}
|
||||
}
|
||||
Task::none()
|
||||
}
|
||||
@@ -187,6 +196,7 @@ impl SyncState {
|
||||
button("↓ Скачать").style(jl_button).on_press(GetLast)
|
||||
]
|
||||
.spacing(DEFAULT_SPACING),
|
||||
toggler(self.auto_fetch).label("Автоматическая синхронизация").on_toggle(|_| SwitchAutoSync),
|
||||
container(network_view).width(Fill),
|
||||
button("Отключить синхронизацию")
|
||||
.style(danger)
|
||||
@@ -229,73 +239,6 @@ impl SyncState {
|
||||
}
|
||||
}
|
||||
|
||||
async fn send_data(id: String) {
|
||||
let path = app_data_dir();
|
||||
let db_file = path.join("data.db");
|
||||
let data = tokio::fs::read(db_file).await.unwrap();
|
||||
let data = compress(data);
|
||||
|
||||
let api_url = API_URL.to_string();
|
||||
let id_url = format!("{api_url}upload/{id}");
|
||||
|
||||
let form = reqwest::multipart::Form::new().part("db", reqwest::multipart::Part::bytes(data));
|
||||
let client = reqwest::Client::new();
|
||||
client.post(&id_url).multipart(form).send().await.unwrap();
|
||||
}
|
||||
|
||||
fn compress(data: Vec<u8>) -> Vec<u8> {
|
||||
let mut encoder = Encoder::new(Vec::new(), DEFAULT_COMPRESSION_LEVEL).unwrap();
|
||||
io::copy(&mut &data[..], &mut encoder).unwrap();
|
||||
let compressed = encoder.finish().unwrap();
|
||||
compressed
|
||||
}
|
||||
|
||||
async fn load_data(id: String) {
|
||||
let api_url = API_URL.to_string();
|
||||
let id_url = format!("{api_url}download/{id}");
|
||||
let client = reqwest::Client::new();
|
||||
let data = client
|
||||
.get(&id_url)
|
||||
.send()
|
||||
.await
|
||||
.unwrap()
|
||||
.bytes()
|
||||
.await
|
||||
.unwrap()
|
||||
.to_vec();
|
||||
let data = decompress(data);
|
||||
|
||||
let path = app_data_dir();
|
||||
let db_file = path.join("data.db");
|
||||
tokio::fs::write(db_file, data).await.unwrap();
|
||||
}
|
||||
|
||||
fn decompress(data: Vec<u8>) -> Vec<u8> {
|
||||
let mut decoder = Decoder::new(&data[..]).unwrap();
|
||||
let mut decompressed = Vec::new();
|
||||
let res = io::copy(&mut decoder, &mut decompressed);
|
||||
if let Err(e) = res {
|
||||
println!("{}", e);
|
||||
}
|
||||
decompressed
|
||||
}
|
||||
|
||||
async fn first_sync() -> String {
|
||||
let id_url = API_URL.to_string() + "generate";
|
||||
|
||||
let client = reqwest::Client::new();
|
||||
let id = client
|
||||
.get(&id_url)
|
||||
.send()
|
||||
.await
|
||||
.unwrap()
|
||||
.text()
|
||||
.await
|
||||
.unwrap();
|
||||
println!("id: {}", id);
|
||||
id
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn validate_id(id: &str) -> bool {
|
||||
id.len() == 24 && id.chars().all(|c| c.is_ascii_alphanumeric())
|
||||
|
||||
Reference in New Issue
Block a user