Skip to content

Commit 1ae3ccf

Browse files
committed
✨ 게시글 삭제 기능 구현
1 parent 65a07ee commit 1ae3ccf

File tree

8 files changed

+80
-10
lines changed

8 files changed

+80
-10
lines changed

data/src/main/java/com/whyranoid/data/post/PostDataSource.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,6 @@ interface PostDataSource {
1919
runningHistoryId: String,
2020
content: String
2121
): Result<Boolean>
22+
23+
suspend fun deletePost(postId: String): Boolean
2224
}

data/src/main/java/com/whyranoid/data/post/PostDataSourceImpl.kt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,4 +268,17 @@ class PostDataSourceImpl @Inject constructor(
268268
}
269269
}
270270
}
271+
272+
override suspend fun deletePost(postId: String): Boolean {
273+
return suspendCancellableCoroutine { cancellableContinuation ->
274+
db.collection(CollectionId.POST_COLLECTION)
275+
.document(postId)
276+
.delete()
277+
.addOnSuccessListener {
278+
cancellableContinuation.resume(true)
279+
}.addOnFailureListener {
280+
cancellableContinuation.resume(false)
281+
}
282+
}
283+
}
271284
}

data/src/main/java/com/whyranoid/data/post/PostRepositoryImpl.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ class PostRepositoryImpl @Inject constructor(
3838
}
3939

4040
override suspend fun deletePost(postId: String): Boolean {
41-
TODO("Not yet implemented")
41+
return postDataSource.deletePost(postId)
4242
}
4343

4444
override suspend fun updatePost(postId: String, postContent: String, updatedAt: Long): Boolean {

presentation/src/main/java/com/whyranoid/presentation/community/CommunityItemFragment.kt

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -83,14 +83,31 @@ internal class CommunityItemFragment :
8383
).show()
8484
}
8585
}
86+
is Event.DeletePost -> {
87+
if (event.isSuccess) {
88+
Snackbar.make(
89+
binding.root,
90+
getString(R.string.text_delete_post_success),
91+
Snackbar.LENGTH_SHORT
92+
).show()
93+
} else {
94+
Snackbar.make(
95+
binding.root,
96+
getString(R.string.text_delete_post_fail),
97+
Snackbar.LENGTH_SHORT
98+
).show()
99+
}
100+
}
86101
}
87102
}
88103

