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