Long-click in home or gallery to toggle hide group.

Add keyboard dpad page turning for bluetooth pedals.
This commit is contained in:
Luke Hubmayer-Werner 2024-09-01 03:45:22 +09:30
parent 033629bb11
commit 03f981d3d8
5 changed files with 66 additions and 4 deletions

View File

@ -8,6 +8,7 @@ import android.hardware.display.DisplayManager.DisplayListener
import android.os.Bundle import android.os.Bundle
import android.util.DisplayMetrics import android.util.DisplayMetrics
import android.view.Display import android.view.Display
import android.view.KeyEvent
import android.view.Menu import android.view.Menu
import android.view.WindowManager import android.view.WindowManager
import android.widget.ImageView import android.widget.ImageView
@ -45,11 +46,17 @@ class MainActivity : AppCompatActivity() {
private var renderAutoCrop = true private var renderAutoCrop = true
private var pagesPerLandscape = 3F private var pagesPerLandscape = 3F
private var thumbnailScrollProgress = 0F private var thumbnailScrollProgress = 0F
private var scrollNumPagesProgress: Float
get() = viewModel.scrollNumPagesProgress.value ?: 0F
set(f) { viewModel.scrollNumPagesProgress.value = f }
private var usePdfBox = false private var usePdfBox = false
private val presentations = mutableMapOf<Int, MyPresentation>() private val presentations = mutableMapOf<Int, MyPresentation>()
private lateinit var pdfDocument: PdfDocument private lateinit var pdfDocument: PdfDocument
private val hiddenPageGroups = mutableSetOf<Int>() private val hiddenPageGroups = mutableSetOf<Int>()
private val pageGroups = mutableListOf<Int>() private val pageGroups = mutableListOf<Int>()
private var enabledPageIds: List<Int>
get() = viewModel.enabledPageIds.value ?: listOf()
set(ids) { viewModel.enabledPageIds.value = ids }
private val defaultCachedFileName = "cached.pdf" private val defaultCachedFileName = "cached.pdf"
private fun setPagesPerLandscape(pages: Float) { private fun setPagesPerLandscape(pages: Float) {
@ -94,12 +101,12 @@ class MainActivity : AppCompatActivity() {
updateBitmaps(maxWidth, maxHeight) updateBitmaps(maxWidth, maxHeight)
val key = Pair(maxWidth, maxHeight) val key = Pair(maxWidth, maxHeight)
val bitmapPages = pdfDocument.bitmapPages[key] 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) p.updateImages(bitmaps)
} }
private fun updatePresentations(reRender: Boolean = true) { 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() if (reRender) pdfDocument.invalidateCache()
presentations.values.forEach(::updatePresentationImages) presentations.values.forEach(::updatePresentationImages)
@ -187,7 +194,6 @@ class MainActivity : AppCompatActivity() {
viewModel.pageGroups.value = pageGroups.toList() viewModel.pageGroups.value = pageGroups.toList()
viewModel.pageThumbnails.value = pdfDocument.bitmapThumbnails.toSortedMap().values.toList() viewModel.pageThumbnails.value = pdfDocument.bitmapThumbnails.toSortedMap().values.toList()
viewModel.pagesPerLandscape.observe(this) { viewModel.pagesPerLandscape.observe(this) {
setPagesPerLandscape(it) setPagesPerLandscape(it)
} }
@ -213,6 +219,9 @@ class MainActivity : AppCompatActivity() {
viewModel.thumbnailScrollProgress.observe(this) { thumbnailScrollProgress -> 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 val drawerLayout: DrawerLayout = binding.drawerLayout
val navView: NavigationView = binding.navView val navView: NavigationView = binding.navView
@ -255,4 +264,27 @@ class MainActivity : AppCompatActivity() {
val navController = findNavController(R.id.nav_host_fragment_content_main) val navController = findNavController(R.id.nav_host_fragment_content_main)
return navController.navigateUp(appBarConfiguration) || super.onSupportNavigateUp() 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)
}
}
} }

View File

@ -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})") println("Scrolling view to x=$progressPx of scrollable ${layout.width} (visible width ${scroll.width})")
scroll.scrollTo(progressPx, 0) 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)
}
} }

