๐งฎ Mental Calculation Game Mode (Developer Docs)
The MentalCalculationFragment implements the Mental Arithmetic Game Mode in zMantra.
It challenges learners to solve arithmetic problems by listening to math expressions and entering answers.
This mode emphasizes auditory math comprehension, step-by-step reading, and time-limited problem solving.
๐ Location
com.zendalona.zmantra.presentation.features.game.mentalcalculation.MentalCalculationFragment
๐ Core Responsibilities
- Display & Read Arithmetic Problems
- Math expressions are broken into tokens (e.g.,
"12 รท 3 + 5"
โ"12"
,"รท"
,"3"
,"+"
,"5"
). - Tokens are revealed sequentially with delays, supporting cognitive load management.
-
Uses
TTSHelper
andAccessibilityUtils
for TalkBack users. -
User Answer Input
- Learner enters numeric answers into
EditText
(answerEt
). -
Submission via button or keyboard Enter/Done action.
-
Validation & Feedback
- Compares learnerโs answer to
correctAnswer
. - Provides toast feedback for empty or invalid inputs.
-
Calls
handleAnswerSubmission()
fromBaseGameFragment
to standardize scoring and flow. -
Accessibility
- Uses TalkBack announcements for token-by-token reading.
- Prevents unwanted auto-keyboard popup (users manually open).
-
Keeps clear separation of Read Question vs Answer Input focus.
-
Game Flow Control
- Cycles through
GameQuestion
list sequentially. - Ends session when all questions are attempted (
endGame()
).
โ๏ธ Key Fields
Variable | Type | Purpose |
---|---|---|
binding |
FragmentGameMentalCalculationBinding? |
View binding for layout. |
handler |
Handler(Looper.getMainLooper()) |
Manages delayed token reveals. |
questions |
List<GameQuestion> |
All math problems for this session. |
currentIndex |
Int |
Current question index. |
correctAnswer |
Int |
Expected numeric result of current problem. |
startTime |
Long |
Timestamp when problem begins (for timing). |
revealTokens |
List<String> |
Tokens of current math expression. |
revealIndex |
Int |
Position of next token to reveal. |
isRevealing |
Boolean |
Prevents overlapping token reveal calls. |
๐ Lifecycle
- onCreateView()
- Inflates layout.
- Sets listeners for:
- Read Question button โ
onReadQuestionClicked()
. - Submit Answer button โ
checkAnswer()
. - IME Action Done/Enter key โ
checkAnswer()
.
- Read Question button โ
-
Handles focus to prevent keyboard auto-popup when toggling between Read/Answer.
-
onQuestionsLoaded()
- Loads provided
GameQuestion
s. - If none provided โ fallback default (
"1 + 2"
). -
Calls
loadNextQuestion()
. -
onReadQuestionClicked()
- Splits expression into tokens (numbers and operators).
- Begins sequential reveal (
showToken()
every 1s). -
Disables button while reading to avoid re-entry.
-
revealNextToken() / showToken()
- Displays token visually and via TalkBack (
announceForAccessibility
). -
Replaces
/
withรท
for better readability. -
loadNextQuestion()
- Resets UI and attempts.
- Reads expression tokens aloud if TalkBack is active.
-
Re-enables input after reveal delay.
-
checkAnswer()
- Validates input from
answerEt
. - If empty โ prompt user with toast.
- If non-numeric โ error toast.
- Else โ compare against
correctAnswer
. - On correct: optional bell sound effect if
celebration == true
. - On incorrect: clear input and refocus.
-
On show correct: skip to next.
-
onPause()
-
Stops token reveals and TTS.
-
onDestroyView()
- Clears binding to avoid leaks.
๐ง Accessibility
- Token-by-token spoken announcements for math expressions.
- Prevents keyboard interference when toggling focus.
- Custom TTS formatting via
TTSHelper.formatMathText()
. - Explicit TalkBack instructions: "Solve 12 รท 3 + 5".
๐งช Testing Notes
- Verify token reveal timing (1 second per token).
- Confirm Read Question button is disabled during reveal.
- Test answer validation:
- Empty โ "Enter answer".
- Wrong input โ "Wrong answer".
- Correct โ proceeds to next with optional bell sound.
- Ensure time tracking works: elapsed vs
timeLimit
. - Check TalkBack flow:
- Announces "Solve expression" at start.
- Announces each token sequentially.