From 0050f6b657a7998b49e87ae4b72023d6fe55de33 Mon Sep 17 00:00:00 2001 From: jisungbin Date: Sun, 5 Feb 2023 19:24:12 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20adapter=20=EC=9D=B8=EC=8A=A4=ED=84=B4?= =?UTF-8?q?=EC=8A=A4=20=EC=9E=AC=ED=99=9C=EC=9A=A9=ED=95=98=EA=B2=8C=20?= =?UTF-8?q?=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../adapter/event/EventAdapter.kt | 19 +++++++++++-- .../fragment/dashboard/DashboardFragment.kt | 28 +++++++++---------- 2 files changed, 30 insertions(+), 17 deletions(-) diff --git a/presentation/src/main/kotlin/team/brave/devevent/android/presentation/adapter/event/EventAdapter.kt b/presentation/src/main/kotlin/team/brave/devevent/android/presentation/adapter/event/EventAdapter.kt index 465e29e..249dcae 100644 --- a/presentation/src/main/kotlin/team/brave/devevent/android/presentation/adapter/event/EventAdapter.kt +++ b/presentation/src/main/kotlin/team/brave/devevent/android/presentation/adapter/event/EventAdapter.kt @@ -18,12 +18,27 @@ import team.brave.devevent.android.presentation.databinding.LayoutEventBinding private const val EventViewType = 0 class EventAdapter( - private val events: List, + private var events: List, // 매번 contains 하는 시간 없애기 위해 Map 으로 변환 private val favoriteEventIds: MutableMap, private val eventItemClickListener: EventItemClickListener, ) : RecyclerView.Adapter() { - private val eventSize = events.size + private val originalEvents = events + private var eventSize = events.size + var favoriteFilter = false + set(value) { + field = value + val filteredEvents = if (value) { + originalEvents.filter { event -> favoriteEventIds[event.id] ?: false } + } else { + originalEvents + } + events = filteredEvents + eventSize = filteredEvents.size + + @Suppress("NotifyDataSetChanged") + notifyDataSetChanged() + } override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): EventViewHolder { val inflater = LayoutInflater.from(parent.context) diff --git a/presentation/src/main/kotlin/team/brave/devevent/android/presentation/fragment/dashboard/DashboardFragment.kt b/presentation/src/main/kotlin/team/brave/devevent/android/presentation/fragment/dashboard/DashboardFragment.kt index d3c826a..2218e26 100644 --- a/presentation/src/main/kotlin/team/brave/devevent/android/presentation/fragment/dashboard/DashboardFragment.kt +++ b/presentation/src/main/kotlin/team/brave/devevent/android/presentation/fragment/dashboard/DashboardFragment.kt @@ -44,7 +44,7 @@ class DashboardFragment : Fragment() { set(value) { field = value binding.isFavorite = value - updateEvents() + initAdapter() } private var _binding: FragmentEventsBinding? = null @@ -84,7 +84,7 @@ class DashboardFragment : Fragment() { set(favoriteEventId.toInt(), true) } } - updateEvents() + initAdapter(updateModels = true) } } @@ -125,12 +125,7 @@ class DashboardFragment : Fragment() { } // TODO: RecyclerView 최적화 - private fun updateEvents() { - val filteredEvents = if (isFavoriteScreen) { - events.filter { event -> favoriteEvents[event.id] ?: false } - } else { - events - } + private fun initAdapter(updateModels: Boolean = false) { val eventItemClickListener = object : EventItemClickListener { override fun onFavoriteClick(event: Event) { vm.toggleEventFavorite(event.id) @@ -158,13 +153,16 @@ class DashboardFragment : Fragment() { } val lastPosition = vm.getLastScrollPosition(isFavoriteScreen) - val adapter = EventAdapter( - events = filteredEvents, - favoriteEventIds = favoriteEvents.toMutableMap(), - eventItemClickListener = eventItemClickListener, - ) - - binding.rvEvents.adapter = adapter + if (binding.rvEvents.adapter == null || updateModels) { + val adapter = EventAdapter( + events = events, + favoriteEventIds = favoriteEvents.toMutableMap(), + eventItemClickListener = eventItemClickListener, + ) + adapter.setHasStableIds(true) + binding.rvEvents.adapter = adapter + } + (binding.rvEvents.adapter as EventAdapter).favoriteFilter = isFavoriteScreen binding.rvEvents.scrollToPosition(lastPosition) } }