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"
)
Column(
modifier = Modifier
.fillMaxSize()
.background(color = orange),
modifier = Modifier.fillMaxSize().background(color = orange),
horizontalAlignment = Alignment.CenterHorizontally
) {
OutlinedTextField(
value = searchQuery,
OutlinedTextField(value = searchQuery,
onValueChange = { newName -> searchQuery = newName; placeholder = "" },
modifier = Modifier.fillMaxWidth(0.8f)
.padding(horizontal = 16.dp)
.padding(top = 16.dp)
.background(color = Color.White),
modifier = Modifier.fillMaxWidth(0.8f).padding(horizontal = 16.dp)
.padding(top = 16.dp).background(color = Color.White),
trailingIcon = {
Row() {
IconButton(
onClick = {
if (searchQuery.length >= 3 && PokemonNames.names.any { name ->
name.contains(
searchQuery.lowercase(
Locale.getDefault()
)
IconButton(onClick = {
if (searchQuery.length >= 3 && PokemonNames.names.any { name ->
name.contains(
searchQuery.lowercase(
Locale.getDefault()
)
}) {
coroutineScope.launch {
withContext(Dispatchers.IO) {
println("Searching for pokemon, query: $searchQuery")
val names: List<String> =
PokemonNames.names.filter { name ->
name.contains(
searchQuery.lowercase(
Locale.getDefault()
)
)
}) {
coroutineScope.launch {
withContext(Dispatchers.IO) {
println("Searching for pokemon, query: $searchQuery")
val names: List<String> =
PokemonNames.names.filter { name ->
name.contains(
searchQuery.lowercase(
Locale.getDefault()
)
}
var newMap = arrayListOf<Pokemon>()
names.forEach { name ->
loadPokemonDataFromName(name)?.let {
newMap.add(it)
}
)
}
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"
var newMap = arrayListOf<Pokemon>()
names.forEach { name ->
loadPokemonDataFromName(name)?.let {
newMap.add(it)
}
}
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")
}
IconButton(
onClick = {
isFilterOpen = !isFilterOpen
}
) {
IconButton(onClick = {
isFilterOpen = !isFilterOpen
}) {
Icon(Icons.Default.Settings, contentDescription = "Filter")
}
}
@ -170,8 +160,7 @@ class HomeScreen : Screen {
focusedLabelColor = orange,
unfocusedLabelColor = orange,
),
placeholder = { Text(placeholder) }
)
placeholder = { Text(placeholder) })
Spacer(modifier = Modifier.height(16.dp))
//Grid
LazyVerticalGrid(
@ -179,9 +168,8 @@ class HomeScreen : Screen {
) {
items(pokemap.size) { index ->
Box(modifier = Modifier.size(256.dp).padding(5.dp)) {
Canvas(
modifier = Modifier.matchParentSize()
.clickable { navigator.push(DetailScreen(pokemap[index])) }) {
Canvas(modifier = Modifier.matchParentSize()
.clickable { navigator.push(DetailScreen(pokemap[index])) }) {
drawRoundRect(
color = Color.White,
topLeft = Offset(0f, 0f),
@ -225,9 +213,7 @@ class HomeScreen : Screen {
}
}
}
DropdownMenu(
expanded = isFilterOpen,
onDismissRequest = { isFilterOpen = false }) {
DropdownMenu(expanded = isFilterOpen, onDismissRequest = { isFilterOpen = false }) {
filterOptions.forEach { option ->
DropdownMenuItem(onClick = { currentFilter = option; isFilterOpen = false }) {
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 sprites = json.optJSONObject("sprites")
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")
.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
var json: JSONObject
try {
json = JSONObject(URL(apiString + trimmedName).readText());
json = JSONObject(URL(apiString + trimmedName).readText());
} catch (e: Exception) {
println(e)
return null
}
val sprites = json.optJSONObject("sprites")
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")
.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.fillMaxSize(), contentAlignment = Alignment.Center) {
CircularProgressIndicator(
modifier = Modifier.size(128.dp),
color = Color.White
modifier = Modifier.size(128.dp), color = Color.White
)
Text(
"Fetching data from API...",

View file

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

View file

@ -5,8 +5,8 @@ data class PokemonData(
val name: String,
val type: String,
val baseExperience: Int,
val height: Int, // Height in decimetres
val weight: Int, // Weight in hectograms
val height: Int,
val weight: Int,
val hp: Int,
val attack: Int,
val defense: Int,
@ -18,8 +18,8 @@ data class PokemonData(
fun fromJson(json: JSONObject): PokemonData {
val name = json.optString("name")
.replaceFirstChar { if (it.isLowerCase()) it.titlecase(Locale.getDefault()) else it.toString() }
val type = json.getJSONArray("types").optJSONObject(0).optJSONObject("type")
.optString("name")
val type =
json.getJSONArray("types").optJSONObject(0).optJSONObject("type").optString("name")
val baseExperience = json.optInt("base_experience")
val height = json.optInt("height")
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.layout.Arrangement
import androidx.compose.foundation.layout.Column
@ -21,16 +20,10 @@ import androidx.compose.ui.unit.dp
@Composable
fun PokemonStatCard(
hp: Int,
attack: Int,
defense: Int,
specialAttack: Int,
specialDefense: Int,
speed: Int
hp: Int, attack: Int, defense: Int, specialAttack: Int, specialDefense: Int, speed: Int
) {
Column(
modifier = Modifier
.background(color = Color.White, shape = RoundedCornerShape(8.dp))
modifier = Modifier.background(color = Color.White, shape = RoundedCornerShape(8.dp))
.padding(16.dp),
verticalArrangement = Arrangement.Bottom,
) {
@ -46,8 +39,7 @@ fun PokemonStatCard(
@Composable
private fun StatRow(statName: String, progress: Int, color: Color) {
Row(
verticalAlignment = Alignment.CenterVertically,
modifier = Modifier.fillMaxWidth()
verticalAlignment = Alignment.CenterVertically, modifier = Modifier.fillMaxWidth()
) {
Text(
text = "$statName: $progress",