More notifications and move start code to another file
This commit is contained in:
1 parent
e1bfd9494d
commit
5a926b1419
17 files changed
+240
-198
No files matched your search
+22
-23
@@ -1,10 +1,11 @@
|
||||
use crate::AppState;
|
||||
use crate::data_provider::import::{ImportData, ImportGroup, get_words_of_group, load_groups};
|
||||
use crate::data_provider::words::{add_group, add_words};
|
||||
use crate::dictionary::app_data_dir;
|
||||
use crate::import::ImportMessage::*;
|
||||
use crate::lang::{WordData, WordGroup};
|
||||
use crate::navigation::{NavigatedPage, Page, RootMessage};
|
||||
use crate::notification::NotificationData::OpenFileError;
|
||||
use crate::notification::{NotificationData, NotificationRequest};
|
||||
use crate::styling::*;
|
||||
use iced::widget::button::danger;
|
||||
use iced::widget::container::success;
|
||||
@@ -25,6 +26,7 @@ use std::path::PathBuf;
|
||||
use std::sync::Arc;
|
||||
use tokio::task::spawn_blocking;
|
||||
use zip::ZipArchive;
|
||||
use crate::app::AppState;
|
||||
|
||||
const DEFAULT_FIELDS: [&str; 6] = ["key", "value", "tags", "reading", "context", "description"];
|
||||
|
||||
@@ -275,9 +277,6 @@ impl ImportState {
|
||||
let mut row = Row::new();
|
||||
|
||||
for default in DEFAULT_FIELDS {
|
||||
// if group.mapping.values().any(|name| name == default) {
|
||||
// continue;
|
||||
// }
|
||||
row = row.push(
|
||||
button(text!("{default}")).on_press(SetupDefaultProperty(default.to_string())),
|
||||
);
|
||||
@@ -365,41 +364,38 @@ impl ImportState {
|
||||
.await
|
||||
.unwrap()
|
||||
},
|
||||
|result: Result<ImportData, ()>| {
|
||||
if let Ok(import) = result {
|
||||
RootMessage::Import(UpdateImport(import))
|
||||
} else {
|
||||
RootMessage::None
|
||||
}
|
||||
|result: Result<ImportData, NotificationData>| match result {
|
||||
Ok(import) => RootMessage::Import(UpdateImport(import)),
|
||||
Err(err) => RootMessage::PushNotification(NotificationRequest::Error(err)),
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
fn extract_import_file(path: PathBuf) -> Result<(), ()> {
|
||||
let file = File::open(path).map_err(|_| ())?;
|
||||
fn extract_import_file(path: PathBuf) -> Result<(), NotificationData> {
|
||||
let file = File::open(path).map_err(|_| OpenFileError)?;
|
||||
let reader = BufReader::new(file);
|
||||
let mut archive = ZipArchive::new(reader).map_err(|_| ())?;
|
||||
let mut archive = ZipArchive::new(reader).map_err(|_| OpenFileError)?;
|
||||
let temp_file_path = app_data_dir().join("import");
|
||||
let mut temp_file = OpenOptions::new()
|
||||
.write(true)
|
||||
.create(true)
|
||||
.truncate(true)
|
||||
.open(&temp_file_path)
|
||||
.map_err(|_| ())?;
|
||||
.map_err(|_| OpenFileError)?;
|
||||
if let Ok(mut file) = archive.by_name("collection.anki21") {
|
||||
std::io::copy(&mut file, &mut temp_file).map_err(|_| ())?;
|
||||
std::io::copy(&mut file, &mut temp_file).map_err(|_| OpenFileError)?;
|
||||
Ok(())
|
||||
} else if let Ok(mut file) = archive.by_name("collection.anki2") {
|
||||
std::io::copy(&mut file, &mut temp_file).map_err(|_| ())?;
|
||||
std::io::copy(&mut file, &mut temp_file).map_err(|_| OpenFileError)?;
|
||||
Ok(())
|
||||
} else {
|
||||
Err(())
|
||||
Err(OpenFileError)
|
||||
}
|
||||
}
|
||||
fn read_import_file() -> Result<ImportData, ()> {
|
||||
fn read_import_file() -> Result<ImportData, NotificationData> {
|
||||
let temp_file_path = app_data_dir().join("import");
|
||||
let connection = Connection::open(&temp_file_path).map_err(|_| ())?;
|
||||
let data = load_groups(&connection);
|
||||
let connection = Connection::open(&temp_file_path).map_err(|_| OpenFileError)?;
|
||||
let data = load_groups(&connection)?;
|
||||
Ok(data)
|
||||
}
|
||||
fn setup_mapping(&mut self, property_name: String) {
|
||||
@@ -435,8 +431,8 @@ impl ImportState {
|
||||
async move {
|
||||
spawn_blocking(move || {
|
||||
let temp_file_path = app_data_dir().join("import");
|
||||
let connection = Connection::open(&temp_file_path).map_err(|_| ())?;
|
||||
let import_list = get_words_of_group(&connection, group.id);
|
||||
let connection = Connection::open(&temp_file_path).map_err(|_| OpenFileError)?;
|
||||
let import_list = get_words_of_group(&connection, group.id)?;
|
||||
let mut words_list = Vec::with_capacity(import_list.len());
|
||||
|
||||
let map_indices = Self::get_mapping_indices(&group);
|
||||
@@ -500,7 +496,10 @@ impl ImportState {
|
||||
.await
|
||||
.unwrap()
|
||||
},
|
||||
|_: Result<(), ()>| RootMessage::Import(ImportFinished),
|
||||
|r: Result<(), NotificationData>| match r {
|
||||
Ok(_) => RootMessage::Import(ImportFinished),
|
||||
Err(err) => RootMessage::PushNotification(NotificationRequest::Error(err))
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user