97 lines
3.5 KiB
Kotlin
97 lines
3.5 KiB
Kotlin
package com.example.api
|
|
|
|
import android.os.Bundle
|
|
import androidx.activity.ComponentActivity
|
|
import androidx.activity.compose.setContent
|
|
import androidx.activity.enableEdgeToEdge
|
|
import androidx.compose.foundation.layout.*
|
|
import androidx.compose.foundation.lazy.LazyColumn
|
|
import androidx.compose.foundation.lazy.items
|
|
import androidx.compose.material3.*
|
|
import androidx.compose.runtime.*
|
|
import androidx.compose.ui.Alignment
|
|
import androidx.compose.ui.Modifier
|
|
import androidx.compose.ui.unit.dp
|
|
import androidx.lifecycle.viewmodel.compose.viewModel
|
|
import com.example.api.ui.components.CharacterItem
|
|
import com.example.api.ui.theme.ApiTheme
|
|
import com.example.api.viewmodel.CharacterViewModel
|
|
|
|
class MainActivity : ComponentActivity() {
|
|
override fun onCreate(savedInstanceState: Bundle?) {
|
|
super.onCreate(savedInstanceState)
|
|
enableEdgeToEdge()
|
|
setContent {
|
|
ApiTheme {
|
|
CharacterScreen()
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
@OptIn(ExperimentalMaterial3Api::class)
|
|
@Composable
|
|
fun CharacterScreen(viewModel: CharacterViewModel = viewModel()) {
|
|
val characters by viewModel.characters.collectAsState()
|
|
val isLoading by viewModel.isLoading.collectAsState()
|
|
val error by viewModel.error.collectAsState()
|
|
val searchQuery by viewModel.searchQuery.collectAsState()
|
|
|
|
Scaffold(
|
|
topBar = {
|
|
TopAppBar(title = { Text("Rick and Morty Finder") })
|
|
}
|
|
) { innerPadding ->
|
|
Column(
|
|
modifier = Modifier
|
|
.fillMaxSize()
|
|
.padding(innerPadding)
|
|
) {
|
|
// Barra de Busca - Isso faz o app "fazer alguma coisa" interativa
|
|
OutlinedTextField(
|
|
value = searchQuery,
|
|
onValueChange = { viewModel.onSearchQueryChange(it) },
|
|
modifier = Modifier
|
|
.fillMaxWidth()
|
|
.padding(8.dp),
|
|
placeholder = { Text("Pesquisar personagem (ex: Rick, Morty...)") },
|
|
label = { Text("Buscar por Nome") },
|
|
singleLine = true
|
|
)
|
|
|
|
Box(modifier = Modifier.weight(1f)) {
|
|
when {
|
|
isLoading && characters.isEmpty() -> {
|
|
CircularProgressIndicator(modifier = Modifier.align(Alignment.Center))
|
|
}
|
|
error != null && characters.isEmpty() -> {
|
|
Column(
|
|
modifier = Modifier.align(Alignment.Center),
|
|
horizontalAlignment = Alignment.CenterHorizontally
|
|
) {
|
|
Text(text = error ?: "Erro desconhecido")
|
|
Spacer(modifier = Modifier.height(16.dp))
|
|
Button(onClick = { viewModel.fetchCharacters(searchQuery) }) {
|
|
Text("Tentar novamente")
|
|
}
|
|
}
|
|
}
|
|
characters.isEmpty() && !isLoading -> {
|
|
Text(
|
|
text = "Nenhum resultado encontrado.",
|
|
modifier = Modifier.align(Alignment.Center)
|
|
)
|
|
}
|
|
else -> {
|
|
LazyColumn {
|
|
items(characters) { character ->
|
|
CharacterItem(character = character)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|