From 3a2b8453d50471b32cbfe732ecbe6283edd75514 Mon Sep 17 00:00:00 2001 From: Yair Morgenstern Date: Mon, 11 May 2020 20:20:24 +0300 Subject: [PATCH] Solved ANR when loading game to display its metadata --- core/src/com/unciv/ui/saves/LoadGameScreen.kt | 31 ++++++++++++------- 1 file changed, 19 insertions(+), 12 deletions(-) diff --git a/core/src/com/unciv/ui/saves/LoadGameScreen.kt b/core/src/com/unciv/ui/saves/LoadGameScreen.kt index e4932e7923..a44f5b5083 100644 --- a/core/src/com/unciv/ui/saves/LoadGameScreen.kt +++ b/core/src/com/unciv/ui/saves/LoadGameScreen.kt @@ -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() }