-
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
[refactor] 운동모아보기 뷰 listadapter로 변경 #106
base: develop
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) { | ||
|
@@ -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>() { | ||
override fun areItemsTheSame( | ||
oldItem: ExerciseItemInfo, | ||
newItem: ExerciseItemInfo | ||
): Boolean { | ||
return oldItem == newItem | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 객체의 고유 식별자를 정해서 비교해줘야할 것 같네요! |
||
} | ||
|
||
override fun areContentsTheSame( | ||
oldItem: ExerciseItemInfo, | ||
newItem: ExerciseItemInfo | ||
): Boolean { | ||
return oldItem == newItem | ||
} | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
이미 ExerciseData를 가지고 있는데 이렇게 하면 안되나요?? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 우선 해당 로직은 관찰하는 로직이므로 따로 빼서 observeLiveData에 넣어주는 것이 일관성 있어보입니다 ! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 그리고 준희님 말씀처럼 exerciseHistoryInfoList를 관찰해야 할까요!? 요건 저도 정확히 모르니 설명해주시면 감사하겠습니다 ! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 추가적으로 Fragment에서 관찰하실 때에는 viewLifeCycleOwner를 사용해주는 것이 좋습니다. 그 이유까지 알아보면 더 좋을 것 같아요 ! 찾다가 잘 모르시겠으면 질문해주세요 ~~ |
||||||||||
binding.rvExerciseEachDateExercise.adapter = adapter | ||||||||||
} | ||||||||||
|
||||||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 우선 _exerciseHistoryInfoList는 사용되지 않는것 같네요. |
||
private val _exerciseHistoryInfo = MutableLiveData<UiState<ExerciseData>>(UiState.Loading) | ||
val exerciseHistoryInfo: LiveData<UiState<ExerciseData>> = _exerciseHistoryInfo | ||
|
||
|
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.
utli -> extension에 보시면 ItemDiffCallback 구현해둔거 있어서 그거 쓰셔도 될것 같습니다~