Skip to content

Commit 8f9feef

Browse files
committed
✨ 게시글 화면에서 홍보 글, 인증 글 모두 불러오도록 변경
1 parent 182b6b5 commit 8f9feef

File tree

6 files changed

+130
-43
lines changed

6 files changed

+130
-43
lines changed

data/src/main/java/com/whyranoid/data/Post/PostDataSource.kt

Lines changed: 77 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,14 @@ import com.google.firebase.firestore.Query
55
import com.whyranoid.data.constant.CollectionId
66
import com.whyranoid.data.model.GroupInfoResponse
77
import com.whyranoid.data.model.RecruitPostResponse
8+
import com.whyranoid.data.model.RunningPostResponse
89
import com.whyranoid.data.model.UserResponse
910
import com.whyranoid.data.model.toGroupInfo
1011
import com.whyranoid.data.model.toUser
1112
import com.whyranoid.domain.model.Post
1213
import com.whyranoid.domain.model.RecruitPost
14+
import com.whyranoid.domain.model.RunningHistory
15+
import com.whyranoid.domain.model.RunningPost
1316
import com.whyranoid.domain.model.toRule
1417
import kotlinx.coroutines.channels.awaitClose
1518
import kotlinx.coroutines.flow.Flow
@@ -23,52 +26,92 @@ class PostDataSource @Inject constructor(
2326
private val db: FirebaseFirestore
2427
) {
2528

26-
// TODO : 타입을 확인하고 캐스팅하는 부분 필요
29+
// TODO : 조금 더 간결하게 처리 필요.
2730
fun getAllPostFlow(): Flow<List<Post>> =
2831
callbackFlow {
2932
db.collection(CollectionId.POST_COLLECTION)
3033
.orderBy("updatedAt", Query.Direction.DESCENDING)
3134
.addSnapshotListener { snapshot, _ ->
32-
val recruitPostList = mutableListOf<Post>()
35+
val postList = mutableListOf<Post>()
3336
snapshot?.forEach { docuemnt ->
34-
docuemnt.toObject(RecruitPostResponse::class.java).let { postResponse ->
3537

36-
db.collection(CollectionId.USERS_COLLECTION)
37-
.document(postResponse.authorId)
38-
.get()
39-
.addOnSuccessListener { authorDocument ->
40-
val authorResponse =
41-
authorDocument?.toObject(UserResponse::class.java)
38+
if (docuemnt["runningHistoryId"] != null) {
39+
docuemnt.toObject(RunningPostResponse::class.java).let { postResponse ->
40+
db.collection(CollectionId.USERS_COLLECTION)
41+
.document(postResponse.authorId)
42+
.get()
43+
.addOnSuccessListener { authorDocument ->
44+
val authorResponse =
45+
authorDocument?.toObject(UserResponse::class.java)
4246

43-
authorResponse?.let {
44-
db.collection(CollectionId.GROUPS_COLLECTION)
45-
.document(postResponse.groupId)
46-
.get()
47-
.addOnSuccessListener { groupDocument ->
48-
val groupInfoResponse =
49-
groupDocument.toObject(GroupInfoResponse::class.java)
47+
authorResponse?.let {
48+
db.collection(CollectionId.RUNNING_HISTORY_COLLECTION)
49+
.document(postResponse.runningHistoryId)
50+
.get()
51+
.addOnSuccessListener { runningHistoryDocument ->
52+
val runningHistoryResponse =
53+
runningHistoryDocument.toObject(
54+
RunningHistory::class.java
55+
)
56+
57+
runningHistoryResponse?.let {
58+
val author = authorResponse.toUser()
59+
60+
postList.add(
61+
RunningPost(
62+
postId = postResponse.postId,
63+
author = author,
64+
updatedAt = postResponse.updatedAt,
65+
runningHistory = it,
66+
likeCount = 0,
67+
content = postResponse.content
68+
)
69+
)
70+
}
71+
}
72+
}
73+
}
74+
}
75+
} else {
76+
docuemnt.toObject(RecruitPostResponse::class.java).let { postResponse ->
77+
78+
db.collection(CollectionId.USERS_COLLECTION)
79+
.document(postResponse.authorId)
80+
.get()
81+
.addOnSuccessListener { authorDocument ->
82+
val authorResponse =
83+
authorDocument?.toObject(UserResponse::class.java)
84+
85+
authorResponse?.let {
86+
db.collection(CollectionId.GROUPS_COLLECTION)
87+
.document(postResponse.groupId)
88+
.get()
89+
.addOnSuccessListener { groupDocument ->
90+
val groupInfoResponse =
91+
groupDocument.toObject(GroupInfoResponse::class.java)
5092

51-
groupInfoResponse?.let { groupInfoResponse ->
52-
val author = authorResponse.toUser()
53-
recruitPostList.add(
54-
RecruitPost(
55-
postId = postResponse.postId,
56-
author = author,
57-
updatedAt = postResponse.updatedAt,
58-
groupInfo = groupInfoResponse
59-
.toGroupInfo(
60-
author,
61-
rules = groupInfoResponse.rules.map {
62-
it.toRule()
63-
}
64-
)
93+
groupInfoResponse?.let { groupInfoResponse ->
94+
val author = authorResponse.toUser()
95+
postList.add(
96+
RecruitPost(
97+
postId = postResponse.postId,
98+
author = author,
99+
updatedAt = postResponse.updatedAt,
100+
groupInfo = groupInfoResponse
101+
.toGroupInfo(
102+
author,
103+
rules = groupInfoResponse.rules.map {
104+
it.toRule()
105+
}
106+
)
107+
)
65108
)
66-
)
67-
trySend(recruitPostList)
109+
trySend(postList)
110+
}
68111
}
69-
}
112+
}
70113
}
71-
}
114+
}
72115
}
73116
}
74117
}

data/src/main/java/com/whyranoid/data/model/PostResponse.kt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,11 @@ data class RecruitPostResponse(
1212
override val postId: String = "",
1313
override val updatedAt: Long = 0L
1414
) : PostResponse
15+
16+
data class RunningPostResponse(
17+
override val postId: String = "",
18+
override val authorId: String = "",
19+
override val updatedAt: Long = 0L,
20+
val runningHistoryId: String = "",
21+
val content: String = ""
22+
) : PostResponse

presentation/src/main/java/com/whyranoid/presentation/community/PostAdapter.kt

Lines changed: 42 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,18 @@ package com.whyranoid.presentation.community
33
import android.view.LayoutInflater
44
import android.view.View
55
import android.view.ViewGroup
6+
import androidx.databinding.ViewDataBinding
67
import androidx.recyclerview.widget.DiffUtil
78
import androidx.recyclerview.widget.ListAdapter
89
import androidx.recyclerview.widget.RecyclerView
910
import com.whyranoid.domain.model.Post
1011
import com.whyranoid.domain.model.RecruitPost
12+
import com.whyranoid.domain.model.RunningPost
1113
import com.whyranoid.presentation.databinding.ItemRecruitPostBinding
14+
import com.whyranoid.presentation.databinding.ItemRunningPostBinding
1215

1316
class PostAdapter(private val myUid: String) :
14-
ListAdapter<Post, PostAdapter.RecruitPostViewHolder>(diffUtil) {
17+
ListAdapter<Post, PostAdapter.PostViewHolder>(diffUtil) {
1518

1619
companion object {
1720
val diffUtil = object : DiffUtil.ItemCallback<Post>() {
@@ -21,6 +24,14 @@ class PostAdapter(private val myUid: String) :
2124
override fun areContentsTheSame(oldItem: Post, newItem: Post) =
2225
oldItem == newItem
2326
}
27+
28+
const val RECRUIT_POST_TYPE = 0
29+
const val RUNNING_POST_TYPE = 1
30+
}
31+
32+
abstract class PostViewHolder(binding: ViewDataBinding) :
33+
RecyclerView.ViewHolder(binding.root) {
34+
abstract fun bind(post: Post)
2435
}
2536

2637
inner class RecruitPostViewHolder(
@@ -30,12 +41,11 @@ class PostAdapter(private val myUid: String) :
3041
parent,
3142
false
3243
)
33-
) : RecyclerView.ViewHolder(binding.root) {
34-
fun bind(post: Post) {
44+
) : PostViewHolder(binding) {
45+
override fun bind(post: Post) {
3546
if (post is RecruitPost) {
3647
binding.recruitPost = post
3748
if (myUid == post.author.uid) {
38-
println("테스트 $myUid ${post.author.uid}")
3949
binding.btnJoinGroup.visibility = View.GONE
4050
} else {
4151
binding.btnJoinGroup.visibility = View.VISIBLE
@@ -44,11 +54,36 @@ class PostAdapter(private val myUid: String) :
4454
}
4555
}
4656

47-
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecruitPostViewHolder {
48-
return RecruitPostViewHolder(parent)
57+
inner class RunningHistoryPostViewHolder(
58+
parent: ViewGroup,
59+
private val binding: ItemRunningPostBinding = ItemRunningPostBinding.inflate(
60+
LayoutInflater.from(parent.context),
61+
parent,
62+
false
63+
)
64+
) : PostViewHolder(binding) {
65+
override fun bind(post: Post) {
66+
if (post is RunningPost) {
67+
binding.runningPost = post
68+
}
69+
}
70+
}
71+
72+
override fun getItemViewType(position: Int): Int {
73+
return when (getItem(position)) {
74+
is RecruitPost -> RECRUIT_POST_TYPE
75+
else -> RUNNING_POST_TYPE
76+
}
77+
}
78+
79+
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): PostViewHolder {
80+
return when (viewType) {
81+
RECRUIT_POST_TYPE -> RecruitPostViewHolder(parent)
82+
else -> RunningHistoryPostViewHolder(parent)
83+
}
4984
}
5085

51-
override fun onBindViewHolder(holder: RecruitPostViewHolder, position: Int) {
86+
override fun onBindViewHolder(holder: PostViewHolder, position: Int) {
5287
holder.bind(getItem(position))
5388
}
5489
}

presentation/src/main/java/com/whyranoid/presentation/community/group/detail/GroupDetailViewModel.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ class GroupDetailViewModel @Inject constructor(
5454

5555
// TODO : uid를 DataStore에서 가져오도록 변경
5656
getGroupInfoUseCase("hsjeon", groupId).onEach { groupInfo ->
57-
println("테스트 $groupInfo")
5857
_groupInfo.value = groupInfo.toGroupInfoUiModel()
5958
}.launchIn(viewModelScope)
6059

presentation/src/main/res/layout/item_recruit_post.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
android:text="@{recruitPost.author.name}"
3232
app:layout_constraintStart_toEndOf="@id/iv_leader_profile"
3333
app:layout_constraintTop_toTopOf="parent"
34+
app:layout_constraintBottom_toBottomOf="@id/iv_leader_profile"
3435
tools:text="수피치" />
3536

3637
<TextView
@@ -44,7 +45,7 @@
4445
android:text="@{recruitPost.groupInfo.name}"
4546
app:layout_constraintEnd_toEndOf="parent"
4647
app:layout_constraintStart_toStartOf="parent"
47-
app:layout_constraintTop_toBottomOf="@id/tv_leader_name"
48+
app:layout_constraintTop_toBottomOf="@id/iv_leader_profile"
4849
tools:text="수피치와 함께 춤을\n출까 말까" />
4950

5051
<TextView

presentation/src/main/res/values/strings.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,5 +89,6 @@
8989
<string name="text_join_group">그룹 가입하기</string>
9090
<string name="text_duplicated_group_name">중복된 그룹 이름입니다. 변경해주세요!</string>
9191
<string name="text_un_duplicated_group_name">사용 가능한 그룹 이름입니다!</string>
92+
<string name="text_like_count">좋아요 %d개</string>
9293

9394
</resources>

0 commit comments

Comments
 (0)