Skip to content

Commit

Permalink
#12 제목 수정 조건문 고침, #4 update 뷰모델에서 호출
Browse files Browse the repository at this point in the history
  • Loading branch information
comye1 committed Feb 22, 2022
1 parent 369a90d commit 5e71502
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 22 deletions.
14 changes: 4 additions & 10 deletions app/src/main/java/com/comye1/cheggprep/screens/DeckScreen.kt
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ import androidx.navigation.compose.navArgument
import androidx.navigation.compose.rememberNavController
import com.comye1.cheggprep.models.Card
import com.comye1.cheggprep.models.DECK_CREATED
import com.comye1.cheggprep.models.DECK_ONLY_BOOKMARKED
import com.comye1.cheggprep.models.Deck
import com.comye1.cheggprep.ui.theme.DeepOrange
import com.comye1.cheggprep.ui.theme.Teal
Expand Down Expand Up @@ -221,12 +220,8 @@ fun DeckScreen(
}
) { // onDone

if (viewModel.updateDeckDetail(
title = deckTitle,
shared = visibility
)
) {
// 리턴 값이 true일 때 업데이트 필요
viewModel.updateDeckDetail(title = deckTitle, shared = visibility)
{ // 리턴 값이 true일 때 업데이트 필요
update()
}
subNavController.popBackStack()
Expand Down Expand Up @@ -259,7 +254,7 @@ fun DeckScreen(
onDone = {
// 뷰모델 함수 호출
// popBackStack
viewModel.updateCardList()
viewModel.updateCardList { update() }
subNavController.popBackStack()
}
)
Expand All @@ -286,8 +281,7 @@ fun DeckScreen(
onDone = {
// 뷰모델 함수 호출
// popBackStack
// 리턴 값 -> 업데이트 판단
viewModel.updateCardList()
viewModel.updateCardList { update() }
subNavController.popBackStack()
}
)
Expand Down
20 changes: 8 additions & 12 deletions app/src/main/java/com/comye1/cheggprep/viewmodel/DeckViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -197,38 +197,34 @@ class DeckViewModel : ViewModel() {
}
}

fun updateDeckDetail(title: String, shared: Boolean): Boolean {
fun updateDeckDetail(title: String, shared: Boolean, update: () -> Unit){

val database = Firebase.database.reference

deck.value?.let {
return if (shared == it.shared && title == it.deckTitle) false
else {
deck.value?.also {
if (shared != it.shared || title != it.deckTitle){
database.child("all/decks/${it.key}").apply {
child("deckTitle").setValue(title)
child("shared").setValue(shared)
}
deck.value!!.deckTitle = title
deck.value!!.shared = shared
true
update()
}
}
return false
}

fun updateCardList(): Boolean {
fun updateCardList(update: () -> Unit) {

val database = Firebase.database.reference

deck.value?.let {
return if (edittingCardList == it.cardList) false
else {
deck.value?.also {
if (edittingCardList != it.cardList){
database.child("all/decks/${it.key}").child("cardList").setValue(edittingCardList)
deck.value!!.cardList = edittingCardList
true
update()
}
}
return false
}

// When user practices this Deck
Expand Down

0 comments on commit 5e71502

Please sign in to comment.