From b2cf4a3413e757e2ea1d98831d5c82ad301f34e1 Mon Sep 17 00:00:00 2001 From: jhg3410 <80373033+jhg3410@users.noreply.github.com> Date: Fri, 3 Mar 2023 13:24:47 +0900 Subject: [PATCH] =?UTF-8?q?fix:=20#21=20-=20=EA=B2=80=EC=83=89=EB=90=9C=20?= =?UTF-8?q?=EC=9D=B4=EB=B2=A4=ED=8A=B8(=ED=96=89=EC=82=AC)=EB=93=A4?= =?UTF-8?q?=EC=97=90=20=EB=8C=80=ED=95=9C=20=ED=81=B4=EB=A6=AD=EC=9D=B4?= =?UTF-8?q?=EB=B2=A4=ED=8A=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ui/adapter/like/LikePagingAdapter.kt | 34 ++++++++++++++++--- .../org/inu/events/ui/home/SearchActivity.kt | 11 +++++- .../org/inu/events/ui/home/SearchViewModel.kt | 3 +- 3 files changed, 42 insertions(+), 6 deletions(-) diff --git a/app/src/main/java/org/inu/events/ui/adapter/like/LikePagingAdapter.kt b/app/src/main/java/org/inu/events/ui/adapter/like/LikePagingAdapter.kt index 7a3f85e..ae5c92f 100644 --- a/app/src/main/java/org/inu/events/ui/adapter/like/LikePagingAdapter.kt +++ b/app/src/main/java/org/inu/events/ui/adapter/like/LikePagingAdapter.kt @@ -1,6 +1,7 @@ package org.inu.events.ui.adapter.like import android.view.LayoutInflater +import android.view.View import android.view.ViewGroup import androidx.paging.PagingDataAdapter import androidx.recyclerview.widget.DiffUtil @@ -8,9 +9,14 @@ import androidx.recyclerview.widget.RecyclerView import org.inu.events.data.model.entity.Event import org.inu.events.databinding.ItemLikeEventBinding -class LikePagingAdapter : PagingDataAdapter(LikeDiffUtil) { +class LikePagingAdapter( + val onClickLikeIcon: (eventId: Int, view: View) -> Unit, + val onClickDetail: (event: Event, view: View) -> Unit +) : PagingDataAdapter(LikeDiffUtil) { - override fun onCreateViewHolder(parent: ViewGroup, viewType: Int) = ViewHolder.from(parent) + override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder { + return ViewHolder.from(parent, onClickLikeIcon, onClickDetail) + } override fun onBindViewHolder(holder: ViewHolder, position: Int) { getItem(position)?.let { event -> @@ -19,20 +25,40 @@ class LikePagingAdapter : PagingDataAdapter } class ViewHolder private constructor( + onClickLikeIcon: (eventId: Int, view: View) -> Unit, + onClickDetail: (event: Event, view: View) -> Unit, val binding: ItemLikeEventBinding ) : RecyclerView.ViewHolder(binding.root) { + init { + binding.bookmarkIcon.setOnClickListener { + binding.item?.let { event -> + onClickLikeIcon(event.id, it) + } + } + + binding.cardWrap.setOnClickListener { + binding.item?.let { event -> + onClickDetail(event, it) + } + } + } + fun bind(item: Event) { binding.item = item binding.executePendingBindings() } companion object { - fun from(parent: ViewGroup): ViewHolder { + fun from( + parent: ViewGroup, + onClickLikeIcon: (eventId: Int, view: View) -> Unit, + onClickDetail: (event: Event, view: View) -> Unit + ): ViewHolder { val layoutInflater = LayoutInflater.from(parent.context) val binding = ItemLikeEventBinding.inflate(layoutInflater, parent, false) - return ViewHolder(binding) + return ViewHolder(onClickLikeIcon, onClickDetail, binding) } } } diff --git a/app/src/main/java/org/inu/events/ui/home/SearchActivity.kt b/app/src/main/java/org/inu/events/ui/home/SearchActivity.kt index 182fc26..a4ce2e8 100644 --- a/app/src/main/java/org/inu/events/ui/home/SearchActivity.kt +++ b/app/src/main/java/org/inu/events/ui/home/SearchActivity.kt @@ -10,12 +10,21 @@ import kotlinx.coroutines.flow.collectLatest import kotlinx.coroutines.launch import org.inu.events.databinding.ActivitySearchBinding import org.inu.events.ui.adapter.like.LikePagingAdapter +import org.inu.events.ui.mypage.store.LikeViewModel class SearchActivity : AppCompatActivity() { private val vm: SearchViewModel by viewModels() + private val likeVm: LikeViewModel by viewModels() private lateinit var binding: ActivitySearchBinding - private val adapter = LikePagingAdapter() + private val adapter = LikePagingAdapter( + onClickLikeIcon = { eventId, view -> + likeVm.onClickLikeIcon(eventId, view) + }, + onClickDetail = { event, view -> + likeVm.onClickDetail(view, event) + } + ) override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) diff --git a/app/src/main/java/org/inu/events/ui/home/SearchViewModel.kt b/app/src/main/java/org/inu/events/ui/home/SearchViewModel.kt index 506d99e..2494480 100644 --- a/app/src/main/java/org/inu/events/ui/home/SearchViewModel.kt +++ b/app/src/main/java/org/inu/events/ui/home/SearchViewModel.kt @@ -3,6 +3,7 @@ package org.inu.events.ui.home import androidx.lifecycle.ViewModel import androidx.lifecycle.viewModelScope import androidx.paging.PagingData +import androidx.paging.cachedIn import kotlinx.coroutines.FlowPreview import kotlinx.coroutines.Job import kotlinx.coroutines.flow.* @@ -51,7 +52,7 @@ class SearchViewModel : ViewModel(), KoinComponent { categoryId = category.value, eventStatus = eventStatus.value, content = searchText.value - ).collectLatest { pagingData -> + ).cachedIn(viewModelScope).collectLatest { pagingData -> searchResult.value = pagingData } }