Skip to content

Commit 5826d80

Browse files
authored
Use the new Decorator API for highlights (#446)
1 parent 1327077 commit 5826d80

File tree

19 files changed

+659
-739
lines changed

19 files changed

+659
-739
lines changed

test-app/r2-testapp/src/main/AndroidManifest.xml

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -185,9 +185,6 @@
185185

186186
</activity>
187187

188-
<activity
189-
android:name=".epub.EpubActivity"
190-
android:label="@string/title_activity_epub" />
191188
<activity
192189
android:name=".reader.ReaderActivity"
193190
android:label="Reader" />
@@ -199,4 +196,4 @@
199196
android:label="@string/title_activity_epub" />
200197
</application>
201198

202-
</manifest>
199+
</manifest>

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

Lines changed: 15 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66

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

9+
import androidx.annotation.ColorInt
910
import androidx.lifecycle.LiveData
11+
import kotlinx.coroutines.flow.Flow
1012
import org.joda.time.DateTime
1113
import org.readium.r2.shared.publication.Locator
1214
import org.readium.r2.shared.publication.Publication
@@ -16,11 +18,12 @@ import org.readium.r2.testapp.domain.model.Book
1618
import org.readium.r2.testapp.domain.model.Bookmark
1719
import org.readium.r2.testapp.domain.model.Highlight
1820
import org.readium.r2.testapp.utils.extensions.authorName
21+
import java.util.*
1922
import org.readium.r2.navigator.epub.Highlight as NavigatorHighlight
2023

2124
class BookRepository(private val booksDao: BooksDao) {
2225

23-
fun getBooksFromDatabase(): LiveData<List<Book>> = booksDao.getAllBooks()
26+
fun books(): LiveData<List<Book>> = booksDao.getAllBooks()
2427

2528
suspend fun insertBook(href: String, extension: String, publication: Publication): Long {
2629
val book = Book(
@@ -60,75 +63,27 @@ class BookRepository(private val booksDao: BooksDao) {
6063
return booksDao.insertBookmark(bookmark)
6164
}
6265

63-
fun getBookmarks(bookId: Long): LiveData<MutableList<Bookmark>> =
66+
fun bookmarksForBook(bookId: Long): LiveData<MutableList<Bookmark>> =
6467
booksDao.getBookmarksForBook(bookId)
6568

6669
suspend fun deleteBookmark(bookmarkId: Long) = booksDao.deleteBookmark(bookmarkId)
6770

68-
fun getHighlights(bookId: Long, href: String): LiveData<List<Highlight>> =
69-
booksDao.getHighlightsForBook(bookId, href)
71+
suspend fun highlightById(id: Long): Highlight? =
72+
booksDao.getHighlightById(id)
7073

71-
fun getHighlights(bookId: Long): LiveData<List<Highlight>> =
74+
fun highlightsForBook(bookId: Long): Flow<List<Highlight>> =
7275
booksDao.getHighlightsForBook(bookId)
7376

74-
suspend fun insertHighlight(
75-
bookId: Long,
76-
publication: Publication,
77-
navigatorHighlight: NavigatorHighlight,
78-
progression: Double,
79-
annotation: String? = null
80-
): Long {
81-
val resource =
82-
publication.readingOrder.indexOfFirstWithHref(navigatorHighlight.locator.href)!!
83-
84-
// This is required to be able to go right to a highlight from the Outline fragment,
85-
// as Navigator.go doesn't support DOM ranges yet.
86-
val locations = navigatorHighlight.locator.locations.copy(progression = progression)
87-
88-
val highlight = Highlight(
89-
creation = DateTime().toDate().time,
90-
bookId = bookId,
91-
highlightId = navigatorHighlight.id,
92-
publicationId = publication.metadata.identifier ?: publication.metadata.title,
93-
style = "style",
94-
color = navigatorHighlight.color,
95-
annotation = annotation ?: "",
96-
annotationMarkStyle = navigatorHighlight.annotationMarkStyle ?: "",
97-
resourceIndex = resource.toLong(),
98-
resourceHref = navigatorHighlight.locator.href,
99-
resourceType = navigatorHighlight.locator.type,
100-
resourceTitle = navigatorHighlight.locator.title.orEmpty(),
101-
location = locations.toJSON().toString(),
102-
locatorText = navigatorHighlight.locator.text.toJSON().toString()
103-
)
77+
suspend fun addHighlight(bookId: Long, style: Highlight.Style, @ColorInt tint: Int, locator: Locator, annotation: String): Long =
78+
booksDao.insertHighlight(Highlight(bookId, style, tint, locator, annotation))
10479

105-
return booksDao.insertHighlight(highlight)
106-
}
107-
108-
// This will be used when highlights are redone
109-
// suspend fun deleteHighlight(id: Long) = booksDao.deleteHighlight(id)
110-
111-
suspend fun updateHighlight(
112-
id: String,
113-
color: Int? = null,
114-
annotation: String? = null,
115-
markStyle: String? = null
116-
) {
117-
val highlight = getHighlightByHighlightId(id)
80+
suspend fun deleteHighlight(id: Long) = booksDao.deleteHighlight(id)
11881

119-
if (highlight != null) {
120-
val updatedColor = color ?: highlight.color
121-
val updatedAnnotation = annotation ?: highlight.annotation
122-
val updatedMarkStyle = markStyle ?: highlight.annotationMarkStyle
123-
124-
booksDao.updateHighlight(id, updatedColor, updatedAnnotation, updatedMarkStyle)
125-
}
82+
suspend fun updateHighlightAnnotation(id: Long, annotation: String) {
83+
booksDao.updateHighlightAnnotation(id, annotation)
12684
}
12785

128-
suspend fun getHighlightByHighlightId(highlightId: String): Highlight? {
129-
return booksDao.getHighlightByHighlightId(highlightId).firstOrNull()
86+
suspend fun updateHighlightStyle(id: Long, style: Highlight.Style, @ColorInt tint: Int) {
87+
booksDao.updateHighlightStyle(id, style, tint)
13088
}
131-
132-
suspend fun deleteHighlightByHighlightId(highlightId: String) =
133-
booksDao.deleteHighlightByHighlightId(highlightId)
13489
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ class BookshelfViewModel(application: Application) : AndroidViewModel(applicatio
6868
val channel = EventChannel(Channel<Event>(Channel.BUFFERED), viewModelScope)
6969
val showProgressBar = ObservableBoolean()
7070

71-
val books = repository.getBooksFromDatabase()
71+
val books = repository.books()
7272

7373
fun deleteBook(book: Book) = viewModelScope.launch {
7474
book.id?.let { repository.deleteBook(it) }

test-app/r2-testapp/src/main/java/org/readium/r2/testapp/db/BooksDao.kt

Lines changed: 18 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,13 @@
66

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

9+
import androidx.annotation.ColorInt
910
import androidx.lifecycle.LiveData
1011
import androidx.room.Dao
1112
import androidx.room.Insert
1213
import androidx.room.OnConflictStrategy
1314
import androidx.room.Query
15+
import kotlinx.coroutines.flow.Flow
1416
import org.readium.r2.testapp.domain.model.Book
1517
import org.readium.r2.testapp.domain.model.Bookmark
1618
import org.readium.r2.testapp.domain.model.Highlight
@@ -51,28 +53,15 @@ interface BooksDao {
5153

5254
/**
5355
* Retrieve all highlights for a specific book
54-
* @param bookId The ID of the book
55-
* @param href
56-
* @return List of highlights for the book as LiveData
5756
*/
58-
@Query("SELECT * FROM " + Highlight.TABLE_NAME + " WHERE " + Highlight.BOOK_ID + " = :bookId AND " + Highlight.RESOURCE_HREF + " = :href")
59-
fun getHighlightsForBook(bookId: Long, href: String): LiveData<List<Highlight>>
57+
@Query("SELECT * FROM ${Highlight.TABLE_NAME} WHERE ${Highlight.BOOK_ID} = :bookId ORDER BY ${Highlight.TOTAL_PROGRESSION} ASC")
58+
fun getHighlightsForBook(bookId: Long): Flow<List<Highlight>>
6059

6160
/**
62-
* Retrieve all highlights for a specific book
63-
* @param bookId The ID of the book
64-
* @return List of highlights for the book as LiveData
65-
*/
66-
@Query("SELECT * FROM " + Highlight.TABLE_NAME + " WHERE " + Highlight.BOOK_ID + " = :bookId")
67-
fun getHighlightsForBook(bookId: Long): LiveData<List<Highlight>>
68-
69-
/**
70-
* Retrieve all highlights for a specific book
71-
* @param highlightId The ID of the highlight
72-
* @return List of highlights that match highlightId
61+
* Retrieves the highlight with the given ID.
7362
*/
74-
@Query("SELECT * FROM " + Highlight.TABLE_NAME + " WHERE " + Highlight.HIGHLIGHT_ID + " = :highlightId")
75-
suspend fun getHighlightByHighlightId(highlightId: String): MutableList<Highlight>
63+
@Query("SELECT * FROM ${Highlight.TABLE_NAME} WHERE ${Highlight.ID} = :highlightId")
64+
suspend fun getHighlightById(highlightId: Long): Highlight?
7665

7766
/**
7867
* Inserts a bookmark
@@ -91,42 +80,29 @@ interface BooksDao {
9180
suspend fun insertHighlight(highlight: Highlight): Long
9281

9382
/**
94-
* Updates a highlight
95-
* @param highlightId The navigator highlight ID
96-
* @param color Updated color
97-
* @param annotation Updated annotation
98-
* @param annotationMarkStyle Updated mark style
99-
* @return The ID of the highlight that was added (primary key)
83+
* Updates a highlight's annotation.
84+
*/
85+
@Query("UPDATE ${Highlight.TABLE_NAME} SET ${Highlight.ANNOTATION} = :annotation WHERE ${Highlight.ID} = :id")
86+
suspend fun updateHighlightAnnotation(id: Long, annotation: String)
87+
88+
/**
89+
* Updates a highlight's tint and style.
10090
*/
101-
@Query("UPDATE " + Highlight.TABLE_NAME + " SET " + Highlight.COLOR + " = :color, " + Highlight.ANNOTATION + " = :annotation, " + Highlight.ANNOTATION_MARK_STYLE + " =:annotationMarkStyle WHERE " + Highlight.HIGHLIGHT_ID + "= :highlightId")
102-
suspend fun updateHighlight(
103-
highlightId: String,
104-
color: Int,
105-
annotation: String,
106-
annotationMarkStyle: String
107-
)
91+
@Query("UPDATE ${Highlight.TABLE_NAME} SET ${Highlight.TINT} = :tint, ${Highlight.STYLE} = :style WHERE ${Highlight.ID} = :id")
92+
suspend fun updateHighlightStyle(id: Long, style: Highlight.Style, @ColorInt tint: Int)
10893

10994
/**
11095
* Deletes a bookmark
111-
* @param id The id of the bookmark to delete
11296
*/
11397
@Query("DELETE FROM " + Bookmark.TABLE_NAME + " WHERE " + Bookmark.ID + " = :id")
11498
suspend fun deleteBookmark(id: Long)
11599

116100
/**
117-
* Deletes a highlight
118-
* @param id The ID of the highlight to delete
101+
* Deletes the highlight with given id.
119102
*/
120-
@Query("DELETE FROM " + Highlight.TABLE_NAME + " WHERE " + Highlight.ID + " = :id")
103+
@Query("DELETE FROM ${Highlight.TABLE_NAME} WHERE ${Highlight.ID} = :id")
121104
suspend fun deleteHighlight(id: Long)
122105

123-
/**
124-
* Deletes a highlight by the highlightId
125-
* @param highlightId The highlightId of the highlight to delete
126-
*/
127-
@Query("DELETE FROM " + Highlight.TABLE_NAME + " WHERE " + Highlight.HIGHLIGHT_ID + " = :highlightId")
128-
suspend fun deleteHighlightByHighlightId(highlightId: String)
129-
130106
/**
131107
* Saves book progression
132108
* @param locator Location of the book

test-app/r2-testapp/src/main/java/org/readium/r2/testapp/db/Database.kt

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,15 @@ import android.content.Context
1010
import androidx.room.Database
1111
import androidx.room.Room
1212
import androidx.room.RoomDatabase
13-
import org.readium.r2.testapp.domain.model.Book
14-
import org.readium.r2.testapp.domain.model.Bookmark
15-
import org.readium.r2.testapp.domain.model.Highlight
16-
import org.readium.r2.testapp.domain.model.Catalog
13+
import androidx.room.TypeConverters
14+
import org.readium.r2.testapp.domain.model.*
1715

1816
@Database(
1917
entities = [Book::class, Bookmark::class, Highlight::class, Catalog::class],
2018
version = 1,
2119
exportSchema = false
2220
)
21+
@TypeConverters(HighlightConverters::class)
2322
abstract class BookDatabase : RoomDatabase() {
2423

2524
abstract fun booksDao(): BooksDao

0 commit comments

Comments
 (0)