Skip to content

Commit

Permalink
[Feat] #187 - 리뷰 삭제 기능 구현
Browse files Browse the repository at this point in the history
- 리뷰 삭제 버튼 클릭 시 확인 문구 한번 출력
  • Loading branch information
WooBinHam committed Sep 20, 2023
1 parent fd4abda commit 9c6a270
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 3 deletions.
18 changes: 16 additions & 2 deletions app/src/main/java/org/cazait/ui/cafeinfo/CafeInfoViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import androidx.lifecycle.viewModelScope
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.launch
import org.bmsk.data.repository.CafeRepository
Expand Down Expand Up @@ -41,6 +42,10 @@ class CafeInfoViewModel @Inject constructor(
val favoriteData: LiveData<Resource<String>>
get() = _favoriteData

private val _messageLiveData = MutableLiveData<Resource<String>>()
val messageLiveData: LiveData<Resource<String>>
get() = _messageLiveData

private val _showToast = MutableLiveData<SingleEvent<Any>>()
val showToast: LiveData<SingleEvent<Any>>
get() = _showToast
Expand All @@ -66,7 +71,7 @@ class CafeInfoViewModel @Inject constructor(
}
}

fun getReviews(cafeId: String, sortBy: String?, nums:Int, score: Int?) {
fun getReviews(cafeId: String, sortBy: String?, nums: Int, score: Int?) {
viewModelScope.launch {
_listReviewData.value = Resource.Loading()
cafeRepository.getReviews(cafeId, sortBy, nums, score).collect {
Expand All @@ -75,6 +80,15 @@ class CafeInfoViewModel @Inject constructor(
}
}

fun deleteReviews(reviewId: String) {
viewModelScope.launch {
_messageLiveData.value = Resource.Loading()
cafeRepository.deleteReviewAuth(reviewId).collect {
_messageLiveData.value = it
}
}
}

fun postFavoriteCafeAuth() {
viewModelScope.launch {
Log.d("즐겨찾기 등록 전 상태", _isFavoriteCafe.value.toString())
Expand Down Expand Up @@ -112,7 +126,7 @@ class CafeInfoViewModel @Inject constructor(
viewModelScope.launch {
val cafeWithTimestamp = cafe.copy(timestamp = System.currentTimeMillis())
cafeRepository.insertRecentlyViewedCafe(cafeWithTimestamp)
Log.e("cafeId1",cafeWithTimestamp.cafeId)
Log.e("cafeId1", cafeWithTimestamp.cafeId)
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ class CafeInfoReviewFragment(

private fun observeViewModel() {
observe(viewModel.listReviewData, ::handleCafeReview)
observe(viewModel.messageLiveData, ::handleDeleteReview)
}

private fun handleCafeReview(status: Resource<CafeReviews>) {
Expand Down Expand Up @@ -120,6 +121,27 @@ class CafeInfoReviewFragment(
}
}

private fun handleDeleteReview(status: Resource<String>){
when(status){
is Resource.Loading -> {
binding.lottieReview.toVisible()
binding.lottieReview.playAnimation()
}

is Resource.Success -> status.data.let {
binding.lottieReview.pauseAnimation()
binding.lottieReview.toGone()
viewModel.showToastMessage(it)
}

is Resource.Error -> {
binding.lottieReview.pauseAnimation()
binding.lottieReview.toGone()
viewModel.showToastMessage(status.message)
}
}
}

private fun navigateToReviewWriteFragment(cafe: Cafe, score: Float, content: String, reviewId:String?) {
val parentFragment = parentFragment
Log.d("CafeInfoReviewFragment의 부모 Fragment는 누구?", parentFragment.toString())
Expand All @@ -141,7 +163,13 @@ class CafeInfoReviewFragment(
}

override fun onDeleteClick(item: CafeReview) {

AlertDialog.Builder(requireContext())
.setMessage(resources.getString(R.string.review_delete_confirm))
.setPositiveButton("확인") { dialog, which ->
viewModel.deleteReviews(item.reviewId)
dialog.dismiss()
}
.show()
}

override fun onReportClick(item: CafeReview) {
Expand Down
23 changes: 23 additions & 0 deletions app/src/main/java/org/cazait/ui/review/ReviewWriteFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ class ReviewWriteFragment : BaseFragment<FragmentReviewWriteBinding, ReviewWrite

private fun oberveViewModel() {
observe(viewModel.messageLiveData, ::handleReviewSend)
observe(viewModel.messageLiveData, ::handleReviewPatch)
observeToast(viewModel.showToast)
}

Expand Down Expand Up @@ -70,6 +71,28 @@ class ReviewWriteFragment : BaseFragment<FragmentReviewWriteBinding, ReviewWrite
}
}

private fun handleReviewPatch(status: Resource<String>){
when(status){
is Resource.Loading -> {
binding.lottieReviewWrite.toVisible()
binding.lottieReviewWrite.playAnimation()
}

is Resource.Success -> status.data.let {
binding.lottieReviewWrite.pauseAnimation()
binding.lottieReviewWrite.toGone()
viewModel.showToastMessage(it)
findNavController().popBackStack()
}

is Resource.Error -> {
binding.lottieReviewWrite.pauseAnimation()
binding.lottieReviewWrite.toGone()
viewModel.showToastMessage(status.message)
}
}
}

private fun setReviewSendBtn() {
binding.btnSendReview.setOnClickListener {
val reviewId = navArgs.reviewId
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@
<string name="review_edit">수정</string>
<string name="review_delete">삭제</string>
<string name="review_report">신고</string>
<string name="review_delete_confirm">정말로 삭제하시겠습니까?</string>

<!-- Item Cafe Menu -->
<string name="menu_name">아메리카노</string>
Expand Down

0 comments on commit 9c6a270

Please sign in to comment.