예제 #1
0
        public async void _EditMetadata()
        {
            if (SelectedTableRow == null)
            {
                return;
            }

            HashSet <string> authors    = new HashSet <string>();
            HashSet <string> series     = new HashSet <string>();
            HashSet <string> publishers = new HashSet <string>();

            foreach (Database.BookEntry i in CombinedLibrary)
            {
                authors.Add(i.Author);
                series.Add(i.Series);
                publishers.Add(i.Publisher);
            }

            authors.Remove("");
            series.Remove("");
            publishers.Remove("");

            var dlg = new Dialogs.MetadataEditor(new Database.BookEntry(SelectedTableRow), authors, series, publishers);
            await MaterialDesignThemes.Wpf.DialogHost.Show(dlg);

            if (dlg.DialogResult == false)
            {
                return;
            }

            List <Exception> errs = new List <Exception>();

            string title = null;

            if (CombinedLibrary.FirstOrDefault(x => x.IsLocal && x.Id == SelectedTableRow.Id) is Database.BookEntry bookEntry)
            {
                title = bookEntry.Title;
                try
                {
                    App.LocalLibrary.UpdateBookMetadata(dlg.ModBook);
                }
                catch (Exception e)
                {
                    errs.Add(e);
                }
            }

            if (SelectedDevice != null)
            {
                if (CombinedLibrary.FirstOrDefault(x => x.IsRemote && x.Id == SelectedTableRow.Id) is Database.BookEntry deviceBook)
                {
                    if (title == null)
                    {
                        title = deviceBook.Title;
                    }
                    try
                    {
                        SelectedDevice.UpdateBookMetadata(dlg.ModBook);
                    }
                    catch (Exception e)
                    {
                        errs.Add(e);
                    }
                }
            }


            if (errs.Count != 0)
            {
                string msg    = $"Metadata could not be updated. {string.Join("; ", errs.Select(x => x.Message).ToList())}";
                var    errDlg = new Dialogs.Error("Error updating metadata", msg);
                _ = MaterialDesignThemes.Wpf.DialogHost.Show(errDlg);
            }
            else if (title != null)
            {
                SnackBarQueue.Enqueue($"{title} updated");
            }
        }