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

Commit

Permalink
Use the new Locator model
Browse files Browse the repository at this point in the history
  • Loading branch information
mickael-menu committed Feb 9, 2020
1 parent 7ae44d9 commit 177f125
Show file tree
Hide file tree
Showing 10 changed files with 100 additions and 108 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@ 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.testapp.R
import org.readium.r2.testapp.db.Bookmark
import org.readium.r2.testapp.db.BookmarksDatabase
Expand All @@ -37,7 +35,7 @@ class AudiobookActivity : R2AudiobookActivity(), NavigatorDelegate {
val resource = publication.readingOrder[currentResource]
val resourceHref = resource.href
val resourceType = resource.type ?: ""
Locator(resourceHref, resourceType, publication.metadata.title, Locations(progression = 0.0))
Locator(resourceHref, resourceType, publication.metadata.title, Locator.Locations(progression = 0.0))
}
}

Expand Down Expand Up @@ -130,8 +128,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,8 +19,7 @@ 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.opds.images
import org.readium.r2.testapp.R
import org.readium.r2.testapp.db.BooksDatabase
Expand All @@ -47,7 +46,7 @@ class ComicActivity : R2CbzActivity(), CoroutineScope, NavigatorDelegate {
val resource = publication.readingOrder[resourcePager.currentItem]
val resourceHref = resource.href
val resourceType = resource.type ?: ""
Locator(resourceHref, resourceType, publication.metadata.title, Locations(progression = 0.0))
Locator(resourceHref, resourceType, publication.metadata.title, Locator.Locations(progression = 0.0))
}
}

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,6 +49,7 @@ 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.presentation.presentation
import org.readium.r2.testapp.BuildConfig.DEBUG
Expand All @@ -58,7 +59,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 @@ -81,14 +81,14 @@ class EpubActivity : R2EpubActivity(), CoroutineScope, NavigatorDelegate/*, Visu
val resource = publication.readingOrder[resourcePager.currentItem]
val resourceHref = resource.href
val resourceType = resource.type ?: ""
Locator(resourceHref, resourceType, publication.metadata.title, Locations(progression = 0.0))
Locator(resourceHref, resourceType, publication.metadata.title, Locator.Locations(progression = 0.0))
}
}

override fun locationDidChange(navigator: Navigator?, locator: Locator) {
booksDB.books.saveProgression(locator, bookId)

if (locator.locations?.progression == 0.toDouble()) {
if (locator.locations.progression == 0.0) {
screenReader.currentUtterance = 0
}
}
Expand Down Expand Up @@ -126,7 +126,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 @@ -370,7 +370,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 +512,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 +564,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 +794,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 +832,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 +857,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
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,8 @@ import kotlinx.android.synthetic.main.item_recycle_highlight.view.*
import kotlinx.android.synthetic.main.item_recycle_outline.view.*
import org.joda.time.DateTime
import org.joda.time.format.DateTimeFormat
import org.readium.r2.shared.Locations
import org.readium.r2.shared.Locator
import org.readium.r2.shared.publication.Link
import org.readium.r2.shared.publication.Locator
import org.readium.r2.shared.publication.Publication
import org.readium.r2.shared.publication.epub.landmarks
import org.readium.r2.shared.publication.epub.pageList
Expand Down Expand Up @@ -102,9 +101,9 @@ class R2OutlineActivity : AppCompatActivity() {

if (resourceHref.indexOf("#") > 0) {
val id = resourceHref.substring(resourceHref.indexOf('#'))
intent.putExtra("locator", Locator(resourceHref, resourceType, publication.metadata.title, Locations(fragment = id),null))
intent.putExtra("locator", Locator(resourceHref, resourceType, publication.metadata.title, Locator.Locations(fragments = listOf(id))))
} else {
intent.putExtra("locator", Locator(resourceHref, resourceType, publication.metadata.title, Locations(progression = 0.0),null))
intent.putExtra("locator", Locator(resourceHref, resourceType, publication.metadata.title, Locator.Locations(progression = 0.0)))
}

setResult(Activity.RESULT_OK, intent)
Expand Down Expand Up @@ -137,7 +136,7 @@ class R2OutlineActivity : AppCompatActivity() {
val bookmarkProgression = bookmarks[position].location.progression

val intent = Intent()
intent.putExtra("locator", Locator(resourceHref, resourceType, publication.metadata.title, Locations(progression = bookmarkProgression),null))
intent.putExtra("locator", Locator(resourceHref, resourceType, publication.metadata.title, Locator.Locations(progression = bookmarkProgression)))
setResult(Activity.RESULT_OK, intent)
finish()
}
Expand All @@ -156,7 +155,7 @@ class R2OutlineActivity : AppCompatActivity() {
//Progression of the selected bookmark
val highlightProgression = highlights[position].location.progression
val intent = Intent()
intent.putExtra("locator", Locator(resourceHref, resourceType, publication.metadata.title, Locations(progression = highlightProgression),null))
intent.putExtra("locator", Locator(resourceHref, resourceType, publication.metadata.title, Locator.Locations(progression = highlightProgression)))
setResult(Activity.RESULT_OK, intent)
finish()

Expand All @@ -182,7 +181,7 @@ class R2OutlineActivity : AppCompatActivity() {


val intent = Intent()
intent.putExtra("locator", Locator(resourceHref, resourceType, publication.metadata.title, Locations(progression = 0.0),null))
intent.putExtra("locator", Locator(resourceHref, resourceType, publication.metadata.title, Locator.Locations(progression = 0.0)))
setResult(Activity.RESULT_OK, intent)
finish()

Expand All @@ -207,7 +206,7 @@ class R2OutlineActivity : AppCompatActivity() {
val pageProgression = syntheticPageList[position].progression

val intent = Intent()
intent.putExtra("locator", Locator(resourceHref, resourceType, publication.metadata.title, Locations(progression = pageProgression), null))
intent.putExtra("locator", Locator(resourceHref, resourceType, publication.metadata.title, Locator.Locations(progression = pageProgression)))
setResult(Activity.RESULT_OK, intent)
finish()
}
Expand All @@ -231,7 +230,7 @@ class R2OutlineActivity : AppCompatActivity() {
val resourceType = link.type ?: ""

val intent = Intent()
intent.putExtra("locator", Locator(resourceHref, resourceType, publication.metadata.title, Locations(progression = 0.0),null))
intent.putExtra("locator", Locator(resourceHref, resourceType, publication.metadata.title, Locator.Locations(progression = 0.0)))
setResult(Activity.RESULT_OK, intent)
finish()

Expand Down
Loading

0 comments on commit 177f125

Please sign in to comment.