From c64bc5f403b144c6b7a2d8790a3626c07d3f96c5 Mon Sep 17 00:00:00 2001 From: Luke Hubmayer-Werner Date: Sun, 1 Sep 2024 04:04:11 +0930 Subject: [PATCH] Refactor Presentation's view to be reusable --- .../main/java/com/lhw/pdf/MyPresentation.kt | 69 +------------- .../java/com/lhw/pdf/ui/HorizontalGallery.kt | 92 +++++++++++++++++++ 2 files changed, 97 insertions(+), 64 deletions(-) create mode 100644 app/src/main/java/com/lhw/pdf/ui/HorizontalGallery.kt diff --git a/app/src/main/java/com/lhw/pdf/MyPresentation.kt b/app/src/main/java/com/lhw/pdf/MyPresentation.kt index f1992bc..f964fca 100644 --- a/app/src/main/java/com/lhw/pdf/MyPresentation.kt +++ b/app/src/main/java/com/lhw/pdf/MyPresentation.kt @@ -6,11 +6,9 @@ import android.graphics.Bitmap import android.util.DisplayMetrics import android.view.Display import android.view.WindowManager -import android.widget.HorizontalScrollView -import android.widget.ImageView -import android.widget.LinearLayout import androidx.core.view.WindowCompat import androidx.core.view.WindowInsetsCompat +import com.lhw.pdf.ui.HorizontalGallery class MyPresentation(outerContext: Context, display: Display) : Presentation(outerContext, display) { val displayMetrics = DisplayMetrics() @@ -20,8 +18,7 @@ class MyPresentation(outerContext: Context, display: Display) : Presentation(out private val bitmaps = mutableListOf() private val bitmapCumWidths = mutableListOf(0) - private val layout = LinearLayout(this.context) - private val scroll = HorizontalScrollView(this.context) + private val scroll = HorizontalGallery(this.context) private var scrollProgress = 0f private val layoutParams = WindowManager.LayoutParams() init { @@ -31,75 +28,19 @@ class MyPresentation(outerContext: Context, display: Display) : Presentation(out WindowCompat.getInsetsController(it, it.decorView) .hide(WindowInsetsCompat.Type.systemBars()) } - scroll.addView(layout) println(layoutParams) addContentView(scroll, layoutParams) } fun updateImages(bitmaps: Iterable) { - println("Updating presentation images") - println("Layout has scale ${layout.scaleX} ${layout.scaleY}") - layout.removeAllViewsInLayout() - this.bitmaps.clear() - bitmapCumWidths.clear() - bitmapCumWidths.add(0) - bitmaps.forEach { - this.bitmaps.add(it) - bitmapCumWidths.add(it.width + bitmapCumWidths.last()) - } - if (lazyScroll) { - lazyIdxStart = -1 - lazyIdxEnd = -1 - layoutVisibleBitmaps() - } else { - layoutAllBitmaps() - } - } - - private fun layoutBitmap(bitmap: Bitmap) { - val img = ImageView(this.context) - img.setImageBitmap(bitmap) - img.minimumWidth = bitmap.width // No idea what broke when I refactored this class - img.minimumHeight = bitmap.height - println("${bitmap.width}x${bitmap.height}") - layout.addView(img) - } - - private fun layoutAllBitmaps() { - bitmaps.forEach(::layoutBitmap) - println("Presentation heights ${scroll.height} ${layout.height}") - } - - private fun layoutVisibleBitmaps() { - val xStart = (scrollProgress * (bitmapCumWidths.last() - scroll.width)).toInt() - val xEnd = xStart + scroll.width - // bitmapCumWidths has a prepended 0 - val idxStart = bitmapCumWidths.indexOfFirst { it > xStart }.let { if (it >= 1) it-1 else 0 } - val idxEnd = bitmapCumWidths.indexOfFirst { it > xEnd }.let { if (it >= 0) it else bitmaps.size} - - layout.removeAllViewsInLayout() - (idxStart..() + private val bitmapCumWidths = mutableListOf(0) + private val layout = LinearLayout(this.context) + private var scrollProgress = 0f + init { + addView(layout) + } + + fun updateImages(bitmaps: Iterable) { + println("Updating presentation images") + println("Layout has scale ${layout.scaleX} ${layout.scaleY}") + layout.removeAllViewsInLayout() + this.bitmaps.clear() + bitmapCumWidths.clear() + bitmapCumWidths.add(0) + bitmaps.forEach { + this.bitmaps.add(it) + bitmapCumWidths.add(it.width + bitmapCumWidths.last()) + } + if (lazyScroll) { + lazyIdxStart = -1 + lazyIdxEnd = -1 + layoutVisibleBitmaps() + } else { + layoutAllBitmaps() + } + } + + private fun layoutBitmap(bitmap: Bitmap) { + val img = ImageView(this.context) + img.setImageBitmap(bitmap) + img.minimumWidth = bitmap.width // No idea what broke when I refactored this class + img.minimumHeight = bitmap.height + println("${bitmap.width}x${bitmap.height}") + layout.addView(img) + } + + private fun layoutAllBitmaps() { + bitmaps.forEach(::layoutBitmap) + println("Presentation heights $height ${layout.height}") + } + + private fun layoutVisibleBitmaps() { + val xStart = (scrollProgress * (bitmapCumWidths.last() - width)).toInt() + val xEnd = xStart + width + // bitmapCumWidths has a prepended 0 + val idxStart = bitmapCumWidths.indexOfFirst { it > xStart }.let { if (it >= 1) it-1 else 0 } + val idxEnd = bitmapCumWidths.indexOfFirst { it > xEnd }.let { if (it >= 0) it else bitmaps.size} + + layout.removeAllViewsInLayout() + (idxStart..