Ejemplo n.º 1
0
        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);
        }
Ejemplo n.º 2
0
        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");
                }
            });
        }