-
Notifications
You must be signed in to change notification settings - Fork 0
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
isolino
wants to merge
1
commit into
template/coroutines
Choose a base branch
from
migrating-to-listadapter
base: template/coroutines
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
app/src/main/java/br/com/renatoarg/ui/home/adapter/UsersDiffUtilCallback.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>() { | ||
override fun areItemsTheSame(oldItem: User, newItem: User): Boolean { | ||
return oldItem == newItem | ||
} | ||
|
||
override fun areContentsTheSame(oldItem: User, newItem: User): Boolean { | ||
return oldItem == newItem | ||
} | ||
} |
43 changes: 13 additions & 30 deletions
43
app/src/main/java/br/com/renatoarg/ui/home/adapter/UsersListAdapter.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
app/src/main/java/br/com/renatoarg/ui/home/adapter/viewholder/ErrorViewHolder.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
|
||
} | ||
|
26 changes: 10 additions & 16 deletions
26
app/src/main/java/br/com/renatoarg/ui/home/adapter/viewholder/UserOkViewHolder.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
) | ||
|
30 changes: 30 additions & 0 deletions
30
app/src/main/java/br/com/renatoarg/ui/home/adapter/viewholder/UsersViewHolder.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.