Solved ANR when waiting for the list of maps

This commit is contained in:
Yair Morgenstern 2020-01-30 19:40:01 +02:00
parent 375c39c61e
commit c2eac14b3a

View File

@ -14,42 +14,52 @@ import com.unciv.ui.worldscreen.mainmenu.DropBox
import kotlin.concurrent.thread import kotlin.concurrent.thread
class MapDownloadPopup(loadMapScreen: LoadMapScreen): Popup(loadMapScreen) { class MapDownloadPopup(loadMapScreen: LoadMapScreen): Popup(loadMapScreen) {
val contentTable = Table()
init { init {
val folderList: DropBox.FolderList thread(name="LoadMapList") { loadContent() }
add(contentTable).row()
addCloseButton()
}
fun loadContent() {
try { try {
folderList = DropBox().getFolderList("/Maps") val folderList = DropBox().getFolderList("/Maps")
val scrollableMapTable = Table().apply { defaults().pad(10f) } Gdx.app.postRunnable {
for (downloadableMap in folderList.entries) { val scrollableMapTable = Table().apply { defaults().pad(10f) }
val downloadMapButton = TextButton(downloadableMap.name, CameraStageBaseScreen.skin) for (downloadableMap in folderList.entries) {
downloadMapButton.onClick { val downloadMapButton = TextButton(downloadableMap.name, CameraStageBaseScreen.skin)
thread(name="MapDownload") { downloadMapButton.onClick {
try { thread(name = "MapDownload") { loadMap(downloadableMap) }
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()
}
}
} }
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) { } catch (ex: Exception) {
addGoodSizedLabel("Could not get list of maps!").row() 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()
}
}
} }
} }