Page groups mockup on Home fragment

This commit is contained in:
Luke Hubmayer-Werner 2024-08-27 17:09:54 +09:30
parent 53e3bd6229
commit 34fda4ed34
6 changed files with 79 additions and 28 deletions

View File

@ -49,6 +49,7 @@ class MainActivity : AppCompatActivity() {
private val presentations = mutableMapOf<Int, MyPresentation>()
private lateinit var pdfDocument: PdfDocument
private val showPages = mutableListOf<Boolean>()
private val pageGroups = mutableListOf<Int>()
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..<pdfDocument.numPages).forEach {
showPages.add(true)
pageGroups.add(it)
}
savedInstanceState?.getBooleanArray("showPages")?.forEachIndexed { i, show -> 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) {

View File

@ -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..<numPages) {

View File

@ -30,8 +30,6 @@ class GalleryFragment : Fragment() {
container: ViewGroup?,
savedInstanceState: Bundle?
): View {
// val galleryViewModel = ViewModelProvider(this)[GalleryViewModel::class.java]
_binding = FragmentGalleryBinding.inflate(inflater, container, false)
binding.viewModel = galleryViewModel
val root: View = binding.root

View File

@ -8,6 +8,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 pageGroups = MutableLiveData<List<Int>>().apply { value = listOf() }
val thumbnailScrollProgress = MutableLiveData<Float>().apply { value = 0F }
val textDisplays = MutableLiveData<String>().apply { value = "" }
val pagesPerLandscape = MutableLiveData<Float>().apply { value = 3F }

View File

@ -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<ImageView>()
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<Int, LinearLayout>()
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<Bitmap>) {
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()
}
}

View File

@ -6,17 +6,17 @@
android:layout_height="match_parent"
tools:context=".ui.home.HomeFragment">
<TextView
android:id="@+id/text_home"
<ScrollView
android:id="@+id/thumbnails_scroll"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:textAlignment="center"
android:textSize="20sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
android:layout_height="match_parent"
android:layout_weight="3">
<LinearLayout
android:id="@+id/thumbnails_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
tools:showIn="@layout/content_main" />
</ScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>