implement note+highlight on pdf viewer in android

This commit is contained in:
Satindar Dhillon 2022-12-13 15:49:14 -08:00
parent 17edcc436a
commit 80e039dca4
3 changed files with 33 additions and 37 deletions

View file

@ -322,12 +322,13 @@ class PDFReaderActivity: AppCompatActivity(), DocumentListener, TextSelectionMan
p0.dismiss()
return@OnPopupToolbarItemClickedListener true
}
// 2 -> {
// Log.d("pdf", "user selected annotate action")
2 -> {
Log.d("pdf", "user selected annotate action")
showAnnotationView("")
// textSelectionController?.textSelection = null
// p0.dismiss()
// return@OnPopupToolbarItemClickedListener true
// }
p0.dismiss()
return@OnPopupToolbarItemClickedListener true
}
3 -> {
val text = textSelectionController?.textSelection?.text ?: ""
val clipboard = getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
@ -349,7 +350,7 @@ class PDFReaderActivity: AppCompatActivity(), DocumentListener, TextSelectionMan
p0.menuItems = listOf(
PopupToolbarMenuItem(1, R.string.pdf_highlight_menu_action),
// PopupToolbarMenuItem(2, R.string.annotate_menu_action),
PopupToolbarMenuItem(2, R.string.annotate_menu_action),
PopupToolbarMenuItem(3, R.string.pdf_highlight_copy),
)
}
@ -432,6 +433,7 @@ class PDFReaderActivity: AppCompatActivity(), DocumentListener, TextSelectionMan
actionMode = null
clickedHighlight = null
clickedHighlightPosition = null
textSelectionController?.textSelection = null
viewModel.annotationUnderNoteEdit = null
}
@ -443,8 +445,15 @@ class PDFReaderActivity: AppCompatActivity(), DocumentListener, TextSelectionMan
val annotationEditFragment = AnnotationEditFragment()
annotationEditFragment.configure(
onSave = { newNote ->
clickedHighlight?.let { highlight ->
viewModel.updateHighlightNote(highlight, newNote)
if (clickedHighlight != null) {
viewModel.updateHighlightNote(clickedHighlight!!, newNote)
} else {
pendingHighlightAnnotation?.let { annotation ->
val quote = textSelectionController?.textSelection?.text ?: ""
fragment.addAnnotationToPage(annotation, false) {
viewModel.syncHighlightUpdates(annotation, quote, listOf(), newNote)
}
}
}
resetHighlightTap()
},

View file

@ -101,7 +101,7 @@ class PDFReaderViewModel @Inject constructor(
}
}
fun syncHighlightUpdates(newAnnotation: Annotation, quote: String, overlapIds: List<String>) {
fun syncHighlightUpdates(newAnnotation: Annotation, quote: String, overlapIds: List<String>, note: String? = null) {
val itemID = pdfReaderParamsLiveData.value?.item?.id ?: return
val highlightID = UUID.randomUUID().toString()
val shortID = UUID.randomUUID().toString().replace("-","").substring(0,8)
@ -130,7 +130,7 @@ class PDFReaderViewModel @Inject constructor(
}
} else {
val createHighlightInput = CreateHighlightInput(
annotation = Optional.presentIfNotNull(null),
annotation = Optional.presentIfNotNull(note),
articleId = itemID,
id = highlightID,
patch = newAnnotation.toInstantJson(),
@ -141,17 +141,16 @@ class PDFReaderViewModel @Inject constructor(
viewModelScope.launch {
networker.createHighlight(createHighlightInput)
}
if (note != null) {
storeUpdatedNoteLocally(newAnnotation, note!!)
}
}
}
fun updateHighlightNote(annotation: Annotation, note: String) {
// Save the updated note locally
val omnivoreHighlight = annotation.customData?.get("omnivoreHighlight") as? JSONObject
omnivoreHighlight?.put("editedNote", note)
omnivoreHighlight?.let {
Log.d("pdf", "setting custom data: $omnivoreHighlight")
annotation.customData = JSONObject().put("omnivoreHighlight", it)
}
storeUpdatedNoteLocally(annotation, note)
// Sync update with data service
viewModelScope.launch {
@ -165,6 +164,15 @@ class PDFReaderViewModel @Inject constructor(
}
}
private fun storeUpdatedNoteLocally(annotation: Annotation, note: String) {
val omnivoreHighlight = annotation.customData?.get("omnivoreHighlight") as? JSONObject
omnivoreHighlight?.put("editedNote", note)
omnivoreHighlight?.let {
Log.d("pdf", "setting custom data: $omnivoreHighlight")
annotation.customData = JSONObject().put("omnivoreHighlight", it)
}
}
fun deleteHighlight(annotation: Annotation) {
val highlightID = pluckHighlightID(annotation) ?: return
viewModelScope.launch {

View file

@ -1,21 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/copyPdfHighlight"
android:title="@string/pdf_highlight_copy"
app:showAsAction="always">
</item>
<item
android:id="@+id/createHighlight"
android:title="@string/pdf_highlight_menu_action"
app:showAsAction="always">
</item>
<item
android:id="@+id/annotate"
android:title="@string/pdf_highlight_menu_note"
app:showAsAction="always">
</item>
</menu>