Skip to content

Commit 8dd181d

Browse files
committed
♻️ PagingDataSource를 내가 쓴 글 탭에서 활용할 수 있게 변경
1 parent 940b318 commit 8dd181d

File tree

5 files changed

+44
-153
lines changed

5 files changed

+44
-153
lines changed

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: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ interface PostDataSource {
1616

1717
suspend fun getNextPagingPost(lastDocumentSnapshot: DocumentSnapshot): QuerySnapshot
1818

19+
suspend fun getMyCurrentPagingPost(key: QuerySnapshot?, uid: String): QuerySnapshot
20+
21+
suspend fun getMyNextPagingPost(lastDocumentSnapshot: DocumentSnapshot, uid: String): QuerySnapshot
22+
1923
suspend fun convertPostType(document: QueryDocumentSnapshot): Post?
2024

2125
suspend fun createRecruitPost(

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,26 @@ class PostDataSourceImpl @Inject constructor(
238238
.await()
239239
}
240240

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+
241261
// TODO : 예외 처리
242262
override suspend fun convertPostType(document: QueryDocumentSnapshot): Post? {
243263
return if (document[RUNNING_HISTORY_ID] != null) {

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

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import com.google.firebase.firestore.QuerySnapshot
66
import com.whyranoid.domain.model.Post
77

88
class PostPagingDataSource(
9+
private val myUid: String = EMPTY_STRING,
910
private val postDataSource: PostDataSource
1011
) : PagingSource<QuerySnapshot, Post>() {
1112

@@ -18,7 +19,12 @@ class PostPagingDataSource(
1819
val postList = mutableListOf<Post>()
1920

2021
// 현재 페이지
21-
val currentPage = postDataSource.getCurrentPagingPost(params.key)
22+
val currentPage =
23+
if (myUid == EMPTY_STRING) {
24+
postDataSource.getCurrentPagingPost(params.key)
25+
} else {
26+
postDataSource.getMyCurrentPagingPost(params.key, myUid)
27+
}
2228

2329
// Post 타입 캐스팅
2430
// TODO 예외 처리
@@ -32,7 +38,12 @@ class PostPagingDataSource(
3238
val lastDocumentSnapshot = currentPage.documents[currentPage.size() - 1]
3339

3440
// 마지막 스냅샷 이후 페이지 불러오기
35-
val nextPage = postDataSource.getNextPagingPost(lastDocumentSnapshot)
41+
val nextPage =
42+
if (myUid == EMPTY_STRING) {
43+
postDataSource.getNextPagingPost(lastDocumentSnapshot)
44+
} else {
45+
postDataSource.getMyNextPagingPost(lastDocumentSnapshot, myUid)
46+
}
3647

3748
LoadResult.Page(
3849
data = postList,
@@ -43,4 +54,8 @@ class PostPagingDataSource(
4354
LoadResult.Error(e)
4455
}
4556
}
57+
58+
companion object {
59+
private const val EMPTY_STRING = ""
60+
}
4661
}

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

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,25 +11,23 @@ import kotlinx.coroutines.flow.Flow
1111
import javax.inject.Inject
1212

1313
class PostRepositoryImpl @Inject constructor(
14-
private val postDataSource: PostDataSource,
15-
private val myPostPagingDataSource: MyPostPagingDataSource
14+
private val postDataSource: PostDataSource
1615
) : PostRepository {
1716

1817
// TODO : 캐싱하기
1918
override fun getPagingPosts(coroutineScope: CoroutineScope): Flow<PagingData<Post>> {
2019
return Pager(
2120
PagingConfig(pageSize = 5)
2221
) {
23-
PostPagingDataSource(postDataSource)
22+
PostPagingDataSource(postDataSource = postDataSource)
2423
}.flow.cachedIn(coroutineScope)
2524
}
2625

2726
override fun getMyPagingPosts(uid: String): Flow<PagingData<Post>> {
28-
myPostPagingDataSource.setMyUid(uid)
2927
return Pager(
3028
PagingConfig(pageSize = 5)
3129
) {
32-
myPostPagingDataSource
30+
PostPagingDataSource(myUid = uid, postDataSource)
3331
}.flow
3432
}
3533

0 commit comments

Comments
 (0)