diff --git a/app/src/main/java/sopt/motivoo/presentation/exercise/ExerciseAdapter.kt b/app/src/main/java/sopt/motivoo/presentation/exercise/ExerciseAdapter.kt index 721dc894..87cfecfb 100644 --- a/app/src/main/java/sopt/motivoo/presentation/exercise/ExerciseAdapter.kt +++ b/app/src/main/java/sopt/motivoo/presentation/exercise/ExerciseAdapter.kt @@ -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() { - private var exerciseItemInfoList: List = emptyList() - +class ExerciseAdapter(private val userType: String) : ListAdapter(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) { - 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() { + override fun areItemsTheSame( + oldItem: ExerciseItemInfo, + newItem: ExerciseItemInfo + ): Boolean { + return oldItem == newItem + } + + override fun areContentsTheSame( + oldItem: ExerciseItemInfo, + newItem: ExerciseItemInfo + ): Boolean { + return oldItem == newItem + } + } } } diff --git a/app/src/main/java/sopt/motivoo/presentation/exercise/ExerciseFragment.kt b/app/src/main/java/sopt/motivoo/presentation/exercise/ExerciseFragment.kt index 7b49582e..03c6dc67 100644 --- a/app/src/main/java/sopt/motivoo/presentation/exercise/ExerciseFragment.kt +++ b/app/src/main/java/sopt/motivoo/presentation/exercise/ExerciseFragment.kt @@ -79,7 +79,9 @@ class ExerciseFragment : BindingFragment(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()) + } binding.rvExerciseEachDateExercise.adapter = adapter } diff --git a/app/src/main/java/sopt/motivoo/presentation/exercise/ExerciseViewModel.kt b/app/src/main/java/sopt/motivoo/presentation/exercise/ExerciseViewModel.kt index 45226845..04e3e3ca 100644 --- a/app/src/main/java/sopt/motivoo/presentation/exercise/ExerciseViewModel.kt +++ b/app/src/main/java/sopt/motivoo/presentation/exercise/ExerciseViewModel.kt @@ -19,6 +19,11 @@ class ExerciseViewModel @Inject constructor( private val networkRepository: NetworkRepository, ) : ViewModel() { + private val _exerciseHistoryInfoList = ArrayList() + val exerciseHistoryInfoList: MutableLiveData> by lazy { + MutableLiveData>() + } + private val _exerciseHistoryInfo = MutableLiveData>(UiState.Loading) val exerciseHistoryInfo: LiveData> = _exerciseHistoryInfo