Skip to content

Commit e9c72c9

Browse files
authored
๐Ÿ”€ #56 from boostcampwm-2022/feat/runningPost
์ธ์ฆ ๊ธ€ ๋ถˆ๋Ÿฌ์˜ค๋Š” ๊ธฐ๋Šฅ ์ถ”๊ฐ€
2 parents 2ca3d88 + 0cde809 commit e9c72c9

File tree

9 files changed

+334
-44
lines changed

9 files changed

+334
-44
lines changed

โ€Ždata/src/main/java/com/whyranoid/data/Post/PostDataSource.kt

Lines changed: 80 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,18 @@ package com.whyranoid.data.Post
33
import com.google.firebase.firestore.FirebaseFirestore
44
import com.google.firebase.firestore.Query
55
import com.whyranoid.data.constant.CollectionId
6+
import com.whyranoid.data.constant.FieldId.RUNNING_HISTORY_ID
7+
import com.whyranoid.data.constant.FieldId.UPDATED_AT
68
import com.whyranoid.data.model.GroupInfoResponse
79
import com.whyranoid.data.model.RecruitPostResponse
10+
import com.whyranoid.data.model.RunningPostResponse
811
import com.whyranoid.data.model.UserResponse
912
import com.whyranoid.data.model.toGroupInfo
1013
import com.whyranoid.data.model.toUser
1114
import com.whyranoid.domain.model.Post
1215
import com.whyranoid.domain.model.RecruitPost
16+
import com.whyranoid.domain.model.RunningHistory
17+
import com.whyranoid.domain.model.RunningPost
1318
import com.whyranoid.domain.model.toRule
1419
import kotlinx.coroutines.channels.awaitClose
1520
import kotlinx.coroutines.flow.Flow
@@ -23,52 +28,92 @@ class PostDataSource @Inject constructor(
2328
private val db: FirebaseFirestore
2429
) {
2530

26-
// TODO : ํƒ€์ž…์„ ํ™•์ธํ•˜๊ณ  ์บ์ŠคํŒ…ํ•˜๋Š” ๋ถ€๋ถ„ ํ•„์š”
31+
// TODO : ์กฐ๊ธˆ ๋” ๊ฐ„๊ฒฐํ•˜๊ฒŒ ์ฒ˜๋ฆฌ ํ•„์š”.
2732
fun getAllPostFlow(): Flow<List<Post>> =
2833
callbackFlow {
2934
db.collection(CollectionId.POST_COLLECTION)
30-
.orderBy("updatedAt", Query.Direction.DESCENDING)
35+
.orderBy(UPDATED_AT, Query.Direction.DESCENDING)
3136
.addSnapshotListener { snapshot, _ ->
32-
val recruitPostList = mutableListOf<Post>()
37+
val postList = mutableListOf<Post>()
3338
snapshot?.forEach { docuemnt ->
34-
docuemnt.toObject(RecruitPostResponse::class.java).let { postResponse ->
3539

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

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

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-
)
95+
groupInfoResponse?.let { groupInfoResponse ->
96+
val author = authorResponse.toUser()
97+
postList.add(
98+
RecruitPost(
99+
postId = postResponse.postId,
100+
author = author,
101+
updatedAt = postResponse.updatedAt,
102+
groupInfo = groupInfoResponse
103+
.toGroupInfo(
104+
author,
105+
rules = groupInfoResponse.rules.map {
106+
it.toRule()
107+
}
108+
)
109+
)
65110
)
66-
)
67-
trySend(recruitPostList)
111+
trySend(postList)
112+
}
68113
}
69-
}
114+
}
70115
}
71-
}
116+
}
72117
}
73118
}
74119
}

โ€Ždata/src/main/java/com/whyranoid/data/constant/FieldId.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,6 @@ object FieldId {
88
const val GROUP_INTRODUCE = "introduce"
99
const val RULES = "rules"
1010
const val JOINED_GROUP_LIST = "joinedGroupList"
11+
const val UPDATED_AT = "updatedAt"
12+
const val RUNNING_HISTORY_ID = "runningHistoryId"
1113
}

โ€Ž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/java/com/whyranoid/presentation/util/BindingAdapters.kt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,3 +48,9 @@ fun TextView.finishLongToDate(long: Long) {
4848
val formatter = SimpleDateFormat("yyyy.MM.dd / HH ์‹œ mm ๋ถ„ ์šด๋™ ์ข…๋ฃŒ", Locale.KOREA)
4949
text = formatter.format(Date(long))
5050
}
51+
52+
@BindingAdapter("LongToTime")
53+
fun TextView.longToTime(long: Long) {
54+
val formatter = SimpleDateFormat("HH : mm", Locale.KOREA)
55+
text = formatter.format(Date(long))
56+
}

โ€Ž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

0 commit comments

Comments
ย (0)