From 6beb5cc74a67afaec297ac626e248dc40a1416de Mon Sep 17 00:00:00 2001 From: Michael Bucari-Tovo Date: Sun, 22 Aug 2021 13:27:39 -0600 Subject: [PATCH] Made changes discussed. --- ApplicationServices/LibraryCommands.cs | 61 +++----------------------- LibationWinForms/GridEntry.cs | 50 ++++++++++++++++----- LibationWinForms/ProductsGrid.cs | 4 ++ 3 files changed, 49 insertions(+), 66 deletions(-) diff --git a/ApplicationServices/LibraryCommands.cs b/ApplicationServices/LibraryCommands.cs index 1d428237..ed36595e 100644 --- a/ApplicationServices/LibraryCommands.cs +++ b/ApplicationServices/LibraryCommands.cs @@ -154,74 +154,25 @@ namespace ApplicationServices } #endregion - #region Update book details - - public static int UpdateTags(Book libraryBook, string newTags) + #region Update book details + + public static int UpdateUserDefinedItem(Book book) { try { using var context = DbContexts.GetContext(); - var udi = libraryBook.UserDefinedItem; - udi.Tags = newTags; - // Attach() NoTracking entities before SaveChanges() - context.Attach(udi).State = Microsoft.EntityFrameworkCore.EntityState.Modified; + context.Attach(book.UserDefinedItem).State = Microsoft.EntityFrameworkCore.EntityState.Modified; var qtyChanges = context.SaveChanges(); if (qtyChanges > 0) - SearchEngineCommands.UpdateBookTags(libraryBook); + SearchEngineCommands.UpdateLiberatedStatus(book); return qtyChanges; } catch (Exception ex) { - Log.Logger.Error(ex, "Error updating tags"); - throw; - } - } - - public static int UpdateBook(Book libraryBook, LiberatedStatus liberatedStatus) - { - try - { - using var context = DbContexts.GetContext(); - - var udi = libraryBook.UserDefinedItem; - udi.BookStatus = liberatedStatus; - - // Attach() NoTracking entities before SaveChanges() - context.Attach(udi).State = Microsoft.EntityFrameworkCore.EntityState.Modified; - var qtyChanges = context.SaveChanges(); - if (qtyChanges > 0) - SearchEngineCommands.UpdateLiberatedStatus(libraryBook); - - return qtyChanges; - } - catch (Exception ex) - { - Log.Logger.Error(ex, "Error updating audiobook status"); - throw; - } - } - - public static int UpdatePdf(Book libraryBook, LiberatedStatus? liberatedStatus) - { - try - { - using var context = DbContexts.GetContext(); - - var udi = libraryBook.UserDefinedItem; - udi.PdfStatus = liberatedStatus; - - // Attach() NoTracking entities before SaveChanges() - context.Attach(udi).State = Microsoft.EntityFrameworkCore.EntityState.Modified; - var qtyChanges = context.SaveChanges(); - - return qtyChanges; - } - catch (Exception ex) - { - Log.Logger.Error(ex, "Error updating pdf status"); + Log.Logger.Error(ex, $"Error updating {nameof(book.UserDefinedItem)}"); throw; } } diff --git a/LibationWinForms/GridEntry.cs b/LibationWinForms/GridEntry.cs index b2184d6e..3cc8922c 100644 --- a/LibationWinForms/GridEntry.cs +++ b/LibationWinForms/GridEntry.cs @@ -29,12 +29,12 @@ namespace LibationWinForms private Book Book => LibraryBook.Book; private Image _cover; - private Action _refilter; + private Action Refilter { get; } public GridEntry(LibraryBook libraryBook, Action refilterOnChanged = null) { LibraryBook = libraryBook; - _refilter = refilterOnChanged; + Refilter = refilterOnChanged; _memberValues = CreateMemberValueDictionary(); @@ -76,7 +76,7 @@ namespace LibationWinForms } } - #region detect changes to the model and update the view + #region detect changes to the model, update the view, and save to database. /// /// This event handler receives notifications from the model that it has changed. @@ -86,33 +86,62 @@ namespace LibationWinForms { var udi = sender as UserDefinedItem; - if (udi.Book.AudibleProductId != LibraryBook.Book.AudibleProductId) + if (udi.Book.AudibleProductId != Book.AudibleProductId) return; switch (itemName) { case nameof(udi.Tags): { - LibraryCommands.UpdateTags(LibraryBook.Book, udi.Tags); + Book.UserDefinedItem.Tags = udi.Tags; NotifyPropertyChanged(nameof(DisplayTags)); } break; case nameof(udi.BookStatus): { - var status = udi.BookStatus == LiberatedStatus.PartialDownload ? LiberatedStatus.NotLiberated : udi.BookStatus; - LibraryCommands.UpdateBook(LibraryBook.Book, status); + Book.UserDefinedItem.BookStatus = udi.BookStatus; NotifyPropertyChanged(nameof(Liberate)); } break; case nameof(udi.PdfStatus): { - LibraryCommands.UpdatePdf(LibraryBook.Book, udi.PdfStatus); + Book.UserDefinedItem.PdfStatus = udi.PdfStatus; NotifyPropertyChanged(nameof(Liberate)); } break; } - _refilter?.Invoke(); + if (!suspendCommit) + Commit(); + + Refilter?.Invoke(); + } + private bool suspendCommit = false; + + /// + /// Begin editing the model, suspending commits until is called. + /// + public void BeginEdit() => suspendCommit = true; + + /// + /// Save all edits to the database. + /// + public void EndEdit() + { + Commit(); + suspendCommit = false; + } + + private void Commit() + { + //We don't want LiberatedStatus.PartialDownload to be a persistent status. + var bookStatus = Book.UserDefinedItem.BookStatus; + var saveStatus = bookStatus == LiberatedStatus.PartialDownload ? LiberatedStatus.NotLiberated : bookStatus; + Book.UserDefinedItem.BookStatus = saveStatus; + + LibraryCommands.UpdateUserDefinedItem(Book); + + Book.UserDefinedItem.BookStatus = bookStatus; } #endregion @@ -144,7 +173,7 @@ namespace LibationWinForms public string Description { get; } public string DisplayTags { - get=> Book.UserDefinedItem.Tags; + get => string.Join("\r\n", Book.UserDefinedItem.TagsEnumerated); set => Book.UserDefinedItem.Tags = value; } public (LiberatedStatus BookStatus, LiberatedStatus? PdfStatus) Liberate @@ -267,5 +296,4 @@ namespace LibationWinForms FileManager.PictureStorage.PictureCached -= PictureStorage_PictureCached; } } - } diff --git a/LibationWinForms/ProductsGrid.cs b/LibationWinForms/ProductsGrid.cs index 043c0a1e..b6c6b5b0 100644 --- a/LibationWinForms/ProductsGrid.cs +++ b/LibationWinForms/ProductsGrid.cs @@ -95,9 +95,13 @@ namespace LibationWinForms if (bookDetailsForm.ShowDialog() != DialogResult.OK) return; + liveGridEntry.BeginEdit(); + liveGridEntry.DisplayTags = bookDetailsForm.NewTags; liveGridEntry.Liberate = (bookDetailsForm.BookLiberatedStatus, bookDetailsForm.PdfLiberatedStatus); + liveGridEntry.EndEdit(); + BackupCountsChanged?.Invoke(this, EventArgs.Empty); }