Skip to content

Commit

Permalink
2.2.0 (#109)
Browse files Browse the repository at this point in the history
  • Loading branch information
mickael-menu committed Apr 22, 2022
1 parent fefb3cb commit 540c80f
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 21 deletions.
6 changes: 6 additions & 0 deletions .idea/kotlinScripting.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 7 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ All notable changes to this project will be documented in this file. Take a look

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

## [Unreleased]
<!--## [Unreleased]-->

## [2.2.0]

### Added

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


[unreleased]: https://github.com/readium/kotlin-toolkit/compare/main...HEAD
[2.1.0]: https://github.com/readium/kotlin-kotlin/compare/2.0.0...2.1.0
[2.1.1]: https://github.com/readium/kotlin-kotlin/compare/2.1.0...2.1.1
[2.1.0]: https://github.com/readium/kotlin-toolkit/compare/2.0.0...2.1.0
[2.1.1]: https://github.com/readium/kotlin-toolkit/compare/2.1.0...2.1.1
[2.2.0]: https://github.com/readium/kotlin-toolkit/compare/2.1.1...2.2.0

10 changes: 1 addition & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ Make sure that you have the `$readium_version` property set in your root `build.

```gradle
buildscript {
ext.readium_version = '2.1.1'
ext.readium_version = '2.2.0'
}
allprojects {
Expand Down Expand Up @@ -56,14 +56,6 @@ First, add the repository as a Git submodule of your app repository, then checko
git submodule add https://github.com/readium/kotlin-toolkit.git
```

Next, declare the Kotlin version used in your root `build.gradle`.

```gradle
buildscript {
ext.kotlin_version = '1.5.31'
}
```

Then, add the following to your project's `settings.gradle` file, altering the paths if needed. Keep only the modules you want to use.

```gradle
Expand Down
2 changes: 1 addition & 1 deletion test-app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ android {

applicationId = "org.readium.r2reader"

versionName = "2.1.1"
versionName = "2.2.0"

testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
ndk.abiFilters.add("armeabi-v7a")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class BookshelfFragment : Fragment() {
bookshelfViewModel.channel.receive(viewLifecycleOwner) { handleEvent(it) }

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

documentPickerLauncher =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

package org.readium.r2.testapp.bookshelf

import android.app.Activity
import android.app.Application
import android.content.Context
import android.graphics.Bitmap
Expand All @@ -31,6 +32,7 @@ import org.readium.r2.streamer.Streamer
import org.readium.r2.testapp.BuildConfig
import org.readium.r2.testapp.domain.model.Book
import org.readium.r2.testapp.reader.ReaderActivityContract
import org.readium.r2.testapp.reader.ReaderRepository
import org.readium.r2.testapp.utils.EventChannel
import org.readium.r2.testapp.utils.extensions.copyToTempFile
import org.readium.r2.testapp.utils.extensions.moveTo
Expand Down Expand Up @@ -151,7 +153,7 @@ class BookshelfViewModel(application: Application) : AndroidViewModel(applicatio
return
}

streamer.open(libraryAsset, allowUserInteraction = false, sender = r2Application)
streamer.open(libraryAsset, allowUserInteraction = false)
.onSuccess {
addPublicationToDatabase(libraryAsset.file.path, libraryAsset.mediaType(), it).let { id ->

Expand All @@ -171,11 +173,15 @@ class BookshelfViewModel(application: Application) : AndroidViewModel(applicatio
@OptIn(ExperimentalTime::class)
fun openBook(
bookId: Long,
activity: Activity
) = viewModelScope.launch {
val readerRepository = r2Application.readerRepository.await()
readerRepository.open(bookId)
readerRepository.open(bookId, activity)
.onFailure { exception ->
val message = when (exception) {
if (exception is ReaderRepository.CancellationException)
return@launch

val message = when (exception) {
is UserException -> exception.getUserMessage(r2Application)
else -> exception.message
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

package org.readium.r2.testapp.reader

import android.app.Activity
import android.app.Application
import kotlinx.coroutines.ExperimentalCoroutinesApi
import org.json.JSONObject
Expand All @@ -15,6 +16,8 @@ import org.readium.r2.shared.Injectable
import org.readium.r2.shared.publication.Locator
import org.readium.r2.shared.publication.Publication
import org.readium.r2.shared.publication.asset.FileAsset
import org.readium.r2.shared.publication.services.isRestricted
import org.readium.r2.shared.publication.services.protectionError
import org.readium.r2.shared.util.Try
import org.readium.r2.shared.util.getOrElse
import org.readium.r2.streamer.Streamer
Expand All @@ -39,22 +42,24 @@ class ReaderRepository(
private val mediaBinder: MediaService.Binder,
private val bookRepository: BookRepository
) {
object CancellationException : Exception()

private val repository: MutableMap<Long, ReaderInitData> =
mutableMapOf()

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

suspend fun open(bookId: Long): Try<Unit, Exception> {
suspend fun open(bookId: Long, activity: Activity): Try<Unit, Exception> {
return try {
openThrowing(bookId)
openThrowing(bookId, activity)
Try.success(Unit)
} catch (e: Exception) {
Try.failure(e)
}
}

private suspend fun openThrowing(bookId: Long) {
private suspend fun openThrowing(bookId: Long, activity: Activity) {
if (bookId in repository.keys) {
return
}
Expand All @@ -66,9 +71,15 @@ class ReaderRepository(
require(file.exists())
val asset = FileAsset(file)

val publication = streamer.open(asset, allowUserInteraction = true, sender = application)
val publication = streamer.open(asset, allowUserInteraction = true, sender = activity)
.getOrThrow()

// The publication is protected with a DRM and not unlocked.
if (publication.isRestricted) {
throw publication.protectionError
?: CancellationException
}

val initialLocator = book.progression?.let { Locator.fromJSON(JSONObject(it)) }

val readerInitData = when {
Expand Down

0 comments on commit 540c80f

Please sign in to comment.