subclass webview to override text selection menu

This commit is contained in:
Satindar Dhillon 2022-09-21 13:20:38 -07:00
parent 38236e6199
commit e850de8aa4
5 changed files with 86 additions and 6 deletions

View file

@ -1,7 +1,9 @@
package app.omnivore.omnivore.ui.reader
import android.annotation.SuppressLint
import android.view.ViewGroup
import android.content.Context
import android.util.Log
import android.view.*
import android.webkit.WebView
import android.webkit.WebViewClient
import androidx.compose.material3.Text
@ -9,6 +11,7 @@ import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.livedata.observeAsState
import androidx.compose.ui.viewinterop.AndroidView
import app.omnivore.omnivore.R
@Composable
fun WebReaderLoadingContainer(slug: String, webReaderViewModel: WebReaderViewModel) {
@ -45,7 +48,7 @@ fun WebReader(params: WebReaderParams) {
val styledContent = webReaderContent.styledContent()
AndroidView(factory = {
WebView(it).apply {
OmnivoreWebView(it).apply {
layoutParams = ViewGroup.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT
@ -66,3 +69,65 @@ fun WebReader(params: WebReaderParams) {
it.loadDataWithBaseURL("file:///android_asset/", styledContent, "text/html; charset=utf-8", "utf-8", null);
})
}
class OmnivoreWebView(context: Context) : WebView(context) {
private val actionModeCallback = object : ActionMode.Callback {
// Called when the action mode is created; startActionMode() was called
override fun onCreateActionMode(mode: ActionMode, menu: Menu): Boolean {
val inflater: MenuInflater = mode.menuInflater
inflater.inflate(R.menu.text_selection_menu, menu)
return true
}
// Called each time the action mode is shown. Always called after onCreateActionMode, but
// may be called multiple times if the mode is invalidated.
override fun onPrepareActionMode(mode: ActionMode, menu: Menu): Boolean {
return false // Return false if nothing is done
}
// Called when the user selects a contextual menu item
override fun onActionItemClicked(mode: ActionMode, item: MenuItem): Boolean {
return when (item.itemId) {
R.id.annotate -> {
Log.d("Loggo", "Annotate action selected")
mode.finish()
true
}
R.id.highlight -> {
Log.d("Loggo", "Highlight action selected")
mode.finish()
true
}
else -> {
Log.d("Loggo", "${item.itemId} selected")
false
}
}
}
// Called when the user exits the action mode
override fun onDestroyActionMode(mode: ActionMode) {
// actionMode = null
}
}
private var currentActionModeCallback: ActionMode.Callback? = actionModeCallback
override fun startActionMode(callback: ActionMode.Callback?): ActionMode {
Log.d("Loggo", "startActionModeCalled")
return super.startActionMode(currentActionModeCallback)
}
override fun startActionModeForChild(
originalView: View?,
callback: ActionMode.Callback?
): ActionMode {
Log.d("Loggo", "startActionModeForChild")
return super.startActionModeForChild(originalView, currentActionModeCallback)
}
override fun startActionMode(callback: ActionMode.Callback?, type: Int): ActionMode {
Log.d("Loggo", "startActionMode:callback:type")
return super.startActionMode(currentActionModeCallback, type)
}
}

View file

@ -104,7 +104,7 @@ data class WebReaderContent(
</html>
"""
Log.d("Loggo", content)
Log.d("Loggos", content)
return content
}

View file

@ -68,7 +68,7 @@ class WebReaderViewModel @Inject constructor(
suffix = it.highlightFields.suffix,
patch = it.highlightFields.patch,
annotation = it.highlightFields.annotation,
createdAt = null,
createdAt = null, // TODO: update gql query to get this
updatedAt = it.highlightFields.updatedAt,
createdByMe = it.highlightFields.createdByMe,
)

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/highlight"
android:title="@string/highlight_menu_action"
app:showAsAction="always">
</item>
<item
android:id="@+id/annotate"
android:title="@string/annotate_menu_action"
app:showAsAction="always">
</item>
</menu>

View file

@ -1,8 +1,8 @@
<resources>
<string name="app_name">Omnivore</string>
<string name="gcp_id">590528454690-mpmbvcb4c3ifmnojmrultt37eo8f64at.apps.googleusercontent.com</string>
<!-- <string name="web_client_id">590528454690-g535h6sd708andjrvrmqgaj1sergtqi9.apps.googleusercontent.com</string>-->
<string name="welcome_title">Never miss a great read</string>
<string name="learn_more">Learn More</string>
<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>
</resources>