Skip to content

Commit

Permalink
Merge pull request #33 from nocap-stone-design/feature/#32-community
Browse files Browse the repository at this point in the history
[BUD-49] feat : 커뮤니티 글 삭제 기능 구현(#32)
  • Loading branch information
KDW03 authored Apr 8, 2023
2 parents a3ed098 + 94edb3f commit ba0d16b
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,11 @@ interface CommunityService {
@Header("Authorization") token: String,
@Path("postId") postId: Long
): CommonResponse<String?>

@DELETE("community/{postId}")
suspend fun deletePost(
@Header("Authorization") token: String,
@Path("postId") postId: Long,
): CommonResponse<String?>

}
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,8 @@ class CommunityUseCase @Inject constructor(
return communityService.deleteReply(token, postId)
}

suspend fun deletePost(token:String, postId: Long) {
communityService.deletePost(token,postId)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import android.util.Log
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.nocapstone.common.domain.usecase.DataStoreUseCase
import com.nocapstone.common.util.printLog
import com.nocapstone.common_ui.ToastSet
import com.nocapstone.community.domain.CommunityUseCase
import com.nocapstone.community.domain.CreatePostRequest
import com.nocapstone.community.dto.Content
Expand Down Expand Up @@ -41,6 +43,9 @@ class CommunityViewModel @Inject constructor(
private val _postList = MutableStateFlow<MutableList<Post>>(mutableListOf())
val postList: StateFlow<List<Post>> = _postList

private val _toastMessage = MutableStateFlow<ToastSet?>(null)
val toastMessage: StateFlow<ToastSet?> = _toastMessage

fun readPostList() {
viewModelScope.launch(Dispatchers.IO) {
try {
Expand Down Expand Up @@ -139,9 +144,27 @@ class CommunityViewModel @Inject constructor(
}
}

fun deletePost(postId: Long) {
viewModelScope.launch(Dispatchers.IO) {
val token = dataStoreUseCase.bearerJsonWebToken.first()
if (token != null) {
try {
communityUseCase.deletePost(token, postId)
} catch (e: Exception) {
printLog("deletePost 오류", e)
}
}
}
}



fun setImage(newUriList: List<Uri>) {
_imageUriList.value = newUriList.toMutableList()
}

fun setToastMessage(toastSet: ToastSet?) {
_toastMessage.value = toastSet
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,12 @@ import androidx.lifecycle.Lifecycle
import androidx.navigation.fragment.findNavController
import androidx.navigation.fragment.navArgs
import androidx.recyclerview.widget.DividerItemDecoration
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import com.nocapstone.common_ui.MainActivityUtil
import com.nocapstone.common_ui.ImageDetailAdapter
import com.nocapstone.common_ui.ToastSet
import com.nocapstone.common_ui.ToastType
import com.nocapstone.community.R
import com.nocapstone.community.ReplyAdapter
import com.nocapstone.community.databinding.FragmentDetailPostBinding
import com.nocapstone.community.dto.Content
Expand All @@ -33,12 +37,10 @@ class DetailPostFragment : Fragment() {
): View? {
// Inflate the layout for this fragment
_binding = FragmentDetailPostBinding.inflate(inflater, container, false)

(activity as MainActivityUtil).run {
setToolbarTitle("글 상세 조회")
setVisibilityBottomAppbar(View.GONE)
}

postId = args.postID
return binding.root
}
Expand Down Expand Up @@ -71,13 +73,30 @@ class DetailPostFragment : Fragment() {
menuHost.addMenuProvider(object : MenuProvider {

override fun onCreateMenu(menu: Menu, menuInflater: MenuInflater) {
menuInflater.inflate(R.menu.detail_community_menu, menu)
}

override fun onMenuItemSelected(menuItem: MenuItem): Boolean {
when (menuItem.itemId) {
android.R.id.home -> {
findNavController().popBackStack()
}
R.id.put_post -> {

}
R.id.delete_post -> {
MaterialAlertDialogBuilder(requireContext())
.setTitle("글 삭제")
.setMessage("정말 삭제하시겠습니까")
.setNegativeButton("취소") {
dialog, which -> dialog.dismiss()
}.setPositiveButton("삭제") { dialog, which ->
communityViewModel.deletePost(postId)
communityViewModel.setToastMessage(ToastSet("일기 삭제 완료", ToastType.SUCCESS))
dialog.dismiss()
findNavController().popBackStack()
}.show()
}
}
return true
}
Expand Down
16 changes: 16 additions & 0 deletions community/src/main/res/menu/detail_community_menu.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">

<item
android:id="@+id/put_post"
android:title="수정"
app:showAsAction="never"
/>
<item
android:id="@+id/delete_post"
android:title="삭제"
app:showAsAction="never"
/>

</menu>

0 comments on commit ba0d16b

Please sign in to comment.