Skip to content

Commit

Permalink
Feat : closestPageToPosition 사용
Browse files Browse the repository at this point in the history
- #53
  • Loading branch information
inwoo13 committed Nov 28, 2024
1 parent 138a18a commit 45f56c1
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import com.kolown.data.datasource.remote.PostDataSource
import com.kolown.data.datasource.remote.ReactionDataSource
import com.kolown.data.datasource.remote.TagDataSource
import com.kolown.model.PostContentModel
import com.kolown.model.PostModel
import kotlinx.coroutines.async
import kotlinx.coroutines.awaitAll
import kotlinx.coroutines.coroutineScope
Expand All @@ -21,14 +22,37 @@ class UserPagingDataSource @Inject constructor(
private val tagDataSource: TagDataSource,
private val reactionDataSource: ReactionDataSource
) : PagingSource<UserPagingKey, PostContentModel>() {
override fun getRefreshKey(state: PagingState<UserPagingKey, PostContentModel>): UserPagingKey {
return UserPagingKey(state.anchorPosition ?: 0, "")
override fun getRefreshKey(state: PagingState<UserPagingKey, PostContentModel>): UserPagingKey? {
return state.anchorPosition?.let { position ->
val closestPage = state.closestPageToPosition(position)
val page = closestPage?.prevKey?.page?.plus(1)
?: closestPage?.nextKey?.page?.minus(1)
UserPagingKey(page ?: 0, "")
}

// return UserPagingKey(state.anchorPosition ?: 0, "")
}

override suspend fun load(params: LoadParams<UserPagingKey>): LoadResult<UserPagingKey, PostContentModel> {
val page = params.key?.page ?: 1
val page = params.key?.page ?: 0
val userId = params.key?.userId ?: ""
val posts = postDataSource.getUserPost(userId, params.loadSize.toLong()).getOrThrow()
val posts = getPosts(userId, params)

return LoadResult.Page(
data = getData(posts),
prevKey = if (page == 0) null else UserPagingKey(page - 1, userId),
nextKey = if (posts.isEmpty()) null else UserPagingKey(page + 1, userId)
)
}

private suspend fun getPosts(
userId: String,
params: LoadParams<UserPagingKey>
): List<PostModel> {
return postDataSource.getUserPost(userId, params.loadSize.toLong()).getOrThrow()
}

private suspend fun getData(posts: List<PostModel>): List<PostContentModel> {
val (tags, reactions) = coroutineScope {
val tagsDeferred =
async {
Expand All @@ -48,21 +72,17 @@ class UserPagingDataSource @Inject constructor(
tagsDeferred.await() to reactionsDeferred.await()
}

return LoadResult.Page(
data = posts.mapIndexed { index, postModel ->
PostContentModel(
postId = postModel.postId,
authorId = postModel.authorId,
imageUrl = postModel.imageUrl,
registerAt = postModel.registerAt,
description = postModel.description,
tags = tags[index].map { it.tagName },
isFollower = true,
reactions = reactions[index].mapNotNull { it.reaction },
)
},
prevKey = if (page == 1) null else UserPagingKey(page - 1, userId),
nextKey = if (posts.isEmpty()) null else UserPagingKey(page + 1, userId)
)
return posts.mapIndexed { index, postModel ->
PostContentModel(
postId = postModel.postId,
authorId = postModel.authorId,
imageUrl = postModel.imageUrl,
registerAt = postModel.registerAt,
description = postModel.description,
tags = tags[index].map { it.tagName },
isFollower = true,
reactions = reactions[index].mapNotNull { it.reaction },
)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ interface PostRepository {
suspend fun getRandomDetailPostList(): Flow<PagingData<PostContentModel>>
suspend fun reactPost(postId: String, reaction: Reactions): Result<Unit>
suspend fun removePostReaction(postId: String): Result<Unit>
fun getUserPosts(userId: String): Flow<PagingData<PostContentModel>>
fun getUserPosts(userId: String? = null): Flow<PagingData<PostContentModel>>
suspend fun getPostBySearch(tagId: String): Flow<PagingData<PostContentModel>>
}

Expand All @@ -51,13 +51,20 @@ class PostRepositoryImpl @Inject constructor(
private val userPagingDataSource: UserPagingDataSource,
) : PostRepository {

override fun getUserPosts(userId: String): Flow<PagingData<PostContentModel>> {
override fun getUserPosts(userId: String?): Flow<PagingData<PostContentModel>> {
val initialKey = if(userId == null) {
val authorId = googleAuthDataSource.getUserId()
UserPagingKey(0, authorId)
} else {
UserPagingKey(0, userId)
}

return Pager(
config = PagingConfig(
pageSize = 10,
pageSize = GALLERY_PAGE_SIZE,
enablePlaceholders = false
),
initialKey = UserPagingKey(1, userId),
initialKey = initialKey,
pagingSourceFactory = { userPagingDataSource }
).flow
}
Expand Down Expand Up @@ -240,5 +247,6 @@ class PostRepositoryImpl @Inject constructor(
companion object {
const val DETAIL_PER_PAGE = 2
const val SEARCH_PER_PAGE = 5
const val GALLERY_PAGE_SIZE = 10
}
}

0 comments on commit 45f56c1

Please sign in to comment.