Solved ANR when loading game to display its metadata

This commit is contained in:
Yair Morgenstern 2020-05-11 20:20:24 +03:00
parent c756014fec
commit 3a2b8453d5

View File

@ -13,6 +13,7 @@ import com.unciv.ui.pickerscreens.PickerScreen
import com.unciv.ui.utils.*
import java.text.SimpleDateFormat
import java.util.*
import kotlin.concurrent.thread
import com.unciv.ui.utils.AutoScrollPane as ScrollPane
class LoadGameScreen(previousScreen:CameraStageBaseScreen) : PickerScreen() {
@ -122,20 +123,26 @@ class LoadGameScreen(previousScreen:CameraStageBaseScreen) : PickerScreen() {
var textToSet = save
val savedAt = Date(GameSaver.getSave(save).lastModified())
descriptionLabel.setText("Loading...".tr())
textToSet += "\n{Saved at}: ".tr() + SimpleDateFormat("yyyy-MM-dd HH:mm").format(savedAt)
try {
val game = GameSaver.loadGameByName(save)
val playerCivNames = game.civilizations.filter { it.isPlayerCivilization() }.joinToString { it.civName.tr() }
textToSet += "\n" + playerCivNames +
", " + game.difficulty.tr() + ", {Turn} ".tr() + game.turns
} catch (ex: Exception) {
textToSet += "\n{Could not load game}!".tr()
thread { // Even loading the game to get its metadata can take a long time on older phones
try {
val game = GameSaver.loadGameByName(save)
val playerCivNames = game.civilizations.filter { it.isPlayerCivilization() }.joinToString { it.civName.tr() }
textToSet += "\n" + playerCivNames +
", " + game.difficulty.tr() + ", {Turn} ".tr() + game.turns
} catch (ex: Exception) {
textToSet += "\n{Could not load game}!".tr()
}
Gdx.app.postRunnable {
descriptionLabel.setText(textToSet)
rightSideButton.setText("Load [$save]".tr())
rightSideButton.enable()
deleteSaveButton.enable()
deleteSaveButton.color = Color.RED
}
}
descriptionLabel.setText(textToSet)
rightSideButton.setText("Load [$save]".tr())
rightSideButton.enable()
deleteSaveButton.enable()
deleteSaveButton.color = Color.RED
}
saveTable.add(textButton).pad(5f).row()
}