Skip to content

Commit

Permalink
[FEAT/#9] FollowerAdapter ListAdapter로 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
b1urrrr committed Dec 15, 2022
1 parent 3a271d3 commit 19cc37d
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ data class MusicBaseResponse<T>(
val statusCode: Int,
val success: Boolean,
val message: String,
val data: T
val data: T?
)
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,20 @@ package org.sopt.sample.presentation.main.home
import android.content.Context
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.ListAdapter
import androidx.recyclerview.widget.RecyclerView
import org.sopt.sample.data.dto.response.ResponseGetFollowerListDto
import org.sopt.sample.data.dto.response.ResponseGetFollowerListDto.Follower
import org.sopt.sample.databinding.HeaderHomeFollowerBinding
import org.sopt.sample.databinding.ItemHomeFollowerBinding

class FollowerAdapter(context: Context) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {
class FollowerAdapter(context: Context) : ListAdapter<Follower, RecyclerView.ViewHolder>(diffUtil) {
private val inflater by lazy { LayoutInflater.from(context) }
private var followerList: List<ResponseGetFollowerListDto.Follower> = emptyList()

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
override fun onCreateViewHolder(
parent: ViewGroup,
viewType: Int
): RecyclerView.ViewHolder {
return when (viewType) {
VIEW_TYPE_HEADER -> FollowerHeaderViewHolder(
HeaderHomeFollowerBinding.inflate(
Expand All @@ -33,26 +37,21 @@ class FollowerAdapter(context: Context) : RecyclerView.Adapter<RecyclerView.View
}

override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
if (holder is FollowerViewHolder) holder.setFollower(followerList[position - 1])
if (holder is FollowerViewHolder) holder.setFollower(getItem(position - 1))
}

override fun getItemCount(): Int {
return followerList.size + 1
return super.getItemCount() + 1
}

override fun getItemViewType(position: Int): Int {
if (position == 0) return VIEW_TYPE_HEADER
else return VIEW_TYPE_ITEM
}

fun setFollowerList(followerList: List<ResponseGetFollowerListDto.Follower>) {
this.followerList = followerList.toList()
notifyDataSetChanged()
return if (position == 0) VIEW_TYPE_HEADER
else VIEW_TYPE_ITEM
}

class FollowerViewHolder(private val binding: ItemHomeFollowerBinding) :
RecyclerView.ViewHolder(binding.root) {
fun setFollower(follower: ResponseGetFollowerListDto.Follower) {
fun setFollower(follower: Follower) {
binding.data = follower
}
}
Expand All @@ -61,6 +60,14 @@ class FollowerAdapter(context: Context) : RecyclerView.Adapter<RecyclerView.View
RecyclerView.ViewHolder(binding.root)

companion object {
val diffUtil = object : DiffUtil.ItemCallback<Follower>() {
override fun areItemsTheSame(oldItem: Follower, newItem: Follower): Boolean =
oldItem == newItem

override fun areContentsTheSame(oldItem: Follower, newItem: Follower): Boolean =
oldItem.id == newItem.id
}

const val VIEW_TYPE_HEADER = 0
const val VIEW_TYPE_ITEM = 1
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,10 @@ class HomeFragment : BindingFragment<FragmentHomeBinding>(R.layout.fragment_home
}

private fun observeStateMessage() {
viewModel.stateMessage.observe(viewLifecycleOwner) {
viewModel.stateMessage.observe(viewLifecycleOwner) { it ->
when (it) {
is UiState.Success -> viewModel.followerList.value?.let {
followerAdapter.setFollowerList(it)
followerAdapter.submitList(it)
}
is UiState.Failure -> requireContext().showSnackbar(
binding.root,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class HomeViewModel @Inject constructor(
}

/** Reqres 서버에 팔로워 리스트 중 1페이지 요청 */
fun getFollowerList() {
private fun getFollowerList() {
viewModelScope.launch {
followerRepository.getFollowerList(1)
.onSuccess { response ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class MusicViewModel @Inject constructor(
}

/** 서버에 음악 리스트 요청 */
fun getMusicList() {
private fun getMusicList() {
viewModelScope.launch {
musicRepository.getMusicList()
.onSuccess { response ->
Expand Down
14 changes: 14 additions & 0 deletions app/src/main/java/org/sopt/sample/util/DiffCallback.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package org.sopt.sample.util

import androidx.recyclerview.widget.DiffUtil

class DiffCallback<T : Any>(
val onItemsTheSame: (T, T) -> Boolean,
val onContentsTheSame: (T, T) -> Boolean
) : DiffUtil.ItemCallback<T>() {
override fun areItemsTheSame(oldItem: T, newItem: T): Boolean =
onItemsTheSame(oldItem, newItem)

override fun areContentsTheSame(oldItem: T, newItem: T): Boolean =
onContentsTheSame(oldItem, newItem)
}

0 comments on commit 19cc37d

Please sign in to comment.