Skip to content
This repository has been archived by the owner on Mar 8, 2024. It is now read-only.

Commit

Permalink
⚡️ :: 게시글 페이징 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
Tmdhoon2 committed Nov 17, 2023
1 parent 7823884 commit ee5bf52
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.LazyListState
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.DropdownMenu
Expand Down Expand Up @@ -130,6 +131,11 @@ internal fun Feed(
onDismissRequest = { expanded = -1 },
onDelete = { showDialog = true },
onEdit = { moveToCreatePost(state.feedId) },
nextPage = {
if (state.hasNextPage) {
feedViewModel.nextPage()
}
},
)

Column(
Expand Down Expand Up @@ -251,8 +257,21 @@ private fun Posts(
expanded: Long,
onDelete: () -> Unit,
onEdit: () -> Unit,
nextPage: () -> Unit,
) {
LazyColumn(modifier = Modifier.fillMaxSize()) {
val lazyListState = remember { LazyListState() }

LaunchedEffect(lazyListState.layoutInfo.visibleItemsInfo.lastIndex) {
val visibleItemsInfo = lazyListState.layoutInfo.visibleItemsInfo
if (visibleItemsInfo.lastIndex != -1 && (visibleItemsInfo.last().index % 7 == 0)) {
nextPage()
}
}

LazyColumn(
state = lazyListState,
modifier = Modifier.fillMaxSize(),
) {
items(posts()) {
Post(
moveToFeedDetails = { moveToFeedDetails(it.id) },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,14 @@ data class FeedState(
val comments: List<PostCommentsEntity.CommentEntity>,
val comment: String,
val buttonEnabled: Boolean,
val hasNextPage: Boolean,
) {
companion object {
fun getDefaultState() = FeedState(
posts = mutableStateListOf(),
tag = Tag.GENERAL,
page = 0,
size = 8,
size = 10,
title = "",
content = "",
postDetailsEntity = PostDetailsEntity(
Expand All @@ -44,6 +45,7 @@ data class FeedState(
comments = listOf(),
comment = "",
buttonEnabled = false,
hasNextPage = false,
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,13 @@ internal class FeedViewModel(
size = size,
)
}.onSuccess {
_posts.clear()
_posts.addAll(it.postEntities)
setState(copy(posts = _posts.toMutableStateList()))
setState(
copy(
posts = _posts.toMutableStateList(),
hasNextPage = it.postEntities.isNotEmpty()
)
)
}
}
}
Expand Down Expand Up @@ -141,6 +145,13 @@ internal class FeedViewModel(
}
}

internal fun nextPage() {
with(state.value) {
setState(copy(page = page + 1))
}
fetchPosts()
}

internal fun setTitle(title: String) {
setState(state.value.copy(title = title))
setButtonEnabled()
Expand Down

0 comments on commit ee5bf52

Please sign in to comment.