Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactoring RecyclerView.Adapter to ListAdapter #3

Open
wants to merge 1 commit into
base: template/coroutines
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 2 additions & 10 deletions app/src/main/java/br/com/renatoarg/ui/home/HomeFragment.kt
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
package br.com.renatoarg.ui.home

import android.content.Context
import android.os.Bundle
import android.view.View
import android.widget.ImageView
import androidx.fragment.app.Fragment
import androidx.lifecycle.Observer
import androidx.navigation.fragment.FragmentNavigatorExtras
import androidx.navigation.fragment.findNavController
import br.com.renatoarg.R
import br.com.renatoarg.model.pojo.User
import br.com.renatoarg.ui.home.adapter.UsersListAdapter
import br.com.renatoarg.ui.home.adapter.viewholder.UsersListInterface
import kotlinx.android.synthetic.main.fragment_home.*
import kotlinx.android.synthetic.main.item_user.*
import org.koin.androidx.viewmodel.ext.android.sharedViewModel
import timber.log.Timber

Expand All @@ -23,12 +20,7 @@ import timber.log.Timber
class HomeFragment : Fragment(R.layout.fragment_home), UsersListInterface {

private val viewModel: HomeViewModel by sharedViewModel()
private lateinit var usersListAdapter : UsersListAdapter

override fun onAttach(context: Context) {
super.onAttach(context)
usersListAdapter = UsersListAdapter(context, emptyList(), this)
}
private val usersListAdapter : UsersListAdapter = UsersListAdapter(this)

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
Expand All @@ -48,7 +40,7 @@ class HomeFragment : Fragment(R.layout.fragment_home), UsersListInterface {
}

private fun setupForUsersLoaded(usersList: List<User>) {
usersListAdapter.updateUsers(usersList)
usersListAdapter.submitList(usersList)
}

private fun setupForInit() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package br.com.renatoarg.ui.home.adapter

import androidx.recyclerview.widget.DiffUtil
import br.com.renatoarg.model.pojo.User

class UsersDiffUtilCallback : DiffUtil.ItemCallback<User>() {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Este callback é especializado para o UsersListAdapter e só consegue diferenciar Users.

Porque ter um arquivo separado para essa classe? Eu penso que é melhor ter apenas o arquivo do UsersListAdapter e o UsersDiffUtilCallback declarado dentro dela.

override fun areItemsTheSame(oldItem: User, newItem: User): Boolean {
return oldItem == newItem
}

override fun areContentsTheSame(oldItem: User, newItem: User): Boolean {
return oldItem == newItem
}
}
Original file line number Diff line number Diff line change
@@ -1,57 +1,40 @@
package br.com.renatoarg.ui.home.adapter

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import br.com.renatoarg.R
import androidx.recyclerview.widget.ListAdapter
import br.com.renatoarg.model.pojo.User
import br.com.renatoarg.ui.home.adapter.viewholder.ErrorViewHolder
import br.com.renatoarg.ui.home.adapter.viewholder.UserOkViewHolder
import br.com.renatoarg.ui.home.adapter.viewholder.UsersListInterface
import br.com.renatoarg.ui.home.adapter.viewholder.UsersViewHolder

class UsersListAdapter(
private val context: Context,
private var users: List<User>,
private val usersInterface: UsersListInterface
) : RecyclerView.Adapter<UsersListAdapter.BaseViewHolder<*>>() {
) : ListAdapter<User, UsersViewHolder>(UsersDiffUtilCallback()) {

companion object {
private const val TYPE_OK = 0
private const val TYPE_ERROR = 100
}

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BaseViewHolder<*> {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): UsersViewHolder {
return when(viewType) {
TYPE_OK -> UserOkViewHolder(
LayoutInflater.from(context).inflate(R.layout.item_user, parent, false),
usersInterface
)
TYPE_OK -> UserOkViewHolder(parent, usersInterface)
TYPE_ERROR -> ErrorViewHolder(parent, usersInterface)
else -> throw IllegalArgumentException("Invalid view type")
}
}

override fun getItemViewType(position: Int): Int {
return TYPE_OK
}


override fun getItemCount(): Int {
return users.size
}

override fun onBindViewHolder(holder: BaseViewHolder<*>, position: Int) {
when(holder) {
is UserOkViewHolder -> holder.bind(users[position])
val firstNameSize = getItem(position).first_name?.length ?: 0
return when(firstNameSize){
in 0 .. 5 -> TYPE_OK
else -> TYPE_ERROR
}
}

fun updateUsers(usersList: List<User>) {
this.users = usersList
notifyDataSetChanged()
}

abstract class BaseViewHolder<T>(itemView: View) : RecyclerView.ViewHolder(itemView) {
abstract fun bind(item: T)
override fun onBindViewHolder(holder: UsersViewHolder, position: Int) {
holder.bind(getItem(position))
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package br.com.renatoarg.ui.home.adapter.viewholder
import android.text.Spannable
import android.text.SpannableString
import android.text.style.StrikethroughSpan
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.core.text.set
import br.com.renatoarg.R
import br.com.renatoarg.model.pojo.User
import com.bumptech.glide.Glide
import com.bumptech.glide.request.RequestOptions
import kotlinx.android.synthetic.main.item_user.view.*

class ErrorViewHolder(
parent: ViewGroup,
usersInterface: UsersListInterface
) : UsersViewHolder(
LayoutInflater.from(parent.context).inflate(R.layout.item_error, parent, false),
usersInterface
) {

override fun bind(user: User) {
super.bind(user)
val name = itemView.name.text
val spannableString = SpannableString(name)
spannableString.setSpan(
StrikethroughSpan(),
0,
user.first_name?.length ?: 0,
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE
)
itemView.name.text = spannableString
}

}

Original file line number Diff line number Diff line change
@@ -1,29 +1,23 @@
package br.com.renatoarg.ui.home.adapter.viewholder
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.recyclerview.widget.RecyclerView
import br.com.renatoarg.R
import br.com.renatoarg.model.pojo.User
import br.com.renatoarg.ui.home.adapter.UsersListAdapter
import com.bumptech.glide.Glide
import com.bumptech.glide.request.RequestOptions
import kotlinx.android.synthetic.main.item_user.view.*

class UserOkViewHolder(
itemView: View,
var usersInterface: UsersListInterface
) :
UsersListAdapter.BaseViewHolder<User>(itemView) {

override fun bind(item: User) {
val photo = itemView.findViewById<ImageView>(R.id.photo)
Glide.with(itemView.context).load(item.avatar).apply(RequestOptions().circleCrop()).into(photo)
itemView.findViewById<TextView>(R.id.name).text = itemView.context.getString(R.string.user_name, item.first_name, item.last_name)
itemView.findViewById<TextView>(R.id.email).text = item.email
itemView.findViewById<ConstraintLayout>(R.id.wrapper).setOnClickListener {
usersInterface.selectUser(item, photo)
}
}

}
parent: ViewGroup,
usersInterface: UsersListInterface
) : UsersViewHolder(
LayoutInflater.from(parent.context).inflate(R.layout.item_user, parent, false),
usersInterface
)

Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package br.com.renatoarg.ui.home.adapter.viewholder

import android.view.View
import androidx.recyclerview.widget.RecyclerView
import br.com.renatoarg.R
import br.com.renatoarg.model.pojo.User
import com.bumptech.glide.Glide
import com.bumptech.glide.request.RequestOptions
import kotlinx.android.synthetic.main.item_user.view.*

abstract class UsersViewHolder(
itemView : View,
private val usersInterface: UsersListInterface
) : RecyclerView.ViewHolder(itemView) {
open fun bind(user : User){
with(itemView){

Glide.with(itemView.context)
.load(user.avatar)
.apply(RequestOptions().circleCrop())
.into(photo)

name.text = itemView.context.getString(R.string.user_name, user.first_name, user.last_name)
email.text = user.email
wrapper.setOnClickListener {
usersInterface.selectUser(user, photo)
}
}
}
}
40 changes: 40 additions & 0 deletions app/src/main/res/layout/item_error.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?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:id="@+id/wrapper"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorAccent"
android:padding="16dp">

<ImageView
android:id="@+id/photo"
android:layout_width="@dimen/photo_size"
android:layout_height="@dimen/photo_size"
android:transitionName="photo"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:textAppearance="@style/TextAppearance.AppCompat.Title"
app:layout_constraintStart_toEndOf="@+id/photo"
app:layout_constraintTop_toTopOf="parent"
tools:text="Fulano de tal" />

<TextView
android:id="@+id/email"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
app:layout_constraintStart_toEndOf="@+id/photo"
app:layout_constraintTop_toBottomOf="@+id/name"
tools:text="[email protected]" />

</androidx.constraintlayout.widget.ConstraintLayout>