From c2eac14b3abd066bcec0397ba51aecbb4f6f4bc1 Mon Sep 17 00:00:00 2001 From: Yair Morgenstern Date: Thu, 30 Jan 2020 19:40:01 +0200 Subject: [PATCH] Solved ANR when waiting for the list of maps --- .../unciv/ui/mapeditor/MapDownloadPopup.kt | 68 +++++++++++-------- 1 file changed, 39 insertions(+), 29 deletions(-) diff --git a/core/src/com/unciv/ui/mapeditor/MapDownloadPopup.kt b/core/src/com/unciv/ui/mapeditor/MapDownloadPopup.kt index 6469f2a80a..3e019c8c96 100644 --- a/core/src/com/unciv/ui/mapeditor/MapDownloadPopup.kt +++ b/core/src/com/unciv/ui/mapeditor/MapDownloadPopup.kt @@ -14,42 +14,52 @@ import com.unciv.ui.worldscreen.mainmenu.DropBox import kotlin.concurrent.thread class MapDownloadPopup(loadMapScreen: LoadMapScreen): Popup(loadMapScreen) { + val contentTable = Table() init { - val folderList: DropBox.FolderList + thread(name="LoadMapList") { loadContent() } + add(contentTable).row() + addCloseButton() + } + + fun loadContent() { try { - folderList = DropBox().getFolderList("/Maps") - val scrollableMapTable = Table().apply { defaults().pad(10f) } - for (downloadableMap in folderList.entries) { - val downloadMapButton = TextButton(downloadableMap.name, CameraStageBaseScreen.skin) - downloadMapButton.onClick { - thread(name="MapDownload") { - try { - val mapJsonGzipped = DropBox().downloadFileAsString(downloadableMap.path_display) - val decodedMapJson = Gzip.unzip(mapJsonGzipped) - val mapObject = MapSaver().mapFromJson(decodedMapJson) - MapSaver().saveMap(downloadableMap.name, mapObject) - - // creating a screen is a GL task - Gdx.app.postRunnable { UncivGame.Current.setScreen(MapEditorScreen(mapObject)) } - } catch (ex: Exception) { - print(ex) - - // Yes, even creating popups. - Gdx.app.postRunnable { - val couldNotDownloadMapPopup = Popup(screen) - couldNotDownloadMapPopup.addGoodSizedLabel("Could not download map!").row() - couldNotDownloadMapPopup.addCloseButton() - couldNotDownloadMapPopup.open() - } - } + val folderList = DropBox().getFolderList("/Maps") + Gdx.app.postRunnable { + val scrollableMapTable = Table().apply { defaults().pad(10f) } + for (downloadableMap in folderList.entries) { + val downloadMapButton = TextButton(downloadableMap.name, CameraStageBaseScreen.skin) + downloadMapButton.onClick { + thread(name = "MapDownload") { loadMap(downloadableMap) } } + scrollableMapTable.add(downloadMapButton).row() } - scrollableMapTable.add(downloadMapButton).row() + contentTable.add(ScrollPane(scrollableMapTable)).height(screen.stage.height * 2 / 3).row() } - add(ScrollPane(scrollableMapTable)).height(screen.stage.height * 2 / 3).row() } catch (ex: Exception) { addGoodSizedLabel("Could not get list of maps!").row() } - addCloseButton() + } + + fun loadMap(downloadableMap: DropBox.FolderListEntry) { + + try { + val mapJsonGzipped = DropBox().downloadFileAsString(downloadableMap.path_display) + val decodedMapJson = Gzip.unzip(mapJsonGzipped) + val mapObject = MapSaver().mapFromJson(decodedMapJson) + MapSaver().saveMap(downloadableMap.name, mapObject) + + // creating a screen is a GL task + Gdx.app.postRunnable { UncivGame.Current.setScreen(MapEditorScreen(mapObject)) } + } catch (ex: Exception) { + print(ex) + + // Yes, even creating popups. + Gdx.app.postRunnable { + val couldNotDownloadMapPopup = Popup(screen) + couldNotDownloadMapPopup.addGoodSizedLabel("Could not download map!").row() + couldNotDownloadMapPopup.addCloseButton() + couldNotDownloadMapPopup.open() + } + } } }