Formatting

This commit is contained in:
FirephoenixX02 2024-04-17 13:11:02 +02:00
parent a9fb0a1e17
commit 76f7b84f5f
7 changed files with 68 additions and 91 deletions

View file

@ -86,78 +86,68 @@ class HomeScreen : Screen {
"fairy" "fairy"
) )
Column( Column(
modifier = Modifier modifier = Modifier.fillMaxSize().background(color = orange),
.fillMaxSize()
.background(color = orange),
horizontalAlignment = Alignment.CenterHorizontally horizontalAlignment = Alignment.CenterHorizontally
) { ) {
OutlinedTextField( OutlinedTextField(value = searchQuery,
value = searchQuery,
onValueChange = { newName -> searchQuery = newName; placeholder = "" }, onValueChange = { newName -> searchQuery = newName; placeholder = "" },
modifier = Modifier.fillMaxWidth(0.8f) modifier = Modifier.fillMaxWidth(0.8f).padding(horizontal = 16.dp)
.padding(horizontal = 16.dp) .padding(top = 16.dp).background(color = Color.White),
.padding(top = 16.dp)
.background(color = Color.White),
trailingIcon = { trailingIcon = {
Row() { Row() {
IconButton( IconButton(onClick = {
onClick = { if (searchQuery.length >= 3 && PokemonNames.names.any { name ->
if (searchQuery.length >= 3 && PokemonNames.names.any { name -> name.contains(
name.contains( searchQuery.lowercase(
searchQuery.lowercase( Locale.getDefault()
Locale.getDefault()
)
) )
}) { )
coroutineScope.launch { }) {
withContext(Dispatchers.IO) { coroutineScope.launch {
println("Searching for pokemon, query: $searchQuery") withContext(Dispatchers.IO) {
val names: List<String> = println("Searching for pokemon, query: $searchQuery")
PokemonNames.names.filter { name -> val names: List<String> =
name.contains( PokemonNames.names.filter { name ->
searchQuery.lowercase( name.contains(
Locale.getDefault() searchQuery.lowercase(
) Locale.getDefault()
) )
} )
var newMap = arrayListOf<Pokemon>()
names.forEach { name ->
loadPokemonDataFromName(name)?.let {
newMap.add(it)
}
} }
println("currentFilter: $currentFilter") var newMap = arrayListOf<Pokemon>()
if (!currentFilter.equals("all")) { names.forEach { name ->
newMap = newMap.filter { pokemon -> loadPokemonDataFromName(name)?.let {
pokemon.type == currentFilter newMap.add(it)
} as ArrayList<Pokemon>
}
println(names)
println(newMap)
if (newMap.isNotEmpty()) {
println("Updating map")
pokemap = newMap;
} else {
searchQuery = ""
placeholder =
"No results for this filter: $currentFilter"
} }
} }
println("currentFilter: $currentFilter")
if (!currentFilter.equals("all")) {
newMap = newMap.filter { pokemon ->
pokemon.type == currentFilter
} as ArrayList<Pokemon>
}
println(names)
println(newMap)
if (newMap.isNotEmpty()) {
println("Updating map")
pokemap = newMap;
} else {
searchQuery = ""
placeholder =
"No results for this filter: $currentFilter"
}
} }
} else {
searchQuery = ""
placeholder =
"No results, make sure to use more than 2 letters."
} }
} else {
searchQuery = ""
placeholder = "No results, make sure to use more than 2 letters."
} }
) { }) {
Icon(Icons.Default.Search, contentDescription = "Search") Icon(Icons.Default.Search, contentDescription = "Search")
} }
IconButton( IconButton(onClick = {
onClick = { isFilterOpen = !isFilterOpen
isFilterOpen = !isFilterOpen }) {
}
) {
Icon(Icons.Default.Settings, contentDescription = "Filter") Icon(Icons.Default.Settings, contentDescription = "Filter")
} }
} }
@ -170,8 +160,7 @@ class HomeScreen : Screen {
focusedLabelColor = orange, focusedLabelColor = orange,
unfocusedLabelColor = orange, unfocusedLabelColor = orange,
), ),
placeholder = { Text(placeholder) } placeholder = { Text(placeholder) })
)
Spacer(modifier = Modifier.height(16.dp)) Spacer(modifier = Modifier.height(16.dp))
//Grid //Grid
LazyVerticalGrid( LazyVerticalGrid(
@ -179,9 +168,8 @@ class HomeScreen : Screen {
) { ) {
items(pokemap.size) { index -> items(pokemap.size) { index ->
Box(modifier = Modifier.size(256.dp).padding(5.dp)) { Box(modifier = Modifier.size(256.dp).padding(5.dp)) {
Canvas( Canvas(modifier = Modifier.matchParentSize()
modifier = Modifier.matchParentSize() .clickable { navigator.push(DetailScreen(pokemap[index])) }) {
.clickable { navigator.push(DetailScreen(pokemap[index])) }) {
drawRoundRect( drawRoundRect(
color = Color.White, color = Color.White,
topLeft = Offset(0f, 0f), topLeft = Offset(0f, 0f),
@ -225,9 +213,7 @@ class HomeScreen : Screen {
} }
} }
} }
DropdownMenu( DropdownMenu(expanded = isFilterOpen, onDismissRequest = { isFilterOpen = false }) {
expanded = isFilterOpen,
onDismissRequest = { isFilterOpen = false }) {
filterOptions.forEach { option -> filterOptions.forEach { option ->
DropdownMenuItem(onClick = { currentFilter = option; isFilterOpen = false }) { DropdownMenuItem(onClick = { currentFilter = option; isFilterOpen = false }) {
Text(option) Text(option)

View file

@ -98,7 +98,8 @@ suspend fun loadPokemonData(startId: Int, endId: Int): List<Pokemon> {
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 type: String = val type: String =
json.getJSONArray("types").optJSONObject(0).optJSONObject("type")?.optString("name") ?: "null" json.getJSONArray("types").optJSONObject(0).optJSONObject("type")?.optString("name")
?: "null"
val name: String = json.optString("name") val name: String = json.optString("name")
.replaceFirstChar { if (it.isLowerCase()) it.titlecase(Locale.getDefault()) else it.toString() } .replaceFirstChar { if (it.isLowerCase()) it.titlecase(Locale.getDefault()) else it.toString() }
@ -117,14 +118,15 @@ suspend fun loadPokemonDataFromName(name: String): Pokemon? {
if (trimmedName == "") return null if (trimmedName == "") return null
var json: JSONObject var json: JSONObject
try { try {
json = JSONObject(URL(apiString + trimmedName).readText()); json = JSONObject(URL(apiString + trimmedName).readText());
} catch (e: Exception) { } catch (e: Exception) {
println(e) println(e)
return null return null
} }
val sprites = json.optJSONObject("sprites") val sprites = json.optJSONObject("sprites")
val type: String = val type: String =
json.getJSONArray("types").optJSONObject(0).optJSONObject("type")?.optString("name") ?: "null" json.getJSONArray("types").optJSONObject(0).optJSONObject("type")?.optString("name")
?: "null"
val name: String = json.optString("name") val name: String = json.optString("name")
.replaceFirstChar { if (it.isLowerCase()) it.titlecase(Locale.getDefault()) else it.toString() } .replaceFirstChar { if (it.isLowerCase()) it.titlecase(Locale.getDefault()) else it.toString() }

View file

@ -21,8 +21,7 @@ class LoadingScreen : Screen {
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) {
CircularProgressIndicator( CircularProgressIndicator(
modifier = Modifier.size(128.dp), modifier = Modifier.size(128.dp), color = Color.White
color = Color.White
) )
Text( Text(
"Fetching data from API...", "Fetching data from API...",

View file

@ -1,8 +1,5 @@
import java.net.URL import java.net.URL
data class Pokemon ( data class Pokemon(
val name: String, val name: String, val imageUrl: URL, val type: String, val id: Int
val imageUrl: URL,
val type: String,
val id: Int
) )

View file

@ -5,8 +5,8 @@ data class PokemonData(
val name: String, val name: String,
val type: String, val type: String,
val baseExperience: Int, val baseExperience: Int,
val height: Int, // Height in decimetres val height: Int,
val weight: Int, // Weight in hectograms val weight: Int,
val hp: Int, val hp: Int,
val attack: Int, val attack: Int,
val defense: Int, val defense: Int,
@ -18,8 +18,8 @@ data class PokemonData(
fun fromJson(json: JSONObject): PokemonData { fun fromJson(json: JSONObject): PokemonData {
val name = json.optString("name") val name = json.optString("name")
.replaceFirstChar { if (it.isLowerCase()) it.titlecase(Locale.getDefault()) else it.toString() } .replaceFirstChar { if (it.isLowerCase()) it.titlecase(Locale.getDefault()) else it.toString() }
val type = json.getJSONArray("types").optJSONObject(0).optJSONObject("type") val type =
.optString("name") json.getJSONArray("types").optJSONObject(0).optJSONObject("type").optString("name")
val baseExperience = json.optInt("base_experience") val baseExperience = json.optInt("base_experience")
val height = json.optInt("height") val height = json.optInt("height")
val weight = json.optInt("weight") val weight = json.optInt("weight")

File diff suppressed because one or more lines are too long

View file

@ -1,4 +1,3 @@
import androidx.compose.foundation.background import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Arrangement import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column import androidx.compose.foundation.layout.Column
@ -21,16 +20,10 @@ import androidx.compose.ui.unit.dp
@Composable @Composable
fun PokemonStatCard( fun PokemonStatCard(
hp: Int, hp: Int, attack: Int, defense: Int, specialAttack: Int, specialDefense: Int, speed: Int
attack: Int,
defense: Int,
specialAttack: Int,
specialDefense: Int,
speed: Int
) { ) {
Column( Column(
modifier = Modifier modifier = Modifier.background(color = Color.White, shape = RoundedCornerShape(8.dp))
.background(color = Color.White, shape = RoundedCornerShape(8.dp))
.padding(16.dp), .padding(16.dp),
verticalArrangement = Arrangement.Bottom, verticalArrangement = Arrangement.Bottom,
) { ) {
@ -46,8 +39,7 @@ fun PokemonStatCard(
@Composable @Composable
private fun StatRow(statName: String, progress: Int, color: Color) { private fun StatRow(statName: String, progress: Int, color: Color) {
Row( Row(
verticalAlignment = Alignment.CenterVertically, verticalAlignment = Alignment.CenterVertically, modifier = Modifier.fillMaxWidth()
modifier = Modifier.fillMaxWidth()
) { ) {
Text( Text(
text = "$statName: $progress", text = "$statName: $progress",