diff --git a/core/data/src/main/java/com/kolown/data/datasource/paging/UserPagingDataSource.kt b/core/data/src/main/java/com/kolown/data/datasource/paging/UserPagingDataSource.kt index 7863d1ea..61b9607a 100644 --- a/core/data/src/main/java/com/kolown/data/datasource/paging/UserPagingDataSource.kt +++ b/core/data/src/main/java/com/kolown/data/datasource/paging/UserPagingDataSource.kt @@ -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 @@ -21,14 +22,37 @@ class UserPagingDataSource @Inject constructor( private val tagDataSource: TagDataSource, private val reactionDataSource: ReactionDataSource ) : PagingSource() { - override fun getRefreshKey(state: PagingState): UserPagingKey { - return UserPagingKey(state.anchorPosition ?: 0, "") + override fun getRefreshKey(state: PagingState): 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): LoadResult { - 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 + ): List { + return postDataSource.getUserPost(userId, params.loadSize.toLong()).getOrThrow() + } + + private suspend fun getData(posts: List): List { val (tags, reactions) = coroutineScope { val tagsDeferred = async { @@ -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 }, + ) + } } } \ No newline at end of file diff --git a/core/data/src/main/java/com/kolown/data/repository/PostRepository.kt b/core/data/src/main/java/com/kolown/data/repository/PostRepository.kt index df6a6616..32e76f86 100644 --- a/core/data/src/main/java/com/kolown/data/repository/PostRepository.kt +++ b/core/data/src/main/java/com/kolown/data/repository/PostRepository.kt @@ -36,7 +36,7 @@ interface PostRepository { suspend fun getRandomDetailPostList(): Flow> suspend fun reactPost(postId: String, reaction: Reactions): Result suspend fun removePostReaction(postId: String): Result - fun getUserPosts(userId: String): Flow> + fun getUserPosts(userId: String? = null): Flow> suspend fun getPostBySearch(tagId: String): Flow> } @@ -51,13 +51,20 @@ class PostRepositoryImpl @Inject constructor( private val userPagingDataSource: UserPagingDataSource, ) : PostRepository { - override fun getUserPosts(userId: String): Flow> { + override fun getUserPosts(userId: String?): Flow> { + 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 } @@ -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 } }