Skip to content

Commit bf1d9bd

Browse files
Ensure "Title Missing" shows for appropriate books (BL-16027)
1 parent 7392301 commit bf1d9bd

6 files changed

Lines changed: 92 additions & 5 deletions

File tree

src/BloomExe/Book/BookInfo.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,8 @@ public string Copyright
341341
/// </summary>
342342
public string QuickTitleUserDisplay => FolderName;
343343

344+
public string ThumbnailLabel;
345+
344346
public bool TryGetPremadeThumbnail(out Image image)
345347
{
346348
string path = Path.Combine(FolderPath, "thumbnail.png");

src/BloomExe/Collection/BookCollection.cs

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,11 @@ public enum CollectionType
2424
SourceCollection,
2525
}
2626

27-
public delegate BookCollection Factory(string path, CollectionType collectionType); //autofac uses this
27+
public delegate BookCollection Factory(
28+
string path,
29+
CollectionType collectionType,
30+
CollectionSettings settings = null
31+
); //autofac uses this
2832

2933
public EventHandler CollectionChanged;
3034

@@ -37,6 +41,8 @@ public enum CollectionType
3741
private static HashSet<string> _changingFolders = new HashSet<string>();
3842
private BloomWebSocketServer _webSocketServer;
3943

44+
private CollectionSettings _collectionSettings;
45+
4046
public static event EventHandler CollectionCreated;
4147

4248
//for moq only
@@ -53,13 +59,15 @@ public BookCollection(
5359
CollectionType collectionType,
5460
BookSelection bookSelection,
5561
TeamCollectionManager tcm = null,
62+
CollectionSettings collectionSettings = null,
5663
BloomWebSocketServer webSocketServer = null
5764
)
5865
{
5966
_path = path;
6067
_bookSelection = bookSelection;
6168
_tcManager = tcm;
6269
_webSocketServer = webSocketServer;
70+
_collectionSettings = collectionSettings;
6371

6472
Type = collectionType;
6573

@@ -412,7 +420,45 @@ private void AddBookInfo(string folderPath)
412420
)
413421
? _bookSelection.CurrentSelection.BookInfo
414422
: new BookInfo(folderPath, editable, sc);
415-
423+
if (bookInfo.FileNameLocked)
424+
{
425+
// The user has explicitly chosen a name to use for the book, distinct from its titles.
426+
bookInfo.ThumbnailLabel = Path.GetFileName(folderPath);
427+
}
428+
else
429+
{
430+
bookInfo.ThumbnailLabel = bookInfo.Title;
431+
}
432+
if (
433+
bookInfo.IsInEditableCollection
434+
&& !bookInfo.FileNameLocked
435+
&& String.IsNullOrEmpty(bookInfo.Title)
436+
)
437+
{
438+
if (
439+
folderPath == _bookSelection.CurrentSelection?.FolderPath
440+
&& _bookSelection.CurrentSelection.BookInfo.SaveContext == sc
441+
)
442+
{
443+
bookInfo.ThumbnailLabel = _bookSelection
444+
.CurrentSelection
445+
.TitleBestForUserDisplay;
446+
}
447+
else
448+
{
449+
var htmlPath = BookStorage.FindBookHtmlInFolder(folderPath);
450+
if (!String.IsNullOrEmpty(htmlPath) && RobustFile.Exists(htmlPath))
451+
{
452+
var dom = HtmlDom.CreateFromHtmlFile(htmlPath);
453+
var bookData = new BookData(dom, _collectionSettings, null);
454+
bookInfo.ThumbnailLabel = Book.Book.GetBestTitleForDisplay(
455+
bookData.GetMultiTextVariableOrEmpty("bookTitle"),
456+
bookData.GetBasicBookLanguageCodes().ToList(),
457+
bookInfo.IsInEditableCollection
458+
);
459+
}
460+
}
461+
}
416462
_bookInfos.Add(bookInfo);
417463
}
418464
catch (Exception e)

src/BloomExe/CollectionTab/CollectionModel.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,8 @@ private IEnumerable<BookCollection> GetBookCollectionsOnce()
360360
{
361361
editableCollection = _bookCollectionFactory(
362362
_pathToCollection,
363-
BookCollection.CollectionType.TheOneEditableCollection
363+
BookCollection.CollectionType.TheOneEditableCollection,
364+
_collectionSettings
364365
);
365366
if (_bookCollectionHolder != null)
366367
_bookCollectionHolder.TheOneEditableCollection = editableCollection;

src/BloomExe/CollectionTab/CollectionTabView.cs

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Collections.Generic;
23
using System.Diagnostics;
34
using System.Drawing;
45
using System.IO;
@@ -13,6 +14,7 @@
1314
using Bloom.web;
1415
using Bloom.Workspace;
1516
using L10NSharp;
17+
using Newtonsoft.Json;
1618
using SIL.Reporting;
1719

1820
namespace Bloom.CollectionTab
@@ -132,8 +134,36 @@ public BookInfo GetBookInfoByFolderPath(string path)
132134

133135
private void UpdateForBookChanges(Book.Book book)
134136
{
137+
if (book.BookData.GetVariableOrNull("bookTitle", book.Language1Tag) == null)
138+
{
139+
book.BookInfo.Title = "";
140+
if (book.BookInfo.AllTitles != null)
141+
{
142+
var titleDict = JsonConvert.DeserializeObject<Dictionary<string, string>>(
143+
book.BookInfo.AllTitles
144+
);
145+
if (titleDict.ContainsKey(book.Language1Tag))
146+
{
147+
titleDict.Remove(book.Language1Tag);
148+
book.BookInfo.AllTitles = JsonConvert.SerializeObject(titleDict);
149+
}
150+
}
151+
book.BookInfo.ThumbnailLabel = book.TitleBestForUserDisplay;
152+
book.BookInfo.Save();
153+
}
135154
_model.UpdateThumbnailAsync(book);
136-
_model.UpdateLabelOfBookInEditableCollection(book);
155+
// Delay the label update slightly to give the collection book pane
156+
// time to settle down and be ready for it. Do it a few times to
157+
// make sure it gets through.
158+
_ = Task.Run(async () =>
159+
{
160+
await Task.Delay(200);
161+
_model.UpdateLabelOfBookInEditableCollection(book);
162+
await Task.Delay(300);
163+
_model.UpdateLabelOfBookInEditableCollection(book);
164+
await Task.Delay(400);
165+
_model.UpdateLabelOfBookInEditableCollection(book);
166+
});
137167
// This message causes the preview to update.
138168
_webSocketServer.SendEvent("bookContent", "reload");
139169
_bookChangesPending = false;

src/BloomExe/web/controllers/CollectionApi.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -618,6 +618,13 @@ public void HandleBooksRequest(ApiRequest request)
618618
ignoreFolderName: true
619619
);
620620
}
621+
else if (
622+
String.IsNullOrEmpty(info.Title)
623+
&& !String.IsNullOrEmpty(info.ThumbnailLabel)
624+
)
625+
{
626+
title = info.ThumbnailLabel;
627+
}
621628
return new
622629
{
623630
id = info.Id,

src/BloomTests/TestDoubles/CollectionTab/FakeCollectionModel.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@ protected static SourceCollectionsList GetDefaultSourceCollectionsList()
5555

5656
public static BookCollection BookCollectionFactory(
5757
string path,
58-
BookCollection.CollectionType collectionType
58+
BookCollection.CollectionType collectionType,
59+
CollectionSettings settings = null
5960
)
6061
{
6162
return new BookCollection(path, collectionType, null);

0 commit comments

Comments
 (0)