Skip to content

Commit

Permalink
feat: adapter 인스턴스 재활용하게 변경
Browse files Browse the repository at this point in the history
  • Loading branch information
jisungbin committed Feb 5, 2023
1 parent 30bfd0a commit 0050f6b
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,27 @@ import team.brave.devevent.android.presentation.databinding.LayoutEventBinding
private const val EventViewType = 0

class EventAdapter(
private val events: List<Event>,
private var events: List<Event>,
// 매번 contains 하는 시간 없애기 위해 Map 으로 변환
private val favoriteEventIds: MutableMap<Int, Boolean>,
private val eventItemClickListener: EventItemClickListener,
) : RecyclerView.Adapter<EventViewHolder>() {
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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class DashboardFragment : Fragment() {
set(value) {
field = value
binding.isFavorite = value
updateEvents()
initAdapter()
}

private var _binding: FragmentEventsBinding? = null
Expand Down Expand Up @@ -84,7 +84,7 @@ class DashboardFragment : Fragment() {
set(favoriteEventId.toInt(), true)
}
}
updateEvents()
initAdapter(updateModels = true)
}
}

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
}
}

0 comments on commit 0050f6b

Please sign in to comment.