feat(android): scroll with volume buttons

closes: github issue #2936
This commit is contained in:
Avadhut Tanugade
2024-02-04 14:25:19 +05:30
parent dde80091b4
commit f59b944bd6

View File

@ -23,6 +23,7 @@ import androidx.compose.runtime.*
import androidx.compose.runtime.livedata.observeAsState
import androidx.compose.ui.viewinterop.AndroidView
import app.omnivore.omnivore.R
import app.omnivore.omnivore.ui.reader.OmnivoreWebView.Direction
import com.google.gson.Gson
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
@ -160,6 +161,24 @@ fun WebReader(
"utf-8",
null
)
requestFocus()
setOnKeyListener { _, keyCode, event ->
if (event.action == KeyEvent.ACTION_DOWN) {
when (keyCode) {
KeyEvent.KEYCODE_VOLUME_UP -> {
scrollVertically(Direction.UP)
return@setOnKeyListener true
}
KeyEvent.KEYCODE_VOLUME_DOWN -> {
scrollVertically(Direction.DOWN)
return@setOnKeyListener true
}
}
}
// default value
false
}
}
}, update = {
if (javascriptActionLoopUUID != webReaderViewModel.lastJavascriptActionLoopUUID) {
@ -194,6 +213,18 @@ class OmnivoreWebView(context: Context) : WebView(context), OnScrollChangeListen
setOnScrollChangeListener(this)
}
enum class Direction(val value: Int) {
UP(-1),
DOWN(1)
}
fun scrollVertically(direction: Direction, heightFactor: Int = 10) {
if (canScrollVertically(direction.value)) {
val scrollByValue = height.div(heightFactor)
scrollBy(0, direction.value.times(scrollByValue))
}
}
private val actionModeCallback = object : ActionMode.Callback2() {
// Called when the action mode is created; startActionMode() was called
override fun onCreateActionMode(mode: ActionMode, menu: Menu): Boolean {