load annotations and scroll to saved index ancor page

This commit is contained in:
Satindar Dhillon 2022-10-03 22:16:37 -07:00
parent d89a8d2158
commit 59fe797feb
3 changed files with 21 additions and 59 deletions

View file

@ -1,14 +1,5 @@
package app.omnivore.omnivore.models
import android.graphics.Color
import android.graphics.Rect
import android.graphics.RectF
import app.omnivore.omnivore.networking.CreateHighlightParams
import com.google.gson.Gson
import com.pspdfkit.annotations.AnnotationType
import com.pspdfkit.annotations.BlendMode
import com.pspdfkit.annotations.HighlightAnnotation
data class Highlight(
val id: String,
val shortId: String,
@ -20,39 +11,5 @@ data class Highlight(
val createdAt: Any?,
val updatedAt: Any?,
val createdByMe : Boolean,
) {
fun asHighlightAnnotation(): HighlightAnnotation {
val highlightPatch = Gson().fromJson(patch, HighlightPatch::class.java)
var highlightAnnotation = HighlightAnnotation(
highlightPatch.pageIndex,
highlightPatch.rectList()
)
highlightAnnotation.color = Color.parseColor(highlightPatch.color)
highlightAnnotation.boundingBox = highlightPatch.boundingBox()
highlightAnnotation.blendMode = BlendMode.MULTIPLY
return highlightAnnotation
}
}
data class HighlightPatch(
val bbox: List<Float>,
val blendMode: String,
val color: String,
val rects: List<List<Float>>,
val pageIndex: Int
) {
fun rectList(): List<RectF> {
return rects.map {
RectF(it[0], it[1], it[2], it[3])
}
}
fun boundingBox(): RectF {
return RectF(bbox[0], bbox[1], bbox[2], bbox[3])
}
}
)

View file

@ -1,5 +1,6 @@
package app.omnivore.omnivore.ui.reader
import android.graphics.RectF
import android.net.Uri
import android.os.Bundle
import android.util.Log
@ -27,9 +28,6 @@ class PDFReaderActivity: AppCompatActivity(), DocumentListener {
super.onCreate(savedInstanceState)
setContentView(R.layout.pdf_reader_fragment)
val slug = intent.getStringExtra("LINKED_ITEM_SLUG") ?: ""
viewModel.loadItem(slug, this)
// Create the observer which updates the UI.
val pdfParamsObserver = Observer<PDFReaderParams?> { params ->
if (params != null) {
@ -39,6 +37,9 @@ class PDFReaderActivity: AppCompatActivity(), DocumentListener {
// Observe the LiveData, passing in this activity as the LifecycleOwner and the observer.
viewModel.pdfReaderParamsLiveData.observe(this, pdfParamsObserver)
val slug = intent.getStringExtra("LINKED_ITEM_SLUG") ?: ""
viewModel.loadItem(slug, this)
}
private fun load(params: PDFReaderParams) {
@ -56,24 +57,30 @@ class PDFReaderActivity: AppCompatActivity(), DocumentListener {
}
}
@UiThread
override fun onDocumentLoaded(document: PdfDocument) {
if (hasLoadedHighlights) return
hasLoadedHighlights = true
val params = viewModel.pdfReaderParamsLiveData.value
params?.let {
for (highlight in it.articleContent.highlights) {
Log.d("anno", "adding highlight: ${highlight.patch}")
val highlightAnnotation = fragment
.document
?.annotationProvider
?.createAnnotationFromInstantJson(highlight.patch)
val highlightAnnotation = highlight.asHighlightAnnotation()
Log.d("anno", "created highlight annotation: $highlightAnnotation")
fragment.addAnnotationToPage(highlightAnnotation, true)
highlightAnnotation?.let {
fragment.addAnnotationToPage(highlightAnnotation, true)
}
}
fragment.scrollTo(
RectF(0f, 0f, 0f, 0f),
params.item.readingProgressAnchor,
0,
true
)
}
}

View file

@ -46,8 +46,6 @@ class PDFReaderViewModel @Inject constructor(
val job = DownloadJob.startDownload(request)
Log.d("anno", "highlights: ${articleQueryResult.highlights}")
job.setProgressListener(object : DownloadJob.ProgressListenerAdapter() {
override fun onProgress(progress: Progress) {
// progressBar.setProgress((100f * progress.bytesReceived / progress.totalBytes).toInt())
@ -63,7 +61,7 @@ class PDFReaderViewModel @Inject constructor(
labelsJSONString = Gson().toJson(articleQueryResult.labels)
)
pdfReaderParamsLiveData.value = PDFReaderParams(article, articleContent, Uri.fromFile(output))
pdfReaderParamsLiveData.postValue(PDFReaderParams(article, articleContent, Uri.fromFile(output)))
}
override fun onError(exception: Throwable) {
@ -74,6 +72,6 @@ class PDFReaderViewModel @Inject constructor(
}
fun reset() {
pdfReaderParamsLiveData.value = null
pdfReaderParamsLiveData.postValue(null)
}
}