Skip to content

Commit 940b318

Browse files
committed
✨ PagingDataSource의 Firebase 의존성 제거
1 parent dca30bd commit 940b318

File tree

7 files changed

+115
-109
lines changed

7 files changed

+115
-109
lines changed

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

Lines changed: 9 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,12 @@ 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 convertPostType(document: QueryDocumentSnapshot): Post?
20+
1221
suspend fun createRecruitPost(
1322
authorUid: String,
1423
groupUid: String

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

Lines changed: 88 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,87 @@ 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+
// TODO : 예외 처리
242+
override suspend fun convertPostType(document: QueryDocumentSnapshot): Post? {
243+
return if (document[RUNNING_HISTORY_ID] != null) {
244+
document.toObject(RunningPostResponse::class.java).let { postResponse ->
245+
val authorResponse = db.collection(CollectionId.USERS_COLLECTION)
246+
.document(postResponse.authorId)
247+
.get()
248+
.await()
249+
.toObject(UserResponse::class.java)
250+
251+
authorResponse?.let {
252+
val runningHistory =
253+
db.collection(CollectionId.RUNNING_HISTORY_COLLECTION)
254+
.document(postResponse.runningHistoryId)
255+
.get()
256+
.await()
257+
.toObject(RunningHistory::class.java)
258+
259+
runningHistory?.let {
260+
RunningPost(
261+
postId = postResponse.postId,
262+
author = authorResponse.toUser(),
263+
updatedAt = postResponse.updatedAt,
264+
runningHistory = it,
265+
likeCount = 0,
266+
content = postResponse.content
267+
)
268+
}
269+
}
270+
}
271+
} else {
272+
document.toObject(RecruitPostResponse::class.java).let { postResponse ->
273+
val authorResponse = db.collection(CollectionId.USERS_COLLECTION)
274+
.document(postResponse.authorId)
275+
.get()
276+
.await()
277+
.toObject(UserResponse::class.java)
278+
279+
authorResponse?.let {
280+
val groupInfoResponse = db.collection(CollectionId.GROUPS_COLLECTION)
281+
.document(postResponse.groupId)
282+
.get()
283+
.await()
284+
.toObject(GroupInfoResponse::class.java)
285+
286+
groupInfoResponse?.let {
287+
val author = authorResponse.toUser()
288+
RecruitPost(
289+
postId = postResponse.postId,
290+
author = author,
291+
updatedAt = postResponse.updatedAt,
292+
groupInfo = groupInfoResponse
293+
.toGroupInfo(
294+
author,
295+
rules = groupInfoResponse.rules.map {
296+
it.toRule()
297+
}
298+
)
299+
)
300+
}
301+
}
302+
}
303+
}
304+
}
305+
222306
override suspend fun createRecruitPost(
223307
authorUid: String,
224308
groupUid: String
@@ -310,4 +394,8 @@ class PostDataSourceImpl @Inject constructor(
310394
}
311395
}
312396
}
397+
398+
companion object {
399+
private const val DATA_COUNT_PER_PAGE = 10L
400+
}
313401
}

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

Lines changed: 7 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -2,31 +2,11 @@ package com.whyranoid.data.post
22

33
import androidx.paging.PagingSource
44
import androidx.paging.PagingState
5-
import com.google.firebase.firestore.FirebaseFirestore
6-
import com.google.firebase.firestore.Query
75
import com.google.firebase.firestore.QuerySnapshot
8-
import com.whyranoid.data.constant.CollectionId
9-
import com.whyranoid.data.constant.CollectionId.POST_COLLECTION
10-
import com.whyranoid.data.constant.FieldId.RUNNING_HISTORY_ID
11-
import com.whyranoid.data.constant.FieldId.UPDATED_AT
12-
import com.whyranoid.data.model.GroupInfoResponse
13-
import com.whyranoid.data.model.RecruitPostResponse
14-
import com.whyranoid.data.model.RunningPostResponse
15-
import com.whyranoid.data.model.UserResponse
16-
import com.whyranoid.data.model.toGroupInfo
17-
import com.whyranoid.data.model.toUser
186
import com.whyranoid.domain.model.Post
19-
import com.whyranoid.domain.model.RecruitPost
20-
import com.whyranoid.domain.model.RunningHistory
21-
import com.whyranoid.domain.model.RunningPost
22-
import com.whyranoid.domain.model.toRule
23-
import kotlinx.coroutines.tasks.await
24-
import javax.inject.Inject
25-
import javax.inject.Singleton
267

