Skip to content
This repository has been archived by the owner on Jul 29, 2022. It is now read-only.

Add the Publication's positionList #292

Merged
merged 7 commits into from
Mar 20, 2020
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,32 +16,21 @@ import org.jetbrains.anko.toast
import org.readium.r2.navigator.Navigator
import org.readium.r2.navigator.NavigatorDelegate
import org.readium.r2.navigator.audiobook.R2AudiobookActivity
import org.readium.r2.shared.Locations
import org.readium.r2.shared.Locator
import org.readium.r2.shared.LocatorText
import org.readium.r2.shared.publication.Locator
import org.readium.r2.shared.publication.indexOfFirstWithHref
import org.readium.r2.testapp.R
import org.readium.r2.testapp.db.Bookmark
import org.readium.r2.testapp.db.BookmarksDatabase
import org.readium.r2.testapp.db.BooksDatabase
import org.readium.r2.testapp.library.activitiesLaunched
import org.readium.r2.testapp.outline.R2OutlineActivity
import timber.log.Timber


class AudiobookActivity : R2AudiobookActivity(), NavigatorDelegate {

override val currentLocation: Locator?
get() {
return booksDB.books.currentLocator(bookId)?.let {
it
} ?: run {
val resource = publication.readingOrder[currentResource]
val resourceHref = resource.href
val resourceType = resource.type ?: ""
Locator(resourceHref, resourceType, publication.metadata.title, Locations(progression = 0.0))
}
}

override fun locationDidChange(navigator: Navigator?, locator: Locator) {
Timber.d("locationDidChange $locator")
booksDB.books.saveProgression(locator, bookId)
}

Expand All @@ -63,7 +52,9 @@ class AudiobookActivity : R2AudiobookActivity(), NavigatorDelegate {

progressDialog = indeterminateProgressDialog(getString(R.string.progress_wait_while_preparing_audiobook))

currentResource = publication.readingOrder.indexOfFirst { it.href == currentLocation?.href }
currentResource = booksDB.books.currentLocator(bookId)
?.let { publication.readingOrder.indexOfFirstWithHref(it.href) }
?: 0

Handler().postDelayed({
//Setting cover
Expand Down Expand Up @@ -130,8 +121,8 @@ class AudiobookActivity : R2AudiobookActivity(), NavigatorDelegate {
resourceHref,
resourceType,
resourceTitle,
Locations(progression = seekBar!!.progress.toDouble()),
LocatorText()
Locator.Locations(progression = seekBar!!.progress.toDouble()),
Locator.Text()
)

bookmarksDB.bookmarks.insert(bookmark)?.let {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,14 @@ import kotlinx.coroutines.Dispatchers
import org.readium.r2.navigator.Navigator
import org.readium.r2.navigator.NavigatorDelegate
import org.readium.r2.navigator.cbz.R2CbzActivity
import org.readium.r2.shared.Locations
import org.readium.r2.shared.Locator
import org.readium.r2.shared.publication.Locator
import org.readium.r2.shared.publication.indexOfFirstWithHref
import org.readium.r2.shared.publication.opds.images
import org.readium.r2.testapp.R
import org.readium.r2.testapp.db.BooksDatabase
import org.readium.r2.testapp.library.activitiesLaunched
import org.readium.r2.testapp.outline.R2OutlineActivity
import timber.log.Timber
import kotlin.coroutines.CoroutineContext


Expand All @@ -38,20 +39,8 @@ import kotlin.coroutines.CoroutineContext
*/
class ComicActivity : R2CbzActivity(), CoroutineScope, NavigatorDelegate {


override val currentLocation: Locator?
get() {
return booksDB.books.currentLocator(bookId)?.let {
it
} ?: run {
val resource = publication.readingOrder[resourcePager.currentItem]
val resourceHref = resource.href
val resourceType = resource.type ?: ""
Locator(resourceHref, resourceType, publication.metadata.title, Locations(progression = 0.0))
}
}

override fun locationDidChange(navigator: Navigator?, locator: Locator) {
Timber.d("locationDidChange position ${locator.locations.position ?: 0}/${publication.positionList.size} $locator")
booksDB.books.saveProgression(locator, bookId)
}

Expand All @@ -74,7 +63,10 @@ class ComicActivity : R2CbzActivity(), CoroutineScope, NavigatorDelegate {
navigatorDelegate = this
bookId = intent.getLongExtra("bookId", -1)

currentPagerPosition = publication.readingOrder.indexOfFirst { it.href == currentLocation?.href }
currentPagerPosition = booksDB.books.currentLocator(bookId)
?.let { publication.readingOrder.indexOfFirstWithHref(it.href) }
?: 0

resourcePager.currentItem = currentPagerPosition

toggleActionBar()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import android.view.MenuItem
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import org.readium.r2.navigator.divina.R2DiViNaActivity
import org.readium.r2.shared.Locator
import org.readium.r2.shared.publication.Locator
import org.readium.r2.testapp.BuildConfig.DEBUG
import org.readium.r2.testapp.R
import org.readium.r2.testapp.library.activitiesLaunched
Expand Down Expand Up @@ -76,7 +76,7 @@ class DiViNaActivity : R2DiViNaActivity(), CoroutineScope {
super.onActivityResult(requestCode, resultCode, data)
data ?: return
if (requestCode == 2 && resultCode == Activity.RESULT_OK) {
val locator = data.getSerializableExtra("locator") as Locator
val locator = data.getParcelableExtra("locator") as Locator
if (DEBUG) Timber.d("locator href ${locator.href}")

// Call the player's goTo function with the considered href
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,30 @@ import nl.komponents.kovenant.then
import org.jetbrains.anko.db.*
import org.joda.time.DateTime
import org.json.JSONObject
import org.readium.r2.shared.Locations
import org.readium.r2.shared.Locator
import org.readium.r2.shared.LocatorText

class Bookmark(val bookID: Long,
val publicationID: String,
val resourceIndex: Long,
val resourceHref: String,
val resourceType: String,
val resourceTitle: String,
val location: Locations,
val locatorText: LocatorText,
var creationDate: Long = DateTime().toDate().time,
var id: Long? = null):
Locator(resourceHref, resourceType, resourceTitle, location, locatorText)
import org.readium.r2.shared.publication.Locator

class Bookmark(
val bookID: Long,
val publicationID: String,
val resourceIndex: Long,
val resourceHref: String,
val resourceType: String,
val resourceTitle: String,
val location: Locator.Locations,
val locatorText: Locator.Text,
var creationDate: Long = DateTime().toDate().time,
var id: Long? = null
) {

val locator get() = Locator(
href = resourceHref,
type = resourceType,
title = resourceTitle,
locations = location,
text = locatorText
)

}

class BookmarksDatabase(context: Context) {

Expand Down Expand Up @@ -99,7 +108,7 @@ class BookmarksDatabaseOpenHelper(ctx: Context) : ManagedSQLiteOpenHelper(ctx, "
val id = cursor.getInt(cursor.getColumnIndex(BOOKMARKSTable.ID))
val progression = cursor.getDouble(cursor.getColumnIndex("progression"))
val values = ContentValues()
values.put(BOOKMARKSTable.LOCATION, Locations(progression = progression).toJSON().toString())
values.put(BOOKMARKSTable.LOCATION, Locator.Locations(progression = progression).toJSON().toString())
db.update(BOOKMARKSTable.NAME, values, "${BOOKMARKSTable.ID}=?", arrayOf(id.toString()))
hasItem = cursor.moveToNext()
}
Expand Down Expand Up @@ -306,7 +315,7 @@ class BOOKMARKS(private var database: BookmarksDatabaseOpenHelper) {
return@let it
} ?: kotlin.run { return@run null }

return Bookmark(bookID as Long, publicationID as String, resourceIndex as Long, resourceHref as String, resourceType as String, resourceTitle as String, Locations.fromJSON(JSONObject(location as String)), LocatorText.fromJSON(JSONObject(locatorText as String)), created as Long, id as Long)
return Bookmark(bookID as Long, publicationID as String, resourceIndex as Long, resourceHref as String, resourceType as String, resourceTitle as String, Locator.Locations.fromJSON(JSONObject(location as String)), Locator.Text.fromJSON(JSONObject(locatorText as String)), created as Long, id as Long)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import android.os.Build
import org.jetbrains.anko.db.*
import org.joda.time.DateTime
import org.json.JSONObject
import org.readium.r2.shared.Locator
import org.readium.r2.shared.publication.Locator
import org.readium.r2.shared.publication.Publication
import java.net.URI
import java.nio.file.Paths
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,26 +14,33 @@ import android.database.sqlite.SQLiteDatabase
import org.jetbrains.anko.db.*
import org.joda.time.DateTime
import org.json.JSONObject
import org.readium.r2.shared.Locations
import org.readium.r2.shared.Locator
import org.readium.r2.shared.LocatorText

class Highlight(val highlightID: String,
val publicationID: String,
val style: String,
val color: Int,
val annotation: String,
val annotationMarkStyle: String,
val resourceIndex: Long,
val resourceHref: String,
val resourceType: String,
val resourceTitle: String,
val location: Locations,
val locatorText: LocatorText,
var creationDate: Long = DateTime().toDate().time,
var id: Long? = null,
val bookID: Long):
Locator(resourceHref, resourceType, resourceTitle, location, locatorText)
import org.readium.r2.shared.publication.Locator

class Highlight(
val highlightID: String,
val publicationID: String,
val style: String,
val color: Int,
val annotation: String,
val annotationMarkStyle: String,
val resourceIndex: Long,
val resourceHref: String,
val resourceType: String,
val resourceTitle: String,
val location: Locator.Locations,
val locatorText: Locator.Text,
var creationDate: Long = DateTime().toDate().time,
var id: Long? = null,
val bookID: Long
) {
val locator: Locator get() = Locator(
href = resourceHref,
type = resourceType,
title = resourceTitle,
locations = location,
text = locatorText
)
}

class HighligtsDatabase(context: Context) {

Expand Down Expand Up @@ -370,7 +377,7 @@ class HIGHLIGHTS(private var database: HighlightsDatabaseOpenHelper) {
return@let it
} ?: kotlin.run { return@run 0 }

return Highlight(highlightID as String, publicationID as String, style as String, (color as Long).toInt(), annotation as String, annotationMarkStyle as String, resourceIndex as Long, resourceHref as String, resourceType as String, resourceTitle as String, Locations.fromJSON(JSONObject(location as String)), LocatorText.fromJSON(JSONObject(locatorText as String)), created as Long, id as Long, bookID = bookID as Long)
return Highlight(highlightID as String, publicationID as String, style as String, (color as Long).toInt(), annotation as String, annotationMarkStyle as String, resourceIndex as Long, resourceHref as String, resourceType as String, resourceTitle as String, Locator.Locations.fromJSON(JSONObject(location as String)), Locator.Text.fromJSON(JSONObject(locatorText as String)), created as Long, id as Long, bookID = bookID as Long)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,9 @@ import org.readium.r2.navigator.pager.R2EpubPageFragment
import org.readium.r2.navigator.pager.R2PagerAdapter
import org.readium.r2.shared.*
import org.readium.r2.shared.publication.ContentLayout
import org.readium.r2.shared.publication.Locator
import org.readium.r2.shared.publication.epub.EpubLayout
import org.readium.r2.shared.publication.indexOfFirstWithHref
import org.readium.r2.shared.publication.presentation.presentation
import org.readium.r2.testapp.BuildConfig.DEBUG
import org.readium.r2.testapp.DRMManagementActivity
Expand All @@ -58,7 +60,6 @@ import org.readium.r2.testapp.db.*
import org.readium.r2.testapp.library.activitiesLaunched
import org.readium.r2.testapp.outline.R2OutlineActivity
import org.readium.r2.testapp.search.MarkJSSearchEngine
import org.readium.r2.testapp.search.SearchLocator
import org.readium.r2.testapp.search.SearchLocatorAdapter
import timber.log.Timber
import kotlin.coroutines.CoroutineContext
Expand All @@ -73,22 +74,11 @@ import kotlin.coroutines.CoroutineContext
*/
class EpubActivity : R2EpubActivity(), CoroutineScope, NavigatorDelegate/*, VisualNavigatorDelegate, OutlineTableViewControllerDelegate*/ {

override val currentLocation: Locator?
get() {
return booksDB.books.currentLocator(bookId)?.let {
it
} ?: run {
val resource = publication.readingOrder[resourcePager.currentItem]
val resourceHref = resource.href
val resourceType = resource.type ?: ""
Locator(resourceHref, resourceType, publication.metadata.title, Locations(progression = 0.0))
}
}

override fun locationDidChange(navigator: Navigator?, locator: Locator) {
Timber.d("locationDidChange position ${locator.locations.position ?: 0}/${publication.positionList.size} $locator")

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since the last position could contain several screen pages, it would be helpful with an event that notifies when we reach the end of the book.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I opened an issue for that: readium/architecture#115

booksDB.books.saveProgression(locator, bookId)

if (locator.locations?.progression == 0.toDouble()) {
if (this::screenReader.isInitialized && locator.locations.progression == 0.0) {
screenReader.currentUtterance = 0
}
}
Expand Down Expand Up @@ -126,7 +116,7 @@ class EpubActivity : R2EpubActivity(), CoroutineScope, NavigatorDelegate/*, Visu
private var searchTerm = ""
private lateinit var searchStorage: SharedPreferences
private lateinit var searchResultAdapter: SearchLocatorAdapter
private lateinit var searchResult: MutableList<SearchLocator>
private lateinit var searchResult: MutableList<Locator>

private var mode: ActionMode? = null
private var popupWindow: PopupWindow? = null
Expand Down Expand Up @@ -166,7 +156,9 @@ class EpubActivity : R2EpubActivity(), CoroutineScope, NavigatorDelegate/*, Visu

resourcePager.offscreenPageLimit = 1

currentPagerPosition = publication.readingOrder.indexOfFirst { it.href == currentLocation?.href }
currentPagerPosition = booksDB.books.currentLocator(bookId)
?.let { publication.readingOrder.indexOfFirstWithHref(it.href) }
?: 0
resourcePager.currentItem = currentPagerPosition

titleView.text = publication.metadata.title
Expand Down Expand Up @@ -370,7 +362,7 @@ class EpubActivity : R2EpubActivity(), CoroutineScope, NavigatorDelegate/*, Visu
val tmp = searchStorage.getString("result", null)
if (tmp != null) {
searchResult.clear()
searchResult.addAll(Gson().fromJson(tmp, Array<SearchLocator>::class.java).asList().toMutableList())
searchResult.addAll(Gson().fromJson(tmp, Array<Locator>::class.java).asList().toMutableList())
searchResultAdapter.notifyDataSetChanged()

val keyword = searchStorage.getString("term", null)
Expand Down Expand Up @@ -512,8 +504,8 @@ class EpubActivity : R2EpubActivity(), CoroutineScope, NavigatorDelegate/*, Visu
resourceHref,
resourceType,
resourceTitle,
Locations(progression = currentLocation?.locations?.progression, position = currentPage),
LocatorText()
Locator.Locations(progression = currentLocation?.locations?.progression, position = currentPage?.toInt()),
Locator.Text()
)

bookmarksDB.bookmarks.insert(bookmark)?.let {
Expand Down Expand Up @@ -564,8 +556,8 @@ class EpubActivity : R2EpubActivity(), CoroutineScope, NavigatorDelegate/*, Visu
} else {
super.onActivityResult(requestCode, resultCode, data)
if (requestCode == 2 && resultCode == Activity.RESULT_OK && data != null) {
val locator = data.getSerializableExtra("locator") as Locator
locator.locations?.fragment?.let { fragment ->
val locator = data.getParcelableExtra("locator") as Locator
locator.locations.fragments.firstOrNull()?.let { fragment ->

// TODO handle fragment anchors (id=) instead of catching the json exception
try {
Expand Down Expand Up @@ -794,7 +786,7 @@ class EpubActivity : R2EpubActivity(), CoroutineScope, NavigatorDelegate/*, Visu
(getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager).hideSoftInputFromWindow(note.applicationWindowToken, InputMethodManager.HIDE_NOT_ALWAYS)
}
if (highlight != null) {
findViewById<TextView>(R.id.select_text).text = highlight.locator.text?.highlight
findViewById<TextView>(R.id.select_text).text = highlight.locator.text.highlight
note.setText(annotation)
} else {
currentSelection {
Expand Down Expand Up @@ -832,11 +824,11 @@ class EpubActivity : R2EpubActivity(), CoroutineScope, NavigatorDelegate/*, Visu
it
}

val highlightLocations = highlight.locator.locations?.apply {
progression = currentLocation?.locations?.progression
position = currentPage
} ?: Locations()
val locationText = highlight.locator.text ?: LocatorText()
val highlightLocations = highlight.locator.locations.copy(
progression = currentLocation?.locations?.progression,
position = currentPage?.toInt()
)
val locationText = highlight.locator.text

return Highlight(
highlight.id,
Expand All @@ -857,12 +849,7 @@ class EpubActivity : R2EpubActivity(), CoroutineScope, NavigatorDelegate/*, Visu

private fun convertHighlight2NavigationHighlight(highlight: Highlight) = org.readium.r2.navigator.epub.Highlight(
highlight.highlightID,
Locator(
highlight.resourceHref,
highlight.resourceType,
locations = highlight.locations,
text = highlight.locatorText
),
highlight.locator,
highlight.color,
Style.highlight,
highlight.annotationMarkStyle
Expand Down
Loading