Skip to content

Commit

Permalink
optimize error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
aman-alfresco committed Mar 22, 2024
1 parent 809ed06 commit 5126386
Show file tree
Hide file tree
Showing 14 changed files with 235 additions and 120 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ data class FieldsData(
var dateDisplayFormat: String? = null,
var hyperlinkUrl: String? = null,
var displayText: String? = null,
var hasErrorInValue: Boolean = false,
) : Parcelable {

companion object {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,30 @@ package com.alfresco.content.process.ui.components
import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.input.ImeAction
import androidx.compose.ui.text.input.KeyboardType
import androidx.compose.ui.tooling.preview.Preview
import com.alfresco.content.data.payloads.FieldsData
import com.alfresco.content.process.R

@Composable
fun AmountInputField(
textFieldValue: String? = null,
onValueChanged: (String) -> Unit = { },
fieldsData: FieldsData = FieldsData(),
errorData: Pair<Boolean, String> = Pair(false, ""),
) {
val keyboardOptions = KeyboardOptions.Default.copy(
imeAction = ImeAction.Next,
keyboardType = KeyboardType.Number,
)

val isError by remember { mutableStateOf(errorData.first) }
val errorMessage by remember { mutableStateOf(errorData.second) }

val leadingIcon: @Composable () -> Unit = when {
!fieldsData.currency.isNullOrEmpty() -> {
{
Expand All @@ -36,8 +41,6 @@ fun AmountInputField(
}
}

val errorData = isValidInput(inputText = textFieldValue, fieldsData = fieldsData)

InputFieldWithLeading(
modifier = Modifier.inputField(),
maxLines = 1,
Expand All @@ -46,8 +49,8 @@ fun AmountInputField(
fieldsData = fieldsData,
keyboardOptions = keyboardOptions,
leadingIcon = leadingIcon,
isError = errorData.first,
errorMessage = errorData.second,
isError = isError,
errorMessage = errorMessage,
)
}

Expand All @@ -56,29 +59,3 @@ fun AmountInputField(
fun AmountInputFieldPreview() {
AmountInputField()
}

@Composable
fun isValidInput(inputText: String?, fieldsData: FieldsData): Pair<Boolean, String> {
val errorData = Pair(false, "")

if (inputText.isNullOrEmpty()) {
return errorData
}

if (inputText.toFloatOrNull() == null) {
return Pair(true, stringResource(R.string.error_invalid_format))
}

val minValue = fieldsData.minValue?.toFloat() ?: 0f
val maxValue = fieldsData.maxValue?.toFloat() ?: 0f

if (inputText.toFloat() < minValue) {
return Pair(true, stringResource(R.string.error_min_value, minValue.toInt()))
}

if (inputText.toFloat() > maxValue) {
return Pair(true, stringResource(R.string.error_max_value, maxValue.toInt()))
}

return errorData
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,14 @@ import com.alfresco.content.process.ui.theme.AlfrescoError
fun CheckBoxField(
title: String = "",
checkedValue: Boolean = false,
onCheckChanged: (Boolean) -> Unit = {},
onCheckChanged: (Boolean) -> Unit = { },
fieldsData: FieldsData = FieldsData(),
errorData: Pair<Boolean, String> = Pair(false, ""),
) {
val context = LocalContext.current
var showError by remember { mutableStateOf(false) }

val isError by remember { mutableStateOf(errorData.first) }
val errorMessage by remember { mutableStateOf(errorData.second) }

val minimumLineLength = 2 // Change this to your desired value

Expand Down Expand Up @@ -70,9 +73,6 @@ fun CheckBoxField(
checked = checkedValue,
onCheckedChange = { isChecked ->
onCheckChanged(isChecked)
if (fieldsData.required) {
showError = !isChecked
}
},
)
ClickableText(
Expand Down Expand Up @@ -117,9 +117,9 @@ fun CheckBoxField(
)
}

if (showError) {
if (isError) {
Text(
text = stringResource(R.string.error_required_field),
text = errorMessage,
color = AlfrescoError,
modifier = Modifier
.padding(horizontal = 16.dp, vertical = 0.dp), // Adjust padding as needed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,35 +5,32 @@ import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.OutlinedTextFieldDefaults
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.input.ImeAction
import androidx.compose.ui.text.input.KeyboardType
import androidx.compose.ui.tooling.preview.Preview
import com.alfresco.content.DATE_FORMAT_4
import com.alfresco.content.component.DatePickerBuilder
import com.alfresco.content.data.payloads.FieldsData
import com.alfresco.content.process.R

@Composable
fun DateTimeField(
dateTimeValue: String = "",
onValueChanged: (String) -> Unit = { },
onValueChanged: (String) -> Unit = {},
fieldsData: FieldsData = FieldsData(),
errorData: Pair<Boolean, String> = Pair(false, ""),
) {
val keyboardOptions = KeyboardOptions.Default.copy(
imeAction = ImeAction.Next,
keyboardType = KeyboardType.Text,
)

val isError = dateTimeValue.isNotEmpty() && dateTimeValue.length < fieldsData.minLength

val errorMessage = if (isError) {
stringResource(R.string.error_required_field, fieldsData.minLength)
} else {
""
}
val isError by remember { mutableStateOf(errorData.first) }
val errorMessage by remember { mutableStateOf(errorData.second) }

val context = LocalContext.current
InputField(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,39 +5,36 @@ import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.OutlinedTextFieldDefaults
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.input.ImeAction
import androidx.compose.ui.text.input.KeyboardType
import androidx.compose.ui.tooling.preview.Preview
import com.alfresco.content.component.ComponentBuilder
import com.alfresco.content.component.ComponentData
import com.alfresco.content.data.payloads.FieldsData
import com.alfresco.content.process.R

@Composable
fun DropdownField(
nameText: String = "",
queryText: String = "",
onValueChanged: (Pair<String, String>) -> Unit = { },
fieldsData: FieldsData = FieldsData(),
errorData: Pair<Boolean, String> = Pair(false, ""),
) {
val keyboardOptions = KeyboardOptions.Default.copy(
imeAction = ImeAction.Next,
keyboardType = KeyboardType.Text,
)

val isError = nameText.isNotEmpty() && nameText.length < fieldsData.minLength

val errorMessage = if (isError) {
stringResource(R.string.error_required_field, fieldsData.minLength)
} else {
""
}

val context = LocalContext.current

val isError by remember { mutableStateOf(errorData.first) }
val errorMessage by remember { mutableStateOf(errorData.second) }

InputField(
colors = OutlinedTextFieldDefaults.colors(
disabledBorderColor = MaterialTheme.colorScheme.onSurfaceVariant,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,42 +2,30 @@ package com.alfresco.content.process.ui.components

import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.input.ImeAction
import androidx.compose.ui.text.input.KeyboardType
import androidx.compose.ui.tooling.preview.Preview
import com.alfresco.content.data.payloads.FieldsData
import com.alfresco.content.process.R

@Composable
fun IntegerInputField(
textFieldValue: String? = null,
onValueChanged: (String) -> Unit = { },
fieldsData: FieldsData = FieldsData(),
errorData: Pair<Boolean, String> = Pair(false, ""),
) {
val keyboardOptions = KeyboardOptions.Default.copy(
imeAction = ImeAction.Next,
keyboardType = KeyboardType.Number,
)

var isError = false
var errorMessage = ""

if (!textFieldValue.isNullOrEmpty()) {
val minValue = fieldsData.minValue?.toLong() ?: 0
val maxValue = fieldsData.maxValue?.toLong() ?: 0

if (textFieldValue.toLong() < minValue) {
isError = true
errorMessage = stringResource(R.string.error_min_value, minValue)
}

if (textFieldValue.toLong() > maxValue) {
isError = true
errorMessage = stringResource(R.string.error_max_value, maxValue)
}
}
val isError by remember { mutableStateOf(errorData.first) }
val errorMessage by remember { mutableStateOf(errorData.second) }

InputField(
modifier = Modifier.inputField(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,27 @@ package com.alfresco.content.process.ui.components

import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.input.KeyboardType
import androidx.compose.ui.tooling.preview.Preview
import com.alfresco.content.data.payloads.FieldsData
import com.alfresco.content.process.R

@Composable
fun MultiLineInputField(
textFieldValue: String? = null,
onValueChanged: (String) -> Unit = { },
fieldsData: FieldsData = FieldsData(),
errorData: Pair<Boolean, String> = Pair(false, ""),
) {
val keyboardOptions = KeyboardOptions.Default.copy(
keyboardType = KeyboardType.Text,
)

val isError = !textFieldValue.isNullOrEmpty() && textFieldValue.length < fieldsData.minLength

val errorMessage = if (isError) {
stringResource(R.string.error_min_length, fieldsData.minLength)
} else {
""
}
val isError by remember { mutableStateOf(errorData.first) }
val errorMessage by remember { mutableStateOf(errorData.second) }

InputField(
modifier = Modifier.inputField(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,12 @@ import com.alfresco.content.process.ui.theme.AlfrescoError
@Composable
fun PeopleField(
userDetail: UserGroupDetails? = null,
onAssigneeSelected: (UserGroupDetails?) -> Unit = { },
onAssigneeSelected: (UserGroupDetails?, Boolean) -> Unit = { _: UserGroupDetails?, _: Boolean -> },
fieldsData: FieldsData = FieldsData(),
processEntry: ProcessEntry = ProcessEntry(),
) {
val isError = (fieldsData.required && userDetail == null)

val labelWithAsterisk = buildAnnotatedString {
append(fieldsData.name)
if (fieldsData.required) {
Expand Down Expand Up @@ -63,10 +65,10 @@ fun PeopleField(
IconButton(onClick = {
SearchUserGroupComponentBuilder(context, processEntry)
.onApply { userDetails ->
onAssigneeSelected(userDetails)
onAssigneeSelected(userDetails, isError)
}
.onCancel {
onAssigneeSelected(null)
onAssigneeSelected(null, isError)
}
.show()
}) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,30 @@ package com.alfresco.content.process.ui.components

import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.semantics.semantics
import androidx.compose.ui.text.input.ImeAction
import androidx.compose.ui.text.input.KeyboardType
import androidx.compose.ui.tooling.preview.Preview
import com.alfresco.content.data.payloads.FieldsData
import com.alfresco.content.process.R

@Composable
fun SingleLineInputField(
textFieldValue: String? = null,
onValueChanged: (String) -> Unit = { },
fieldsData: FieldsData = FieldsData(),
errorData: Pair<Boolean, String> = Pair(false, ""),
) {
val keyboardOptions = KeyboardOptions.Default.copy(
imeAction = ImeAction.Next,
keyboardType = KeyboardType.Text,
)

val isError = !textFieldValue.isNullOrEmpty() && textFieldValue.length < fieldsData.minLength

val errorMessage = if (isError) {
stringResource(R.string.error_min_length, fieldsData.minLength)
} else {
""
}
val isError by remember { mutableStateOf(errorData.first) }
val errorMessage by remember { mutableStateOf(errorData.second) }

InputField(
modifier = Modifier
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,15 @@ fun TrailingInputField(
tint = trailingIconColor(),
)
}

FieldType.DROPDOWN.value(), FieldType.RADIO_BUTTONS.value() -> {
Icon(
imageVector = Icons.Default.KeyboardArrowDown,
contentDescription = stringResource(R.string.accessibility_date_icon),
tint = trailingIconColor(),
)
}

else -> {
if (focusState && !textValue.isNullOrEmpty()) {
if (isError) {
Expand Down
Loading

0 comments on commit 5126386

Please sign in to comment.