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 16, 2023
1 parent a9fe42d commit abe94ba
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,12 @@ internal fun Feed(
var showDialog by remember { mutableStateOf(false) }

val alpha by animateFloatAsState(
targetValue = if (state.isPostsEmpty) 1f else 0f,
targetValue = if (state.posts.isEmpty()) 1f else 0f,
label = "",
)

LaunchedEffect(Unit) {
if (state.isPostsEmpty) {
feedViewModel.fetchPosts()
}
feedViewModel.fetchPosts()
}

if (showDialog) {
Expand Down Expand Up @@ -120,7 +118,7 @@ internal fun Feed(
Posts(
moveToFeedDetails = moveToFeedDetails,
moveToReport = moveToReport,
posts = state.posts,
posts = { state.posts },
showDropDown = {
feedViewModel.setFeedId(it)
expanded = it
Expand Down Expand Up @@ -152,7 +150,7 @@ internal fun Feed(
Body(
modifier = Modifier.signalClickable(
onClick = moveToCreatePost,
enabled = state.isPostsEmpty
enabled = state.posts.isEmpty()
),
text = stringResource(id = R.string.feed_posts_add),
color = SignalColor.Primary100,
Expand Down Expand Up @@ -244,13 +242,13 @@ 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,
) {
LazyColumn(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 @@ -12,7 +12,6 @@ data class FeedState(
val tag: Tag,
val page: Long,
val size: Long,
val isPostsEmpty: Boolean,
val title: String,
val content: String,
val postDetailsEntity: PostDetailsEntity,
Expand All @@ -27,7 +26,6 @@ data class FeedState(
tag = Tag.GENERAL,
page = 0,
size = 8,
isPostsEmpty = true,
title = "",
content = "",
postDetailsEntity = PostDetailsEntity(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,9 @@ internal class FeedViewModel(
size = size,
)
}.onSuccess {
_posts.clear()
_posts.addAll(it.postEntities)
setState(
copy(
posts = _posts.toMutableStateList(),
isPostsEmpty = _posts.isEmpty(),
)
)
}.onFailure {
setState(copy(isPostsEmpty = _posts.isEmpty()))
setState(copy(posts = _posts.toMutableStateList()))
}
}
}
Expand All @@ -50,6 +44,7 @@ internal class FeedViewModel(
image = imageUrl,
).onSuccess {
postSideEffect(FeedSideEffect.PostSuccess)
fetchPosts()
}
}
}
Expand Down Expand Up @@ -108,11 +103,17 @@ internal class FeedViewModel(

internal fun deletePost() {
with(state.value) {
val remove = {
_posts.remove(_posts.find { it.id == feedId })
setState(copy(posts = _posts.toMutableStateList()))
}
viewModelScope.launch(Dispatchers.IO) {
feedRepository.deletePost(feedId = feedId).onSuccess {

remove()
}.onFailure {

if (it is KotlinNullPointerException) {
remove()
}
}
}
}
Expand Down

0 comments on commit abe94ba

Please sign in to comment.