alterações
This commit is contained in:
@@ -4,23 +4,11 @@ import android.os.Bundle
|
|||||||
import androidx.activity.ComponentActivity
|
import androidx.activity.ComponentActivity
|
||||||
import androidx.activity.compose.setContent
|
import androidx.activity.compose.setContent
|
||||||
import androidx.activity.enableEdgeToEdge
|
import androidx.activity.enableEdgeToEdge
|
||||||
import androidx.compose.foundation.layout.Box
|
import androidx.compose.foundation.layout.*
|
||||||
import androidx.compose.foundation.layout.Column
|
|
||||||
import androidx.compose.foundation.layout.Spacer
|
|
||||||
import androidx.compose.foundation.layout.fillMaxSize
|
|
||||||
import androidx.compose.foundation.layout.height
|
|
||||||
import androidx.compose.foundation.layout.padding
|
|
||||||
import androidx.compose.foundation.lazy.LazyColumn
|
import androidx.compose.foundation.lazy.LazyColumn
|
||||||
import androidx.compose.foundation.lazy.items
|
import androidx.compose.foundation.lazy.items
|
||||||
import androidx.compose.material3.Button
|
import androidx.compose.material3.*
|
||||||
import androidx.compose.material3.CircularProgressIndicator
|
import androidx.compose.runtime.*
|
||||||
import androidx.compose.material3.ExperimentalMaterial3Api
|
|
||||||
import androidx.compose.material3.Scaffold
|
|
||||||
import androidx.compose.material3.Text
|
|
||||||
import androidx.compose.material3.TopAppBar
|
|
||||||
import androidx.compose.runtime.Composable
|
|
||||||
import androidx.compose.runtime.collectAsState
|
|
||||||
import androidx.compose.runtime.getValue
|
|
||||||
import androidx.compose.ui.Alignment
|
import androidx.compose.ui.Alignment
|
||||||
import androidx.compose.ui.Modifier
|
import androidx.compose.ui.Modifier
|
||||||
import androidx.compose.ui.unit.dp
|
import androidx.compose.ui.unit.dp
|
||||||
@@ -47,37 +35,58 @@ fun CharacterScreen(viewModel: CharacterViewModel = viewModel()) {
|
|||||||
val characters by viewModel.characters.collectAsState()
|
val characters by viewModel.characters.collectAsState()
|
||||||
val isLoading by viewModel.isLoading.collectAsState()
|
val isLoading by viewModel.isLoading.collectAsState()
|
||||||
val error by viewModel.error.collectAsState()
|
val error by viewModel.error.collectAsState()
|
||||||
|
val searchQuery by viewModel.searchQuery.collectAsState()
|
||||||
|
|
||||||
Scaffold(
|
Scaffold(
|
||||||
topBar = {
|
topBar = {
|
||||||
TopAppBar(title = { Text("Rick and Morty Characters") })
|
TopAppBar(title = { Text("Rick and Morty Finder") })
|
||||||
}
|
}
|
||||||
) { innerPadding ->
|
) { innerPadding ->
|
||||||
Box(
|
Column(
|
||||||
modifier = Modifier
|
modifier = Modifier
|
||||||
.fillMaxSize()
|
.fillMaxSize()
|
||||||
.padding(innerPadding)
|
.padding(innerPadding)
|
||||||
) {
|
) {
|
||||||
when {
|
// Barra de Busca - Isso faz o app "fazer alguma coisa" interativa
|
||||||
isLoading -> {
|
OutlinedTextField(
|
||||||
CircularProgressIndicator(modifier = Modifier.align(Alignment.Center))
|
value = searchQuery,
|
||||||
}
|
onValueChange = { viewModel.onSearchQueryChange(it) },
|
||||||
error != null -> {
|
modifier = Modifier
|
||||||
Column(
|
.fillMaxWidth()
|
||||||
modifier = Modifier.align(Alignment.Center),
|
.padding(8.dp),
|
||||||
horizontalAlignment = Alignment.CenterHorizontally
|
placeholder = { Text("Pesquisar personagem (ex: Rick, Morty...)") },
|
||||||
) {
|
label = { Text("Buscar por Nome") },
|
||||||
Text(text = error ?: "Erro desconhecido")
|
singleLine = true
|
||||||
Spacer(modifier = Modifier.height(8.dp))
|
)
|
||||||
Button(onClick = { viewModel.fetchCharacters() }) {
|
|
||||||
Text("Tentar novamente")
|
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 -> {
|
||||||
else -> {
|
Text(
|
||||||
LazyColumn {
|
text = "Nenhum resultado encontrado.",
|
||||||
items(characters) { character ->
|
modifier = Modifier.align(Alignment.Center)
|
||||||
CharacterItem(character = character)
|
)
|
||||||
|
}
|
||||||
|
else -> {
|
||||||
|
LazyColumn {
|
||||||
|
items(characters) { character ->
|
||||||
|
CharacterItem(character = character)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,8 +2,12 @@ package com.example.api.network
|
|||||||
|
|
||||||
import com.example.api.model.CharacterResponse
|
import com.example.api.model.CharacterResponse
|
||||||
import retrofit2.http.GET
|
import retrofit2.http.GET
|
||||||
|
import retrofit2.http.Query
|
||||||
|
|
||||||
interface RickAndMortyService {
|
interface RickAndMortyService {
|
||||||
@GET("character")
|
@GET("character")
|
||||||
suspend fun getCharacters(): CharacterResponse
|
suspend fun getCharacters(
|
||||||
|
@Query("page") page: Int? = null,
|
||||||
|
@Query("name") name: String? = null
|
||||||
|
): CharacterResponse
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -18,19 +18,28 @@ class CharacterViewModel : ViewModel() {
|
|||||||
private val _error = MutableStateFlow<String?>(null)
|
private val _error = MutableStateFlow<String?>(null)
|
||||||
val error: StateFlow<String?> = _error
|
val error: StateFlow<String?> = _error
|
||||||
|
|
||||||
|
private val _searchQuery = MutableStateFlow("")
|
||||||
|
val searchQuery: StateFlow<String> = _searchQuery
|
||||||
|
|
||||||
init {
|
init {
|
||||||
fetchCharacters()
|
fetchCharacters()
|
||||||
}
|
}
|
||||||
|
|
||||||
fun fetchCharacters() {
|
fun onSearchQueryChange(query: String) {
|
||||||
|
_searchQuery.value = query
|
||||||
|
fetchCharacters(query)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun fetchCharacters(name: String? = null) {
|
||||||
viewModelScope.launch {
|
viewModelScope.launch {
|
||||||
_isLoading.value = true
|
_isLoading.value = true
|
||||||
_error.value = null
|
_error.value = null
|
||||||
try {
|
try {
|
||||||
val response = RetrofitClient.apiService.getCharacters()
|
val response = RetrofitClient.apiService.getCharacters(name = name)
|
||||||
_characters.value = response.results
|
_characters.value = response.results
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
_error.value = "Erro ao carregar personagens: ${e.localizedMessage}"
|
_characters.value = emptyList()
|
||||||
|
_error.value = if (name != null) "Nenhum personagem encontrado com esse nome." else "Erro ao carregar personagens."
|
||||||
} finally {
|
} finally {
|
||||||
_isLoading.value = false
|
_isLoading.value = false
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user