From 03f981d3d869f845e8c2645eb864196aef62b65c Mon Sep 17 00:00:00 2001 From: Luke Hubmayer-Werner Date: Sun, 1 Sep 2024 03:45:22 +0930 Subject: [PATCH] Long-click in home or gallery to toggle hide group. Add keyboard dpad page turning for bluetooth pedals. --- app/src/main/java/com/lhw/pdf/MainActivity.kt | 40 +++++++++++++++++-- .../main/java/com/lhw/pdf/MyPresentation.kt | 8 ++++ .../com/lhw/pdf/ui/gallery/GalleryFragment.kt | 5 +++ .../lhw/pdf/ui/gallery/GalleryViewModel.kt | 1 + .../java/com/lhw/pdf/ui/home/HomeFragment.kt | 16 ++++++++ 5 files changed, 66 insertions(+), 4 deletions(-) diff --git a/app/src/main/java/com/lhw/pdf/MainActivity.kt b/app/src/main/java/com/lhw/pdf/MainActivity.kt index 634469f..2a76716 100644 --- a/app/src/main/java/com/lhw/pdf/MainActivity.kt +++ b/app/src/main/java/com/lhw/pdf/MainActivity.kt @@ -8,6 +8,7 @@ import android.hardware.display.DisplayManager.DisplayListener import android.os.Bundle import android.util.DisplayMetrics import android.view.Display +import android.view.KeyEvent import android.view.Menu import android.view.WindowManager import android.widget.ImageView @@ -45,11 +46,17 @@ class MainActivity : AppCompatActivity() { private var renderAutoCrop = true private var pagesPerLandscape = 3F private var thumbnailScrollProgress = 0F + private var scrollNumPagesProgress: Float + get() = viewModel.scrollNumPagesProgress.value ?: 0F + set(f) { viewModel.scrollNumPagesProgress.value = f } private var usePdfBox = false private val presentations = mutableMapOf() private lateinit var pdfDocument: PdfDocument private val hiddenPageGroups = mutableSetOf() private val pageGroups = mutableListOf() + private var enabledPageIds: List + get() = viewModel.enabledPageIds.value ?: listOf() + set(ids) { viewModel.enabledPageIds.value = ids } private val defaultCachedFileName = "cached.pdf" private fun setPagesPerLandscape(pages: Float) { @@ -94,12 +101,12 @@ class MainActivity : AppCompatActivity() { updateBitmaps(maxWidth, maxHeight) val key = Pair(maxWidth, maxHeight) val bitmapPages = pdfDocument.bitmapPages[key] - val bitmaps = viewModel.enabledPageIds.value?.mapNotNull { bitmapPages?.get(it) }?.toList() ?: listOf() + val bitmaps = enabledPageIds.mapNotNull { bitmapPages?.get(it) }.toList() p.updateImages(bitmaps) } private fun updatePresentations(reRender: Boolean = true) { - viewModel.enabledPageIds.value = pageGroups.mapIndexedNotNull { i, group -> if (group !in hiddenPageGroups) i else null }.toList() + enabledPageIds = pageGroups.mapIndexedNotNull { i, group -> if (group !in hiddenPageGroups) i else null }.toList() if (reRender) pdfDocument.invalidateCache() presentations.values.forEach(::updatePresentationImages) @@ -187,7 +194,6 @@ class MainActivity : AppCompatActivity() { viewModel.pageGroups.value = pageGroups.toList() viewModel.pageThumbnails.value = pdfDocument.bitmapThumbnails.toSortedMap().values.toList() - viewModel.pagesPerLandscape.observe(this) { setPagesPerLandscape(it) } @@ -211,7 +217,10 @@ class MainActivity : AppCompatActivity() { updatePresentations(false) } viewModel.thumbnailScrollProgress.observe(this) { thumbnailScrollProgress -> - presentations.values.forEach {it.setScrollProgress(thumbnailScrollProgress)} + presentations.values.forEach { it.setScrollProgress(thumbnailScrollProgress) } + } + viewModel.scrollNumPagesProgress.observe(this) { scrollNumPagesProgress -> + presentations.values.forEach { it.setScrollPagesProgress(scrollNumPagesProgress) } } val drawerLayout: DrawerLayout = binding.drawerLayout @@ -255,4 +264,27 @@ class MainActivity : AppCompatActivity() { val navController = findNavController(R.id.nav_host_fragment_content_main) return navController.navigateUp(appBarConfiguration) || super.onSupportNavigateUp() } + + override fun onKeyUp(keyCode: Int, event: KeyEvent): Boolean { + // Use the directional arrows to turn pages + return when (keyCode) { + KeyEvent.KEYCODE_DPAD_LEFT, KeyEvent.KEYCODE_DPAD_UP -> { + scrollNumPagesProgress = max(scrollNumPagesProgress - 1F, 0F) + true + } + KeyEvent.KEYCODE_DPAD_RIGHT, KeyEvent.KEYCODE_DPAD_DOWN -> { + scrollNumPagesProgress = min(scrollNumPagesProgress + 1F, enabledPageIds.size - 1F) + true + } + else -> super.onKeyUp(keyCode, event) + } + } + override fun onKeyDown(keyCode: Int, event: KeyEvent): Boolean { + // Block the directional arrows from interacting with other views + return when (keyCode) { + KeyEvent.KEYCODE_DPAD_LEFT, KeyEvent.KEYCODE_DPAD_UP -> { true } + KeyEvent.KEYCODE_DPAD_RIGHT, KeyEvent.KEYCODE_DPAD_DOWN -> { true } + else -> super.onKeyDown(keyCode, event) + } + } } \ No newline at end of file diff --git a/app/src/main/java/com/lhw/pdf/MyPresentation.kt b/app/src/main/java/com/lhw/pdf/MyPresentation.kt index 4fb0292..f1992bc 100644 --- a/app/src/main/java/com/lhw/pdf/MyPresentation.kt +++ b/app/src/main/java/com/lhw/pdf/MyPresentation.kt @@ -94,4 +94,12 @@ class MyPresentation(outerContext: Context, display: Display) : Presentation(out println("Scrolling view to x=$progressPx of scrollable ${layout.width} (visible width ${scroll.width})") scroll.scrollTo(progressPx, 0) } + + fun setScrollPagesProgress(pagesProgress: Float) { + // NB this is nonlinear with unequal page widths + val p = pagesProgress.toInt() + val progressPx = bitmapCumWidths[p] + (bitmaps[p].width * (pagesProgress % 1F)) + val progress = progressPx / (bitmapCumWidths.last() - scroll.width).toFloat() + setScrollProgress(progress) + } } diff --git a/app/src/main/java/com/lhw/pdf/ui/gallery/GalleryFragment.kt b/app/src/main/java/com/lhw/pdf/ui/gallery/GalleryFragment.kt index e5d131f..ed54406 100644 --- a/app/src/main/java/com/lhw/pdf/ui/gallery/GalleryFragment.kt +++ b/app/src/main/java/com/lhw/pdf/ui/gallery/GalleryFragment.kt @@ -87,6 +87,10 @@ class GalleryFragment : Fragment() { val img = ImageView(context) img.setImageBitmap(bitmap) img.setOnClickListener { + val idx = galleryViewModel.enabledPageIds.value?.indexOf(index) ?: 0 + galleryViewModel.scrollNumPagesProgress.value = idx.toFloat() + } + img.setOnLongClickListener { val hiddenPageGroups = galleryViewModel.hiddenPageGroups.value!!.toMutableSet() val group = galleryViewModel.pageGroups.value!![index] if (group in hiddenPageGroups) { @@ -96,6 +100,7 @@ class GalleryFragment : Fragment() { } galleryViewModel.hiddenPageGroups.value = hiddenPageGroups.toSet() reLayoutThumbnails() + true } thumbnailImageViews.add(img) } diff --git a/app/src/main/java/com/lhw/pdf/ui/gallery/GalleryViewModel.kt b/app/src/main/java/com/lhw/pdf/ui/gallery/GalleryViewModel.kt index 32963fd..b1dfb08 100644 --- a/app/src/main/java/com/lhw/pdf/ui/gallery/GalleryViewModel.kt +++ b/app/src/main/java/com/lhw/pdf/ui/gallery/GalleryViewModel.kt @@ -10,6 +10,7 @@ class GalleryViewModel : ViewModel() { val pageGroups = MutableLiveData>().apply { value = listOf() } val enabledPageIds = MutableLiveData>().apply { value = listOf() } val thumbnailScrollProgress = MutableLiveData().apply { value = 0F } + val scrollNumPagesProgress = MutableLiveData().apply { value = 0F } val textDisplays = MutableLiveData().apply { value = "" } val pagesPerLandscape = MutableLiveData().apply { value = 3F } val renderAutoCrop = MutableLiveData().apply { value = true } diff --git a/app/src/main/java/com/lhw/pdf/ui/home/HomeFragment.kt b/app/src/main/java/com/lhw/pdf/ui/home/HomeFragment.kt index b759112..83431a8 100644 --- a/app/src/main/java/com/lhw/pdf/ui/home/HomeFragment.kt +++ b/app/src/main/java/com/lhw/pdf/ui/home/HomeFragment.kt @@ -46,6 +46,16 @@ class HomeFragment : Fragment() { _binding = null } + private fun toggleGroupHide(group: Int) { + val hiddenPageGroups = galleryViewModel.hiddenPageGroups.value!!.toMutableSet() + if (group in hiddenPageGroups) { + hiddenPageGroups -= group + } else { + hiddenPageGroups += group + } + galleryViewModel.hiddenPageGroups.value = hiddenPageGroups.toSet() + } + private val groupContainers = mutableMapOf() private fun reLayoutThumbnails() { val container = binding.thumbnailsLayout @@ -62,6 +72,7 @@ class HomeFragment : Fragment() { c.setBackgroundColor(Color.RED) } } + c.setOnLongClickListener { toggleGroupHide(groupIdx); true } groupContainers[groupIdx] = c container.addView(c) } @@ -74,6 +85,11 @@ class HomeFragment : Fragment() { val img = ImageView(context) img.setImageBitmap(bitmap) img.setPadding(2, 0, 2, 0) + img.setOnLongClickListener { + val group = galleryViewModel.pageGroups.value!![index] + toggleGroupHide(group) + true + } if (index > 0) { img.setOnClickListener { val pageGroups = galleryViewModel.pageGroups.value!!.toMutableList()