Skip to content

Commit

Permalink
fix: #21 - 검색된 이벤트(행사)들에 대한 클릭이벤트
Browse files Browse the repository at this point in the history
  • Loading branch information
jhg3410 committed Mar 3, 2023
1 parent 56f0955 commit b2cf4a3
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
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
import androidx.recyclerview.widget.RecyclerView
import org.inu.events.data.model.entity.Event
import org.inu.events.databinding.ItemLikeEventBinding

class LikePagingAdapter : PagingDataAdapter<Event, LikePagingAdapter.ViewHolder>(LikeDiffUtil) {
class LikePagingAdapter(
val onClickLikeIcon: (eventId: Int, view: View) -> Unit,
val onClickDetail: (event: Event, view: View) -> Unit
) : PagingDataAdapter<Event, LikePagingAdapter.ViewHolder>(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 ->
Expand All @@ -19,20 +25,40 @@ class LikePagingAdapter : PagingDataAdapter<Event, LikePagingAdapter.ViewHolder>
}

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)
}
}
}
Expand Down
11 changes: 10 additions & 1 deletion app/src/main/java/org/inu/events/ui/home/SearchActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion app/src/main/java/org/inu/events/ui/home/SearchViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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.*
Expand Down Expand Up @@ -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
}
}
Expand Down

0 comments on commit b2cf4a3

Please sign in to comment.