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 27, 2023
1 parent 14734f6 commit 89b781c
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 58 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ data class FeedState(
val title: String,
val content: String,
val postDetailsEntity: PostDetailsEntity,
val feedId: UUID,
val feedId: UUID?,
val image: String,
val comments: SnapshotStateList<PostCommentsEntity.CommentEntity>,
val comment: String,
Expand All @@ -41,7 +41,7 @@ data class FeedState(
profile = "https://github.com/Team-SIGNAL/SIGNAL-ANDROID/blob/develop/presentation/src/main/res/drawable/ic_profile_image.png?raw=true",
isMine = false,
),
feedId = UUID.randomUUID(),
feedId = null,
image = "",
comments = mutableStateListOf(),
comment = "",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,40 +63,44 @@ internal class FeedViewModel(

internal fun fetchPostDetails() {
with(state.value) {
viewModelScope.launch(Dispatchers.IO) {
feedRepository.fetchPostDetails(feedId = feedId).onSuccess {
setState(
copy(
postDetailsEntity = PostDetailsEntity(
id = it.id,
image = it.image,
if (feedId != null) {
viewModelScope.launch(Dispatchers.IO) {
feedRepository.fetchPostDetails(feedId = feedId).onSuccess {
setState(
copy(
postDetailsEntity = PostDetailsEntity(
id = it.id,
image = it.image,
title = it.title,
date = it.date,
writer = it.writer,
content = it.content,
profile = it.profile,
isMine = it.isMine,
),
title = it.title,
date = it.date,
writer = it.writer,
content = it.content,
profile = it.profile,
isMine = it.isMine,
),
title = it.title,
content = it.content,
),
)
)
}
}
}
}
}

internal fun fetchComments() {
with(state.value) {
viewModelScope.launch(Dispatchers.IO) {
feedRepository.fetchComments(feedId).onSuccess {
if (_comments.size < it.comments.size) {
if (_comments.contains(it.comments.firstOrNull())) {
_comments.add(it.comments.last())
} else {
_comments.addAll(it.comments)
if (feedId != null) {
viewModelScope.launch(Dispatchers.IO) {
feedRepository.fetchComments(feedId).onSuccess {
if (_comments.size < it.comments.size) {
if (_comments.contains(it.comments.firstOrNull())) {
_comments.add(it.comments.last())
} else {
_comments.addAll(it.comments)
}
setState(copy(comments = _comments.reversed().toMutableStateList()))
}
setState(copy(comments = _comments.reversed().toMutableStateList()))
}
}
}
Expand All @@ -105,38 +109,42 @@ internal class FeedViewModel(

internal fun createComment() {
with(state.value) {
viewModelScope.launch(Dispatchers.IO) {
feedRepository.createComment(
feedId = feedId,
content = comment,
).onSuccess {
postSideEffect(FeedSideEffect.ClearFocus)
setState(
copy(
buttonEnabled = false,
comment = "",
if (feedId != null) {
viewModelScope.launch(Dispatchers.IO) {
feedRepository.createComment(
feedId = feedId,
content = comment,
).onSuccess {
postSideEffect(FeedSideEffect.ClearFocus)
setState(
copy(
buttonEnabled = false,
comment = "",
)
)
)
fetchComments()
fetchComments()
}
}
}
}
}

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()
postSideEffect(FeedSideEffect.DeleteSuccess)
}.onFailure {
if (it is KotlinNullPointerException) {
if (feedId != null) {
val remove = {
_posts.remove(_posts.find { it.id == feedId })
setState(copy(posts = _posts.toMutableStateList()))
}
viewModelScope.launch(Dispatchers.IO) {
feedRepository.deletePost(feedId = feedId).onSuccess {
remove()
postSideEffect(FeedSideEffect.DeleteSuccess)
}.onFailure {
if (it is KotlinNullPointerException) {
remove()
postSideEffect(FeedSideEffect.DeleteSuccess)
}
}
}
}
Expand All @@ -145,18 +153,20 @@ internal class FeedViewModel(

internal fun editPost(imageUrl: String? = null) {
with(state.value) {
viewModelScope.launch(Dispatchers.IO) {
feedRepository.editPost(
feedId = feedId,
title = title,
image = imageUrl ?: image.ifEmpty { postDetailsEntity.image },
content = content,
).onSuccess {
postSideEffect(FeedSideEffect.PostSuccess)
fetchPosts()
}.onFailure {
if (it is KotlinNullPointerException) {
if (feedId != null) {
viewModelScope.launch(Dispatchers.IO) {
feedRepository.editPost(
feedId = feedId,
title = title,
image = imageUrl ?: image.ifEmpty { postDetailsEntity.image },
content = content,
).onSuccess {
postSideEffect(FeedSideEffect.PostSuccess)
fetchPosts()
}.onFailure {
if (it is KotlinNullPointerException) {
postSideEffect(FeedSideEffect.PostSuccess)
}
}
}
}
Expand Down

0 comments on commit 89b781c

Please sign in to comment.