diff --git a/src/BloomExe/Collection/BookCollection.cs b/src/BloomExe/Collection/BookCollection.cs index 11e707bc44df..8a6cea6172cb 100644 --- a/src/BloomExe/Collection/BookCollection.cs +++ b/src/BloomExe/Collection/BookCollection.cs @@ -306,12 +306,19 @@ public void UpdateBookInfo(BookInfo info) _bookInfos.Insert(newIndex, info); NotifyCollectionChanged(); } - - public void AddBookInfo(BookInfo bookInfo) - { - _bookInfos.Add(bookInfo); - NotifyCollectionChanged(); - } + private void AddBookInfoInternalNoSideEffects(BookInfo bookInfo) + { + // Note BL-12890 it's actually the norm, when we add a book, that the new book will already be there + // by the time we officially add it. + if (_bookInfos.FindIndex(i => i.Id == bookInfo.Id) < 0) + _bookInfos.Add(bookInfo); + } + + public void AddBookInfo(BookInfo bookInfo) + { + AddBookInfoInternalNoSideEffects(bookInfo); + NotifyCollectionChanged(); + } /// /// Insert a book into the appropriate place. If there is already a book with the same FolderPath, replace it. @@ -331,13 +338,13 @@ public void InsertBookInfo(BookInfo bookInfo) if (compare > 0) { _bookInfos.Insert(i, bookInfo); - return; - } - } - _bookInfos.Add(bookInfo); - } + return; + } + } + AddBookInfoInternalNoSideEffects(bookInfo); + } - private bool BackupFileExists(string folderPath) + private bool BackupFileExists(string folderPath) { var bakFiles = Directory.GetFiles(folderPath, BookStorage.BackupFilename); return bakFiles.Length == 1 && RobustFile.Exists(bakFiles[0]); diff --git a/src/BloomExe/CollectionTab/CollectionModel.cs b/src/BloomExe/CollectionTab/CollectionModel.cs index d4d1ab1a085f..dbdb5f54ecc4 100644 --- a/src/BloomExe/CollectionTab/CollectionModel.cs +++ b/src/BloomExe/CollectionTab/CollectionModel.cs @@ -776,12 +776,15 @@ private void CreateFromSourceBook(Book.Book sourceBook) // (We can't fix this by reordering things, because there will also be problems if we attempt // to select an item before it is present in the collection to select.) // We know this is a new book object, so there's no point in using EnsureUpToDate. - // When we later select it, that code uses EnsureUpToDate and will not do it again. - newBook.BringBookUpToDate(new NullProgress(), false); + // When we later select it, that code uses EnsureUpToDate and will not do it again. + newBook.BringBookUpToDate(new NullProgress(), false); - TheOneEditableCollection.AddBookInfo(newBook.BookInfo); + // You'd think this was needed, but it's not. The book is already in the collection due to + // the BringBookUpToDate side effects. However, I'm too chicken to rely on that, so instead + // I made AddBookInfo() avoid adding a duplicate (BL-12890) + TheOneEditableCollection.AddBookInfo(newBook.BookInfo); - if (_bookSelection != null) + if (_bookSelection != null) { Book.Book.SourceToReportForNextRename = Path.GetFileName(sourceBook.FolderPath); _bookSelection.SelectBook(newBook, aboutToEdit: true);