27-
@Singleton
28-
class PostPagingDataSource @Inject constructor(
29-
private val db: FirebaseFirestore
8+
class PostPagingDataSource(
9+
private val postDataSource: PostDataSource
3010
) : PagingSource<QuerySnapshot, Post>() {
3111

3212
override fun getRefreshKey(state: PagingState<QuerySnapshot, Post>): QuerySnapshot? {
@@ -36,93 +16,23 @@ class PostPagingDataSource @Inject constructor(
3616
override suspend fun load(params: LoadParams<QuerySnapshot>): LoadResult<QuerySnapshot, Post> {
3717
return try {
3818
val postList = mutableListOf<Post>()
19+
3920
// 현재 페이지
40-
val currentPage = params.key ?: db.collection(POST_COLLECTION)
41-
.orderBy(UPDATED_AT, Query.Direction.DESCENDING)
42-
.limit(DATA_COUNT_PER_PAGE)
43-
.get()
44-
.await()
21+
val currentPage = postDataSource.getCurrentPagingPost(params.key)
4522

4623
// Post 타입 캐스팅
4724
// TODO 예외 처리
4825
currentPage.forEach { document ->
49-
50-
if (document[RUNNING_HISTORY_ID] != null) {
51-
document.toObject(RunningPostResponse::class.java).let { postResponse ->
52-
val authorResponse = db.collection(CollectionId.USERS_COLLECTION)
53-
.document(postResponse.authorId)
54-
.get()
55-
.await()
56-
.toObject(UserResponse::class.java)
57-
58-
authorResponse?.let {
59-
val runningHistory =
60-
db.collection(CollectionId.RUNNING_HISTORY_COLLECTION)
61-
.document(postResponse.runningHistoryId)
62-
.get()
63-
.await()
64-
.toObject(RunningHistory::class.java)
65-
66-
runningHistory?.let {
67-
postList.add(
68-
RunningPost(
69-
postId = postResponse.postId,
70-
author = authorResponse.toUser(),
71-
updatedAt = postResponse.updatedAt,
72-
runningHistory = it,
73-
likeCount = 0,
74-
content = postResponse.content
75-
)
76-
)
77-
}
78-
}
79-
}
80-
} else {
81-
document.toObject(RecruitPostResponse::class.java).let { postResponse ->
82-
val authorResponse = db.collection(CollectionId.USERS_COLLECTION)
83-
.document(postResponse.authorId)
84-
.get()
85-
.await()
86-
.toObject(UserResponse::class.java)
87-
88-
authorResponse?.let {
89-
val groupInfoResponse = db.collection(CollectionId.GROUPS_COLLECTION)
90-
.document(postResponse.groupId)
91-
.get()
92-
.await()
93-
.toObject(GroupInfoResponse::class.java)
94-
95-
groupInfoResponse?.let {
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-
)
110-
)
111-
}
112-
}
113-
}
26+
postDataSource.convertPostType(document)?.let { post ->
27+
postList.add(post)
11428
}
11529
}
11630

11731
// 마지막 스냅샷 저장
11832
val lastDocumentSnapshot = currentPage.documents[currentPage.size() - 1]
11933

12034
// 마지막 스냅샷 이후 페이지 불러오기
121-
val nextPage = db.collection(POST_COLLECTION)
122-
.orderBy(UPDATED_AT, Query.Direction.DESCENDING)
123-
.limit(DATA_COUNT_PER_PAGE).startAfter(lastDocumentSnapshot)
124-
.get()
125-
.await()
35+
val nextPage = postDataSource.getNextPagingPost(lastDocumentSnapshot)
12636

12737
LoadResult.Page(
12838
data = postList,
@@ -133,8 +43,4 @@ class PostPagingDataSource @Inject constructor(
13343
LoadResult.Error(e)
13444
}
13545
}
136-
137-
companion object {
138-
private const val DATA_COUNT_PER_PAGE = 10L
139-
}
14046
}

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,25 @@ package com.whyranoid.data.post
33
import androidx.paging.Pager
44
import androidx.paging.PagingConfig
55
import androidx.paging.PagingData
6+
import androidx.paging.cachedIn
67
import com.whyranoid.domain.model.Post
78
import com.whyranoid.domain.repository.PostRepository
9+
import kotlinx.coroutines.CoroutineScope
810
import kotlinx.coroutines.flow.Flow
911
import javax.inject.Inject
1012

1113
class PostRepositoryImpl @Inject constructor(
1214
private val postDataSource: PostDataSource,
13-
private val postPagingDataSource: PostPagingDataSource,
1415
private val myPostPagingDataSource: MyPostPagingDataSource
1516
) : PostRepository {
1617

1718
// TODO : 캐싱하기
18-
override fun getPagingPosts(): Flow<PagingData<Post>> {
19+
override fun getPagingPosts(coroutineScope: CoroutineScope): Flow<PagingData<Post>> {
1920
return Pager(
2021
PagingConfig(pageSize = 5)
2122
) {
22-
postPagingDataSource
23-
}.flow
23+
PostPagingDataSource(postDataSource)
24+
}.flow.cachedIn(coroutineScope)
2425
}
2526

2627
override fun getMyPagingPosts(uid: String): Flow<PagingData<Post>> {

domain/src/main/java/com/whyranoid/domain/repository/PostRepository.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@ package com.whyranoid.domain.repository
22

33
import androidx.paging.PagingData
44
import com.whyranoid.domain.model.Post
5+
import kotlinx.coroutines.CoroutineScope
56
import kotlinx.coroutines.flow.Flow
67

78
interface PostRepository {
89

910
// TODO : 페이징 처리
1011
// 글(홍보 / 인증) 페이징으로 가져오기 - 리모트
11-
fun getPagingPosts(): Flow<PagingData<Post>>
12+
fun getPagingPosts(coroutineScope: CoroutineScope): Flow<PagingData<Post>>
1213

1314
fun getMyPagingPosts(uid: String): Flow<PagingData<Post>>
1415

domain/src/main/java/com/whyranoid/domain/usecase/GetPagingPostsUseCase.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@ package com.whyranoid.domain.usecase
33
import androidx.paging.PagingData
44
import com.whyranoid.domain.model.Post
55
import com.whyranoid.domain.repository.PostRepository
6+
import kotlinx.coroutines.CoroutineScope
67
import kotlinx.coroutines.flow.Flow
78
import javax.inject.Inject
89

910
class GetPagingPostsUseCase @Inject constructor(private val postRepository: PostRepository) {
10-
operator fun invoke(): Flow<PagingData<Post>> {
11-
return postRepository.getPagingPosts()
11+
operator fun invoke(coroutineScope: CoroutineScope): Flow<PagingData<Post>> {
12+
return postRepository.getPagingPosts(coroutineScope)
1213
}
1314
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ class CommunityViewModel @Inject constructor(
3838
val eventFlow: SharedFlow<Event>
3939
get() = _eventFlow.asSharedFlow()
4040

41-
val pagingPost = getPagingPostsUseCase()
41+
val pagingPost = getPagingPostsUseCase(viewModelScope)
4242

4343
fun onGroupItemClicked(groupInfo: GroupInfoUiModel) {
4444
emitEvent(Event.GroupItemClick(groupInfo))

0 commit comments

Comments
 (0)