Este projeto é um app em Kotlin que consome a api pública do Rick & Morty.
API REST com Retrofit
: para consumir os dados da api pública do Rick and Morty e receber em um recyclerview.Okhttp
: para notificar problemas de conexão.ViewBinding
: para vincular os elementos visuais às funcionalidades, substitui o findViewById.Coroutines
: para programação assíncrona no Anbdroid.SharedViewModel
: para compartilhar dados de um ViewModel entre os Fragments.
1 - Utilizar o endpoint /character para montar uma lista de personagens.
object RetrofitInstance {
private val retrofit by lazy {
Retrofit.Builder()
.baseUrl("https://rickandmortyapi.com/")
.addConverterFactory(GsonConverterFactory.create())
.build()
}
val api: SimpleApi by lazy {
retrofit.create(SimpleApi::class.java)
}
}
package br.com.denisecastro.desafiorickandmorty.api
import br.com.denisecastro.desafiorickandmorty.model.CharacterList
import retrofit2.Response
import retrofit2.http.GET
import retrofit2.http.Query
interface SimpleApi {
@GET("api/character")
suspend fun getCharacters(@Query("page") page:Int): Response<CharacterList>
@GET("api/character")
suspend fun getCharactersByStatusAndGender
( @Query("status") status: String,
@Query("gender") gender: String,
@Query("page")page: Int ):Response<CharacterList>
@GET("api/character")
suspend fun getCharactersByStatus
( @Query("status") status: String,
@Query("page")page: Int ):Response<CharacterList>
@GET("api/character")
suspend fun getCharactersByGender
( @Query("gender") gender: String,
@Query("page")page: Int ):Response<CharacterList>
@GET("api/character")
suspend fun getCharactersByName
(@Query("name") name: String,
@Query("page") page: Int):Response<CharacterList>
}
A classe Response do Retrofit permite pegar erros de conexão. Também precisa configurar no recurso de strings a mensagem de erro.
<string name="text_error">%1$d - Characters not found!</string>
Caso aconteça algum problema com a conexão, a mensagem será exibida assim:
2 - Cada item da lista deve exibir o nome e a foto dos personagens.
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorPrimaryDark"
tools:context=".view.list.ListFragment">
<androidx.appcompat.widget.SearchView
android:id="@+id/searchView"
android:layout_width="379dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="16dp"
android:background="@drawable/background_search"
app:iconifiedByDefault="false"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:queryHint="@string/search_hint" />
<TextView
android:id="@+id/title_characters"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="60dp"
android:text="@string/title_characters"
android:textSize="18sp"
android:textStyle="bold"
app:layout_constraintEnd_toStartOf="@+id/img_button_filter"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@+id/searchView" />
<ImageView
android:id="@+id/img_button_filter"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginTop="60dp"
android:layout_marginEnd="16dp"
android:background="@android:color/transparent"
android:src="@drawable/ic_filter"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toEndOf="@+id/recycler_view"
app:layout_constraintTop_toTopOf="@+id/searchView" />
<TextView
android:id="@+id/title_action_reset"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:text="@string/title_reset"
android:visibility="invisible"
android:textSize="16sp"
android:textStyle="bold"
android:textColor="@color/text_blue_color"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/img_button_filter" />
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="16dp"
android:layout_marginTop="36dp"
android:layout_marginEnd="16dp"
android:layout_marginBottom="16dp"
tools:listitem="@layout/item_list"
tools:layoutManager="androidx.recyclerview.widget.StaggeredGridLayoutManager"
tools:orientation="vertical"
tools:spanCount="2"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/title_characters" />
<TextView
android:id="@+id/txt_api_error"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"
android:textStyle="bold"
android:visibility="gone"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintHorizontal_bias="0.5"/>
</androidx.constraintlayout.widget.ConstraintLayout>
Utilizar o endpoint /character para filtrar os personagens por parâmetro, como por ex.: name; status; gender. Nesta tela, a filtragem é por status ou gender, podendo escolher os dois parâmetros ou apenas 1 dos dois.
Exemplo de busca utilizando os dois parâmetros:
Resultado da busca por parâmetros:
Busca por nome:
Resultado da busca por nome:
Ao clicar no personagem, deve ser possível navegar para a tela de detalhes.
import androidx.test.core.app.ActivityScenario.launch
import androidx.test.espresso.Espresso.onView
import androidx.test.espresso.assertion.ViewAssertions.matches
import androidx.test.espresso.matcher.ViewMatchers.isDisplayed
import androidx.test.espresso.matcher.ViewMatchers.withText
import org.junit.Test
class ActivityListTest {
@Test
fun toShowTitleCharacter() {
launch(MainActivity::class.java)
onView(withText("Personagens")).check(matches(isDisplayed()))
}
}
Para saber se o teste funciona mesmo, é preciso fazê-lo falhar, sendo assim, alterei o nome para "Personagem", e o teste falhou:
import androidx.test.core.app.ActivityScenario.launch
import androidx.test.espresso.Espresso.onView
import androidx.test.espresso.assertion.ViewAssertions.matches
import androidx.test.espresso.matcher.ViewMatchers.isDisplayed
import androidx.test.espresso.matcher.ViewMatchers.withText
import org.junit.Test
class ActivityListTest {
@Test
fun toShowTitleCharacter() {
launch(MainActivity::class.java)
onView(withText("Personagem")).check(matches(isDisplayed()))
}
}
Teste de UI para verificar se o o campo para pesquisa do personagem por nome está sendo exibido na tela:
package br.com.denisecastro.desafiorickandmorty
import androidx.test.core.app.ActivityScenario.launch
import androidx.test.espresso.Espresso.onView
import androidx.test.espresso.assertion.ViewAssertions.matches
import androidx.test.espresso.matcher.ViewMatchers.*
import org.junit.Test
class ActivityListTest {
@Test
fun toShowTitleCharacter() {
launch(MainActivity::class.java)
onView(withText("Personagens")).check(matches(isDisplayed()))
}
@Test
fun shouldShowFieldToSearchByCharacterName() {
launch(MainActivity::class.java)
onView(withId(R.id.search_view)).check(matches(isDisplayed()))
}
}
./gradlew build
./gradlew connectedCheck
./gradlew lintKotlin