diff --git a/app/src/main/java/com/sopt/peekabookaos/presentation/recommend/RecommendDeleteDialog.kt b/app/src/main/java/com/sopt/peekabookaos/presentation/recommend/RecommendDeleteDialog.kt index 221df551..c205e7e0 100644 --- a/app/src/main/java/com/sopt/peekabookaos/presentation/recommend/RecommendDeleteDialog.kt +++ b/app/src/main/java/com/sopt/peekabookaos/presentation/recommend/RecommendDeleteDialog.kt @@ -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 @@ -46,8 +45,10 @@ class RecommendDeleteDialog : DialogFragment() { when (uiEvent) { UiEvent.IDLE -> {} UiEvent.SUCCESS -> { + recommendViewModel.getRecommend() dismiss() } + UiEvent.ERROR -> { dismiss() } diff --git a/app/src/main/java/com/sopt/peekabookaos/presentation/recommend/RecommendViewModel.kt b/app/src/main/java/com/sopt/peekabookaos/presentation/recommend/RecommendViewModel.kt index 6e46770a..6c3bb8df 100644 --- a/app/src/main/java/com/sopt/peekabookaos/presentation/recommend/RecommendViewModel.kt +++ b/app/src/main/java/com/sopt/peekabookaos/presentation/recommend/RecommendViewModel.kt @@ -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 @@ -29,12 +30,31 @@ class RecommendViewModel @Inject constructor( private val _recommendingBook = MutableLiveData>() val recommendingBook: LiveData> = _recommendingBook - private val _isEditMode = MutableLiveData(false) - val isEditMode: LiveData = _isEditMode +// private val _isEditMode = MutableLiveData(false) +// val isEditMode: LiveData = _isEditMode private val _recommendId = MutableLiveData(-1) val recommendId: LiveData = _recommendId + private val _isEditMode = MediatorLiveData().apply { + value = false + + addSource(_recommendedBook) { value = checkEditMode(it, _recommendingBook.value) } + addSource(_recommendingBook) { value = checkEditMode(_recommendedBook.value, it) } + } + val isEditMode: LiveData = _isEditMode + + private fun checkEditMode( + recommendedBooks: List?, + recommendingBooks: List? + ): 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 = @@ -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") }