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