Fix Loading Animation

This commit is contained in:
FirephoenixX02 2024-04-11 16:13:38 +02:00
parent f72938cffb
commit d0243477fa
2 changed files with 63 additions and 38 deletions

15
.gitignore vendored Normal file
View file

@ -0,0 +1,15 @@
/.gradle/config.properties
/.idea/vcs.xml
/gradle/
/local.properties
/.idea/.gitignore
/.gradle/8.4/checksums/checksums.lock
/.gradle/8.4/fileHashes/fileHashes.lock
/.idea/gradle.xml
/.idea/migrations.xml
/.idea/misc.xml
/.gradle/
/.idea/
/.fleet/
/composeApp/build/
build/reports/

View file

@ -27,6 +27,8 @@ import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.dp
import io.kamel.image.KamelImage import io.kamel.image.KamelImage
import io.kamel.image.asyncPainterResource import io.kamel.image.asyncPainterResource
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import org.jetbrains.compose.ui.tooling.preview.Preview import org.jetbrains.compose.ui.tooling.preview.Preview
import org.json.JSONObject import org.json.JSONObject
import org.jetbrains.compose.resources.ExperimentalResourceApi import org.jetbrains.compose.resources.ExperimentalResourceApi
@ -39,9 +41,9 @@ import java.net.URL
@Composable @Composable
@Preview @Preview
fun App() { fun App() {
val pokemap = ArrayList<Pokemon>() var pokemap = ArrayList<Pokemon>()
val apiString = "https://pokeapi.co/api/v2/pokemon/" val apiString = "https://pokeapi.co/api/v2/pokemon/"
var dataLoaded by mutableStateOf(false) var dataLoaded by remember { mutableStateOf(false) }
val orange = Color(0xFFffa500) val orange = Color(0xFFffa500)
val pokemonTypeDrawableMap = hashMapOf( val pokemonTypeDrawableMap = hashMapOf(
@ -66,13 +68,26 @@ fun App() {
) )
MaterialTheme { MaterialTheme {
Box(modifier = Modifier.background(color = orange).fillMaxSize()) { if (!dataLoaded) {
if (!dataLoaded) { // Load data asynchronously
println("Should show indicator") LaunchedEffect(Unit) {
//For some reason the application calls this code but does not diplay the ui if the launchedeffect is running pokemap = withContext(Dispatchers.IO) {
//This should not happen as launched-effect is an async co routine and non blocking loadPokemonData(apiString)
} as ArrayList<Pokemon>
pokemap.forEach { pokemon ->
println(pokemon)
}
dataLoaded = true
}
//Show Loading Circle
Box(modifier = Modifier.background(color = orange).fillMaxSize()) {
Box(modifier = Modifier.fillMaxSize(), contentAlignment = Alignment.Center) { Box(modifier = Modifier.fillMaxSize(), contentAlignment = Alignment.Center) {
CircularProgressIndicator(modifier = Modifier.size(128.dp), color = Color.White) CircularProgressIndicator(
modifier = Modifier.size(128.dp),
color = Color.White
)
Text( Text(
"Fetching data from API...", "Fetching data from API...",
color = Color.White, color = Color.White,
@ -80,7 +95,11 @@ fun App() {
modifier = Modifier.height(200.dp) modifier = Modifier.height(200.dp)
) )
} }
} else { }
} else {
println("Pokemon should show up any second!")
//Display Pokemon Grid
Box(modifier = Modifier.background(color = orange).fillMaxSize()) {
LazyVerticalGrid( LazyVerticalGrid(
columns = GridCells.Adaptive(minSize = 256.dp), columns = GridCells.Adaptive(minSize = 256.dp),
) { ) {
@ -116,8 +135,7 @@ fun App() {
Image( Image(
org.jetbrains.compose.resources.painterResource( org.jetbrains.compose.resources.painterResource(
pokemonTypeDrawableMap[type]!! pokemonTypeDrawableMap[type]!!
), ), contentDescription = null
contentDescription = null
) )
} }
Spacer(modifier = Modifier.height(8.dp)) Spacer(modifier = Modifier.height(8.dp))
@ -134,31 +152,23 @@ fun App() {
} }
} }
} }
//Blocks ui rendering thread? }
LaunchedEffect(Unit) {
//PokeDex API: https://pokeapi.co/api/v2/pokemon/ // Function to load Pokemon data asynchronously
for (i in 1..10) { suspend fun loadPokemonData(apiString: String): List<Pokemon> {
val json = JSONObject(URL(apiString + i).readText()); val pokemap = ArrayList<Pokemon>()
val sprites = json.optJSONObject("sprites") for (i in 1..10) {
val types = json.getJSONArray("types") val json = JSONObject(URL(apiString + i).readText());
val firstType = types.optJSONObject(0) val sprites = json.optJSONObject("sprites")
val type: String = firstType.optJSONObject("type").optString("name") val types = json.getJSONArray("types")
val firstType = types.optJSONObject(0)
val pokemon = Pokemon( val type: String = firstType.optJSONObject("type").optString("name")
json.optString("name"),
URL(sprites.optString("front_default")), val pokemon = Pokemon(
type, json.optString("name"), URL(sprites.optString("front_default")), type, json.optInt("id")
json.optInt("id") )
)
pokemap.add(pokemon)
println(pokemon) }
return pokemap
//println(type)
pokemap.add(
pokemon
)
}
dataLoaded = true;
}
} }