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 val presentations = mutableMapOf<Int, MyPresentation>()
private lateinit var pdfDocument: PdfDocument private lateinit var pdfDocument: PdfDocument
private val showPages = mutableListOf<Boolean>() private val showPages = mutableListOf<Boolean>()
private val pageGroups = mutableListOf<Int>()
private val defaultCachedFileName = "cached.pdf" private val defaultCachedFileName = "cached.pdf"
private fun setPagesPerLandscape(pages: Float) { private fun setPagesPerLandscape(pages: Float) {
@ -143,6 +144,7 @@ class MainActivity : AppCompatActivity() {
outState.putBoolean("renderAutoCrop", renderAutoCrop) outState.putBoolean("renderAutoCrop", renderAutoCrop)
outState.putFloat("pagesPerLandscape", pagesPerLandscape) outState.putFloat("pagesPerLandscape", pagesPerLandscape)
outState.putBooleanArray("showPages", showPages.toBooleanArray()) outState.putBooleanArray("showPages", showPages.toBooleanArray())
outState.putIntArray("pageGroups", pageGroups.toIntArray())
outState.putFloat("thumbnailScrollProgress", thumbnailScrollProgress) outState.putFloat("thumbnailScrollProgress", thumbnailScrollProgress)
} }
@ -180,15 +182,18 @@ class MainActivity : AppCompatActivity() {
pdfDocument.renderThumbnails(thumbnailWidth, thumbnailHeight) pdfDocument.renderThumbnails(thumbnailWidth, thumbnailHeight)
displayManager.getDisplays(DisplayManager.DISPLAY_CATEGORY_PRESENTATION).forEach(::makePresentationView) displayManager.getDisplays(DisplayManager.DISPLAY_CATEGORY_PRESENTATION).forEach(::makePresentationView)
val bitmaps = pdfDocument.bitmapThumbnails.toSortedMap().values.toList()
showPages.clear() showPages.clear()
bitmaps.forEach { _ -> showPages.add(true) } pageGroups.clear()
savedInstanceState?.getBooleanArray("showPages")?.forEachIndexed { i, show -> (0..<pdfDocument.numPages).forEach {
showPages[i] = show 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.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) { 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 autoCropSafetyMarginUpper = 2
private val autoCropSafetyMarginLower = 16 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 { init {
for (i in 0..<numPages) { for (i in 0..<numPages) {

View File

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

View File

@ -8,6 +8,7 @@ import androidx.lifecycle.ViewModel
class GalleryViewModel : ViewModel() { class GalleryViewModel : ViewModel() {
val pageThumbnails = MutableLiveData<List<Bitmap>>().apply { value = listOf() } val pageThumbnails = MutableLiveData<List<Bitmap>>().apply { value = listOf() }
val showPages = MutableLiveData<List<Boolean>>().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 thumbnailScrollProgress = 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 }

View File

@ -1,15 +1,23 @@
package com.lhw.pdf.ui.home package com.lhw.pdf.ui.home
import android.graphics.Bitmap
import android.os.Bundle import android.os.Bundle
import android.view.LayoutInflater import android.view.LayoutInflater
import android.view.View import android.view.View
import android.view.ViewGroup import android.view.ViewGroup
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView import android.widget.TextView
import androidx.core.view.children
import androidx.fragment.app.Fragment import androidx.fragment.app.Fragment
import androidx.fragment.app.activityViewModels
import androidx.lifecycle.ViewModelProvider import androidx.lifecycle.ViewModelProvider
import com.lhw.pdf.databinding.FragmentHomeBinding import com.lhw.pdf.databinding.FragmentHomeBinding
import com.lhw.pdf.ui.gallery.GalleryViewModel
class HomeFragment : Fragment() { class HomeFragment : Fragment() {
private val galleryViewModel: GalleryViewModel by activityViewModels()
private val thumbnailImageViews = mutableListOf<ImageView>()
private var _binding: FragmentHomeBinding? = null private var _binding: FragmentHomeBinding? = null
@ -22,16 +30,11 @@ class HomeFragment : Fragment() {
container: ViewGroup?, container: ViewGroup?,
savedInstanceState: Bundle? savedInstanceState: Bundle?
): View { ): View {
val homeViewModel =
ViewModelProvider(this)[HomeViewModel::class.java]
_binding = FragmentHomeBinding.inflate(inflater, container, false) _binding = FragmentHomeBinding.inflate(inflater, container, false)
val root: View = binding.root val root: View = binding.root
val textView: TextView = binding.textHome galleryViewModel.pageThumbnails.observe(viewLifecycleOwner, ::populateThumbnails)
homeViewModel.text.observe(viewLifecycleOwner) {
textView.text = it
}
return root return root
} }
@ -39,4 +42,48 @@ class HomeFragment : Fragment() {
super.onDestroyView() super.onDestroyView()
_binding = null _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" android:layout_height="match_parent"
tools:context=".ui.home.HomeFragment"> tools:context=".ui.home.HomeFragment">
<TextView <ScrollView
android:id="@+id/text_home" android:id="@+id/thumbnails_scroll"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="match_parent"
android:layout_marginStart="8dp" android:layout_weight="3">
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp" <LinearLayout
android:textAlignment="center" android:id="@+id/thumbnails_layout"
android:textSize="20sp" android:layout_width="wrap_content"
app:layout_constraintBottom_toBottomOf="parent" android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent" android:orientation="vertical"
app:layout_constraintStart_toStartOf="parent" tools:showIn="@layout/content_main" />
app:layout_constraintTop_toTopOf="parent" /> </ScrollView>
</androidx.constraintlayout.widget.ConstraintLayout> </androidx.constraintlayout.widget.ConstraintLayout>