diff --git a/app/src/main/java/org/inu/events/ui/adapter/SearchPagingAdapter.kt b/app/src/main/java/org/inu/events/ui/adapter/SearchPagingAdapter.kt index 38471d5..e0f6d3d 100644 --- a/app/src/main/java/org/inu/events/ui/adapter/SearchPagingAdapter.kt +++ b/app/src/main/java/org/inu/events/ui/adapter/SearchPagingAdapter.kt @@ -6,11 +6,17 @@ import androidx.paging.PagingDataAdapter import androidx.recyclerview.widget.DiffUtil import androidx.recyclerview.widget.RecyclerView import org.inu.events.data.model.entity.Event -import org.inu.events.databinding.ItemLikeEventBinding +import org.inu.events.databinding.ItemSearchEventBinding -class SearchPagingAdapter : PagingDataAdapter(LikeDiffUtil) { +class SearchPagingAdapter( + val onClickEvent: (event: Event) -> Unit, + val onCLickLikeIcon: (event: Event) -> Boolean +) : + PagingDataAdapter(SearchDiffUtil) { - override fun onCreateViewHolder(parent: ViewGroup, viewType: Int) = ViewHolder.from(parent) + override fun onCreateViewHolder(parent: ViewGroup, viewType: Int) = ViewHolder.from( + parent, onClickEvent, onCLickLikeIcon + ) override fun onBindViewHolder(holder: ViewHolder, position: Int) { getItem(position)?.let { event -> @@ -19,25 +25,49 @@ class SearchPagingAdapter : PagingDataAdapter Unit, + val _onCLickLikeIcon: (event: Event) -> Boolean ) : RecyclerView.ViewHolder(binding.root) { + + init { + onClickEvent() + onClickLikeIcon() + } + + private fun onClickEvent() { + itemView.setOnClickListener { + binding.item?.let(_onClickEvent) + } + } + + private fun onClickLikeIcon() { + binding.bookmarkIcon.setOnClickListener { + binding.item?.let(_onCLickLikeIcon) + } + } + fun bind(item: Event) { binding.item = item binding.executePendingBindings() } companion object { - fun from(parent: ViewGroup): ViewHolder { + fun from( + parent: ViewGroup, + onClickEvent: (event: Event) -> Unit, + onCLickLikeIcon: (event: Event) -> Boolean + ): ViewHolder { val layoutInflater = LayoutInflater.from(parent.context) - val binding = ItemLikeEventBinding.inflate(layoutInflater, parent, false) + val binding = ItemSearchEventBinding.inflate(layoutInflater, parent, false) - return ViewHolder(binding) + return ViewHolder(binding, onClickEvent, onCLickLikeIcon) } } } - companion object LikeDiffUtil : DiffUtil.ItemCallback() { + companion object SearchDiffUtil : DiffUtil.ItemCallback() { override fun areItemsTheSame(oldItem: Event, newItem: Event) = oldItem.id == newItem.id override fun areContentsTheSame(oldItem: Event, newItem: Event) = oldItem == newItem 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 699cb61..8c4ad38 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 @@ -8,14 +8,23 @@ import androidx.lifecycle.lifecycleScope import androidx.lifecycle.repeatOnLifecycle import kotlinx.coroutines.flow.collectLatest import kotlinx.coroutines.launch +import org.inu.events.data.model.entity.Event import org.inu.events.databinding.ActivitySearchBinding import org.inu.events.ui.adapter.SearchPagingAdapter +import org.inu.events.ui.detail.DetailActivity class SearchActivity : AppCompatActivity() { private val vm: SearchViewModel by viewModels() private lateinit var binding: ActivitySearchBinding - private val adapter = SearchPagingAdapter() + private val adapter = SearchPagingAdapter( + onClickEvent = { + onClickSearchedEvent(it) + }, + onCLickLikeIcon = { + onClickLikeIcon(it) + } + ) override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) @@ -51,4 +60,12 @@ class SearchActivity : AppCompatActivity() { finish() } } + + private fun onClickSearchedEvent(event: Event) { + startActivity(DetailActivity.callingIntent(this, event.id, event.wroteByMe)) + } + + private fun onClickLikeIcon(event: Event): Boolean { + return false + } } \ No newline at end of file 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 } } diff --git a/app/src/main/res/layout/item_search_event.xml b/app/src/main/res/layout/item_search_event.xml new file mode 100644 index 0000000..6e0aa68 --- /dev/null +++ b/app/src/main/res/layout/item_search_event.xml @@ -0,0 +1,102 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + +