Skip to content
This repository was archived by the owner on Dec 21, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,23 +1,41 @@
package com.belmontCrest.cardCrafter.controller.onClickActions

import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.AlertDialog
import androidx.compose.material3.Button
import androidx.compose.material3.ButtonDefaults
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.MutableState
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import androidx.compose.ui.window.Dialog
import com.belmontCrest.cardCrafter.controller.viewModels.cardViewsModels.EditCardViewModel
import com.belmontCrest.cardCrafter.localDatabase.tables.CT
import com.belmontCrest.cardCrafter.localDatabase.tables.Card
import com.belmontCrest.cardCrafter.model.ui.Fields
import com.belmontCrest.cardCrafter.ui.theme.GetUIStyle
import com.belmontCrest.cardCrafter.ui.theme.deleteTextColor
import kotlinx.coroutines.CoroutineScope
import com.belmontCrest.cardCrafter.R
import com.belmontCrest.cardCrafter.controller.cardHandlers.toCard
import com.belmontCrest.cardCrafter.localDatabase.tables.Deck
import com.belmontCrest.cardCrafter.model.TAProp
import com.belmontCrest.cardCrafter.model.TCProp
import com.belmontCrest.cardCrafter.model.TextProps
import com.belmontCrest.cardCrafter.model.Type
import com.belmontCrest.cardCrafter.navigation.NavViewModel
import com.belmontCrest.cardCrafter.ui.theme.mainViewModifier
import com.belmontCrest.cardCrafter.uiFunctions.CustomText
import com.belmontCrest.cardCrafter.uiFunctions.buttons.CancelButton
import com.belmontCrest.cardCrafter.uiFunctions.buttons.SubmitButton
import kotlinx.coroutines.launch
Expand Down Expand Up @@ -228,22 +246,17 @@ fun DeleteCard(
)
},
confirmButton = {
Button(
SubmitButton(
onClick = {
pressed.value = false
fields.mainClicked.value = false
coroutineScope.launch {
navViewModel.deleteCard(card)
onDelete()
}
},
colors = ButtonDefaults.buttonColors(
containerColor = getUIStyle.secondaryButtonColor(),
contentColor = deleteTextColor
)
) {
Text("OK")
}
}, enabled = true, getUIStyle = getUIStyle,
string = stringResource(R.string.okay)
)
},
dismissButton = {
CancelButton(onClick = { pressed.value = false }, enabled = true, getUIStyle)
Expand Down Expand Up @@ -278,5 +291,85 @@ fun DeleteCards(
}
)
}
}

@Composable
fun DuplicateCards(
showDialog: Boolean, onDismiss: (Boolean) -> Unit,
onDuplicate: () -> Unit, getUIStyle: GetUIStyle, enabled: Boolean
) {
if (showDialog) {
AlertDialog(
onDismissRequest = { if (enabled) onDismiss(false) },
title = { Text(stringResource(R.string.duplicate_card_list)) },
text = {
Text(
text = stringResource(R.string.sure_to_duplicate_card_list),
color = getUIStyle.titleColor()
)
},
confirmButton = {
SubmitButton(
onClick = { onDuplicate() }, enabled = enabled,
getUIStyle, stringResource(R.string.okay)
)
},
dismissButton = {
CancelButton(onClick = { onDismiss(false) }, enabled, getUIStyle)
}
)
}
}

