Skip to content

Commit

Permalink
ADST-261 (#323)
Browse files Browse the repository at this point in the history
* fixed crash

* optimize error handling

* add more sceanrio to error handling
  • Loading branch information
aman-alfresco authored Mar 28, 2024
1 parent 3380a2f commit 2777b85
Show file tree
Hide file tree
Showing 15 changed files with 292 additions and 151 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 errorData: Pair<Boolean, String> = Pair(false, ""),
) : Parcelable {

companion object {
Expand Down Expand Up @@ -70,6 +71,39 @@ data class FieldsData(
displayText = raw.displayText,
)
}

/**
* returns the FieldsData obj by using Fields obj
*/
fun withUpdateField(raw: FieldsData, value: Any?, errorData: Pair<Boolean, String>): FieldsData {
return FieldsData(
fieldType = raw.fieldType,
id = raw.id,
name = raw.name,
message = raw.message,
type = raw.type,
placeHolder = raw.placeHolder,
value = value,
minLength = raw.minLength,
maxLength = raw.maxLength,
minValue = raw.minValue,
maxValue = raw.maxValue,
regexPattern = raw.regexPattern,
required = raw.required,
readOnly = raw.readOnly,
overrideId = raw.overrideId,
params = raw.params,
currency = raw.currency,
enableFractions = raw.enableFractions,
enablePeriodSeparator = raw.enablePeriodSeparator,
options = raw.options,
fields = raw.fields,
dateDisplayFormat = raw.dateDisplayFormat,
hyperlinkUrl = raw.hyperlinkUrl,
displayText = raw.displayText,
errorData = errorData,
)
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,19 @@ 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.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,
Expand All @@ -36,8 +36,6 @@ fun AmountInputField(
}
}

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

InputFieldWithLeading(
modifier = Modifier.inputField(),
maxLines = 1,
Expand All @@ -56,29 +54,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,11 @@ 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 minimumLineLength = 2 // Change this to your desired value

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

if (showError) {
if (errorData.first) {
Text(
text = stringResource(R.string.error_required_field),
text = errorData.second,
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,36 +5,28 @@ 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.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 context = LocalContext.current
InputField(
colors = OutlinedTextFieldDefaults.colors(
Expand Down Expand Up @@ -65,8 +57,8 @@ fun DateTimeField(
onValueChanged = onValueChanged,
fieldsData = fieldsData,
keyboardOptions = keyboardOptions,
isError = isError,
errorMessage = errorMessage,
isError = errorData.first,
errorMessage = errorData.second,
isEnabled = false,
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,37 +5,29 @@ 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.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

InputField(
Expand Down Expand Up @@ -69,8 +61,8 @@ fun DropdownField(
textFieldValue = nameText,
fieldsData = fieldsData,
keyboardOptions = keyboardOptions,
isError = isError,
errorMessage = errorMessage,
isError = errorData.first,
errorMessage = errorData.second,
isEnabled = false,
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,52 +2,34 @@ 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.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?.toInt() ?: 0
val maxValue = fieldsData.maxValue?.toInt() ?: 0

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

if (textFieldValue.toInt() > maxValue) {
isError = true
errorMessage = stringResource(R.string.error_max_value, maxValue)
}
}

InputField(
modifier = Modifier.inputField(),
maxLines = 1,
textFieldValue = textFieldValue,
onValueChanged = onValueChanged,
fieldsData = fieldsData,
keyboardOptions = keyboardOptions,
isError = isError,
errorMessage = errorMessage,
isError = errorData.first,
errorMessage = errorData.second,
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,40 +2,32 @@ 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.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 {
""
}

InputField(
modifier = Modifier.inputField(),
maxLines = 5,
textFieldValue = textFieldValue,
onValueChanged = onValueChanged,
fieldsData = fieldsData,
keyboardOptions = keyboardOptions,
isError = isError,
errorMessage = errorMessage,
isError = errorData.first,
errorMessage = errorData.second,
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
Expand All @@ -30,9 +31,10 @@ import com.alfresco.content.process.ui.theme.AlfrescoError
@Composable
fun PeopleField(
userDetail: UserGroupDetails? = null,
onAssigneeSelected: (UserGroupDetails?) -> Unit = { },
onAssigneeSelected: (UserGroupDetails?) -> Unit = {},
fieldsData: FieldsData = FieldsData(),
processEntry: ProcessEntry = ProcessEntry(),
errorData: Pair<Boolean, String> = Pair(false, ""),
) {
val labelWithAsterisk = buildAnnotatedString {
append(fieldsData.name)
Expand Down
Loading

0 comments on commit 2777b85

Please sign in to comment.