From 5c980880c61d38cac579a0036e8d9c303982feb8 Mon Sep 17 00:00:00 2001 From: Luke Hubmayer-Werner Date: Tue, 27 Aug 2024 18:40:33 +0930 Subject: [PATCH] Page Groups are toggled hidden instead of Pages --- README.md | 3 +- app/src/main/java/com/lhw/pdf/MainActivity.kt | 34 +++++++++++-------- .../com/lhw/pdf/ui/gallery/GalleryFragment.kt | 21 ++++++++---- .../lhw/pdf/ui/gallery/GalleryViewModel.kt | 2 +- .../java/com/lhw/pdf/ui/home/HomeFragment.kt | 17 ++++++++-- app/src/main/res/layout/fragment_home.xml | 2 +- 6 files changed, 53 insertions(+), 26 deletions(-) diff --git a/README.md b/README.md index 5a94343..e1bc5ec 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/app/src/main/java/com/lhw/pdf/MainActivity.kt b/app/src/main/java/com/lhw/pdf/MainActivity.kt index db4cef6..ff56d7f 100644 --- a/app/src/main/java/com/lhw/pdf/MainActivity.kt +++ b/app/src/main/java/com/lhw/pdf/MainActivity.kt @@ -48,7 +48,7 @@ class MainActivity : AppCompatActivity() { private var usePdfBox = false private val presentations = mutableMapOf() private lateinit var pdfDocument: PdfDocument - private val showPages = mutableListOf() + private val hiddenPageGroups = mutableSetOf() private val pageGroups = mutableListOf() 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) } @@ -182,16 +183,15 @@ class MainActivity : AppCompatActivity() { pdfDocument.renderThumbnails(thumbnailWidth, thumbnailHeight) displayManager.getDisplays(DisplayManager.DISPLAY_CATEGORY_PRESENTATION).forEach(::makePresentationView) - - showPages.clear() + + hiddenPageGroups.clear() pageGroups.clear() - (0.. showPages[i] = show } + (0.. 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 -> 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 d08579a..e5d131f 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 @@ -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) 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 e628b07..1128741 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 @@ -7,7 +7,7 @@ import androidx.lifecycle.ViewModel class GalleryViewModel : ViewModel() { val pageThumbnails = MutableLiveData>().apply { value = listOf() } - val showPages = MutableLiveData>().apply { value = listOf() } + val hiddenPageGroups = MutableLiveData>().apply { value = setOf() } val pageGroups = MutableLiveData>().apply { value = listOf() } val thumbnailScrollProgress = MutableLiveData().apply { value = 0F } val textDisplays = MutableLiveData().apply { value = "" } 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 1036c59..b759112 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 @@ -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() 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() + 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) diff --git a/app/src/main/res/layout/fragment_home.xml b/app/src/main/res/layout/fragment_home.xml index d5c6d63..9addf8d 100644 --- a/app/src/main/res/layout/fragment_home.xml +++ b/app/src/main/res/layout/fragment_home.xml @@ -14,7 +14,7 @@