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

Commit c467f64

Browse files
authored
Merge pull request #292 from readium/feature/position-list
Add the Publication's positionList
2 parents 1370548 + 8d585b7 commit c467f64

File tree

11 files changed

+127
-156
lines changed

11 files changed

+127
-156
lines changed

r2-testapp/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,4 +181,5 @@ dependencies {
181181
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
182182
testImplementation "org.jetbrains.kotlin:kotlin-test-junit:$kotlin_version"
183183

184+
implementation "readium:liblcp:1.0.0@aar"
184185
}

r2-testapp/src/main/java/org/readium/r2/testapp/audiobook/AudiobookActivity.kt

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -16,32 +16,21 @@ import org.jetbrains.anko.toast
1616
import org.readium.r2.navigator.Navigator
1717
import org.readium.r2.navigator.NavigatorDelegate
1818
import org.readium.r2.navigator.audiobook.R2AudiobookActivity
19-
import org.readium.r2.shared.Locations
20-
import org.readium.r2.shared.Locator
21-
import org.readium.r2.shared.LocatorText
19+
import org.readium.r2.shared.publication.Locator
20+
import org.readium.r2.shared.publication.indexOfFirstWithHref
2221
import org.readium.r2.testapp.R
2322
import org.readium.r2.testapp.db.Bookmark
2423
import org.readium.r2.testapp.db.BookmarksDatabase
2524
import org.readium.r2.testapp.db.BooksDatabase
2625
import org.readium.r2.testapp.library.activitiesLaunched
2726
import org.readium.r2.testapp.outline.R2OutlineActivity
27+
import timber.log.Timber
2828

2929

