Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix part of #5070: Display empty answer message in ratio input interaction #5263

Merged
merged 21 commits into from
Jan 11, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
Expand Up @@ -29,6 +29,7 @@ class StringToRatioParser {
val normalized = text.normalizeWhitespace()
val ratio = parseRatioOrNull(normalized)
return when {
normalized.isBlank() -> RatioParsingError.EMPTY_INPUT
!normalized.matches(invalidRatioRegex) || ratio == null -> RatioParsingError.INVALID_FORMAT
numberOfTerms != 0 && ratio.ratioComponentCount != numberOfTerms -> {
RatioParsingError.INVALID_SIZE
Expand Down Expand Up @@ -77,7 +78,8 @@ class StringToRatioParser {
INVALID_FORMAT(error = R.string.ratio_error_invalid_format),
INVALID_COLONS(error = R.string.ratio_error_invalid_colons),
INVALID_SIZE(error = R.string.ratio_error_invalid_size),
INCLUDES_ZERO(error = R.string.ratio_error_includes_zero);
INCLUDES_ZERO(error = R.string.ratio_error_includes_zero),
EMPTY_INPUT(error = R.string.ratio_error_empty_input);

/**
* Returns the string corresponding to this error's string resources, or null if there is none.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,17 @@ class RatioExpressionInputInteractionViewModel private constructor(
override fun onPropertyChanged(sender: Observable, propertyId: Int) {
errorOrAvailabilityCheckReceiver.onPendingAnswerErrorOrAvailabilityCheck(
pendingAnswerError,
answerText.isNotEmpty()
true
adhiamboperes marked this conversation as resolved.
Show resolved Hide resolved
)
}
}
errorMessage.addOnPropertyChangedCallback(callback)
isAnswerAvailable.addOnPropertyChangedCallback(callback)

errorOrAvailabilityCheckReceiver.onPendingAnswerErrorOrAvailabilityCheck(
null,
true
adhiamboperes marked this conversation as resolved.
Show resolved Hide resolved
)
}

override fun getPendingAnswer(): UserAnswer = UserAnswer.newBuilder().apply {
Expand All @@ -67,23 +72,24 @@ class RatioExpressionInputInteractionViewModel private constructor(
}
}.build()

/** It checks the pending error for the current ratio input, and correspondingly updates the error string based on the specified error category. */
/**
* It checks the pending error for the current ratio input, and correspondingly
* updates the error string based on the specified error category.
*/
override fun checkPendingAnswerError(category: AnswerErrorCategory): String? {
if (answerText.isNotEmpty()) {
when (category) {
AnswerErrorCategory.REAL_TIME ->
pendingAnswerError =
stringToRatioParser.getRealTimeAnswerError(answerText.toString())
.getErrorMessageFromStringRes(resourceHandler)
AnswerErrorCategory.SUBMIT_TIME ->
pendingAnswerError =
stringToRatioParser.getSubmitTimeError(
answerText.toString(),
numberOfTerms = numberOfTerms
).getErrorMessageFromStringRes(resourceHandler)
}
errorMessage.set(pendingAnswerError)
pendingAnswerError = when (category) {
AnswerErrorCategory.REAL_TIME ->
if (answerText.isNotEmpty())
stringToRatioParser.getRealTimeAnswerError(answerText.toString())
.getErrorMessageFromStringRes(resourceHandler)
else null
AnswerErrorCategory.SUBMIT_TIME ->
stringToRatioParser.getSubmitTimeError(
answerText.toString(),
numberOfTerms = numberOfTerms
).getErrorMessageFromStringRes(resourceHandler)
}
errorMessage.set(pendingAnswerError)
return pendingAnswerError
}

Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@
<string name="ratio_error_invalid_colons" description="The error message for ratio when there is colons next to each other.">Your answer has two colons (:) next to each other.</string>
<string name="ratio_error_invalid_size" description="The error message for ratio when there is less number of terms than the creator has specified in customization arg.">Number of terms is not equal to the required terms.</string>
<string name="ratio_error_includes_zero" description="The error message for ratio when there is 0 term in the ratio.">Ratios cannot have 0 as an element.</string>
<string name="ratio_error_empty_input">Enter a ratio to continue</string>
adhiamboperes marked this conversation as resolved.
Show resolved Hide resolved
<string name="unknown_size">Unknown size</string>
<string name="size_bytes">%s Bytes</string>
<string name="size_kb">%s KB</string>
Expand Down
Loading