Skip to content

Commit

Permalink
#299 [feat] 추천 수정하기 버튼 버그 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
2zerozu committed Feb 25, 2024
1 parent d482ed1 commit a541b72
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.WindowManager
import androidx.core.view.isVisible
import androidx.fragment.app.DialogFragment
import androidx.fragment.app.activityViewModels
import com.sopt.peekabookaos.R
Expand Down Expand Up @@ -46,8 +45,10 @@ class RecommendDeleteDialog : DialogFragment() {
when (uiEvent) {
UiEvent.IDLE -> {}
UiEvent.SUCCESS -> {
recommendViewModel.getRecommend()
dismiss()
}

UiEvent.ERROR -> {
dismiss()
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.sopt.peekabookaos.presentation.recommend

import androidx.lifecycle.LiveData
import androidx.lifecycle.MediatorLiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
Expand Down Expand Up @@ -29,12 +30,31 @@ class RecommendViewModel @Inject constructor(
private val _recommendingBook = MutableLiveData<List<Recommend>>()
val recommendingBook: LiveData<List<Recommend>> = _recommendingBook

private val _isEditMode = MutableLiveData(false)
val isEditMode: LiveData<Boolean> = _isEditMode
// private val _isEditMode = MutableLiveData(false)
// val isEditMode: LiveData<Boolean> = _isEditMode

private val _recommendId = MutableLiveData(-1)
val recommendId: LiveData<Int> = _recommendId

private val _isEditMode = MediatorLiveData<Boolean>().apply {
value = false

addSource(_recommendedBook) { value = checkEditMode(it, _recommendingBook.value) }
addSource(_recommendingBook) { value = checkEditMode(_recommendedBook.value, it) }
}
val isEditMode: LiveData<Boolean> = _isEditMode

private fun checkEditMode(
recommendedBooks: List<Recommend>?,
recommendingBooks: List<Recommend>?
): Boolean {
val recommendedBooksEmpty = recommendedBooks.isNullOrEmpty()
val recommendingBooksEmpty = recommendingBooks.isNullOrEmpty()

return if (recommendedBooksEmpty && recommendingBooksEmpty) false else _isEditMode.value
?: false
}

fun toggleEditMode() {
_isEditMode.value = _isEditMode.value?.not()
val updatedRecommendedBooks =
Expand Down Expand Up @@ -67,8 +87,11 @@ class RecommendViewModel @Inject constructor(
viewModelScope.launch {
getRecommendUseCase()
.onSuccess { response ->
_recommendingBook.value = response.recommendingBook
_recommendedBook.value = response.recommendedBook
_recommendingBook.value =
response.recommendingBook.map { it.copy(isEditMode = _isEditMode.value!!) }
_recommendedBook.value =
response.recommendedBook.map { it.copy(isEditMode = _isEditMode.value!!) }
checkEditMode(_recommendingBook.value, _recommendedBook.value)
}.onFailure { throwable ->
Timber.e("$throwable")
}
Expand Down

0 comments on commit a541b72

Please sign in to comment.