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
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()
}
}
}
}