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 } }