89104
private fun setPostAdapter() {
90105
viewLifecycleOwner.lifecycleScope.launch {
91-
val postAdapter = PostAdapter {
92-
viewModel.onGroupJoinButtonClicked(it)
93-
}
106+
val postAdapter = PostAdapter(
107+
buttonClickListener = {
108+
viewModel.onGroupJoinButtonClicked(it)
109+
}
110+
)
94111
binding.rvCommunity.adapter = postAdapter
95112

96113
viewLifecycleOwner.repeatWhenUiStarted {
@@ -124,9 +141,18 @@ internal class CommunityItemFragment :
124141

125142
private fun setMyPostAdapter() {
126143
viewLifecycleOwner.lifecycleScope.launch {
127-
val postAdapter = PostAdapter {
128-
viewModel.onGroupJoinButtonClicked(it)
129-
}
144+
val postAdapter = PostAdapter(
145+
isMyPost = true,
146+
itemLongClickListener = { postId ->
147+
Snackbar.make(
148+
binding.root,
149+
getString(R.string.text_check_delete_post),
150+
Snackbar.LENGTH_SHORT
151+
).setAction(R.string.text_delete) {
152+
viewModel.deletePost(postId)
153+
}.show()
154+
}
155+
)
130156
binding.rvCommunity.adapter = postAdapter
131157

132158
viewLifecycleOwner.repeatWhenUiStarted {

presentation/src/main/java/com/whyranoid/presentation/community/CommunityViewModel.kt

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package com.whyranoid.presentation.community
33
import androidx.lifecycle.ViewModel
44
import androidx.lifecycle.viewModelScope
55
import com.whyranoid.domain.model.Post
6+
import com.whyranoid.domain.usecase.DeletePostUseCase
67
import com.whyranoid.domain.usecase.GetMyGroupListUseCase
78
import com.whyranoid.domain.usecase.GetMyPostUseCase
89
import com.whyranoid.domain.usecase.GetPostsUseCase
@@ -26,7 +27,8 @@ class CommunityViewModel @Inject constructor(
2627
getMyGroupListUseCase: GetMyGroupListUseCase,
2728
getPostsUseCase: GetPostsUseCase,
2829
private val joinGroupUseCase: JoinGroupUseCase,
29-
private val getMyPostUseCase: GetMyPostUseCase
30+
private val getMyPostUseCase: GetMyPostUseCase,
31+
private val deletePostUseCase: DeletePostUseCase
3032
) : ViewModel() {
3133

3234
private val _postList = MutableStateFlow<List<Post>>(emptyList())
@@ -62,6 +64,13 @@ class CommunityViewModel @Inject constructor(
6264
_eventFlow.emit(event.copy(isSuccess = false))
6365
}
6466
}
67+
is Event.DeletePost -> {
68+
if (event.isSuccess) {
69+
_eventFlow.emit(event)
70+
} else {
71+
_eventFlow.emit(event.copy(isSuccess = false))
72+
}
73+
}
6574
}
6675
}
6776
}
@@ -72,6 +81,12 @@ class CommunityViewModel @Inject constructor(
7281
}
7382
}
7483

84+
fun deletePost(postId: String) {
85+
viewModelScope.launch {
86+
emitEvent(Event.DeletePost(deletePostUseCase(postId)))
87+
}
88+
}
89+
7590
init {
7691
viewModelScope.launch {
7792
getMyGroupListUseCase().onEach { groupInfoList ->

presentation/src/main/java/com/whyranoid/presentation/community/Event.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@ import com.whyranoid.presentation.model.GroupInfoUiModel
55
sealed class Event {
66
data class GroupItemClick(val groupInfo: GroupInfoUiModel) : Event()
77
data class JoinGroup(val isSuccess: Boolean = true) : Event()
8+
data class DeletePost(val isSuccess: Boolean = true) : Event()
89
}

presentation/src/main/java/com/whyranoid/presentation/community/PostAdapter.kt

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ import com.whyranoid.presentation.databinding.ItemRunningPostBinding
1515
import com.whyranoid.presentation.model.GroupInfoUiModel
1616

1717
class PostAdapter(
18-
private val buttonClickListener: (String) -> Unit
18+
private val isMyPost: Boolean = false,
19+
private val itemLongClickListener: (String) -> Unit = {},
20+
private val buttonClickListener: (String) -> Unit = {}
1921
) : ListAdapter<Post, PostAdapter.PostViewHolder>(diffUtil) {
2022

2123
private lateinit var myGroupList: List<GroupInfoUiModel>
@@ -95,7 +97,14 @@ class PostAdapter(
9597
}
9698

9799
override fun onBindViewHolder(holder: PostViewHolder, position: Int) {
98-
holder.bind(getItem(position))
100+
val curPost = getItem(position)
101+
holder.apply {
102+
bind(curPost)
103+
if (isMyPost) this.itemView.setOnLongClickListener {
104+
itemLongClickListener(curPost.postId)
105+
true
106+
}
107+
}
99108
}
100109

101110
fun setMyGroupList(myGroupList: List<GroupInfoUiModel>) {

presentation/src/main/res/values/strings.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@
3434
<string name="community_fail_create_running_post">인증글 작성에 실패했습니다</string>
3535
<string name="community_success_create_running_post">인증글이 등록되었습니다!</string>
3636
<string name="community_create_running_post_hint">인증글 내용을 입력하세요!</string>
37+
<string name="text_delete_post_success">게시글 삭제에 성공하였습니다!</string>
38+
<string name="text_delete_post_fail">게시글 삭제에 실패하였습니다!</string>
39+
<string name="text_check_delete_post">해당 포스트를 삭제하시겠습니까?</string>
40+
<string name="text_delete">삭제</string>
3741

3842
<!-- 마이런 탭 화면 -->
3943
<string name="my_run_tool_bar_menu_setting">설정</string>

0 commit comments

Comments
 (0)