Basic savedInstanceState bundle usage.

Restores hidden pages, zoom level, approximate scroll, autocrop, after Activity is soft-reset by things like Splitscreen toggling.
This commit is contained in:
Luke Hubmayer-Werner 2024-08-26 22:19:58 +09:30
parent cefa7ad82d
commit f24522d728
1 changed files with 32 additions and 9 deletions

View File

@ -41,10 +41,7 @@ class MainActivity : AppCompatActivity() {
private val thumbnailHeight = 700
private var renderAutoCrop = true
private var pagesPerLandscape = 3F
private set(pages) {
field = pages
updatePresentations()
}
private var thumbnailScrollProgress = 0F
private var usePdfBox = false
private val presentations = mutableMapOf<Int, MyPresentation>()
private lateinit var pdfDocument: PdfDocument
@ -52,6 +49,11 @@ class MainActivity : AppCompatActivity() {
private val thumbnailImageViews = mutableListOf<ImageView>()
private val defaultCachedFileName = "cached.pdf"
private fun setPagesPerLandscape(pages: Float) {
pagesPerLandscape = pages
updatePresentations()
}
private val displayListener = object: DisplayListener {
override fun onDisplayAdded(p0: Int) {
reevaluateDisplay(p0)
@ -93,7 +95,7 @@ class MainActivity : AppCompatActivity() {
private fun updatePresentationImages(p: MyPresentation) {
val maxHeight: Int = p.displayMetrics.heightPixels
val maxWidth: Int = min(maxHeight * 5 / 7, (p.displayMetrics.widthPixels / pagesPerLandscape).toInt())
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()
@ -128,6 +130,7 @@ class MainActivity : AppCompatActivity() {
}
private fun populateThumbnails() {
showPages.clear()
for ((index, bitmap) in pdfDocument.bitmapThumbnails) {
val img = ImageView(this)
img.setImageBitmap(bitmap)
@ -159,6 +162,16 @@ class MainActivity : AppCompatActivity() {
displayText.text = text.toString()
}
override fun onSaveInstanceState(outState: Bundle) {
super.onSaveInstanceState(outState)
// Store some things to restore
outState.putString("fileName", "TODO")
outState.putBoolean("renderAutoCrop", renderAutoCrop)
outState.putFloat("pagesPerLandscape", pagesPerLandscape)
outState.putBooleanArray("showPages", showPages.toBooleanArray())
outState.putFloat("thumbnailScrollProgress", thumbnailScrollProgress)
}
override fun onCreate(savedInstanceState: Bundle?) {
//enableEdgeToEdge()
super.onCreate(savedInstanceState)
@ -177,6 +190,7 @@ class MainActivity : AppCompatActivity() {
setSupportActionBar(binding.appBarMain.toolbar)
// Load previous pdf, or included test pdf first
val lastFileName = savedInstanceState?.getString("fileName") // TODO: proper filenames
var file = File(cacheDir, defaultCachedFileName)
if (!file.exists()) {
file = inputStreamToCache(defaultCachedFileName, resources.openRawResource(R.raw.testpdf))
@ -185,17 +199,26 @@ class MainActivity : AppCompatActivity() {
// Then overwrite it with any pdf sent via intent
handleIntent(intent)
renderAutoCrop = savedInstanceState?.getBoolean("renderAutoCrop") ?: true
pdfDocument.renderThumbnails(thumbnailWidth, thumbnailHeight)
displayManager.getDisplays(DisplayManager.DISPLAY_CATEGORY_PRESENTATION).forEach(::makePresentationView)
populateThumbnails()
populateThumbnails() // Populates showPages
savedInstanceState?.getBooleanArray("showPages")?.forEachIndexed { i, show ->
showPages[i] = show
}
reLayoutThumbnails()
val container = binding.appBarMain.contentMain.thumbnailsLayout
val containerScroll = binding.appBarMain.contentMain.thumbnailsScroll
containerScroll.viewTreeObserver.addOnScrollChangedListener {
val progress = containerScroll.scrollX.toFloat() / (container.width - containerScroll.width)
presentations.values.forEach {it.setScrollProgress(progress)}
thumbnailScrollProgress = containerScroll.scrollX.toFloat() / (container.width - containerScroll.width)
presentations.values.forEach {it.setScrollProgress(thumbnailScrollProgress)}
}
savedInstanceState?.getFloat("thumbnailScrollProgress")?.let { thumbnailScrollProgress = it }
containerScroll.scrollTo((thumbnailScrollProgress * (container.width - containerScroll.width)).toInt(), 0)
savedInstanceState?.getFloat("pagesPerLandscape")?.let { pagesPerLandscape = it }
val pageZoomLevels = arrayOf(1F, 1.5F, 2F, 2.5F, 3F, 3.5F, 4F, 5F, 6F, 8F, 10F)
val chipGroupPages = binding.appBarMain.contentMain.chipGroupPages
pageZoomLevels.forEach { pages ->
@ -203,7 +226,7 @@ class MainActivity : AppCompatActivity() {
chip.text = "$pages pages"
chip.isCheckable = true
chip.isChecked = (pages == pagesPerLandscape)
chip.setOnCheckedChangeListener { _, checked -> if (checked) pagesPerLandscape = pages }
chip.setOnCheckedChangeListener { _, checked -> if (checked) setPagesPerLandscape(pages) }
chipGroupPages.addView(chip)
}
binding.appBarMain.contentMain.chipAutoCrop.setOnCheckedChangeListener { _, checked ->