3030
class AudiobookActivity : R2AudiobookActivity(), NavigatorDelegate {
3131

32-
override val currentLocation: Locator?
33-
get() {
34-
return booksDB.books.currentLocator(bookId)?.let {
35-
it
36-
} ?: run {
37-
val resource = publication.readingOrder[currentResource]
38-
val resourceHref = resource.href
39-
val resourceType = resource.type ?: ""
40-
Locator(resourceHref, resourceType, publication.metadata.title, Locations(progression = 0.0))
41-
}
42-
}
43-
4432
override fun locationDidChange(navigator: Navigator?, locator: Locator) {
33+
Timber.d("locationDidChange $locator")
4534
booksDB.books.saveProgression(locator, bookId)
4635
}
4736

@@ -63,8 +52,6 @@ class AudiobookActivity : R2AudiobookActivity(), NavigatorDelegate {
6352

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

66-
currentResource = publication.readingOrder.indexOfFirst { it.href == currentLocation?.href }
67-
6855
Handler().postDelayed({
6956
//Setting cover
7057
launch {
@@ -82,6 +69,10 @@ class AudiobookActivity : R2AudiobookActivity(), NavigatorDelegate {
8269
}, 100)
8370

8471

72+
// Loads the last read location
73+
booksDB.books.currentLocator(bookId)?.let {
74+
go(it, false, {})
75+
}
8576
}
8677

8778
private var menuDrm: MenuItem? = null
@@ -130,8 +121,8 @@ class AudiobookActivity : R2AudiobookActivity(), NavigatorDelegate {
130121
resourceHref,
131122
resourceType,
132123
resourceTitle,
133-
Locations(progression = seekBar!!.progress.toDouble()),
134-
LocatorText()
124+
Locator.Locations(progression = seekBar!!.progress.toDouble()),
125+
Locator.Text()
135126
)
136127

137128
bookmarksDB.bookmarks.insert(bookmark)?.let {

r2-testapp/src/main/java/org/readium/r2/testapp/comic/ComicActivity.kt

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,14 @@ import kotlinx.coroutines.Dispatchers
1919
import org.readium.r2.navigator.Navigator
2020
import org.readium.r2.navigator.NavigatorDelegate
2121
import org.readium.r2.navigator.cbz.R2CbzActivity
22-
import org.readium.r2.shared.Locations
23-
import org.readium.r2.shared.Locator
22+
import org.readium.r2.shared.publication.Locator
23+
import org.readium.r2.shared.publication.indexOfFirstWithHref
2424
import org.readium.r2.shared.publication.opds.images
2525
import org.readium.r2.testapp.R
2626
import org.readium.r2.testapp.db.BooksDatabase
2727
import org.readium.r2.testapp.library.activitiesLaunched
2828
import org.readium.r2.testapp.outline.R2OutlineActivity
29+
import timber.log.Timber
2930
import kotlin.coroutines.CoroutineContext
3031

3132

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

41-
42-
override val currentLocation: Locator?
43-
get() {
44-
return booksDB.books.currentLocator(bookId)?.let {
45-
it
46-
} ?: run {
47-
val resource = publication.readingOrder[resourcePager.currentItem]
48-
val resourceHref = resource.href
49-
val resourceType = resource.type ?: ""
50-
Locator(resourceHref, resourceType, publication.metadata.title, Locations(progression = 0.0))
51-
}
52-
}
53-
5442
override fun locationDidChange(navigator: Navigator?, locator: Locator) {
43+
Timber.d("locationDidChange position ${locator.locations.position ?: 0}/${publication.positions.size} $locator")
5544
booksDB.books.saveProgression(locator, bookId)
5645
}
5746

@@ -74,10 +63,12 @@ class ComicActivity : R2CbzActivity(), CoroutineScope, NavigatorDelegate {
7463
navigatorDelegate = this
7564
bookId = intent.getLongExtra("bookId", -1)
7665

77-
currentPagerPosition = publication.readingOrder.indexOfFirst { it.href == currentLocation?.href }
78-
resourcePager.currentItem = currentPagerPosition
79-
8066
toggleActionBar()
67+
68+
// Loads the last read location
69+
booksDB.books.currentLocator(bookId)?.let {
70+
go(it, false, {})
71+
}
8172
}
8273

8374
override fun onCreateOptionsMenu(menu: Menu?): Boolean {

r2-testapp/src/main/java/org/readium/r2/testapp/comic/DiViNaActivity.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import android.view.MenuItem
1818
import kotlinx.coroutines.CoroutineScope
1919
import kotlinx.coroutines.Dispatchers
2020
import org.readium.r2.navigator.divina.R2DiViNaActivity
21-
import org.readium.r2.shared.Locator
21+
import org.readium.r2.shared.publication.Locator
2222
import org.readium.r2.testapp.BuildConfig.DEBUG
2323
import org.readium.r2.testapp.R
2424
import org.readium.r2.testapp.library.activitiesLaunched
@@ -76,7 +76,7 @@ class DiViNaActivity : R2DiViNaActivity(), CoroutineScope {
7676
super.onActivityResult(requestCode, resultCode, data)
7777
data ?: return
7878
if (requestCode == 2 && resultCode == Activity.RESULT_OK) {
79-
val locator = data.getSerializableExtra("locator") as Locator
79+
val locator = data.getParcelableExtra("locator") as Locator
8080
if (DEBUG) Timber.d("locator href ${locator.href}")
8181

8282
// Call the player's goTo function with the considered href

r2-testapp/src/main/java/org/readium/r2/testapp/db/BookmarksDatabase.kt

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -19,21 +19,30 @@ import nl.komponents.kovenant.then
1919
import org.jetbrains.anko.db.*
2020
import org.joda.time.DateTime
2121
import org.json.JSONObject
22-
import org.readium.r2.shared.Locations
23-
import org.readium.r2.shared.Locator
24-
import org.readium.r2.shared.LocatorText
25-
26-
class Bookmark(val bookID: Long,
27-
val publicationID: String,
28-
val resourceIndex: Long,
29-
val resourceHref: String,
30-
val resourceType: String,
31-
val resourceTitle: String,
32-
val location: Locations,
33-
val locatorText: LocatorText,
34-
var creationDate: Long = DateTime().toDate().time,
35-
var id: Long? = null):
36-
Locator(resourceHref, resourceType, resourceTitle, location, locatorText)
22+
import org.readium.r2.shared.publication.Locator
23+
24+
class Bookmark(
25+
val bookID: Long,
26+
val publicationID: String,
27+
val resourceIndex: Long,
28+
val resourceHref: String,
29+
val resourceType: String,
30+
val resourceTitle: String,
31+
val location: Locator.Locations,
32+
val locatorText: Locator.Text,
33+
var creationDate: Long = DateTime().toDate().time,
34+
var id: Long? = null
35+
) {
36+
37+
val locator get() = Locator(
38+
href = resourceHref,
39+
type = resourceType,
40+
title = resourceTitle,
41+
locations = location,
42+
text = locatorText
43+
)
44+
45+
}
3746

3847
class BookmarksDatabase(context: Context) {
3948

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

309-
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)
318+
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)
310319
}
311320
}
312321

r2-testapp/src/main/java/org/readium/r2/testapp/db/BooksDatabase.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import android.os.Build
1919
import org.jetbrains.anko.db.*
2020
import org.joda.time.DateTime
2121
import org.json.JSONObject
22-
import org.readium.r2.shared.Locator
22+
import org.readium.r2.shared.publication.Locator
2323
import org.readium.r2.shared.publication.Publication
2424
import java.net.URI
2525
import java.nio.file.Paths

r2-testapp/src/main/java/org/readium/r2/testapp/db/HighlightsDatabase.kt

Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -14,26 +14,33 @@ import android.database.sqlite.SQLiteDatabase
1414
import org.jetbrains.anko.db.*
1515
import org.joda.time.DateTime
1616
import org.json.JSONObject
17-
import org.readium.r2.shared.Locations
18-
import org.readium.r2.shared.Locator
19-
import org.readium.r2.shared.LocatorText
20-
21-
class Highlight(val highlightID: String,
22-
val publicationID: String,
23-
val style: String,
24-
val color: Int,
25-
val annotation: String,
26-
val annotationMarkStyle: String,
27-
val resourceIndex: Long,
28-
val resourceHref: String,
29-
val resourceType: String,
30-
val resourceTitle: String,
31-
val location: Locations,
32-
val locatorText: LocatorText,
33-
var creationDate: Long = DateTime().toDate().time,
34-
var id: Long? = null,
35-
val bookID: Long):
36-
Locator(resourceHref, resourceType, resourceTitle, location, locatorText)
17+
import org.readium.r2.shared.publication.Locator
18+
19+
class Highlight(
20+
val highlightID: String,
21+
val publicationID: String,
22+
val style: String,
23+
val color: Int,
24+
val annotation: String,
25+
val annotationMarkStyle: String,
26+
val resourceIndex: Long,
27+
val resourceHref: String,
28+
val resourceType: String,
29+
val resourceTitle: String,
30+
val location: Locator.Locations,
31+
val locatorText: Locator.Text,
32+
var creationDate: Long = DateTime().toDate().time,
33+
var id: Long? = null,
34+
val bookID: Long
35+
) {
36+
val locator: Locator get() = Locator(
37+
href = resourceHref,
38+
type = resourceType,
39+
title = resourceTitle,
40+
locations = location,
41+
text = locatorText
42+
)
43+
}
3744

3845
class HighligtsDatabase(context: Context) {
3946

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

373-
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)
380+
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)
374381
}
375382
}
376383

0 commit comments

Comments
 (0)