์ด๋ฒ์๋ ๊ฒ์๋ฌผ์ ์ธ๋ถ ๊ธฐ๋ฅ๋ค์ ๋ง๋ค์ด๋ณด๊ฒ ๋ค.
๊ฒ์๊ธ ๋ฆฌ์คํธ recycler view item
<?xml version="1.0" encoding="utf-8"?>
<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">
<de.hdodenhof.circleimageview.CircleImageView
android:id="@+id/post_user"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginStart="28dp"
android:layout_marginBottom="8dp"
android:background="@drawable/post_img"
app:layout_constraintBottom_toTopOf="@+id/post_main_img"
app:layout_constraintStart_toStartOf="parent" />
<TextView
android:id="@+id/post_user_text"
android:layout_width="230dp"
android:layout_height="wrap_content"
android:fontFamily="@font/cafe24ssurround"
android:text="username"
android:textSize="17sp"
android:maxLines="1"
android:ellipsize="end"
android:layout_marginStart="20dp"
app:layout_constraintBottom_toBottomOf="@+id/post_user"
app:layout_constraintStart_toEndOf="@+id/post_user"
app:layout_constraintTop_toTopOf="@+id/post_user" />
<TextView
android:id="@+id/post_timestamp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="@font/cafe24ssurround"
android:text="time"
android:textSize="10sp"
android:layout_marginEnd="30dp"
app:layout_constraintBottom_toBottomOf="@+id/post_user_text"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="@+id/post_user_text" />
<androidx.cardview.widget.CardView
android:id="@+id/post_main_img"
android:layout_width="350dp"
android:layout_height="350dp"
android:layout_marginTop="65dp"
app:cardCornerRadius="20dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<ImageView
android:id="@+id/post_main_image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/post_img" />
</androidx.cardview.widget.CardView>
<View
android:id="@+id/line"
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#9A9A9A"
android:layout_marginTop="15dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/post_main_img" />
<ImageView
android:id="@+id/like"
android:layout_width="40dp"
android:layout_height="40dp"
android:src="@drawable/hanger_unselected"
app:layout_constraintStart_toStartOf="@+id/post_main_img"
app:layout_constraintTop_toBottomOf="@+id/line" />
<ImageView
android:id="@+id/star"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginStart="12dp"
android:src="@drawable/star_unselected"
app:layout_constraintBottom_toBottomOf="@+id/like"
app:layout_constraintStart_toEndOf="@+id/like"
app:layout_constraintTop_toTopOf="@+id/like"
app:layout_constraintVertical_bias="1.0" />
<ImageView
android:id="@+id/comment"
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_marginStart="15dp"
android:src="@drawable/comment_unselected"
app:layout_constraintBottom_toBottomOf="@+id/star"
app:layout_constraintStart_toEndOf="@+id/star"
app:layout_constraintTop_toTopOf="@+id/star" />
<ImageView
android:id="@+id/bookmark"
android:layout_width="30dp"
android:layout_height="30dp"
android:src="@drawable/tag_unselected"
app:layout_constraintEnd_toEndOf="@+id/post_main_img"
app:layout_constraintTop_toTopOf="@+id/comment" />
<TextView
android:id="@+id/like_count"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="@font/cafe24ssurround"
android:text="0"
app:layout_constraintEnd_toEndOf="@+id/like"
app:layout_constraintStart_toStartOf="@+id/like"
app:layout_constraintTop_toBottomOf="@+id/like" />
<TextView
android:id="@+id/post_str"
android:layout_width="350dp"
android:layout_height="100dp"
android:text="์ค๋ช
"
android:fontFamily="@font/cafe24ssurround"
android:textSize="20dp"
android:textColor="@color/black"
android:layout_marginTop="70dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/line" />
<TextView
android:id="@+id/hasi1_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:fontFamily="@font/cafe24ssurround"
android:text="#์
์ธ "
android:textSize="15dp"
app:layout_constraintStart_toStartOf="@+id/post_str"
app:layout_constraintTop_toBottomOf="@+id/post_str" />
<TextView
android:id="@+id/hasi2_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:fontFamily="@font/cafe24ssurround"
android:text="#๋งจํฌ๋งจ"
android:layout_marginStart="15dp"
android:textSize="15dp"
app:layout_constraintStart_toEndOf="@+id/hasi1_text"
app:layout_constraintTop_toBottomOf="@+id/post_str" />
<TextView
android:id="@+id/hasi3_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:fontFamily="@font/cafe24ssurround"
android:text="#๊พธ์๊พธ"
android:layout_marginStart="15dp"
android:textSize="15dp"
app:layout_constraintStart_toEndOf="@+id/hasi2_text"
app:layout_constraintTop_toBottomOf="@+id/post_str" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginTop="25dp"
android:background="@color/black"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/hasi1_text" />
</androidx.constraintlayout.widget.ConstraintLayout>
์ด๋ฒ์๋ ์ท๊ฑธ์ด(์ข์์ ์นด์ดํธ), ๋ถ๋งํฌ ๊ธฐ๋ฅ๋ง ๊ตฌํํด ๋ณด๊ฒ ๋ค.
์๋ ์๋ recyclerView Adapter ์ฝ๋์์
. . .
override fun onBindViewHolder(holder: MainPageFragmentRecyclerAdapter.CustomViewHolder, position: Int) {
. . .
// like ๋ฒํผ ์ด๋ฒคํธ
holder.like.setOnClickListener {
favoriteEvent(position)
}
holder.bookMark.setOnClickListener {
bookMarkEvent(position)
}
// like count, like image ์ด๋ฒคํธ
// like๋ฅผ ํด๋ฆญํ ๊ฒฝ์ฐ
if (postDto[position].favorite.containsKey(uid)) {
holder.like.setImageResource(R.drawable.hanger_selected)
} else { // like๋ฅผ ํด๋ฆญํ์ง ์์ ๊ฒฝ์ฐ
holder.like.setImageResource(R.drawable.hanger_unselected)
}
if (postDto[position].bookmark.containsKey(uid)) {
holder.bookMark.setImageResource(R.drawable.tag_selected)
} else {
holder.bookMark.setImageResource(R.drawable.tag_unselected)
}
. . .
}
viewHolder ๋ถ๋ถ์ ํด๋ฆญ ์ด๋ฒคํธ๋ฅผ ์ค์ ํด ์ค๋ค.
ํด๋ฆญํ๋ฉด ์ด๋ฏธ์ง๊ฐ ๋ฐ๋ ์ ์๊ฒ ํด ์คฌ๋ค.
์ข์์ ์นด์ดํธ ์ธ๊ธฐ
private fun favoriteEvent(position: Int) {
val tsDoc = fireStore.collection("posts").document(postUidList[position])
fireStore.runTransaction { transition ->
val postDto = transition.get(tsDoc).toObject(PostDto::class.java)
// ์ข์์๊ฐ ๋๋ฆฐ ๊ฒฝ์ฐ
if (postDto!!.favorite.containsKey(uid)) {
postDto.favoriteCount -= 1
postDto.favorite.remove(uid)
} else { // ๋๋ฆฌ์ง ์์ ๊ฒฝ์ฐ
postDto.favoriteCount += 1
postDto.favorite[uid!!] = true
}
transition.set(tsDoc, postDto)
}
}
fireStore์์ posts์ ํด๋น๋๋ post์ ์ ๊ทผํด
๋๊ฐ ์ข์์๋ฅผ ๋๋ ๋์ง ํด๋น ์ ์ ์ uid๋ฅผ ๋ถ๋ฌ์์ true๋ก ์ง์ ํด ์ฃผ์๊ณ ,
์ข์์ ์นด์ดํธ๋ฅผ ์ฆ๊ฐ์์ผฐ๋ค.
๋ถ๋งํฌ ๋ฐ์ดํฐ๋ฒ ์ด์ค์ ์ ์ฅํ๊ธฐ
private fun bookMarkEvent(position: Int) {
val tsDoc = fireStore.collection("posts").document(postUidList[position])
fireStore.runTransaction {
val post = it.get(tsDoc).toObject(PostDto::class.java)
if (post!!.bookmark.containsKey(uid)) {
post.bookmark.remove(uid)
} else {
post.bookmark[uid!!] = true
}
it.set(tsDoc, post)
}
}
fireStore์์ posts์ ํด๋น๋๋ post์ ์ ๊ทผํด
๋๊ฐ ๋ถ๋งํฌ๋ฅผ ๋๋ ๋์ง true๋ก ๋ํ๋๊ฒ ํ๋ค.
๊ฒฐ๊ณผ
์ข์์ ์ด๋ฏธ์ง๊ฐ ์ ๋ณ๊ฒฝ๋์๊ณ , ์ข์์ ์นด์ดํธ์ ์ข์์๋ฅผ ๋๋ฅธ ์ ์ ์ uid๊ฐ ์ ์์ ์ผ๋ก ๋ค์ด์๋ค.
๋ถ๋งํฌ ์ด๋ฏธ์ง ์ ๋ณ๊ฒฝ๋ ์ ๋์๊ณ , ๋๋ฅธ ์ ์ ์ ์ ๋ณด๋ ์ ๋ค์ด์๋ค.
๋ค์ ํฌ์คํ ์๋ ๋ถ๋งํฌ ๋ฆฌ์คํธ๋ฅผ ๋ง๋ค์ด ๋ณด๊ฒ ๋ค.