Skip to content

Commit 4fe76c9

Browse files
committed
Fix an issue causing previews to break (PP-2837).
1 parent c8dd47f commit 4fe76c9

File tree

1 file changed

+59
-3
lines changed

1 file changed

+59
-3
lines changed

simplified-viewer-preview/src/main/java/org/librarysimplified/viewer/preview/BookPreviewActivity.kt

Lines changed: 59 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ import org.nypl.simplified.accounts.database.api.AccountType
3838
import org.nypl.simplified.books.api.BookContentProtections
3939
import org.nypl.simplified.books.api.BookDRMInformation
4040
import org.nypl.simplified.books.api.BookFormat
41+
import org.nypl.simplified.books.book_database.api.BookFormats
42+
import org.nypl.simplified.books.book_database.api.BookFormats.BookFormatDefinition.BOOK_FORMAT_AUDIO
43+
import org.nypl.simplified.books.book_database.api.BookFormats.BookFormatDefinition.BOOK_FORMAT_EPUB
44+
import org.nypl.simplified.books.book_database.api.BookFormats.BookFormatDefinition.BOOK_FORMAT_PDF
4145
import org.nypl.simplified.books.book_registry.BookPreviewRegistryType
4246
import org.nypl.simplified.books.book_registry.BookPreviewStatus
4347
import org.nypl.simplified.books.controller.api.BooksPreviewControllerType
@@ -162,11 +166,53 @@ class BookPreviewActivity : AppCompatActivity(R.layout.activity_book_preview) {
162166
return
163167
}
164168

169+
/*
170+
* Synthesize a `BookFormat` value for the preview. At the time of writing, we're
171+
* very lax about exact MIME type values, and we assume no DRM. All of our current
172+
* preview data seems to be fine with this.
173+
*/
174+
165175
try {
176+
val formatDefinition =
177+
BookFormats.inferFormat(this.feedEntry.feedEntry)
178+
166179
this.bookFormat =
167-
this.account.bookDatabase.entry(this.feedEntry.bookID)
168-
.book
169-
.findPreferredFormat()!!
180+
when (formatDefinition) {
181+
BOOK_FORMAT_EPUB -> {
182+
BookFormat.BookFormatEPUB(
183+
drmInformation = BookDRMInformation.None,
184+
file = null,
185+
lastReadLocation = null,
186+
bookmarks = listOf(),
187+
contentType = BookFormats.epubMimeTypes().first()
188+
)
189+
}
190+
191+
BOOK_FORMAT_AUDIO -> {
192+
BookFormat.BookFormatAudioBook(
193+
drmInformation = BookDRMInformation.None,
194+
file = null,
195+
lastReadLocation = null,
196+
bookmarks = listOf(),
197+
manifest = null,
198+
contentType = BookFormats.audioBookGenericMimeTypes().first()
199+
)
200+
}
201+
202+
BOOK_FORMAT_PDF -> {
203+
BookFormat.BookFormatPDF(
204+
drmInformation = BookDRMInformation.None,
205+
file = null,
206+
lastReadLocation = null,
207+
bookmarks = listOf(),
208+
contentType = BookFormats.pdfMimeTypes().first()
209+
)
210+
}
211+
212+
null -> {
213+
throw IllegalStateException("No recognized preview format.")
214+
}
215+
}
170216
} catch (e: Throwable) {
171217
this.logger.debug("Unable to locate book format: ", e)
172218
this.finish()
@@ -222,17 +268,21 @@ class BookPreviewActivity : AppCompatActivity(R.layout.activity_book_preview) {
222268
is SR2ReaderFragment -> {
223269
super.onBackPressed()
224270
}
271+
225272
is SR2SearchFragment -> {
226273
this.switchFragment(SR2ReaderFragment())
227274
}
275+
228276
is SR2TOCFragment -> {
229277
this.switchFragment(SR2ReaderFragment())
230278
}
231279
}
232280
}
281+
233282
null -> {
234283
super.onBackPressed()
235284
}
285+
236286
else -> {
237287
super.onBackPressed()
238288
}
@@ -247,9 +297,11 @@ class BookPreviewActivity : AppCompatActivity(R.layout.activity_book_preview) {
247297
is SR2BookLoadingFailed -> {
248298
this.onBookLoadingFailed(event.exception)
249299
}
300+
250301
is SR2ControllerBecameAvailable -> {
251302
this.onControllerBecameAvailable()
252303
}
304+
253305
is SR2ControllerBecameUnavailable -> {
254306
// Nothing to do
255307
}
@@ -268,15 +320,19 @@ class BookPreviewActivity : AppCompatActivity(R.layout.activity_book_preview) {
268320
SR2ReaderViewCommand.SR2ReaderViewNavigationReaderClose -> {
269321
this.finish()
270322
}
323+
271324
SR2ReaderViewCommand.SR2ReaderViewNavigationSearchClose -> {
272325
this.switchFragment(SR2ReaderFragment())
273326
}
327+
274328
SR2ReaderViewCommand.SR2ReaderViewNavigationSearchOpen -> {
275329
this.switchFragment(SR2SearchFragment())
276330
}
331+
277332
SR2ReaderViewCommand.SR2ReaderViewNavigationTOCClose -> {
278333
this.switchFragment(SR2ReaderFragment())
279334
}
335+
280336
SR2ReaderViewCommand.SR2ReaderViewNavigationTOCOpen -> {
281337
this.switchFragment(SR2TOCFragment())
282338
}

0 commit comments

Comments
 (0)