Skip to content

Commit

Permalink
Form Patching Support
Browse files Browse the repository at this point in the history
  • Loading branch information
Shade authored and Shade committed Nov 22, 2024
1 parent 0b6887e commit 8378dd8
Show file tree
Hide file tree
Showing 11 changed files with 190 additions and 120 deletions.
44 changes: 44 additions & 0 deletions src/main/kotlin/core/models/SelectedCredential.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package core.models

import repository.creditcard.CreditCard
import repository.password.Password
import java.util.*

data class SelectedCredential(
val password: Password?,
val creditCard: CreditCard?
) {

fun getTitle(type: DefaultMenuItem): String {
return when(type) {
DefaultMenuItem.PASSWORDS -> {
password?.name?.replaceFirstChar { if (it.isLowerCase()) it.titlecase(Locale.getDefault()) else it.toString() } ?: ""
}

DefaultMenuItem.CREDIT_CARD -> {
creditCard?.name?.replaceFirstChar { if (it.isLowerCase()) it.titlecase(Locale.getDefault()) else it.toString() } ?: ""
}

DefaultMenuItem.NOTES -> TODO()
}
}

fun getDescription(type: DefaultMenuItem): String {
return when(type) {
DefaultMenuItem.PASSWORDS -> {
password?.email?.takeIf { it.isNotEmpty() } ?: password?.username ?: ""
}

DefaultMenuItem.CREDIT_CARD -> {
creditCard?.number ?: ""
}

DefaultMenuItem.NOTES -> TODO()
}
}

fun isSelected(): Boolean {
return password != null || creditCard != null
}

}
42 changes: 5 additions & 37 deletions src/main/kotlin/core/models/UiModel.kt
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package core.models

import androidx.compose.ui.graphics.Color
import repository.creditcard.CreditCard
import repository.password.Password
import java.util.*