@Composable
fun CopyMoveCardList(
showDialog: Boolean, onDismiss: (Boolean) -> Unit, getUIStyle: GetUIStyle,
deckList: List<Deck>, onCopyOrMove: (Int) -> Unit, enabled: Boolean, selectedDeck: Deck?
) {
if (showDialog) {
Dialog(
onDismissRequest = { onDismiss(false) }
) {
Column(
modifier = Modifier
.fillMaxSize(0.9f)
.background(
color = getUIStyle.altBackground(), shape = RoundedCornerShape(16.dp)
),
horizontalAlignment = Alignment.CenterHorizontally,
verticalArrangement = Arrangement.Top
) {
LazyColumn(
modifier = Modifier
.fillMaxWidth(),
contentPadding = PaddingValues(
vertical = 24.dp,
horizontal = 6.dp
)
) {
items(deckList) { deck ->
CustomText(
text = deck.name,
getUIStyle = getUIStyle,
modifier = Modifier
.padding(4.dp)
.mainViewModifier(getUIStyle.getColorScheme())
.clickable(enabled = deck.id != selectedDeck?.id) {
onCopyOrMove(deck.id)
},
props = TextProps(
ta = TAProp.Center,
tc =
if (deck.id != selectedDeck?.id) TCProp.Default
else TCProp.Disabled
)
)
}
}
CancelButton(
onClick = { onDismiss(false) }, enabled = enabled, getUIStyle = getUIStyle
)
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package com.belmontCrest.cardCrafter.controller.viewModels

import com.belmontCrest.cardCrafter.localDatabase.dbInterface.repositories.FlashCardRepository
import com.belmontCrest.cardCrafter.localDatabase.tables.Deck
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import kotlinx.coroutines.withTimeout
import java.util.Date

class ReusedFunc(private val flashCardRepository: FlashCardRepository) {
companion object {
private const val TIMEOUT_MILLIS = 4_000L
}
suspend fun updateCardsLeft(deck: Deck, cardsToAdd: Int) {
return withContext(Dispatchers.IO) {
/** Only add the cards if the deck's review is due */
if (deck.nextReview <= Date()) {
withTimeout(TIMEOUT_MILLIS) {
/** This keeps a record of the amount of cards left vs done and compares
* it to the card amount. Here's three examples
* cardsLeft = CL, cardsDone = CD, cardAmount = CA
* 1 represents how many cardsToAdd
* CA = 20
* 1. CL = 19, CD = 1
* (CL + 1 = 20) < CA && (CD + 1 = 2) < CA -> False,
* (CD + 1 = 2) >= CA -> False; just update to CA
*
* 2. CL = 1, CD = 19
* (CL + 1 = 2) && (CD + 1 = 20) < CA -> F; move down
* (CD + 1 = 20) >= CA -> True, Don't update just return.
*
* 3. CL = 15, CD = 5
* (CL + 1 = 16) ** (CD + 1 = 6) < CA -> True; Update accordingly.
*/
if (((deck.cardsLeft + cardsToAdd) < deck.cardAmount) &&
((deck.cardsDone + cardsToAdd) < deck.cardAmount)
) {
flashCardRepository.updateCardsLeft(
deckId = deck.id, cardsDone = deck.cardsDone,
cardsLeft = (deck.cardsLeft + cardsToAdd),
)
} else if ((deck.cardsDone + cardsToAdd) >= deck.cardAmount) {
return@withTimeout
} else {
flashCardRepository.updateCardsLeft(
deck.id, deck.cardAmount, deck.cardsDone
)
}
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.belmontCrest.cardCrafter.controller.viewModels.cardViewsModels

import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.belmontCrest.cardCrafter.controller.viewModels.ReusedFunc
import com.belmontCrest.cardCrafter.localDatabase.dbInterface.repositories.FlashCardRepository
import com.belmontCrest.cardCrafter.localDatabase.tables.Deck
import com.belmontCrest.cardCrafter.localDatabase.tables.PartOfQorA
Expand All @@ -13,9 +14,6 @@ import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.update
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import kotlinx.coroutines.withTimeout
import java.util.Date

class AddCardViewModel(
private val flashCardRepository: FlashCardRepository,
Expand All @@ -25,10 +23,11 @@ class AddCardViewModel(
private val _errorMessage: MutableStateFlow<String> = MutableStateFlow("")
val errorMessage = _errorMessage.asStateFlow()
private val isOwner = MutableStateFlow(false)
private val rf = ReusedFunc(flashCardRepository)

companion object {
/*companion object {
private const val TIMEOUT_MILLIS = 4_000L
}
}*/

init {
viewModelScope.launch {
Expand Down Expand Up @@ -64,7 +63,7 @@ class AddCardViewModel(

fun addThreeCard(
deck: Deck, question: String,
middle: String, answer: String, isQOrA : PartOfQorA
middle: String, answer: String, isQOrA: PartOfQorA
) {
if (question.isNotBlank() && answer.isNotBlank()
&& middle.isNotBlank()
Expand Down Expand Up @@ -119,44 +118,7 @@ class AddCardViewModel(


suspend fun updateCardsLeft(deck: Deck, cardsToAdd: Int = 1) {
return withContext(Dispatchers.IO) {
/** Only add the cards if the deck's review is due */
if (deck.nextReview <= Date()) {
viewModelScope.launch(Dispatchers.IO) {
withTimeout(TIMEOUT_MILLIS) {
/** This keeps a record of the amount of cards left vs done and compares
* it to the card amount. Here's three examples
* cardsLeft = CL, cardsDone = CD, cardAmount = CA
* CA = 20
* 1. CL = 19, CD = 1
* CL + 1 = 20, CD + 1 = 2, ! < CA, F; move down
* CD + 1 = 2, ! >= CA, F; just update to CA
*
* 2. CL = 1, CD = 19
* CL + 1 = 2, CD + 1 = 20, ! < CA, F; move down
* CD + 1 = 20 >= CA, T, Don't update just return.
*
* 3. CL = 15, CD = 5
* CL + 1 = 16, CD + 1 = 6, 16 && 6 < CA, T; Update accordingly.
*/
if (((deck.cardsLeft + cardsToAdd) < deck.cardAmount) &&
((deck.cardsDone + cardsToAdd) < deck.cardAmount)
) {
flashCardRepository.updateCardsLeft(
deckId = deck.id, cardsDone = deck.cardsDone,
cardsLeft = (deck.cardsLeft + cardsToAdd),
)
} else if ((deck.cardsDone + cardsToAdd) >= deck.cardAmount) {
return@withTimeout
} else {
flashCardRepository.updateCardsLeft(
deck.id, deck.cardAmount, deck.cardsDone
)
}
}
}
}
}
rf.updateCardsLeft(deck, cardsToAdd)
}

fun setErrorMessage(message: String) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ class CardDeckViewModel(
if (dueDetails == null) {
flowOf(SealedDueCTs())
} else {
cardTypeRepository.getAllDueCards(dueDetails.id, dueDetails.cardsLeft, Date().time)
cardTypeRepository.getAllDueCardsStream(dueDetails.id, dueDetails.cardsLeft, Date().time)
.map {
transitionTo(CardState.Finished)
SealedDueCTs(
Expand Down
Loading