Resolved ANRs for slow phones when loading the construction list

This commit is contained in:
Yair Morgenstern 2021-01-15 15:12:14 +02:00
parent 3a729874c2
commit 6f8add77e8

View File

@ -1,5 +1,6 @@
package com.unciv.ui.cityscreen package com.unciv.ui.cityscreen
import com.badlogic.gdx.Gdx
import com.badlogic.gdx.graphics.Color import com.badlogic.gdx.graphics.Color
import com.badlogic.gdx.scenes.scene2d.Touchable import com.badlogic.gdx.scenes.scene2d.Touchable
import com.badlogic.gdx.scenes.scene2d.ui.Table import com.badlogic.gdx.scenes.scene2d.ui.Table
@ -16,6 +17,7 @@ import com.unciv.models.stats.Stat
import com.unciv.models.translations.tr import com.unciv.models.translations.tr
import com.unciv.ui.cityscreen.ConstructionInfoTable.Companion.turnOrTurns import com.unciv.ui.cityscreen.ConstructionInfoTable.Companion.turnOrTurns
import com.unciv.ui.utils.* import com.unciv.ui.utils.*
import kotlin.concurrent.thread
import com.unciv.ui.utils.AutoScrollPane as ScrollPane import com.unciv.ui.utils.AutoScrollPane as ScrollPane
class ConstructionsTable(val cityScreen: CityScreen) : Table(CameraStageBaseScreen.skin) { class ConstructionsTable(val cityScreen: CityScreen) : Table(CameraStageBaseScreen.skin) {
@ -54,17 +56,10 @@ class ConstructionsTable(val cityScreen: CityScreen) : Table(CameraStageBaseScre
} }
fun update(selectedConstruction: IConstruction?) { fun update(selectedConstruction: IConstruction?) {
updateButtons(selectedConstruction) updateButtons(selectedConstruction)
updateConstructionQueue() updateConstructionQueue()
pack() // Need to pack before computing space left for bottom panel
// Need to pack before computing space left for bottom panel
pack()
updateAvailableConstructions() updateAvailableConstructions()
pack() pack()
} }
@ -160,16 +155,21 @@ class ConstructionsTable(val cityScreen: CityScreen) : Table(CameraStageBaseScre
private fun updateAvailableConstructions() { private fun updateAvailableConstructions() {
val constrScrollY = availableConstructionsScrollPane.scrollY val constrScrollY = availableConstructionsScrollPane.scrollY
val usedHeight = showCityInfoTableButton.height + constructionsQueueScrollPane.height + buttons.height + 3f * pad + 10f
availableConstructionsTable.clear() if(!availableConstructionsTable.hasChildren()) { //
availableConstructionsTable.add("Loading...".toLabel()).pad(10f)
}
val units = ArrayList<Table>() val units = ArrayList<Table>()
val buildableWonders = ArrayList<Table>() val buildableWonders = ArrayList<Table>()
val buildableNationalWonders = ArrayList<Table>() val buildableNationalWonders = ArrayList<Table>()
val buildableBuildings = ArrayList<Table>() val buildableBuildings = ArrayList<Table>()
val specialConstructions = ArrayList<Table>() val specialConstructions = ArrayList<Table>()
val constructionButtonDTOList = getConstructionButtonDTOs() thread {
val constructionButtonDTOList = getConstructionButtonDTOs() // Since this can be a heavy operation and leads to many ANRs on older phones...
Gdx.app.postRunnable {
availableConstructionsTable.clear()
for (dto in constructionButtonDTOList) { for (dto in constructionButtonDTOList) {
val constructionButton = getConstructionButton(dto) val constructionButton = getConstructionButton(dto)
@ -196,7 +196,13 @@ class ConstructionsTable(val cityScreen: CityScreen) : Table(CameraStageBaseScre
availableConstructionsScrollPane.layout() availableConstructionsScrollPane.layout()
availableConstructionsScrollPane.scrollY = constrScrollY availableConstructionsScrollPane.scrollY = constrScrollY
availableConstructionsScrollPane.updateVisualScroll() availableConstructionsScrollPane.updateVisualScroll()
val usedHeight = showCityInfoTableButton.height + constructionsQueueScrollPane.height + buttons.height + 3f * pad + 10f
getCell(availableConstructionsScrollPane).maxHeight(stage.height - usedHeight) getCell(availableConstructionsScrollPane).maxHeight(stage.height - usedHeight)
pack()
setPosition(5f, stage.height - 5f, Align.topLeft)
}
}
} }
private fun getQueueEntry(constructionQueueIndex: Int, name: String): Table { private fun getQueueEntry(constructionQueueIndex: Int, name: String): Table {