diff --git a/app/src/main/java/com/lhw/pdf/MainActivity.kt b/app/src/main/java/com/lhw/pdf/MainActivity.kt index 445e001..db4cef6 100644 --- a/app/src/main/java/com/lhw/pdf/MainActivity.kt +++ b/app/src/main/java/com/lhw/pdf/MainActivity.kt @@ -49,6 +49,7 @@ class MainActivity : AppCompatActivity() { private val presentations = mutableMapOf() private lateinit var pdfDocument: PdfDocument private val showPages = mutableListOf() + private val pageGroups = mutableListOf() private val defaultCachedFileName = "cached.pdf" private fun setPagesPerLandscape(pages: Float) { @@ -143,6 +144,7 @@ class MainActivity : AppCompatActivity() { outState.putBoolean("renderAutoCrop", renderAutoCrop) outState.putFloat("pagesPerLandscape", pagesPerLandscape) outState.putBooleanArray("showPages", showPages.toBooleanArray()) + outState.putIntArray("pageGroups", pageGroups.toIntArray()) outState.putFloat("thumbnailScrollProgress", thumbnailScrollProgress) } @@ -180,15 +182,18 @@ class MainActivity : AppCompatActivity() { pdfDocument.renderThumbnails(thumbnailWidth, thumbnailHeight) displayManager.getDisplays(DisplayManager.DISPLAY_CATEGORY_PRESENTATION).forEach(::makePresentationView) - - val bitmaps = pdfDocument.bitmapThumbnails.toSortedMap().values.toList() + showPages.clear() - bitmaps.forEach { _ -> showPages.add(true) } - savedInstanceState?.getBooleanArray("showPages")?.forEachIndexed { i, show -> - showPages[i] = show + pageGroups.clear() + (0.. showPages[i] = show } + savedInstanceState?.getIntArray("pageGroups")?.forEachIndexed { i, groupIdx -> pageGroups[i] = groupIdx } viewModel.showPages.value = showPages.toList() - viewModel.pageThumbnails.value = bitmaps + viewModel.pageGroups.value = pageGroups.toList() + viewModel.pageThumbnails.value = pdfDocument.bitmapThumbnails.toSortedMap().values.toList() viewModel.pagesPerLandscape.observe(this) { diff --git a/app/src/main/java/com/lhw/pdf/PdfDocument.kt b/app/src/main/java/com/lhw/pdf/PdfDocument.kt index 4c0342d..e42fa63 100644 --- a/app/src/main/java/com/lhw/pdf/PdfDocument.kt +++ b/app/src/main/java/com/lhw/pdf/PdfDocument.kt @@ -32,7 +32,7 @@ class PdfDocument(private val fileCached: File, var usePdfBox: Boolean = true, p private val autoCropSafetyMarginUpper = 2 private val autoCropSafetyMarginLower = 16 - private val numPages get() = if (usePdfBox) pdfDocument.numberOfPages else renderer.pageCount + val numPages get() = if (usePdfBox) pdfDocument.numberOfPages else renderer.pageCount init { for (i in 0..>().apply { value = listOf() } val showPages = MutableLiveData>().apply { value = listOf() } + val pageGroups = MutableLiveData>().apply { value = listOf() } val thumbnailScrollProgress = MutableLiveData().apply { value = 0F } val textDisplays = MutableLiveData().apply { value = "" } val pagesPerLandscape = MutableLiveData().apply { value = 3F } 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 fed8f43..1036c59 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,15 +1,23 @@ package com.lhw.pdf.ui.home +import android.graphics.Bitmap import android.os.Bundle import android.view.LayoutInflater import android.view.View import android.view.ViewGroup +import android.widget.ImageView +import android.widget.LinearLayout import android.widget.TextView +import androidx.core.view.children import androidx.fragment.app.Fragment +import androidx.fragment.app.activityViewModels import androidx.lifecycle.ViewModelProvider import com.lhw.pdf.databinding.FragmentHomeBinding +import com.lhw.pdf.ui.gallery.GalleryViewModel class HomeFragment : Fragment() { + private val galleryViewModel: GalleryViewModel by activityViewModels() + private val thumbnailImageViews = mutableListOf() private var _binding: FragmentHomeBinding? = null @@ -22,16 +30,11 @@ class HomeFragment : Fragment() { container: ViewGroup?, savedInstanceState: Bundle? ): View { - val homeViewModel = - ViewModelProvider(this)[HomeViewModel::class.java] - _binding = FragmentHomeBinding.inflate(inflater, container, false) val root: View = binding.root - val textView: TextView = binding.textHome - homeViewModel.text.observe(viewLifecycleOwner) { - textView.text = it - } + galleryViewModel.pageThumbnails.observe(viewLifecycleOwner, ::populateThumbnails) + return root } @@ -39,4 +42,48 @@ class HomeFragment : Fragment() { super.onDestroyView() _binding = null } + + private fun reLayoutThumbnails() { + val container = binding.thumbnailsLayout + container.children.forEach { (it as LinearLayout).removeAllViewsInLayout() } + container.removeAllViewsInLayout() + val groupContainers = mutableMapOf() + galleryViewModel.pageGroups.value?.forEachIndexed {i, groupIdx -> + if (groupIdx !in groupContainers) { + val c = LinearLayout(context) + groupContainers[groupIdx] = c + container.addView(c) + } + groupContainers[groupIdx]?.addView(thumbnailImageViews[i]) + } + } + + private fun populateThumbnails(bitmaps: List) { + bitmaps.forEachIndexed() { index, bitmap -> + val img = ImageView(context) + img.setImageBitmap(bitmap) + if (index > 0) { + img.setOnClickListener { + val pageGroups = galleryViewModel.pageGroups.value!!.toMutableList() + val oldGroup = pageGroups[index] + if (oldGroup > pageGroups[index-1]) { // Merge with previous group + val newGroup = pageGroups[index-1] + pageGroups[index] = newGroup + pageGroups.forEachIndexed { i, value -> + if (value == oldGroup) pageGroups[i] = newGroup + } + } else { // Escape group into original number, taking trailing members with it + pageGroups[index] = index + pageGroups.forEachIndexed { i, value -> + if ((value == oldGroup) and (i > index)) pageGroups[i] = index + } + } + galleryViewModel.pageGroups.value = pageGroups.toList() + reLayoutThumbnails() + } + } + thumbnailImageViews.add(img) + } + reLayoutThumbnails() + } } \ No newline at end of file diff --git a/app/src/main/res/layout/fragment_home.xml b/app/src/main/res/layout/fragment_home.xml index f3d9b08..d5c6d63 100644 --- a/app/src/main/res/layout/fragment_home.xml +++ b/app/src/main/res/layout/fragment_home.xml @@ -6,17 +6,17 @@ android:layout_height="match_parent" tools:context=".ui.home.HomeFragment"> - + android:layout_height="match_parent" + android:layout_weight="3"> + + + \ No newline at end of file