create text selection menu for an existing highlight

This commit is contained in:
Satindar Dhillon
2022-09-28 13:51:15 -07:00
parent 4d889752f6
commit 8e6d4d387b
5 changed files with 84 additions and 10 deletions

View File

@ -0,0 +1,36 @@
package app.omnivore.omnivore.networking
import app.omnivore.omnivore.graphql.generated.SaveArticleReadingProgressMutation
import app.omnivore.omnivore.graphql.generated.type.SaveArticleReadingProgressInput
import android.util.Log
import com.google.gson.Gson
data class ReadingProgressParams(
val id: String?,
val readingProgressPercent: Double?,
val readingProgressAnchorIndex: Int?
) {
fun asSaveReadingProgressInput() = SaveArticleReadingProgressInput(
id = id ?: "",
readingProgressPercent = readingProgressPercent ?: 0.0,
readingProgressAnchorIndex = readingProgressAnchorIndex ?: 0
)
}
suspend fun Networker.updateReadingProgress(jsonString: String): Boolean {
val input = Gson().fromJson(jsonString, ReadingProgressParams::class.java).asSaveReadingProgressInput()
Log.d("Loggo", "created reading progress input: $input")
val result = authenticatedApolloClient()
.mutation(SaveArticleReadingProgressMutation(input))
.execute()
val articleID = result.data?.saveArticleReadingProgress?.onSaveArticleReadingProgressSuccess?.updatedArticle?.id
Log.d("Loggo", "updated article with id: $articleID")
return articleID != null
}

View File

@ -17,6 +17,8 @@ import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.viewinterop.AndroidView
import app.omnivore.omnivore.R
import app.omnivore.omnivore.networking.ReadingProgressParams
import com.google.gson.Gson
import org.json.JSONObject
@ -76,7 +78,17 @@ fun WebReader(params: WebReaderParams, webReaderViewModel: WebReaderViewModel) {
}
val javascriptInterface = AndroidWebKitMessenger { actionID, json ->
webReaderViewModel.handleIncomingWebMessage(actionID, json)
when (actionID) {
"existingHighlightTap" -> {
isExistingHighlightSelected = true
actionTapCoordinates = Gson().fromJson(json, ActionTapCoordinates::class.java)
Log.d("Loggo", "receive existing highlight tap action: $actionTapCoordinates")
startActionMode(null)
}
else -> {
webReaderViewModel.handleIncomingWebMessage(actionID, json)
}
}
}
addJavascriptInterface(javascriptInterface, "AndroidWebKitMessenger")
@ -112,10 +124,18 @@ fun WebReader(params: WebReaderParams, webReaderViewModel: WebReaderViewModel) {
}
class OmnivoreWebView(context: Context) : WebView(context) {
var isExistingHighlightSelected = false
var actionTapCoordinates: ActionTapCoordinates? = null
private val actionModeCallback = object : ActionMode.Callback2() {
// Called when the action mode is created; startActionMode() was called
override fun onCreateActionMode(mode: ActionMode, menu: Menu): Boolean {
mode.menuInflater.inflate(R.menu.text_selection_menu, menu)
if (isExistingHighlightSelected) {
mode.menuInflater.inflate(R.menu.highlight_selection_menu, menu)
isExistingHighlightSelected = false
} else {
mode.menuInflater.inflate(R.menu.text_selection_menu, menu)
}
return true
}
@ -158,21 +178,19 @@ class OmnivoreWebView(context: Context) : WebView(context) {
}
}
private var currentActionModeCallback: ActionMode.Callback? = actionModeCallback
override fun startActionMode(callback: ActionMode.Callback?): ActionMode {
return super.startActionMode(currentActionModeCallback)
return super.startActionMode(actionModeCallback)
}
override fun startActionModeForChild(
originalView: View?,
callback: ActionMode.Callback?
): ActionMode {
return super.startActionModeForChild(originalView, currentActionModeCallback)
return super.startActionModeForChild(originalView, actionModeCallback)
}
override fun startActionMode(callback: ActionMode.Callback?, type: Int): ActionMode {
return super.startActionMode(currentActionModeCallback, type)
return super.startActionMode(actionModeCallback, type)
}
}
@ -182,3 +200,10 @@ class AndroidWebKitMessenger(val messageHandler: (String, String) -> Unit) {
messageHandler(actionID, jsonString)
}
}
data class ActionTapCoordinates(
val rectX: Double,
val rectY: Double,
val rectWidth: Double,
val rectHeight: Double,
)

View File

@ -78,9 +78,6 @@ class WebReaderViewModel @Inject constructor(
annotationLiveData.value = annotation
}
}
"existingHighlightTap" -> {
Log.d("Loggo", "receive existing highlight tap action: $jsonString")
}
"shareHighlight" -> {
// unimplemented
}

View File

@ -0,0 +1,15 @@
<?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/delete"
android:title="@string/delete_highlight_menu_title"
app:showAsAction="always">
</item>
<item
android:id="@+id/annotate"
android:title="@string/annotate_menu_action"
app:showAsAction="always">
</item>
</menu>

View File

@ -5,4 +5,5 @@
<string name="welcome_subtitle">Save articles and read them later in our distraction-free reader.</string>
<string name="highlight_menu_action">Highlight</string>
<string name="annotate_menu_action">Annotate</string>
<string name="delete_highlight_menu_title">Delete</string>
</resources>