Skip to content

Commit 540c80f

Browse files
committed
2.2.0 (#109)
1 parent fefb3cb commit 540c80f

File tree

7 files changed

+40
-21
lines changed

7 files changed

+40
-21
lines changed

.idea/kotlinScripting.xml

+6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

CHANGELOG.md

+7-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ All notable changes to this project will be documented in this file. Take a look
44

55
**Warning:** Features marked as *experimental* may change or be removed in a future release without notice. Use with caution.
66

7-
## [Unreleased]
7+
<!--## [Unreleased]-->
8+
9+
## [2.2.0]
810

911
### Added
1012

@@ -525,5 +527,7 @@ server.loadCustomResource(assets.open("scripts/highlight.js"), "highlight.js", I
525527

526528

527529
[unreleased]: https://github.com/readium/kotlin-toolkit/compare/main...HEAD
528-
[2.1.0]: https://github.com/readium/kotlin-kotlin/compare/2.0.0...2.1.0
529-
[2.1.1]: https://github.com/readium/kotlin-kotlin/compare/2.1.0...2.1.1
530+
[2.1.0]: https://github.com/readium/kotlin-toolkit/compare/2.0.0...2.1.0
531+
[2.1.1]: https://github.com/readium/kotlin-toolkit/compare/2.1.0...2.1.1
532+
[2.2.0]: https://github.com/readium/kotlin-toolkit/compare/2.1.1...2.2.0
533+

README.md

+1-9
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ Make sure that you have the `$readium_version` property set in your root `build.
2424

2525
```gradle
2626
buildscript {
27-
ext.readium_version = '2.1.1'
27+
ext.readium_version = '2.2.0'
2828
}
2929
3030
allprojects {
@@ -56,14 +56,6 @@ First, add the repository as a Git submodule of your app repository, then checko
5656
git submodule add https://github.com/readium/kotlin-toolkit.git
5757
```
5858

59-
Next, declare the Kotlin version used in your root `build.gradle`.
60-
61-
```gradle
62-
buildscript {
63-
ext.kotlin_version = '1.5.31'
64-
}
65-
```
66-
6759
Then, add the following to your project's `settings.gradle` file, altering the paths if needed. Keep only the modules you want to use.
6860

6961
```gradle

test-app/build.gradle.kts

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ android {
2020

2121
applicationId = "org.readium.r2reader"
2222

23-
versionName = "2.1.1"
23+
versionName = "2.2.0"
2424

2525
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
2626
ndk.abiFilters.add("armeabi-v7a")

test-app/src/main/java/org/readium/r2/testapp/bookshelf/BookshelfFragment.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ class BookshelfFragment : Fragment() {
5454
bookshelfViewModel.channel.receive(viewLifecycleOwner) { handleEvent(it) }
5555

5656
bookshelfAdapter = BookshelfAdapter(
57-
onBookClick = { book -> book.id?.let { bookshelfViewModel.openBook(it) } },
57+
onBookClick = { book -> book.id?.let { bookshelfViewModel.openBook(it, requireActivity()) } },
5858
onBookLongClick = { book -> confirmDeleteBook(book) })
5959

6060
documentPickerLauncher =

test-app/src/main/java/org/readium/r2/testapp/bookshelf/BookshelfViewModel.kt

+9-3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
package org.readium.r2.testapp.bookshelf
88

9+
import android.app.Activity
910
import android.app.Application
1011
import android.content.Context
1112
import android.graphics.Bitmap
@@ -31,6 +32,7 @@ import org.readium.r2.streamer.Streamer
3132
import org.readium.r2.testapp.BuildConfig
3233
import org.readium.r2.testapp.domain.model.Book
3334
import org.readium.r2.testapp.reader.ReaderActivityContract
35+
import org.readium.r2.testapp.reader.ReaderRepository
3436
import org.readium.r2.testapp.utils.EventChannel
3537
import org.readium.r2.testapp.utils.extensions.copyToTempFile
3638
import org.readium.r2.testapp.utils.extensions.moveTo
@@ -151,7 +153,7 @@ class BookshelfViewModel(application: Application) : AndroidViewModel(applicatio
151153
return
152154
}
153155

154-
streamer.open(libraryAsset, allowUserInteraction = false, sender = r2Application)
156+
streamer.open(libraryAsset, allowUserInteraction = false)
155157
.onSuccess {
156158
addPublicationToDatabase(libraryAsset.file.path, libraryAsset.mediaType(), it).let { id ->
157159

@@ -171,11 +173,15 @@ class BookshelfViewModel(application: Application) : AndroidViewModel(applicatio
171173
@OptIn(ExperimentalTime::class)
172174
fun openBook(
173175
bookId: Long,
176+
activity: Activity
174177
) = viewModelScope.launch {
175178
val readerRepository = r2Application.readerRepository.await()
176-
readerRepository.open(bookId)
179+
readerRepository.open(bookId, activity)
177180
.onFailure { exception ->
178-
val message = when (exception) {
181+
if (exception is ReaderRepository.CancellationException)
182+
return@launch
183+
184+
val message = when (exception) {
179185
is UserException -> exception.getUserMessage(r2Application)
180186
else -> exception.message
181187
}

test-app/src/main/java/org/readium/r2/testapp/reader/ReaderRepository.kt

+15-4
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
package org.readium.r2.testapp.reader
88

9+
import android.app.Activity
910
import android.app.Application
1011
import kotlinx.coroutines.ExperimentalCoroutinesApi
1112
import org.json.JSONObject
@@ -15,6 +16,8 @@ import org.readium.r2.shared.Injectable
1516
import org.readium.r2.shared.publication.Locator
1617
import org.readium.r2.shared.publication.Publication
1718
import org.readium.r2.shared.publication.asset.FileAsset
19+
import org.readium.r2.shared.publication.services.isRestricted
20+
import org.readium.r2.shared.publication.services.protectionError
1821
import org.readium.r2.shared.util.Try
1922
import org.readium.r2.shared.util.getOrElse
2023
import org.readium.r2.streamer.Streamer
@@ -39,22 +42,24 @@ class ReaderRepository(
3942
private val mediaBinder: MediaService.Binder,
4043
private val bookRepository: BookRepository
4144
) {
45+
object CancellationException : Exception()
46+
4247
private val repository: MutableMap<Long, ReaderInitData> =
4348
mutableMapOf()
4449

4550
operator fun get(bookId: Long): ReaderInitData? =
4651
repository[bookId]
4752

48-
suspend fun open(bookId: Long): Try<Unit, Exception> {
53+
suspend fun open(bookId: Long, activity: Activity): Try<Unit, Exception> {
4954
return try {
50-
openThrowing(bookId)
55+
openThrowing(bookId, activity)
5156
Try.success(Unit)
5257
} catch (e: Exception) {
5358
Try.failure(e)
5459
}
5560
}
5661

57-
private suspend fun openThrowing(bookId: Long) {
62+
private suspend fun openThrowing(bookId: Long, activity: Activity) {
5863
if (bookId in repository.keys) {
5964
return
6065
}
@@ -66,9 +71,15 @@ class ReaderRepository(
6671
require(file.exists())
6772
val asset = FileAsset(file)
6873

69-
val publication = streamer.open(asset, allowUserInteraction = true, sender = application)
74+
val publication = streamer.open(asset, allowUserInteraction = true, sender = activity)
7075
.getOrThrow()
7176

77+
// The publication is protected with a DRM and not unlocked.
78+
if (publication.isRestricted) {
79+
throw publication.protectionError
80+
?: CancellationException
81+
}
82+
7283
val initialLocator = book.progression?.let { Locator.fromJSON(JSONObject(it)) }
7384

7485
val readerInitData = when {

0 commit comments

Comments
 (0)