Skip to content

Commit

Permalink
feat: #21 - 검색 아이템 UI 및, 클릭 이벤트 처리
Browse files Browse the repository at this point in the history
  • Loading branch information
jhg3410 committed Mar 5, 2023
1 parent bf3d3b3 commit 378b054
Show file tree
Hide file tree
Showing 4 changed files with 160 additions and 10 deletions.
46 changes: 38 additions & 8 deletions app/src/main/java/org/inu/events/ui/adapter/SearchPagingAdapter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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<Event, SearchPagingAdapter.ViewHolder>(LikeDiffUtil) {
class SearchPagingAdapter(
val onClickEvent: (event: Event) -> Unit,
val onCLickLikeIcon: (event: Event) -> Boolean
) :
PagingDataAdapter<Event, SearchPagingAdapter.ViewHolder>(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 ->
Expand All @@ -19,25 +25,49 @@ class SearchPagingAdapter : PagingDataAdapter<Event, SearchPagingAdapter.ViewHol
}

class ViewHolder private constructor(
val binding: ItemLikeEventBinding
val binding: ItemSearchEventBinding,
val _onClickEvent: (event: Event) -> 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<Event>() {
companion object SearchDiffUtil : DiffUtil.ItemCallback<Event>() {
override fun areItemsTheSame(oldItem: Event, newItem: Event) = oldItem.id == newItem.id

override fun areContentsTheSame(oldItem: Event, newItem: Event) = oldItem == newItem
Expand Down
19 changes: 18 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 @@ -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)
Expand Down Expand Up @@ -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
}
}
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
102 changes: 102 additions & 0 deletions app/src/main/res/layout/item_search_event.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
<?xml version="1.0" encoding="utf-8"?>
<layout>

<data>

<variable
name="item"
type="org.inu.events.data.model.entity.Event" />
</data>

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="140dp"
android:layout_marginHorizontal="@dimen/horizontal_margin"
android:layout_marginTop="4dp"
android:layout_marginBottom="16dp"
android:elevation="8dp"
app:cardCornerRadius="6dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">

<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/card_wrap"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingHorizontal="12dp">

<ImageView
android:id="@+id/poster"
bindImageRadius4="@{item.imageUuid}"
android:layout_width="85dp"
android:layout_height="120dp"
android:background="@drawable/outline_border_4"
android:padding="1dp"
android:scaleType="centerCrop"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<TextView
android:id="@+id/title"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:ellipsize="end"
android:maxLines="1"
android:text="@{item.title}"
android:textColor="@color/black1"
android:textSize="16sp"
android:textStyle="bold"
app:layout_constraintEnd_toStartOf="@id/bookmark_icon"
app:layout_constraintStart_toEndOf="@id/poster"
app:layout_constraintTop_toTopOf="@id/poster"
tools:text="현장체험 (인천십) 실습생 모집 공고 근데 더 길게 써야 한다." />

<ImageButton
android:id="@+id/bookmark_icon"
android:layout_width="24dp"
android:layout_height="24dp"
android:background="@{item.likedByMe ? @drawable/ic_bookmark_filled : @drawable/ic_bookmark_outlined}"
app:layout_constraintBottom_toBottomOf="@id/title"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="@id/title"
tools:src="@drawable/ic_bookmark_filled" />

<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
android:ellipsize="end"
android:maxLines="3"
android:text="@{item.body}"
android:textColor="@color/black60"
android:textSize="13sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="@id/title"
app:layout_constraintTop_toBottomOf="@id/title"
tools:text="2021년 스타트업 현장체험(인턴십) 실습생 모집합니다. 인천대학교 창업지원단 홈페이지에서 양식을 다운받아 창업지원단 창업지원단 창업지원단 창업지원단 창업지원단 창업지원단 창업지원단 창업지원단" />

<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text='@{"댓글 " + item.comments}'
android:textColor="@color/black20"
android:textSize="13sp"
app:layout_constraintBottom_toBottomOf="@id/poster"
app:layout_constraintStart_toStartOf="@id/title"
tools:text="댓글 12" />

</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>

0 comments on commit 378b054

Please sign in to comment.