From 4826ed0355e5ae0fa5936c6a7439accd695c44f3 Mon Sep 17 00:00:00 2001 From: davidoskky Date: Wed, 2 Feb 2022 09:04:26 +0100 Subject: [PATCH] Scroll articles using the volume keys (#401) --- CHANGELOG.md | 2 ++ .../bou/readerforselfoss/ReaderActivity.kt | 20 ++++++++++++++++++- .../fragments/ArticleFragment.kt | 10 ++++++++++ 3 files changed, 31 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 06602aa..a87b03b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -48,6 +48,8 @@ - Dropped support for android 4, the last version supporting it is v1721030811 +- Added ability to scroll articles up and down using the volume keys #400 + **1.6.x** - Handling hidden tags. diff --git a/app/src/main/java/apps/amine/bou/readerforselfoss/ReaderActivity.kt b/app/src/main/java/apps/amine/bou/readerforselfoss/ReaderActivity.kt index 6982409..b68dce5 100644 --- a/app/src/main/java/apps/amine/bou/readerforselfoss/ReaderActivity.kt +++ b/app/src/main/java/apps/amine/bou/readerforselfoss/ReaderActivity.kt @@ -4,6 +4,7 @@ import android.content.Context import android.content.SharedPreferences import android.graphics.Color import android.os.Bundle +import android.view.KeyEvent import androidx.preference.PreferenceManager import androidx.appcompat.app.AppCompatActivity import android.view.Menu @@ -135,13 +136,30 @@ class ReaderActivity : AppCompatActivity() { private inner class ScreenSlidePagerAdapter(fa: FragmentActivity) : FragmentStateAdapter(fa) { - override fun getItemCount(): Int = allItems.size override fun createFragment(position: Int): Fragment = ArticleFragment.newInstance(allItems[position]) } + override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean { + return when (keyCode) { + KeyEvent.KEYCODE_VOLUME_DOWN -> { + val currentFragment = supportFragmentManager.findFragmentByTag("f" + binding.pager.currentItem) as ArticleFragment + currentFragment.scrollDown() + true + } + KeyEvent.KEYCODE_VOLUME_UP -> { + val currentFragment = supportFragmentManager.findFragmentByTag("f" + binding.pager.currentItem) as ArticleFragment + currentFragment.scrollUp() + true + } + else -> { + super.onKeyDown(keyCode, event) + } + } + } + private fun alignmentMenu(showJustify: Boolean) { toolbarMenu.findItem(R.id.align_left).isVisible = !showJustify toolbarMenu.findItem(R.id.align_justify).isVisible = showJustify diff --git a/app/src/main/java/apps/amine/bou/readerforselfoss/fragments/ArticleFragment.kt b/app/src/main/java/apps/amine/bou/readerforselfoss/fragments/ArticleFragment.kt index 6e84e23..ef011ea 100644 --- a/app/src/main/java/apps/amine/bou/readerforselfoss/fragments/ArticleFragment.kt +++ b/app/src/main/java/apps/amine/bou/readerforselfoss/fragments/ArticleFragment.kt @@ -511,6 +511,16 @@ class ArticleFragment : Fragment() { ) } + fun scrollDown() { + val height = binding.nestedScrollView.measuredHeight + binding.nestedScrollView.smoothScrollBy(0, height/2) + } + + fun scrollUp() { + val height = binding.nestedScrollView.measuredHeight + binding.nestedScrollView.smoothScrollBy(0, -height/2) + } + private fun openInBrowserAfterFailing(customTabsIntent: CustomTabsIntent) { binding.progressBar.visibility = View.GONE requireActivity().openItemUrlInternalBrowser(