Change sync page

This commit is contained in:
2026-06-15 17:20:01 +03:00
parent 4e57f3fd6b
commit 29b1711283
+43 -15
View File
@@ -1,15 +1,16 @@
use std::io; use crate::data_provider::settings::{delete_settings, set_setting};
use crate::data_provider::settings::set_setting; 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::{AppState, NavigatedPage, Page, RootMessage, DEFAULT_SPACING};
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};
use iced::{Element, Fill, Font, Left, Task}; use iced::{Center, Element, Fill, Font, Left, Task};
use std::io;
use std::sync::{Arc, Mutex}; use std::sync::{Arc, Mutex};
use std::time::Duration; use std::time::Duration;
use zstd::{Decoder, Encoder, DEFAULT_COMPRESSION_LEVEL}; use zstd::{Decoder, Encoder, DEFAULT_COMPRESSION_LEVEL};
use crate::dictionary::app_data_dir;
const API_URL: &str = "https://learning.micialware.ru/"; const API_URL: &str = "https://learning.micialware.ru/";
/*const API_URL: &str = "http://localhost:8089/";*/ /*const API_URL: &str = "http://localhost:8089/";*/
@@ -26,6 +27,8 @@ pub enum SyncMessage {
GetLast, GetLast,
NextAnimation, NextAnimation,
NetworkFinished, NetworkFinished,
Disable,
DisableSync,
} }
#[derive(Clone)] #[derive(Clone)]
pub struct SyncState { pub struct SyncState {
@@ -112,7 +115,7 @@ impl SyncState {
]); ]);
return tasks; return tasks;
}, }
NextAnimation => { NextAnimation => {
self.progress += 1.0; self.progress += 1.0;
if self.progress > 5.0 { if self.progress > 5.0 {
@@ -128,6 +131,13 @@ impl SyncState {
SyncMessage::NetworkFinished => { SyncMessage::NetworkFinished => {
self.frozen = false; self.frozen = false;
} }
SyncMessage::Disable => {}
SyncMessage::DisableSync => {
let mut state = self.state.lock().unwrap();
state.sync_data.key = None;
delete_settings("SYNC_KEY".to_string(), &state.connection);
}
} }
Task::none() Task::none()
} }
@@ -135,7 +145,9 @@ impl SyncState {
container( container(
column![ column![
button("Назад").on_press(SyncMessage::Back), button("Назад").on_press(SyncMessage::Back),
row![self.sync_column(), self.app_updater()].spacing(DEFAULT_SPACING) row![space().width(Fill), self.sync_column(), space().width(Fill)]
.spacing(DEFAULT_SPACING)
.width(Fill)
] ]
.align_x(Left) .align_x(Left)
.width(Fill) .width(Fill)
@@ -153,32 +165,40 @@ impl SyncState {
} else { } else {
space().into() space().into()
}; };
{
if let Some(key) = self.state.lock().unwrap().sync_data.key.clone() { if let Some(key) = self.state.lock().unwrap().sync_data.key.clone() {
column![ column![
text!("Ваш ключ синхронизации"), text!("Ваш ключ синхронизации"),
container(container(text!("{}", key).size(20).font(Font::MONOSPACE)).padding(3)) container(
container(text!("{}", key).size(20).font(Font::MONOSPACE)).padding(3)
)
.style(rounded_box), .style(rounded_box),
button("Скопировать в буффер обмена").on_press(SyncMessage::CopyKey), button("Скопировать в буффер обмена")
.on_press(SyncMessage::CopyKey)
.width(Fill),
row![ row![
button("↑ Отправить").on_press(SyncMessage::Send), button("↑ Отправить").on_press(SyncMessage::Send),
space().width(Fill),
button("↓ Скачать").on_press(SyncMessage::GetLast) button("↓ Скачать").on_press(SyncMessage::GetLast)
] ]
.spacing(DEFAULT_SPACING), .spacing(DEFAULT_SPACING),
container(network_view).width(Fill), container(network_view).width(Fill),
button("Отключить синхронизацию")
.style(danger)
.on_press(SyncMessage::DisableSync),
] ]
.spacing(DEFAULT_SPACING)
.width(Fill)
.into()
} else { } else {
column![ column![
button("Создать сохранение").on_press(SyncMessage::InitSync), button("Создать сохранение").on_press(SyncMessage::InitSync),
button("Вставить ключ из буффера").on_press(SyncMessage::GetKey), button("Вставить ключ из буффера").on_press(SyncMessage::GetKey),
] ]
.spacing(DEFAULT_SPACING)
.width(Fill)
.into()
} }
} }
.spacing(DEFAULT_SPACING)
.align_x(Center)
.width(300)
.into()
}
fn app_updater(&self) -> Element<'_, SyncMessage> { fn app_updater(&self) -> Element<'_, SyncMessage> {
column![].width(Fill).into() column![].width(Fill).into()
@@ -225,7 +245,15 @@ async fn load_data(id: String){
let api_url = API_URL.to_string(); let api_url = API_URL.to_string();
let id_url = format!("{api_url}download/{id}"); let id_url = format!("{api_url}download/{id}");
let client = reqwest::Client::new(); let client = reqwest::Client::new();
let data = client.get(&id_url).send().await.unwrap().bytes().await.unwrap().to_vec(); let data = client
.get(&id_url)
.send()
.await
.unwrap()
.bytes()
.await
.unwrap()
.to_vec();
let data = decompress(data); let data = decompress(data);
let path = app_data_dir(); let path = app_data_dir();