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 20, 2023
1 parent a6a40fd commit 30880ee
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ private fun Comments(
Comment(
profileImageUrl = it.profile,
writer = it.name,
time = it.dateTime.toString(),
time = it.dateTime,
content = it.content,
onClick = {},
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ internal fun Feed(
Posts(
moveToFeedDetails = moveToFeedDetails,
moveToReport = moveToReport,
posts = { state.posts },
posts = feedViewModel._posts,
showDropDown = {
feedViewModel.setFeedId(it)
expanded = it
Expand Down Expand Up @@ -254,7 +254,7 @@ private fun Posts(
moveToFeedDetails: (feedId: Long) -> Unit,
moveToReport: () -> Unit,
showDropDown: (feedId: Long) -> Unit,
posts: () -> List<PostsEntity.PostEntity>,
posts: List<PostsEntity.PostEntity>,
onDismissRequest: () -> Unit,
expanded: Long,
onDelete: () -> Unit,
Expand All @@ -274,7 +274,7 @@ private fun Posts(
state = lazyListState,
modifier = Modifier.fillMaxSize(),
) {
items(posts()) {
items(posts) {
Post(
moveToFeedDetails = { moveToFeedDetails(it.id) },
imageUrl = it.image,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import com.signal.domain.entity.PostDetailsEntity
import com.signal.domain.enums.Tag

data class FeedState(
val posts: SnapshotStateList<PostsEntity.PostEntity>,
val posts: List<PostsEntity.PostEntity>,
val tag: Tag,
val page: Long,
val size: Long,
Expand All @@ -24,7 +24,7 @@ data class FeedState(
) {
companion object {
fun getDefaultState() = FeedState(
posts = mutableStateListOf(),
posts = emptyList(),
tag = Tag.GENERAL,
page = 0,
size = 10,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ import kotlinx.coroutines.launch
internal class FeedViewModel(
private val feedRepository: FeedRepository,
) : BaseViewModel<FeedState, FeedSideEffect>(FeedState.getDefaultState()) {
private val _posts: MutableList<PostsEntity.PostEntity> = mutableListOf()
private val _comments: SnapshotStateList<PostCommentsEntity.CommentEntity> = mutableStateListOf()
internal val _posts: SnapshotStateList<PostsEntity.PostEntity> = mutableStateListOf()
private val _comments: SnapshotStateList<PostCommentsEntity.CommentEntity> =
mutableStateListOf()

internal fun fetchPosts() {
with(state.value) {
Expand All @@ -29,13 +30,18 @@ internal class FeedViewModel(
size = size,
)
}.onSuccess {
_posts.addAll(it.postEntities)
setState(
copy(
posts = _posts.toMutableStateList(),
hasNextPage = it.postEntities.isNotEmpty()
with(it.postEntities) {
when (_posts.contains(firstOrNull())) {
true -> _posts.add(last())
else -> _posts.addAll(this)
}
setState(
copy(
posts = _posts,
hasNextPage = isNotEmpty()
)
)
)
}
}
}
}
Expand Down Expand Up @@ -85,7 +91,7 @@ internal class FeedViewModel(
with(state.value) {
viewModelScope.launch(Dispatchers.IO) {
feedRepository.fetchComments(feedId).onSuccess {
if(_comments.contains(it.comments.first())){
if (_comments.contains(it.comments.first()) && _comments.size != it.comments.size) {
_comments.add(it.comments.last())
} else {
_comments.addAll(it.comments)
Expand Down Expand Up @@ -189,7 +195,12 @@ internal class FeedViewModel(

internal fun setTag(tag: Tag) {
with(state.value) {
setState(copy(tag = tag))
setState(
copy(
tag = tag,
page = 0,
)
)
_posts.clear()
fetchPosts()
}
Expand Down

0 comments on commit 30880ee

Please sign in to comment.