enum class NotificationType(val color: Color) {
Expand All @@ -18,41 +16,6 @@ data class CredentialDisplay(
val favorite: Boolean
)

data class SelectedCredential(
val password: Password?,
val creditCard: CreditCard?
) {

fun getTitle(type: DefaultMenuItem): String {
return when(type) {
DefaultMenuItem.PASSWORDS -> {
password?.name?.replaceFirstChar { if (it.isLowerCase()) it.titlecase(Locale.getDefault()) else it.toString() } ?: ""
}

DefaultMenuItem.CREDIT_CARD -> {
creditCard?.name?.replaceFirstChar { if (it.isLowerCase()) it.titlecase(Locale.getDefault()) else it.toString() } ?: ""
}

DefaultMenuItem.NOTES -> TODO()
}
}

fun getDescription(type: DefaultMenuItem): String {
return when(type) {
DefaultMenuItem.PASSWORDS -> {
password?.email?.takeIf { it.isNotEmpty() } ?: password?.username ?: ""
}

DefaultMenuItem.CREDIT_CARD -> {
creditCard?.number ?: ""
}

DefaultMenuItem.NOTES -> TODO()
}
}

}

enum class CredentialSort(val value: String) {
NAME("Name"),
FAVORITE("Favorite"),
Expand All @@ -63,4 +26,9 @@ enum class DefaultMenuItem(val value: String) {
PASSWORDS("Passwords"),
CREDIT_CARD("Credit Cards"),
NOTES("Notes")
}

enum class FormType {
CREATION,
MODIFIATION
}
55 changes: 40 additions & 15 deletions src/main/kotlin/ui/components/forms/passwordmgnt/CreditCardForm.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@ import com.dokar.sonner.ToastType
import com.dokar.sonner.Toaster
import com.dokar.sonner.rememberToasterState
import core.form.validation.FormValidator
import core.models.FormType
import core.models.UiState
import kotlinx.coroutines.delay
import repository.creditcard.CreditCard
import repository.user.User
import ui.components.*
import ui.screens.SecVaultScreen
Expand All @@ -34,12 +36,15 @@ import ui.validators.toCreditCardDto
import viewmodel.PasswordMgntScreenModel
import kotlin.time.Duration.Companion.seconds

class CreditCardForm : Screen {
class CreditCardForm(creditCard: CreditCard?, formType: FormType) : Screen {

private val _creditCard = creditCard
private val _formType = formType

@Composable
override fun Content() {

val formValidator = remember { creditCardFormValidator() }
val formValidator = remember { creditCardFormValidator(_creditCard) }
val screenModel = koinScreenModel<PasswordMgntScreenModel>()
val passwordState by screenModel.passwordState.collectAsState()
val navigator = LocalNavigator.current
Expand All @@ -56,7 +61,8 @@ class CreditCardForm : Screen {
CreditCardFormContent(
formValidator,
screenModel,
navigator
navigator,
_formType
)

when (val state = passwordState) {
Expand Down Expand Up @@ -90,13 +96,17 @@ class CreditCardForm : Screen {
}

@Composable
fun CreditCardFormContent(formValidator: FormValidator, screenModel: PasswordMgntScreenModel, navigator: Navigator?) {
fun CreditCardFormContent(
formValidator: FormValidator,
screenModel: PasswordMgntScreenModel,
navigator: Navigator?,
formType: FormType
) {

val isFormValid by formValidator.isValid
val users by remember { mutableStateOf<List<User>?>(screenModel.fetchUsers()) }
var selectedItem by remember { mutableStateOf<DropdownItem<User>?>(null) }


val cardBank = formValidator.getField(CreditCardFormFieldName.CARD_NAME)
val cardOwner = formValidator.getField(CreditCardFormFieldName.CARD_OWNER)
val cardNumber = formValidator.getField(CreditCardFormFieldName.CARD_NUMBER)
Expand All @@ -105,6 +115,12 @@ class CreditCardForm : Screen {
val expiry = formValidator.getField(CreditCardFormFieldName.CARD_EXPIRY)
val notes = formValidator.getField(CreditCardFormFieldName.CARD_NOTES)

users?.find {
it.id.value.toString() == cardOwner?.value?.value
}?.let {
selectedItem = DropdownItem(it, it.userName)
}

LaunchedEffect(selectedItem) {
selectedItem?.let {
cardOwner?.value?.value = it.id.toString()
Expand All @@ -114,32 +130,33 @@ class CreditCardForm : Screen {

Column(
modifier = Modifier.fillMaxSize()
.background(secondary)
.background(secondary)
)
{

Row(
modifier = Modifier.weight(1f)
.fillMaxSize(),
.fillMaxSize(),
verticalAlignment = Alignment.CenterVertically,
horizontalArrangement = Arrangement.SpaceBetween
) {
Header(
creditCardButtonShown = false,
notesButtonShown = true
notesButtonShown = (formType == FormType.CREATION),
title = if (formType == FormType.CREATION) "Create Credit Card" else "Update Credit Card"
)
}

Row(
modifier = Modifier.weight(7.5f)
.background(primary)
.fillMaxSize(),
.background(primary)
.fillMaxSize(),
horizontalArrangement = Arrangement.Center,
verticalAlignment = Alignment.CenterVertically
) {
Column(
modifier = Modifier.background(primary)
.padding(PaddingValues(end = 20.dp)),
.padding(PaddingValues(end = 20.dp)),
horizontalAlignment = Alignment.Start,
verticalArrangement = Arrangement.spacedBy(5.dp, Alignment.CenterVertically)
) {
Expand Down Expand Up @@ -256,7 +273,7 @@ class CreditCardForm : Screen {
Column(modifier = Modifier.height(80.dp)) {
FormTextField(
value =
expiry?.value?.value ?: "",
expiry?.value?.value ?: "",
onValueChange = { newValue ->
expiry?.value?.value = newValue
formValidator.validateField(CreditCardFormFieldName.CARD_EXPIRY)
Expand Down Expand Up @@ -305,13 +322,21 @@ class CreditCardForm : Screen {

Row(
modifier = Modifier.weight(1.5f)
.fillMaxSize()
.background(tertiary),
.fillMaxSize()
.background(tertiary),
verticalAlignment = Alignment.CenterVertically,
horizontalArrangement = Arrangement.spacedBy(30.dp, Alignment.CenterHorizontally)
) {
Footer(
{ screenModel.saveCreditCard(toCreditCardDto(formValidator, screenModel.getAuthenticatedUser(), selectedItem?.id!!)) },
{
screenModel.saveCreditCard(
toCreditCardDto(
formValidator,
screenModel.getAuthenticatedUser(),
selectedItem?.id!!
)
)
},
{ navigator?.popUntil { screen: Screen -> screen.key == SecVaultScreen().key } },
isFormValid
)
Expand Down
3 changes: 2 additions & 1 deletion src/main/kotlin/ui/components/forms/passwordmgnt/NoteForm.kt
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ class NoteForm : Screen {
) {
Header(
creditCardButtonShown = true,
notesButtonShown = false
notesButtonShown = false,
""
)
}

Expand Down
23 changes: 13 additions & 10 deletions src/main/kotlin/ui/components/forms/passwordmgnt/PasswordForm.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import core.form.validation.FormValidator
import core.models.FormType
import core.models.dto.PasswordDto
import ui.components.FormTextField
import ui.components.MultiSelectDropdown
Expand All @@ -32,7 +33,8 @@ fun PasswordForm(
screenModel: PasswordMgntScreenModel,
isFormValid: Boolean,
onSaveClick: (PasswordDto) -> Unit,
onCancelClick: () -> Unit
onCancelClick: () -> Unit,
formType: FormType
) {

val userName = formValidator.getField(PasswordFormFieldName.USERNAME)
Expand All @@ -45,33 +47,34 @@ fun PasswordForm(

Column(
modifier = Modifier.fillMaxSize()
.background(secondary)
.background(secondary)
)
{

Row(
modifier = Modifier.weight(1f)
.fillMaxSize(),
.fillMaxSize(),
verticalAlignment = Alignment.CenterVertically,
horizontalArrangement = Arrangement.SpaceBetween
) {
Header(
creditCardButtonShown = true,
notesButtonShown = true
creditCardButtonShown = (formType == FormType.CREATION),
notesButtonShown = (formType == FormType.CREATION),
title = if (formType == FormType.CREATION) "Create Password" else "Update Password",
)
}

Row(
modifier = Modifier.weight(7.5f)
.background(primary)
.fillMaxSize(),
.background(primary)
.fillMaxSize(),
horizontalArrangement = Arrangement.Center,
verticalAlignment = Alignment.CenterVertically
) {

Column(
modifier = Modifier.background(primary)
.padding(PaddingValues(end = 20.dp)),
.padding(PaddingValues(end = 20.dp)),
horizontalAlignment = Alignment.Start,
verticalArrangement = Arrangement.spacedBy(5.dp, Alignment.CenterVertically)
) {
Expand Down Expand Up @@ -237,8 +240,8 @@ fun PasswordForm(

Row(
modifier = Modifier.weight(1.5f)
.fillMaxSize()
.background(tertiary),
.fillMaxSize()
.background(tertiary),
verticalAlignment = Alignment.CenterVertically,
horizontalArrangement = Arrangement.spacedBy(30.dp, Alignment.CenterHorizontally)
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import cafe.adriel.voyager.navigator.LocalNavigator
import core.models.FormType.CREATION
import ui.components.OutlineTextButton
import ui.theme.Font
import ui.theme.primary
Expand All @@ -21,14 +22,15 @@ import ui.theme.secondary
@Composable
fun Header(
creditCardButtonShown: Boolean,
notesButtonShown: Boolean
notesButtonShown: Boolean,
title: String
) {
val navigator = LocalNavigator.current

Row {
Spacer(modifier = Modifier.width(25.dp))
Text(
text = "Create a new password",
text = title,
color = Color.White,
fontFamily = Font.RussoOne,
fontWeight = FontWeight.Normal,
Expand All @@ -45,7 +47,7 @@ fun Header(
cornerSize = 4.dp,
fontSize = 12.sp,
text = "Credit Card",
onClick = { navigator?.push(CreditCardForm()) }
onClick = { navigator?.push(CreditCardForm(null, CREATION)) }
)
}

Expand Down
Loading

0 comments on commit 8378dd8

Please sign in to comment.