From 9bdcaa5eaaf08783edf53224ae2dc23009490bef Mon Sep 17 00:00:00 2001 From: Robert McRackan Date: Fri, 13 May 2022 16:30:46 -0400 Subject: [PATCH] only call notifyPropertyChanged if actually set to new value --- .../grid/AsyncNotifyPropertyChanged.cs | 2 + Source/LibationWinForms/grid/GridEntry.cs | 48 ++++++++++++++----- 2 files changed, 39 insertions(+), 11 deletions(-) diff --git a/Source/LibationWinForms/grid/AsyncNotifyPropertyChanged.cs b/Source/LibationWinForms/grid/AsyncNotifyPropertyChanged.cs index 9cc1bd39..87d3a52c 100644 --- a/Source/LibationWinForms/grid/AsyncNotifyPropertyChanged.cs +++ b/Source/LibationWinForms/grid/AsyncNotifyPropertyChanged.cs @@ -8,6 +8,8 @@ namespace LibationWinForms { public event PropertyChangedEventHandler PropertyChanged; + // per standard INotifyPropertyChanged pattern: + // https://docs.microsoft.com/en-us/dotnet/desktop/wpf/data/how-to-implement-property-change-notification public void NotifyPropertyChanged([CallerMemberName] string propertyName = "") => this.UIThreadAsync(() => PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName))); } diff --git a/Source/LibationWinForms/grid/GridEntry.cs b/Source/LibationWinForms/grid/GridEntry.cs index 0d20ef1f..a1aa999f 100644 --- a/Source/LibationWinForms/grid/GridEntry.cs +++ b/Source/LibationWinForms/grid/GridEntry.cs @@ -41,8 +41,11 @@ namespace LibationWinForms get => _cover; private set { - _cover = value; - NotifyPropertyChanged(); + if (_cover != value) + { + _cover = value; + NotifyPropertyChanged(); + } } } @@ -172,16 +175,25 @@ namespace LibationWinForms switch (itemName) { case nameof(udi.Tags): - Book.UserDefinedItem.Tags = udi.Tags; - NotifyPropertyChanged(nameof(DisplayTags)); + if (Book.UserDefinedItem.Tags != udi.Tags) + { + Book.UserDefinedItem.Tags = udi.Tags; + NotifyPropertyChanged(nameof(DisplayTags)); + } break; case nameof(udi.BookStatus): - Book.UserDefinedItem.BookStatus = udi.BookStatus; - NotifyPropertyChanged(nameof(Liberate)); + if (Book.UserDefinedItem.BookStatus != udi.BookStatus) + { + Book.UserDefinedItem.BookStatus = udi.BookStatus; + NotifyPropertyChanged(nameof(Liberate)); + } break; case nameof(udi.PdfStatus): - Book.UserDefinedItem.PdfStatus = udi.PdfStatus; - NotifyPropertyChanged(nameof(Liberate)); + if (Book.UserDefinedItem.PdfStatus != udi.PdfStatus) + { + Book.UserDefinedItem.PdfStatus = udi.PdfStatus; + NotifyPropertyChanged(nameof(Liberate)); + } break; } } @@ -211,9 +223,23 @@ namespace LibationWinForms private void UpdateLiberatedStatus(bool notify = true) { - _bookStatus = LibraryCommands.Liberated_Status(LibraryBook.Book); - _pdfStatus = LibraryCommands.Pdf_Status(LibraryBook.Book); - if (notify) + var changed = false; + + var newBookStatus = LibraryCommands.Liberated_Status(LibraryBook.Book); + if (_bookStatus != newBookStatus) + { + _bookStatus = newBookStatus; + changed = true; + } + + var newPdfStatus = LibraryCommands.Pdf_Status(LibraryBook.Book); + if (_pdfStatus != newPdfStatus) + { + _pdfStatus = newPdfStatus; + changed = true; + } + + if (changed && notify) NotifyPropertyChanged(nameof(Liberate)); }