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

[refactor] 운동모아보기 뷰 listadapter로 변경 #106

Open
wants to merge 3 commits into
base: develop
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
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@ package sopt.motivoo.presentation.exercise

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.ListAdapter
import androidx.recyclerview.widget.RecyclerView
import sopt.motivoo.databinding.ItemExerciseBinding
import sopt.motivoo.databinding.ItemExerciseNoticeBinding
import sopt.motivoo.domain.entity.exercise.ExerciseData.ExerciseItemInfo

class ExerciseAdapter(private val userType: String) :
RecyclerView.Adapter<RecyclerView.ViewHolder>() {
private var exerciseItemInfoList: List<ExerciseItemInfo> = emptyList()

class ExerciseAdapter(private val userType: String) : ListAdapter<ExerciseItemInfo, RecyclerView.ViewHolder>(diffUtil) {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
val inflater = LayoutInflater.from(parent.context)
return when (viewType) {
Expand All @@ -29,33 +28,42 @@ class ExerciseAdapter(private val userType: String) :
override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
when (holder) {
is ExerciseNoticeViewHolder -> {
val noticeInfo = exerciseItemInfoList[position]
val noticeInfo = currentList[position]
holder.onBind(noticeInfo as ExerciseItemInfo.NoticeItemInfo)
}

is ExerciseEachDateInfoViewHolder -> {
val dateExerciseInfo = exerciseItemInfoList[position]
val dateExerciseInfo = currentList[position]
holder.onBind(dateExerciseInfo as ExerciseItemInfo.EachDateItemInfo, userType)
}
}
}

override fun getItemViewType(position: Int): Int {
return when (exerciseItemInfoList[position]) {
return when (currentList[position]) {
is ExerciseItemInfo.NoticeItemInfo -> NOTICE_INFO_TYPE
is ExerciseItemInfo.EachDateItemInfo -> DATE_EXERCISE_INFO_TYPE
}
}

override fun getItemCount() = exerciseItemInfoList.size

fun updateItemList(exerciseList: List<ExerciseItemInfo>) {
this.exerciseItemInfoList = exerciseList.toList()
notifyDataSetChanged()
}

companion object {
const val NOTICE_INFO_TYPE = 0
const val DATE_EXERCISE_INFO_TYPE = 1

val diffUtil = object : DiffUtil.ItemCallback<ExerciseItemInfo>() {
Copy link
Contributor

Choose a reason for hiding this comment

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

utli -> extension에 보시면 ItemDiffCallback 구현해둔거 있어서 그거 쓰셔도 될것 같습니다~

override fun areItemsTheSame(
oldItem: ExerciseItemInfo,
newItem: ExerciseItemInfo
): Boolean {
return oldItem == newItem
Copy link
Contributor

Choose a reason for hiding this comment

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

객체의 고유 식별자를 정해서 비교해줘야할 것 같네요!
areItemsTheSame, areContentsTheSame 메서드가 무슨 차이인지 알아보시면 이해되실겁니당

}

override fun areContentsTheSame(
oldItem: ExerciseItemInfo,
newItem: ExerciseItemInfo
): Boolean {
return oldItem == newItem
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,9 @@ class ExerciseFragment : BindingFragment<FragmentExerciseBinding>(R.layout.fragm

private fun initAdapter(exerciseData: ExerciseData) {
val adapter = ExerciseAdapter(userType = exerciseData.userType)
adapter.updateItemList(exerciseList = exerciseData.exerciseItemInfoList)
exerciseViewModel.exerciseHistoryInfoList.observe(binding.lifecycleOwner!!) {
adapter.submitList(it.toMutableList())
}
Comment on lines +82 to +84
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
exerciseViewModel.exerciseHistoryInfoList.observe(binding.lifecycleOwner!!) {
adapter.submitList(it.toMutableList())
}
adapter.submitList(exerciseData.exerciseItemInfoList)

이미 ExerciseData를 가지고 있는데 이렇게 하면 안되나요??

Copy link
Collaborator

Choose a reason for hiding this comment

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

우선 해당 로직은 관찰하는 로직이므로 따로 빼서 observeLiveData에 넣어주는 것이 일관성 있어보입니다 !

Copy link
Collaborator

@giovannijunseokim giovannijunseokim Mar 6, 2024

Choose a reason for hiding this comment

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

그리고 준희님 말씀처럼 exerciseHistoryInfoList를 관찰해야 할까요!? 요건 저도 정확히 모르니 설명해주시면 감사하겠습니다 !

Copy link
Collaborator

Choose a reason for hiding this comment

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

추가적으로 Fragment에서 관찰하실 때에는 viewLifeCycleOwner를 사용해주는 것이 좋습니다. 그 이유까지 알아보면 더 좋을 것 같아요 ! 찾다가 잘 모르시겠으면 질문해주세요 ~~

binding.rvExerciseEachDateExercise.adapter = adapter
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ class ExerciseViewModel @Inject constructor(
private val networkRepository: NetworkRepository,
) : ViewModel() {

private val _exerciseHistoryInfoList = ArrayList<ExerciseData.ExerciseItemInfo>()
val exerciseHistoryInfoList: MutableLiveData<ArrayList<ExerciseData.ExerciseItemInfo>> by lazy {
MutableLiveData<ArrayList<ExerciseData.ExerciseItemInfo>>()
}

Comment on lines +22 to +26
Copy link
Contributor

Choose a reason for hiding this comment

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

우선 _exerciseHistoryInfoList는 사용되지 않는것 같네요.
그리고 밑에 exerciseHistoryInfo가 ExerciseData를 포함하는 UI 상태이기 때문에 추가적인 라이브데이터는 필요없을 것 같습니다!

private val _exerciseHistoryInfo = MutableLiveData<UiState<ExerciseData>>(UiState.Loading)
val exerciseHistoryInfo: LiveData<UiState<ExerciseData>> = _exerciseHistoryInfo

Expand Down
Loading