Skip to content

Commit 4ec59f0

Browse files
authored
🔀 #84 from boostcampwm-2022/improve/async
복잡한 callback suspend하게 변경, PagingDataSource 개선
2 parents e791eb0 + adff749 commit 4ec59f0

File tree

15 files changed

+254
-326
lines changed

15 files changed

+254
-326
lines changed

data/src/main/java/com/whyranoid/data/group/GroupRepositoryImpl.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import com.whyranoid.domain.model.GroupNotification
77
import com.whyranoid.domain.model.Rule
88
import com.whyranoid.domain.model.RunningHistory
99
import com.whyranoid.domain.repository.GroupRepository
10+
import kotlinx.coroutines.CoroutineScope
1011
import kotlinx.coroutines.flow.Flow
1112
import javax.inject.Inject
1213

@@ -20,7 +21,7 @@ class GroupRepositoryImpl @Inject constructor(
2021
return userDataSource.getMyGroupList(uid)
2122
}
2223

23-
override fun getMyGroupListFlow(uid: String) = userDataSource.getMyGroupListFlow(uid)
24+
override fun getMyGroupListFlow(uid: String, coroutineScope: CoroutineScope) = userDataSource.getMyGroupListFlow(uid, coroutineScope)
2425

2526
override suspend fun updateGroupInfo(
2627
groupId: String,

data/src/main/java/com/whyranoid/data/post/MyPostPagingDataSource.kt

Lines changed: 0 additions & 146 deletions
This file was deleted.

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package com.whyranoid.data.post
22

3+
import com.google.firebase.firestore.DocumentSnapshot
4+
import com.google.firebase.firestore.QueryDocumentSnapshot
5+
import com.google.firebase.firestore.QuerySnapshot
36
import com.whyranoid.domain.model.Post
47
import kotlinx.coroutines.flow.Flow
58

@@ -9,6 +12,16 @@ interface PostDataSource {
912

1013
fun getMyPostFlow(uid: String): Flow<List<Post>>
1114

15+
suspend fun getCurrentPagingPost(key: QuerySnapshot?): QuerySnapshot
16+
17+
suspend fun getNextPagingPost(lastDocumentSnapshot: DocumentSnapshot): QuerySnapshot
18+
19+
suspend fun getMyCurrentPagingPost(key: QuerySnapshot?, uid: String): QuerySnapshot
20+
21+
suspend fun getMyNextPagingPost(lastDocumentSnapshot: DocumentSnapshot, uid: String): QuerySnapshot
22+
23+
suspend fun convertPostType(document: QueryDocumentSnapshot): Post?
24+
1225
suspend fun createRecruitPost(
1326
authorUid: String,
1427
groupUid: String

data/src/main/java/com/whyranoid/data/post/PostDataSourceImpl.kt

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
package com.whyranoid.data.post
22

3+
import com.google.firebase.firestore.DocumentSnapshot
34
import com.google.firebase.firestore.FirebaseFirestore
45
import com.google.firebase.firestore.Query
6+
import com.google.firebase.firestore.QueryDocumentSnapshot
7+
import com.google.firebase.firestore.QuerySnapshot
58
import com.whyranoid.data.constant.CollectionId
69
import com.whyranoid.data.constant.FieldId.AUTHOR_ID
710
import com.whyranoid.data.constant.FieldId.GROUP_ID
@@ -219,6 +222,90 @@ class PostDataSourceImpl @Inject constructor(
219222
awaitClose()
220223
}
221224

225+
override suspend fun getCurrentPagingPost(key: QuerySnapshot?): QuerySnapshot {
226+
return key ?: db.collection(CollectionId.POST_COLLECTION)
227+
.orderBy(UPDATED_AT, Query.Direction.DESCENDING)
228+
.limit(DATA_COUNT_PER_PAGE)
229+
.get()
230+
.await()
231+
}
232+
233+
override suspend fun getNextPagingPost(lastDocumentSnapshot: DocumentSnapshot): QuerySnapshot {
234+
return db.collection(CollectionId.POST_COLLECTION)
235+
.orderBy(UPDATED_AT, Query.Direction.DESCENDING)
236+
.limit(DATA_COUNT_PER_PAGE).startAfter(lastDocumentSnapshot)
237+
.get()
238+
.await()
239+
}
240+
241+
override suspend fun getMyCurrentPagingPost(key: QuerySnapshot?, uid: String): QuerySnapshot {
242+
return key ?: db.collection(CollectionId.POST_COLLECTION)
243+
.whereEqualTo(AUTHOR_ID, uid)
244+
.limit(DATA_COUNT_PER_PAGE)
245+
.get()
246+
.await()
247+
}
248+
249+
override suspend fun getMyNextPagingPost(
250+
lastDocumentSnapshot: DocumentSnapshot,
251+
uid: String
252+
): QuerySnapshot {
253+
return db.collection(CollectionId.POST_COLLECTION)
254+
.whereEqualTo(AUTHOR_ID, uid)
255+
.limit(DATA_COUNT_PER_PAGE)
256+
.startAfter(lastDocumentSnapshot)
257+
.get()
258+
.await()
259+
}
260+
261+
// TODO : 예외 처리
262+
override suspend fun convertPostType(document: QueryDocumentSnapshot): Post? {
263+
return if (document[RUNNING_HISTORY_ID] != null) {
264+
document.toObject(RunningPostResponse::class.java).let { postResponse ->
265+
val authorResponse = getUserResponse(postResponse.authorId)
266+
267+
authorResponse?.let {
268+
val runningHistory = getRunningHistory(postResponse.runningHistoryId)
269+
270+
runningHistory?.let {
271+
RunningPost(
272+
postId = postResponse.postId,
273+
author = authorResponse.toUser(),
274+
updatedAt = postResponse.updatedAt,
275+
runningHistory = it,
276+
likeCount = 0,
277+
content = postResponse.content
278+
)
279+
}
280+
}
281+
}
282+
} else {
283+
document.toObject(RecruitPostResponse::class.java).let { postResponse ->
284+
val authorResponse = getUserResponse(postResponse.authorId)
285+
286+
authorResponse?.let {
287+
val groupInfoResponse = getGroupInfoResponse(postResponse.groupId)
288+
289+
groupInfoResponse?.let {
290+
val author = authorResponse.toUser()
291+
RecruitPost(
292+
postId = postResponse.postId,
293+
author = author,
294+
updatedAt = postResponse.updatedAt,
295+
groupInfo = groupInfoResponse
296+
.toGroupInfo(
297+
author,
298+
rules = groupInfoResponse.rules.map {
299+
it.toRule()
300+
}
301+
)
302+
)
303+
}
304+
}
305+
}
306+
}
307+
}
308+
222309
override suspend fun createRecruitPost(
223310
authorUid: String,
224311
groupUid: String
@@ -310,4 +397,35 @@ class PostDataSourceImpl @Inject constructor(
310397
}
311398
}
312399
}
400+
401+
// TODO : 예외 처리
402+
private suspend fun getUserResponse(userId: String): UserResponse? {
403+
return db.collection(CollectionId.USERS_COLLECTION)
404+
.document(userId)
405+
.get()
406+
.await()
407+
.toObject(UserResponse::class.java)
408+
}
409+
410+
// TODO : 예외 처리
411+
private suspend fun getRunningHistory(runningHistoryId: String): RunningHistory? {
412+
return db.collection(CollectionId.RUNNING_HISTORY_COLLECTION)
413+
.document(runningHistoryId)
414+
.get()
415+
.await()
416+
.toObject(RunningHistory::class.java)
417+
}
418+
419+
// TODO : 예외 처리
420+
private suspend fun getGroupInfoResponse(groupId: String): GroupInfoResponse? {
421+
return db.collection(CollectionId.GROUPS_COLLECTION)
422+
.document(groupId)
423+
.get()
424+
.await()
425+
.toObject(GroupInfoResponse::class.java)
426+
}
427+
428+
companion object {
429+
private const val DATA_COUNT_PER_PAGE = 10L
430+
}
313431
}

0 commit comments

Comments
 (0)