View File

@ -87,6 +87,10 @@ class GalleryFragment : Fragment() {
val img = ImageView(context) val img = ImageView(context)
img.setImageBitmap(bitmap) img.setImageBitmap(bitmap)
img.setOnClickListener { img.setOnClickListener {
val idx = galleryViewModel.enabledPageIds.value?.indexOf(index) ?: 0
galleryViewModel.scrollNumPagesProgress.value = idx.toFloat()
}
img.setOnLongClickListener {
val hiddenPageGroups = galleryViewModel.hiddenPageGroups.value!!.toMutableSet() val hiddenPageGroups = galleryViewModel.hiddenPageGroups.value!!.toMutableSet()
val group = galleryViewModel.pageGroups.value!![index] val group = galleryViewModel.pageGroups.value!![index]
if (group in hiddenPageGroups) { if (group in hiddenPageGroups) {
@ -96,6 +100,7 @@ class GalleryFragment : Fragment() {
} }
galleryViewModel.hiddenPageGroups.value = hiddenPageGroups.toSet() galleryViewModel.hiddenPageGroups.value = hiddenPageGroups.toSet()
reLayoutThumbnails() reLayoutThumbnails()
true
} }
thumbnailImageViews.add(img) thumbnailImageViews.add(img)
} }

View File

@ -10,6 +10,7 @@ class GalleryViewModel : ViewModel() {
val pageGroups = MutableLiveData<List<Int>>().apply { value = listOf() } val pageGroups = MutableLiveData<List<Int>>().apply { value = listOf() }
val enabledPageIds = MutableLiveData<List<Int>>().apply { value = listOf() } val enabledPageIds = MutableLiveData<List<Int>>().apply { value = listOf() }
val thumbnailScrollProgress = MutableLiveData<Float>().apply { value = 0F } val thumbnailScrollProgress = MutableLiveData<Float>().apply { value = 0F }
val scrollNumPagesProgress = MutableLiveData<Float>().apply { value = 0F }
val textDisplays = MutableLiveData<String>().apply { value = "" } val textDisplays = MutableLiveData<String>().apply { value = "" }
val pagesPerLandscape = MutableLiveData<Float>().apply { value = 3F } val pagesPerLandscape = MutableLiveData<Float>().apply { value = 3F }
val renderAutoCrop = MutableLiveData<Boolean>().apply { value = true } val renderAutoCrop = MutableLiveData<Boolean>().apply { value = true }

View File

@ -46,6 +46,16 @@ class HomeFragment : Fragment() {
_binding = null _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<Int, LinearLayout>() private val groupContainers = mutableMapOf<Int, LinearLayout>()
private fun reLayoutThumbnails() { private fun reLayoutThumbnails() {
val container = binding.thumbnailsLayout val container = binding.thumbnailsLayout
@ -62,6 +72,7 @@ class HomeFragment : Fragment() {
c.setBackgroundColor(Color.RED) c.setBackgroundColor(Color.RED)
} }
} }
c.setOnLongClickListener { toggleGroupHide(groupIdx); true }
groupContainers[groupIdx] = c groupContainers[groupIdx] = c
container.addView(c) container.addView(c)
} }
@ -74,6 +85,11 @@ class HomeFragment : Fragment() {
val img = ImageView(context) val img = ImageView(context)
img.setImageBitmap(bitmap) img.setImageBitmap(bitmap)
img.setPadding(2, 0, 2, 0) img.setPadding(2, 0, 2, 0)
img.setOnLongClickListener {
val group = galleryViewModel.pageGroups.value!![index]
toggleGroupHide(group)
true
}
if (index > 0) { if (index > 0) {
img.setOnClickListener { img.setOnClickListener {
val pageGroups = galleryViewModel.pageGroups.value!!.toMutableList() val pageGroups = galleryViewModel.pageGroups.value!!.toMutableList()