|
| 1 | +package co.pokeapi.pokekotlin.demoapp |
| 2 | + |
| 3 | +import androidx.compose.foundation.clickable |
| 4 | +import androidx.compose.foundation.layout.Box |
| 5 | +import androidx.compose.foundation.layout.PaddingValues |
| 6 | +import androidx.compose.foundation.layout.consumeWindowInsets |
| 7 | +import androidx.compose.foundation.layout.fillMaxSize |
| 8 | +import androidx.compose.foundation.layout.padding |
| 9 | +import androidx.compose.foundation.lazy.LazyColumn |
| 10 | +import androidx.compose.foundation.lazy.items |
| 11 | +import androidx.compose.material3.CircularProgressIndicator |
| 12 | +import androidx.compose.material3.ColorScheme |
| 13 | +import androidx.compose.material3.ListItem |
| 14 | +import androidx.compose.material3.MaterialTheme |
| 15 | +import androidx.compose.material3.Scaffold |
| 16 | +import androidx.compose.material3.Text |
| 17 | +import androidx.compose.material3.TopAppBar |
| 18 | +import androidx.compose.runtime.Composable |
| 19 | +import androidx.compose.runtime.LaunchedEffect |
| 20 | +import androidx.compose.runtime.getValue |
| 21 | +import androidx.compose.runtime.mutableStateOf |
| 22 | +import androidx.compose.runtime.remember |
| 23 | +import androidx.compose.runtime.setValue |
| 24 | +import androidx.compose.ui.Alignment |
| 25 | +import androidx.compose.ui.Modifier |
| 26 | +import androidx.compose.ui.text.style.TextOverflow |
| 27 | +import co.pokeapi.pokekotlin.PokeApi |
| 28 | +import co.pokeapi.pokekotlin.model.NamedApiResource |
| 29 | +import co.pokeapi.pokekotlin.model.NamedApiResourceList |
| 30 | +import co.pokeapi.pokekotlin.model.PokemonSpecies |
| 31 | + |
| 32 | +@Composable |
| 33 | +fun PokemonListItem(pokemon: PokemonSpecies) { |
| 34 | + ListItem( |
| 35 | + modifier = Modifier.clickable(onClick = {}), |
| 36 | + headlineContent = { Text(pokemon.names.first { it.language.name == "en" }.name) }, |
| 37 | + supportingContent = { |
| 38 | + Text( |
| 39 | + pokemon.flavorTextEntries |
| 40 | + .filter { it.language.name == "en" } |
| 41 | + .maxBy { it.version.id } |
| 42 | + .flavorText |
| 43 | + .replace(Regex("\\s"), " "), |
| 44 | + maxLines = 2, |
| 45 | + overflow = TextOverflow.Ellipsis, |
| 46 | + ) |
| 47 | + }, |
| 48 | + ) |
| 49 | +} |
| 50 | + |
| 51 | +@Composable |
| 52 | +fun PokemonListItemPlaceholder(summary: NamedApiResource) { |
| 53 | + ListItem(headlineContent = { Text("Loading: ${summary.name}") }) |
| 54 | +} |
| 55 | + |
| 56 | +@Composable |
| 57 | +fun PokemonListItemError(summary: NamedApiResource, message: String) { |
| 58 | + ListItem( |
| 59 | + headlineContent = { Text("Failed to load: ${summary.name}") }, |
| 60 | + supportingContent = { Text(message) }, |
| 61 | + ) |
| 62 | +} |
| 63 | + |
| 64 | +@Composable |
| 65 | +fun PokemonList(padding: PaddingValues, pokemon: NamedApiResourceList) { |
| 66 | + Box(Modifier.consumeWindowInsets(padding).fillMaxSize()) { |
| 67 | + LazyColumn(contentPadding = padding) { |
| 68 | + items(pokemon.results) { summary -> |
| 69 | + var result by remember { mutableStateOf<Result<PokemonSpecies>?>(null) } |
| 70 | + LaunchedEffect(Unit) { result = PokeApi.getPokemonSpecies(summary.id) } |
| 71 | + when { |
| 72 | + result == null -> PokemonListItemPlaceholder(summary) |
| 73 | + result!!.isSuccess -> PokemonListItem(result!!.getOrThrow()) |
| 74 | + result!!.isFailure -> |
| 75 | + PokemonListItemError( |
| 76 | + summary = summary, |
| 77 | + message = result!!.exceptionOrNull()!!.message ?: "Unknown error", |
| 78 | + ) |
| 79 | + } |
| 80 | + } |
| 81 | + } |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +@Composable |
| 86 | +fun CenteredLoading(padding: PaddingValues) { |
| 87 | + Box( |
| 88 | + Modifier.consumeWindowInsets(padding).padding(padding).fillMaxSize(), |
| 89 | + contentAlignment = Alignment.Center, |
| 90 | + ) { |
| 91 | + CircularProgressIndicator() |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +@Composable |
| 96 | +fun ErrorMessage(padding: PaddingValues, message: String) { |
| 97 | + Box( |
| 98 | + Modifier.consumeWindowInsets(padding).padding(padding).fillMaxSize(), |
| 99 | + contentAlignment = Alignment.Center, |
| 100 | + ) { |
| 101 | + Text(message) |
| 102 | + } |
| 103 | +} |
| 104 | + |
| 105 | +@Composable |
| 106 | +fun DemoApp() { |
| 107 | + MaterialTheme(colorScheme = getDefaultColorScheme()) { |
| 108 | + Scaffold( |
| 109 | + topBar = { TopAppBar(title = { Text("PokeKotlin Demo") }) }, |
| 110 | + content = { innerPadding -> |
| 111 | + var result by remember { mutableStateOf<Result<NamedApiResourceList>?>(null) } |
| 112 | + LaunchedEffect(Unit) { result = PokeApi.getPokemonSpeciesList(0, 100000) } |
| 113 | + when { |
| 114 | + result == null -> CenteredLoading(innerPadding) |
| 115 | + result!!.isSuccess -> PokemonList(innerPadding, result!!.getOrThrow()) |
| 116 | + result!!.isFailure -> |
| 117 | + ErrorMessage( |
| 118 | + padding = innerPadding, |
| 119 | + message = result!!.exceptionOrNull()!!.message ?: "Unknown error", |
| 120 | + ) |
| 121 | + } |
| 122 | + }, |
| 123 | + ) |
| 124 | + } |
| 125 | +} |
| 126 | + |
| 127 | +@Composable expect fun getDefaultColorScheme(isDark: Boolean = false): ColorScheme |
0 commit comments