Skip to content

Commit 1570e80

Browse files
mickael-menuqnga
andauthored
Replace the Fetcher by Container and refactor media type sniffing (#373)
Co-authored-by: Quentin Gliosca <quentin.gliosca@gmail.com>
1 parent a9e8b0d commit 1570e80

File tree

392 files changed

+9585
-6122
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

392 files changed

+9585
-6122
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,4 +82,5 @@ lint/reports/
8282
docs/readium
8383
docs/index.md
8484
docs/package-list
85-
site/
85+
site/
86+
androidTestResultsUserPreferences.xml

CHANGELOG.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@ All notable changes to this project will be documented in this file. Take a look
2626

2727
* Readium resources are now prefixed with `readium_`. Take care of updating any overridden resource by following [the migration guide](docs/migration-guide.md#300).
2828

29+
#### Shared
30+
31+
* `Publication.localizedTitle` is nullable, as we cannot guarantee that all publication sources offer a title.
32+
* The `MediaType` sniffing helpers are deprecated in favor of `MediaTypeRetriever` (for media type and file extension hints and raw content) and `AssetRetriever` (for URLs).
33+
2934
#### Navigator
3035

3136
* `EpubNavigatorFragment.firstVisibleElementLocator()` now returns the first *block* element that is visible on the screen, even if it starts on previous pages.
@@ -39,6 +44,10 @@ All notable changes to this project will be documented in this file. Take a look
3944

4045
* All the navigator `Activity` are deprecated in favor of the `Fragment` variants.
4146

47+
#### Streamer
48+
49+
* The `Fetcher` interface was deprecated in favor of the `Container` one in `readium-shared`.
50+
4251
### Fixed
4352

4453
#### Navigator

readium/adapters/pdfium/pdfium-document/src/main/java/org/readium/adapters/pdfium/document/PdfiumDocument.kt

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,10 @@ import org.readium.r2.shared.InternalReadiumApi
1919
import org.readium.r2.shared.error.getOrThrow
2020
import org.readium.r2.shared.extensions.md5
2121
import org.readium.r2.shared.extensions.tryOrNull
22-
import org.readium.r2.shared.fetcher.Fetcher
22+
import org.readium.r2.shared.resource.Resource
2323
import org.readium.r2.shared.util.pdf.PdfDocument
2424
import org.readium.r2.shared.util.pdf.PdfDocumentFactory
25+
import org.readium.r2.shared.util.toFile
2526
import org.readium.r2.shared.util.use
2627
import timber.log.Timber
2728

@@ -87,26 +88,29 @@ public class PdfiumDocumentFactory(context: Context) : PdfDocumentFactory<Pdfium
8788
override suspend fun open(file: File, password: String?): PdfiumDocument =
8889
core.fromFile(file, password)
8990

90-
override suspend fun open(resource: Fetcher.Resource, password: String?): PdfiumDocument {
91+
override suspend fun open(resource: Resource, password: String?): PdfiumDocument {
9192
// First try to open the resource as a file on the FS for performance improvement, as
9293
// PDFium requires the whole PDF document to be loaded in memory when using raw bytes.
9394
return resource.openAsFile(password)
9495
?: resource.openBytes(password)
9596
}
9697

97-
private suspend fun Fetcher.Resource.openAsFile(password: String?): PdfiumDocument? =
98-
file?.let {
99-
tryOrNull { open(it, password) }
98+
private suspend fun Resource.openAsFile(password: String?): PdfiumDocument? =
99+
tryOrNull {
100+
source?.toFile()?.let { open(it, password) }
100101
}
101102

102-
private suspend fun Fetcher.Resource.openBytes(password: String?): PdfiumDocument =
103+
private suspend fun Resource.openBytes(password: String?): PdfiumDocument =
103104
use {
104105
core.fromBytes(read().getOrThrow(), password)
105106
}
106107

107108
private fun PdfiumCore.fromFile(file: File, password: String?): PdfiumDocument =
108109
fromDocument(
109-
newDocument(ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_ONLY), password),
110+
newDocument(
111+
ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_ONLY),
112+
password
113+
),
110114
identifier = file.md5()
111115
)
112116

readium/adapters/pdfium/pdfium-navigator/src/main/java/org/readium/adapters/pdfium/navigator/PdfiumDefaults.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,5 @@ import org.readium.r2.shared.ExperimentalReadiumApi
1919
@ExperimentalReadiumApi
2020
public data class PdfiumDefaults(
2121
val pageSpacing: Double? = null,
22-
val readingProgression: ReadingProgression? = null,
22+
val readingProgression: ReadingProgression? = null
2323
)

readium/adapters/pdfium/pdfium-navigator/src/main/java/org/readium/adapters/pdfium/navigator/PdfiumDocumentFragment.kt

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,6 @@ public class PdfiumDocumentFragment internal constructor(
7979
val context = context?.applicationContext ?: return
8080

8181
viewLifecycleOwner.lifecycleScope.launch {
82-
8382
try {
8483
val document = PdfiumDocumentFactory(context)
8584
// PDFium crashes when reusing the same PdfDocument, so we must not cache it.
@@ -141,8 +140,11 @@ public class PdfiumDocumentFragment internal constructor(
141140
override val pageIndex: Int get() = viewPageIndex ?: initialPageIndex
142141

143142
private val viewPageIndex: Int? get() =
144-
if (pdfView.isRecycled) null
145-
else convertPageIndexFromView(pdfView.currentPage)
143+
if (pdfView.isRecycled) {
144+
null
145+
} else {
146+
convertPageIndexFromView(pdfView.currentPage)
147+
}
146148

147149
override fun goToPageIndex(index: Int, animated: Boolean): Boolean {
148150
if (!isValidPageIndex(index)) {

readium/adapters/pdfium/pdfium-navigator/src/main/java/org/readium/adapters/pdfium/navigator/PdfiumPreferences.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public data class PdfiumPreferences(
2727
val fit: Fit? = null,
2828
val pageSpacing: Double? = null,
2929
val readingProgression: ReadingProgression? = null,
30-
val scrollAxis: Axis? = null,
30+
val scrollAxis: Axis? = null
3131
) : Configurable.Preferences<PdfiumPreferences> {
3232

3333
init {
@@ -40,6 +40,6 @@ public data class PdfiumPreferences(
4040
fit = other.fit ?: fit,
4141
pageSpacing = other.pageSpacing ?: pageSpacing,
4242
readingProgression = other.readingProgression ?: readingProgression,
43-
scrollAxis = other.scrollAxis ?: scrollAxis,
43+
scrollAxis = other.scrollAxis ?: scrollAxis
4444
)
4545
}

readium/adapters/pdfium/pdfium-navigator/src/main/java/org/readium/adapters/pdfium/navigator/PdfiumPreferencesEditor.kt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import org.readium.r2.shared.publication.Metadata
2222
public class PdfiumPreferencesEditor internal constructor(
2323
initialPreferences: PdfiumPreferences,
2424
publicationMetadata: Metadata,
25-
defaults: PdfiumDefaults,
25+
defaults: PdfiumDefaults
2626
) : PreferencesEditor<PdfiumPreferences> {
2727

2828
private data class State(
@@ -55,7 +55,7 @@ public class PdfiumPreferencesEditor internal constructor(
5555
getEffectiveValue = { state.settings.fit },
5656
getIsEffective = { true },
5757
updateValue = { value -> updateValues { it.copy(fit = value) } },
58-
supportedValues = listOf(Fit.CONTAIN, Fit.WIDTH),
58+
supportedValues = listOf(Fit.CONTAIN, Fit.WIDTH)
5959
)
6060

6161
/**
@@ -69,7 +69,7 @@ public class PdfiumPreferencesEditor internal constructor(
6969
updateValue = { value -> updateValues { it.copy(pageSpacing = value) } },
7070
supportedRange = 0.0..50.0,
7171
progressionStrategy = DoubleIncrement(5.0),
72-
valueFormatter = { "${it.format(1)} dp" },
72+
valueFormatter = { "${it.format(1)} dp" }
7373
)
7474

7575
/**
@@ -81,7 +81,7 @@ public class PdfiumPreferencesEditor internal constructor(
8181
getEffectiveValue = { state.settings.readingProgression },
8282
getIsEffective = { true },
8383
updateValue = { value -> updateValues { it.copy(readingProgression = value) } },
84-
supportedValues = listOf(ReadingProgression.LTR, ReadingProgression.RTL),
84+
supportedValues = listOf(ReadingProgression.LTR, ReadingProgression.RTL)
8585
)
8686

8787
/**
@@ -93,7 +93,7 @@ public class PdfiumPreferencesEditor internal constructor(
9393
getEffectiveValue = { state.settings.scrollAxis },
9494
getIsEffective = { true },
9595
updateValue = { value -> updateValues { it.copy(scrollAxis = value) } },
96-
supportedValues = listOf(Axis.VERTICAL, Axis.HORIZONTAL),
96+
supportedValues = listOf(Axis.VERTICAL, Axis.HORIZONTAL)
9797
)
9898

9999
private fun updateValues(updater: (PdfiumPreferences) -> PdfiumPreferences) {

readium/adapters/pdfium/pdfium-navigator/src/main/java/org/readium/adapters/pdfium/navigator/PdfiumPreferencesFilters.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public object PdfiumSharedPreferencesFilter : PreferencesFilter<PdfiumPreference
1717

1818
override fun filter(preferences: PdfiumPreferences): PdfiumPreferences =
1919
preferences.copy(
20-
readingProgression = null,
20+
readingProgression = null
2121
)
2222
}
2323

@@ -29,6 +29,6 @@ public object PdfiumPublicationPreferencesFilter : PreferencesFilter<PdfiumPrefe
2929

3030
override fun filter(preferences: PdfiumPreferences): PdfiumPreferences =
3131
PdfiumPreferences(
32-
readingProgression = preferences.readingProgression,
32+
readingProgression = preferences.readingProgression
3333
)
3434
}

readium/adapters/pdfium/pdfium-navigator/src/main/java/org/readium/adapters/pdfium/navigator/PdfiumSettings.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,5 @@ public data class PdfiumSettings(
1919
val fit: Fit,
2020
val pageSpacing: Double,
2121
val readingProgression: ReadingProgression,
22-
val scrollAxis: Axis,
22+
val scrollAxis: Axis
2323
) : Configurable.Settings

readium/adapters/pdfium/pdfium-navigator/src/main/java/org/readium/adapters/pdfium/navigator/PdfiumSettingsResolver.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ internal class PdfiumSettingsResolver(
4848
fit = fit,
4949
pageSpacing = pageSpacing,
5050
readingProgression = readingProgression,
51-
scrollAxis = scrollAxis,
51+
scrollAxis = scrollAxis
5252
)
5353
}
5454
}

0 commit comments

Comments
 (0)