Make first character capitalized

This commit is contained in:
FirephoenixX02 2024-04-11 16:29:28 +02:00
parent d0243477fa
commit 699a60dffe

View file

@ -36,6 +36,7 @@ import pokedex.composeapp.generated.resources.Res
import pokedex.composeapp.generated.resources.*; import pokedex.composeapp.generated.resources.*;
import java.net.URL import java.net.URL
import java.util.Locale
@OptIn(ExperimentalResourceApi::class) @OptIn(ExperimentalResourceApi::class)
@Composable @Composable
@ -71,9 +72,9 @@ fun App() {
if (!dataLoaded) { if (!dataLoaded) {
// Load data asynchronously // Load data asynchronously
LaunchedEffect(Unit) { LaunchedEffect(Unit) {
pokemap = withContext(Dispatchers.IO) { pokemap = withContext(Dispatchers.IO) {
loadPokemonData(apiString) loadPokemonData(apiString, 1, 10)
} as ArrayList<Pokemon> } as ArrayList<Pokemon>
pokemap.forEach { pokemon -> pokemap.forEach { pokemon ->
println(pokemon) println(pokemon)
@ -81,6 +82,7 @@ fun App() {
dataLoaded = true dataLoaded = true
} }
//Show Loading Circle //Show Loading Circle
Box(modifier = Modifier.background(color = orange).fillMaxSize()) { Box(modifier = Modifier.background(color = orange).fillMaxSize()) {
Box(modifier = Modifier.fillMaxSize(), contentAlignment = Alignment.Center) { Box(modifier = Modifier.fillMaxSize(), contentAlignment = Alignment.Center) {
@ -155,17 +157,19 @@ fun App() {
} }
// Function to load Pokemon data asynchronously // Function to load Pokemon data asynchronously
suspend fun loadPokemonData(apiString: String): List<Pokemon> { suspend fun loadPokemonData(apiString: String, startId: Int, endId: Int): List<Pokemon> {
val pokemap = ArrayList<Pokemon>() val pokemap = ArrayList<Pokemon>()
for (i in 1..10) { for (i in startId..endId) {
val json = JSONObject(URL(apiString + i).readText()); val json = JSONObject(URL(apiString + i).readText());
val sprites = json.optJSONObject("sprites") val sprites = json.optJSONObject("sprites")
val types = json.getJSONArray("types") val types = json.getJSONArray("types")
val firstType = types.optJSONObject(0) val firstType = types.optJSONObject(0)
val type: String = firstType.optJSONObject("type").optString("name") val type: String = firstType.optJSONObject("type").optString("name")
val name: String = json.optString("name")
.replaceFirstChar { if (it.isLowerCase()) it.titlecase(Locale.getDefault()) else it.toString() }
val pokemon = Pokemon( val pokemon = Pokemon(
json.optString("name"), URL(sprites.optString("front_default")), type, json.optInt("id") name, URL(sprites.optString("front_default")), type, json.optInt("id")
) )
pokemap.add(pokemon) pokemap.add(pokemon)