public void ImportBooksDrop(string[] paths) { if (paths.Length == 1 && Directory.Exists(paths[0])) { ImportDirectory(paths[0]); return; } else if (paths.Length == 1) { Task.Run(() => { if (!Formats.Resources.AcceptedFileTypes.Contains(Path.GetExtension(paths[0]))) { var errDlg = new Dialogs.Error("Incompatible Format", $"Importing {Path.GetExtension(paths[0])} format books has not yet been implemented."); _ = MaterialDesignThemes.Wpf.DialogHost.Show(errDlg); return; } try { ImportBook(paths[0]); } catch (Exception e) { App.Current.Dispatcher.Invoke(() => { var dlg = new Dialogs.Error("Import failed", e.Message); _ = MaterialDesignThemes.Wpf.DialogHost.Show(dlg); }); return; } finally { CloseBottomDrawer(); } SnackBarQueue.Enqueue($"{Path.GetFileName(paths[0])} imported"); }); } else { List <Exception> errs = new List <Exception>(); var prgDlg = new Dialogs.Progress("Importing Books", false); OpenBottomDrawer(prgDlg.Content); int step = 100 / paths.Length; Task.Run(() => { foreach (string path in paths) { prgDlg.Current = $"Importing {Path.GetFileName(path)}"; prgDlg.Percent += step; if (!Formats.Resources.AcceptedFileTypes.Contains(Path.GetExtension(path))) { Exception e = new Exception($"Invalid filetype ${Path.GetExtension(path)}"); e.Data["item"] = path; errs.Add(e); continue; } try { ImportBook(path); } catch (Exception e) { e.Data["item"] = path; errs.Add(e); continue; } } if (errs.Count > 0) { prgDlg.Finish("Import finished with errors:"); prgDlg.ShowError(new AggregateException(errs.ToArray())); } else { prgDlg.Close(); SnackBarQueue.Enqueue($"{paths.Length} new books imported successfully"); } }); } }
public async void _SyncDeviceLibrary() { if (SelectedDevice == null) { var errDlg = new Dialogs.Error("No Kindle Selected", "Connect to Kindle Before Transferring Books"); await MaterialDesignThemes.Wpf.DialogHost.Show(errDlg); return; } List <Database.BookEntry> toTransfer = new List <Database.BookEntry>(); foreach (Database.BookEntry book in App.LocalLibrary.Database.BOOKS) { if (!SelectedDevice.Database.BOOKS.Any(x => x.Id == book.Id)) { toTransfer.Add(book); } } var dlg = new Dialogs.SyncConfirm(toTransfer, SelectedDevice.Name); await MaterialDesignThemes.Wpf.DialogHost.Show(dlg); if (dlg.DialogResult == false) { return; } var a = dlg.UserSelectedBooks; foreach (var b in dlg.UserSelectedBooks) { if (!b.Checked) { Database.BookEntry t = toTransfer.FirstOrDefault(x => x.Id == b.Id); if (t != null) { toTransfer.Remove(t); } } } var prgDlg = new Dialogs.Progress("Syncing Kindle Library", false); OpenBottomDrawer(prgDlg.Content); _ = Task.Run(() => { List <Exception> errs = new List <Exception>(); int step = 100 / toTransfer.Count; for (int i = 0; i < toTransfer.Count; i++) { Database.BookEntry book = toTransfer[i]; try { prgDlg.Current = $"Copying {book.Title}"; prgDlg.Percent += step; book.FilePath = App.LocalLibrary.AbsoluteFilePath(book); SelectedDevice.ImportBook(book); } catch (Exception e) { e.Data.Add("item", book.Title); errs.Add(e); } } if (errs.Count > 0) { prgDlg.Finish("Library sync finished with errors:"); prgDlg.ShowError(new AggregateException(errs.ToArray())); } else { prgDlg.Close(); SnackBarQueue.Enqueue($"{SelectedDevice.Name} library synced"); } }); }
public Unit _SendBook(IList bookList) { if (SelectedDevice == null) { var errDlg = new Dialogs.Error("No Kindle Selected", "Connect to Kindle Before Transferring Books"); MaterialDesignThemes.Wpf.DialogHost.Show(errDlg); return(Unit.Default); } Task.Run(() => { if (bookList.Count == 1) { try { BookBase book = (BookBase)bookList[0]; book.FilePath = App.LocalLibrary.AbsoluteFilePath(book); SelectedDevice.ImportBook(book); SnackBarQueue.Enqueue($"{book.Title} transferred to {SelectedDevice.Name}"); } catch (Exception e) { var dlg = new Dialogs.Error("Error transferring book", e.Message); } } else { List <Exception> errs = new List <Exception>(); var prgDlg = new Dialogs.Progress("Syncing Library", false); OpenBottomDrawer(prgDlg.Content); int step = 100 / bookList.Count; foreach (Database.BookEntry book in bookList) { try { book.FilePath = App.LocalLibrary.AbsoluteFilePath(book); SelectedDevice.ImportBook(book); prgDlg.Current = $"Transferred {book.Title}"; } catch (Exception e) { e.Data["item"] = book.Title; errs.Add(e); } finally { prgDlg.Percent += step; } } if (errs.Count > 0) { prgDlg.Finish("Book transfer finished with errors:"); prgDlg.ShowError(new AggregateException(errs.ToArray())); } else { prgDlg.Close(); SnackBarQueue.Enqueue("Book transfer finished"); } } }); return(Unit.Default); }
public Unit _ReceiveBook(IList bookList) { if (bookList.Count == 0) { return(Unit.Default); } Database.BookEntry[] dbRows = new Database.BookEntry[bookList.Count]; bookList.CopyTo(dbRows, 0); Task.Run(() => { if (bookList.Count == 1) { Database.BookEntry book = (Database.BookEntry)bookList[0]; try { book.FilePath = SelectedDevice.AbsoluteFilePath(book); ImportBook(book); } catch (Exception e) { App.Current.Dispatcher.Invoke(() => { var dlg = new Dialogs.Error("Error transferring book", e.Message); MaterialDesignThemes.Wpf.DialogHost.Show(dlg); }); return; } SnackBarQueue.Enqueue($"{book.Title} copied to library."); } else { List <Exception> errs = new List <Exception>(); var prgDlg = new Dialogs.Progress("Syncing Library", false); OpenBottomDrawer(prgDlg.Content); int step = 100 / bookList.Count; foreach (Database.BookEntry b in bookList) { Database.BookEntry book = new Database.BookEntry(b); try { book.FilePath = SelectedDevice.AbsoluteFilePath(book); ImportBook(book); } catch (Exception e) { e.Data["item"] = book.Title; errs.Add(e); } } if (errs.Count > 0) { prgDlg.Finish("Book transfer finished with errors:"); prgDlg.ShowError(new AggregateException(errs.ToArray())); } else { prgDlg.Close(); SnackBarQueue.Enqueue("Book transfer finished"); } } }); return(Unit.Default); }