sync highlight deletion from android

This commit is contained in:
Satindar Dhillon 2022-11-30 13:29:55 -08:00
parent ce56b06488
commit 46ba2f1710
7 changed files with 37 additions and 10 deletions

File diff suppressed because one or more lines are too long

View file

@ -13,7 +13,7 @@ import com.pspdfkit.annotations.HighlightAnnotation
data class CreateHighlightParams(
val shortId: String?,
val highlightID: String?,
val id: String?,
val quote: String?,
val patch: String?,
val articleId: String?,
@ -22,7 +22,7 @@ data class CreateHighlightParams(
fun asCreateHighlightInput() = CreateHighlightInput(
annotation = Optional.presentIfNotNull(`annotation`),
articleId = articleId ?: "",
id = highlightID ?: "",
id = id ?: "",
patch = patch ?: "",
quote = quote ?: "",
shortId = shortId ?: ""
@ -31,7 +31,7 @@ data class CreateHighlightParams(
data class MergeHighlightsParams(
val shortId: String?,
val highlightID: String?,
val id: String?,
val quote: String?,
val patch: String?,
val articleId: String?,
@ -44,7 +44,7 @@ data class MergeHighlightsParams(
annotation = Optional.presentIfNotNull(`annotation`),
prefix = Optional.presentIfNotNull(prefix),
articleId = articleId ?: "",
id = highlightID ?: "",
id = id ?: "",
patch = patch ?: "",
quote = quote ?: "",
shortId = shortId ?: "",
@ -52,6 +52,17 @@ data class MergeHighlightsParams(
)
}
data class DeleteHighlightParams(
val highlightId: String?
) {
fun asIdList() = listOf(highlightId ?: "")
}
suspend fun Networker.deleteHighlight(jsonString: String): Boolean {
val input = Gson().fromJson(jsonString, DeleteHighlightParams::class.java).asIdList()
return deleteHighlights(input)
}
suspend fun Networker.deleteHighlights(highlightIDs: List<String>): Boolean {
val statuses: MutableList<Boolean> = mutableListOf()
for (highlightID in highlightIDs) {

View file

@ -58,7 +58,7 @@ data class WebReaderContent(
<meta charset="utf-8" />
<meta name='viewport' content='width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, user-scalable=no' />
<style>
@import url("highlightCssFilePath");
@import url("$highlightCssFilePath");
</style>
</head>
<body>

View file

@ -71,8 +71,11 @@ class WebReaderViewModel @Inject constructor(
}
}
"deleteHighlight" -> {
// { highlightId }
Log.d("Loggo", "receive delete highlight action: $jsonString")
viewModelScope.launch {
val isHighlightDeletionSynced = networker.deleteHighlight(jsonString)
Log.d("Network", "isHighlightDeletionSynced = $isHighlightDeletionSynced")
}
}
"updateHighlight" -> {
Log.d("Loggo", "receive update highlight action: $jsonString")
@ -111,6 +114,8 @@ class WebReaderViewModel @Inject constructor(
annotationLiveData.value = null
scrollState = ScrollState(0)
javascriptDispatchQueue = mutableListOf()
hasTappedExistingHighlight = false
lastTappedLocationRect = null
}
fun resetJavascriptDispatchQueue() {

File diff suppressed because one or more lines are too long

View file

@ -23,6 +23,7 @@ const mutation = async (name, input) => {
name,
JSON.stringify(input)
)
return true // TODO: see if we can get a result from Android instead
}
}

View file

@ -106,7 +106,11 @@ export function HighlightsLayer(props: HighlightsLayerProps): JSX.Element {
const removeHighlightCallback = useCallback(
async (id?: string) => {
const highlightId = id || focusedHighlight?.id
if (!highlightId) return
if (!highlightId) {
console.error('Failed to identify highlight to be removed')
return
}
const didDeleteHighlight =
await props.articleMutations.deleteHighlightMutation(highlightId)
@ -291,6 +295,8 @@ export function HighlightsLayer(props: HighlightsLayerProps): JSX.Element {
// FIXME: Apply note preview opening on the note icon click only
if (highlight) {
setFocusedHighlight(highlight)
// In the native app we post a message with the rect of the
// highlight, so the app can display a native menu
const rect = (target as Element).getBoundingClientRect()
@ -309,7 +315,6 @@ export function HighlightsLayer(props: HighlightsLayerProps): JSX.Element {
'existingHighlightTap',
JSON.stringify(message)
)
setFocusedHighlight(highlight)
}
} else if ((target as Element).hasAttribute(highlightNoteIdAttribute)) {
const id = (target as HTMLSpanElement).getAttribute(
@ -320,7 +325,9 @@ export function HighlightsLayer(props: HighlightsLayerProps): JSX.Element {
highlight: highlight,
highlightModalAction: 'addComment',
})
} else setFocusedHighlight(undefined)
} else {
setFocusedHighlight(undefined)
}
},
[highlights, highlightLocations]
)