Page Groups are toggled hidden instead of Pages

This commit is contained in:
Luke Hubmayer-Werner 2024-08-27 18:40:33 +09:30
parent 34fda4ed34
commit 5c980880c6
6 changed files with 53 additions and 26 deletions

View File

@ -8,7 +8,8 @@ Minimum Viable Goals:
- - [x] Accept intents to load a single PDF
- - [ ] Built-in file browser with requisite permissions
- [ ] Delete+Reorder Pages - doesn't have to save the source file, but does need to save a change journal
- - [x] Tap thumbnails to hide/show pages
- - [x] Tap thumbnails in Home fragment to (un)group with previous
- - [x] Tap thumbnails in Gallery fragment to (un)hide page groups
- - [ ] Drag thumbnails to rearrange pages
- - [ ] Save metadata of these actions between sessions
- [ ] Text Annotations - as above

View File

@ -48,7 +48,7 @@ class MainActivity : AppCompatActivity() {
private var usePdfBox = false
private val presentations = mutableMapOf<Int, MyPresentation>()
private lateinit var pdfDocument: PdfDocument
private val showPages = mutableListOf<Boolean>()
private val hiddenPageGroups = mutableSetOf<Int>()
private val pageGroups = mutableListOf<Int>()
private val defaultCachedFileName = "cached.pdf"
@ -101,8 +101,9 @@ class MainActivity : AppCompatActivity() {
val maxWidth: Int = (p.displayMetrics.widthPixels / pagesPerLandscape).toInt() //in(maxHeight * 5 / 7, (p.displayMetrics.widthPixels / pagesPerLandscape).toInt())
updateBitmaps(maxWidth, maxHeight)
val key = Pair(maxWidth, maxHeight)
val bitmaps = showPages.withIndex()
.mapNotNull { (i, show) -> if (show) pdfDocument.bitmapPages[key]?.get(i) else null }
val bitmapPages = pdfDocument.bitmapPages[key]
val bitmaps = pageGroups.withIndex()
.mapNotNull { (i, group) -> if (group in hiddenPageGroups) null else bitmapPages?.get(i) }
.toList()
p.updateImages(bitmaps)
}
@ -143,7 +144,7 @@ class MainActivity : AppCompatActivity() {
outState.putString("fileName", "TODO")
outState.putBoolean("renderAutoCrop", renderAutoCrop)
outState.putFloat("pagesPerLandscape", pagesPerLandscape)
outState.putBooleanArray("showPages", showPages.toBooleanArray())
outState.putIntArray("hiddenPageGroups", hiddenPageGroups.toIntArray())
outState.putIntArray("pageGroups", pageGroups.toIntArray())
outState.putFloat("thumbnailScrollProgress", thumbnailScrollProgress)
}
@ -183,15 +184,14 @@ class MainActivity : AppCompatActivity() {
pdfDocument.renderThumbnails(thumbnailWidth, thumbnailHeight)
displayManager.getDisplays(DisplayManager.DISPLAY_CATEGORY_PRESENTATION).forEach(::makePresentationView)
showPages.clear()
hiddenPageGroups.clear()
pageGroups.clear()
(0..<pdfDocument.numPages).forEach {
showPages.add(true)
pageGroups.add(it)
}
savedInstanceState?.getBooleanArray("showPages")?.forEachIndexed { i, show -> showPages[i] = show }
(0..<pdfDocument.numPages).forEach { pageGroups.add(it) }
savedInstanceState?.getIntArray("hiddenPageGroups")?.forEach { hiddenPageGroups += it }
savedInstanceState?.getIntArray("pageGroups")?.forEachIndexed { i, groupIdx -> pageGroups[i] = groupIdx }
viewModel.showPages.value = showPages.toList()
println("Bundle debug: hiddenPageGroups = $hiddenPageGroups")
println("Bundle debug: pageGroups = $pageGroups")
viewModel.hiddenPageGroups.value = hiddenPageGroups.toSet()
viewModel.pageGroups.value = pageGroups.toList()
viewModel.pageThumbnails.value = pdfDocument.bitmapThumbnails.toSortedMap().values.toList()
@ -208,8 +208,14 @@ class MainActivity : AppCompatActivity() {
pdfDocument.usePdfBox = usePdfBox
updatePresentations()
}
viewModel.showPages.observe(this) {
it.forEachIndexed() {i, shown -> showPages[i] = shown}
viewModel.pageGroups.observe(this) {
pageGroups.clear()
pageGroups += it
updatePresentations(false)
}
viewModel.hiddenPageGroups.observe(this) {
hiddenPageGroups.clear()
hiddenPageGroups += it
updatePresentations(false)
}
viewModel.thumbnailScrollProgress.observe(this) { thumbnailScrollProgress ->

View File

@ -51,6 +51,7 @@ class GalleryFragment : Fragment() {
}
galleryViewModel.pageThumbnails.observe(viewLifecycleOwner, ::populateThumbnails)
galleryViewModel.pageGroups.observe(viewLifecycleOwner) { reLayoutThumbnails() }
val container = binding.thumbnailsLayout
val containerScroll = binding.thumbnailsScroll
@ -69,11 +70,14 @@ class GalleryFragment : Fragment() {
private fun reLayoutThumbnails() {
val container = binding.thumbnailsLayout
val disabledContainer = binding.thumbnailsDisabledLayout
val hiddenPageGroups = galleryViewModel.hiddenPageGroups.value!!
container.removeAllViewsInLayout()
disabledContainer.removeAllViewsInLayout()
galleryViewModel.showPages.value
?.map { if (it) container else disabledContainer }
?.zip(thumbnailImageViews)?.forEach { (cont, img) ->
thumbnailImageViews.forEach { (it.parent as? ViewGroup)?.removeView(it) }
galleryViewModel.pageGroups.value
?.map { group -> if (group in hiddenPageGroups) disabledContainer else container }
?.zip(thumbnailImageViews)
?.forEach { (cont, img) ->
cont.addView(img)
}
}
@ -83,9 +87,14 @@ class GalleryFragment : Fragment() {
val img = ImageView(context)
img.setImageBitmap(bitmap)
img.setOnClickListener {
val showPages = galleryViewModel.showPages.value!!.toMutableList()
showPages[index] = !showPages[index]
galleryViewModel.showPages.value = showPages.toList()
val hiddenPageGroups = galleryViewModel.hiddenPageGroups.value!!.toMutableSet()
val group = galleryViewModel.pageGroups.value!![index]
if (group in hiddenPageGroups) {
hiddenPageGroups -= group
} else {
hiddenPageGroups += group
}
galleryViewModel.hiddenPageGroups.value = hiddenPageGroups.toSet()
reLayoutThumbnails()
}
thumbnailImageViews.add(img)

View File

@ -7,7 +7,7 @@ import androidx.lifecycle.ViewModel
class GalleryViewModel : ViewModel() {
val pageThumbnails = MutableLiveData<List<Bitmap>>().apply { value = listOf() }
val showPages = MutableLiveData<List<Boolean>>().apply { value = listOf() }
val hiddenPageGroups = MutableLiveData<Set<Int>>().apply { value = setOf() }
val pageGroups = MutableLiveData<List<Int>>().apply { value = listOf() }
val thumbnailScrollProgress = MutableLiveData<Float>().apply { value = 0F }
val textDisplays = MutableLiveData<String>().apply { value = "" }

View File

@ -1,6 +1,7 @@
package com.lhw.pdf.ui.home
import android.graphics.Bitmap
import android.graphics.Color
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
@ -34,6 +35,8 @@ class HomeFragment : Fragment() {
val root: View = binding.root
galleryViewModel.pageThumbnails.observe(viewLifecycleOwner, ::populateThumbnails)
galleryViewModel.pageGroups.observe(viewLifecycleOwner) { reLayoutThumbnails() }
galleryViewModel.hiddenPageGroups.observe(viewLifecycleOwner) { reLayoutThumbnails() }
return root
}
@ -43,14 +46,22 @@ class HomeFragment : Fragment() {
_binding = null
}
private val groupContainers = mutableMapOf<Int, LinearLayout>()
private fun reLayoutThumbnails() {
val container = binding.thumbnailsLayout
container.children.forEach { (it as LinearLayout).removeAllViewsInLayout() }
thumbnailImageViews.forEach { (it.parent as? ViewGroup)?.removeView(it) }
groupContainers.values.forEach { it.removeAllViewsInLayout() }
container.removeAllViewsInLayout()
val groupContainers = mutableMapOf<Int, LinearLayout>()
groupContainers.clear()
galleryViewModel.pageGroups.value?.forEachIndexed {i, groupIdx ->
if (groupIdx !in groupContainers) {
val c = LinearLayout(context)
c.setPadding(2, 4, 2, 4)
galleryViewModel.hiddenPageGroups.value?.let {
if (groupIdx in it) {
c.setBackgroundColor(Color.RED)
}
}
groupContainers[groupIdx] = c
container.addView(c)
}
@ -62,6 +73,7 @@ class HomeFragment : Fragment() {
bitmaps.forEachIndexed() { index, bitmap ->
val img = ImageView(context)
img.setImageBitmap(bitmap)
img.setPadding(2, 0, 2, 0)
if (index > 0) {
img.setOnClickListener {
val pageGroups = galleryViewModel.pageGroups.value!!.toMutableList()
@ -79,7 +91,6 @@ class HomeFragment : Fragment() {
}
}
galleryViewModel.pageGroups.value = pageGroups.toList()
reLayoutThumbnails()
}
}
thumbnailImageViews.add(img)

View File

@ -14,7 +14,7 @@
<LinearLayout
android:id="@+id/thumbnails_layout"
android:layout_width="wrap_content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
tools:showIn="@layout/content_main" />