Skip to content

Conversation

@OrangeAndGreen
Copy link
Contributor

https://dimagi.atlassian.net/browse/QA-7850

Added key handlers for the Next and Done keys when showing the numeric keypad in the backup code fragment.

When pressed:
-If in recovery: submits the code
-If new registration, first input: proceeds to second input
-If new registration, second input: proceeds to next page

@coderabbitai
Copy link

coderabbitai bot commented Jun 11, 2025

📝 Walkthrough

Walkthrough

A new private method, setupDoneKeys(), has been added to the PersonalIdBackupCodeFragment class. This method sets OnEditorActionListeners on the connectBackupCodeInput and connectBackupCodeRepeatInput fields. The listeners handle IME actions ("Next" or "Done") to either submit the backup code or shift focus based on the fragment's recovery mode and the state of the continue button. The setupDoneKeys() method is invoked from onCreateView() to initialize these listeners. No other logic or error handling has been modified.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant BackupCodeInput
    participant BackupCodeRepeatInput
    participant PersonalIdBackupCodeFragment

    User->>BackupCodeInput: Enter backup code + press "Next"/"Done"
    BackupCodeInput->>PersonalIdBackupCodeFragment: onEditorAction
    alt isRecovery is true
        PersonalIdBackupCodeFragment->>PersonalIdBackupCodeFragment: submitBackupCode()
    else isRecovery is false
        PersonalIdBackupCodeFragment->>BackupCodeRepeatInput: requestFocus()
    end

    User->>BackupCodeRepeatInput: Enter repeat code + press "Next"/"Done"
    BackupCodeRepeatInput->>PersonalIdBackupCodeFragment: onEditorAction
    alt Continue button enabled
        PersonalIdBackupCodeFragment->>PersonalIdBackupCodeFragment: submitBackupCode()
    end
Loading

Possibly related PRs

Suggested reviewers

  • Jignesh-dimagi
  • pm-dimagi
✨ Finishing Touches
  • 📝 Generate Docstrings
🧪 Generate Unit Tests
  • Create PR with Unit Tests
  • Commit Unit Tests in branch dv/backup_code_done_keys
  • Post Copyable Unit Tests in Comment

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai auto-generate unit tests to generate unit tests for this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🧹 Nitpick comments (1)
app/src/org/commcare/fragments/personalId/PersonalIdBackupCodeFragment.java (1)

117-125: Duplicate listener logic could be factored to reduce maintenance overhead.
Both lambdas share the same actionId checks and only differ in the branch that decides whether to submit the backup code. Extracting a small private helper (e.g., boolean handleImeAction(int actionId)) or using a single listener instance would DRY up the code and keep future changes (e.g., adding IME_NULL) in one place.

This is optional, but it will prevent subtle divergence between the two listeners over time.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 277eb5e and dd066e0.

📒 Files selected for processing (1)
  • app/src/org/commcare/fragments/personalId/PersonalIdBackupCodeFragment.java (3 hunks)
🔇 Additional comments (1)
app/src/org/commcare/fragments/personalId/PersonalIdBackupCodeFragment.java (1)

58-60: Sequence placement looks good.
Calling setupDoneKeys() immediately after setupListeners() keeps all listener registration in one place and before any field manipulation (clearBackupCodeFields()), so there is no functional concern here.

Comment on lines 104 to 115
private void setupDoneKeys() {
binding.connectBackupCodeInput.setOnEditorActionListener((v, actionId, event) -> {
if (actionId == EditorInfo.IME_ACTION_NEXT || actionId == EditorInfo.IME_ACTION_DONE) {
if(isRecovery) {
handleBackupCodeSubmission();
} else {
binding.connectBackupCodeRepeatInput.requestFocus();
}
return true;
}
return false;
});
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Verification agent

❓ Verification inconclusive

IME_ACTION_* alone may not catch hardware Enter/Return presses.
OnEditorActionListener is invoked with actionId == EditorInfo.IME_NULL when the user presses the hardware/soft-keyboard Enter key that is not mapped to an explicit IME action. Right now that path falls through and the key does nothing. Consider also checking actionId == EditorInfo.IME_NULL (and optionally event != null && event.getKeyCode() == KeyEvent.KEYCODE_ENTER) so that all Enter/Done presses are handled consistently.

-            if (actionId == EditorInfo.IME_ACTION_NEXT || actionId == EditorInfo.IME_ACTION_DONE) {
+            if (actionId == EditorInfo.IME_ACTION_NEXT ||
+                actionId == EditorInfo.IME_ACTION_DONE ||
+                actionId == EditorInfo.IME_NULL) {

Would you confirm whether the hardware Enter key currently submits the code on target devices?


Handle hardware Enter/Return presses in OnEditorActionListener
On some devices the hardware/soft-keyboard Enter key isn’t mapped to IME_ACTION_NEXT or IME_ACTION_DONE, so actionId becomes IME_NULL and your handler is skipped. Update the condition to also catch Enter presses:

File: app/src/org/commcare/fragments/personalId/PersonalIdBackupCodeFragment.java (around lines 104–115)

-            if (actionId == EditorInfo.IME_ACTION_NEXT || actionId == EditorInfo.IME_ACTION_DONE) {
+            if (actionId == EditorInfo.IME_ACTION_NEXT
+                || actionId == EditorInfo.IME_ACTION_DONE
+                || (actionId == EditorInfo.IME_NULL 
+                    && event != null 
+                    && event.getKeyCode() == KeyEvent.KEYCODE_ENTER)) {

This ensures that both IME actions and hardware Enter/Return submit the backup code as expected.

📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
private void setupDoneKeys() {
binding.connectBackupCodeInput.setOnEditorActionListener((v, actionId, event) -> {
if (actionId == EditorInfo.IME_ACTION_NEXT || actionId == EditorInfo.IME_ACTION_DONE) {
if(isRecovery) {
handleBackupCodeSubmission();
} else {
binding.connectBackupCodeRepeatInput.requestFocus();
}
return true;
}
return false;
});
private void setupDoneKeys() {
binding.connectBackupCodeInput.setOnEditorActionListener((v, actionId, event) -> {
- if (actionId == EditorInfo.IME_ACTION_NEXT || actionId == EditorInfo.IME_ACTION_DONE) {
+ if (actionId == EditorInfo.IME_ACTION_NEXT
+ || actionId == EditorInfo.IME_ACTION_DONE
+ || (actionId == EditorInfo.IME_NULL
+ && event != null
+ && event.getKeyCode() == KeyEvent.KEYCODE_ENTER)) {
if (isRecovery) {
handleBackupCodeSubmission();
} else {
binding.connectBackupCodeRepeatInput.requestFocus();
}
return true;
}
return false;
});
}
🤖 Prompt for AI Agents
In app/src/org/commcare/fragments/personalId/PersonalIdBackupCodeFragment.java
around lines 104 to 115, the OnEditorActionListener currently only handles
IME_ACTION_NEXT and IME_ACTION_DONE, missing hardware Enter/Return key presses
which report actionId as IME_NULL. Update the condition to also check if
actionId equals EditorInfo.IME_NULL and if the event is not null and its keyCode
is KeyEvent.KEYCODE_ENTER, so that pressing the hardware Enter key triggers the
same submission logic as the IME actions.

@shubham1g5
Copy link
Contributor

-If new registration, first input: proceeds to second input

Does this not work for you already ? It's working for me without these changes.

if(isRecovery) {
handleBackupCodeSubmission();
} else {
binding.connectBackupCodeRepeatInput.requestFocus();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

think this is not required.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed it to rely on the default handler for that case: 8ec0edd

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The issue I had before was that the handler was returning true on any Next|Done event, even if it wasn't handled. Now returning false in that case, so the default handler moves the focus to the next input

binding.connectBackupCodeInput.setOnEditorActionListener((v, actionId, event) -> {
if (actionId == EditorInfo.IME_ACTION_NEXT || actionId == EditorInfo.IME_ACTION_DONE) {
if(isRecovery) {
handleBackupCodeSubmission();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we also check if(binding.connectBackupCodeButton.isEnabled()) {

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should also close the keyboard in both cases?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch: 8ec0edd

I decided to leave the keypad open since a wait dialog is displayed while the call is in progress (so the user can't press keys), and then the system automatically hides the keyboard when showing the failure message or proceeding to the next screen.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@OrangeAndGreen Ok, sounds good.

Relaying on default focus behavior when Next button pressed (from first to second backup code input).
@shubham1g5 shubham1g5 merged commit d820ee1 into commcare_2.57 Jun 12, 2025
2 checks passed
@shubham1g5 shubham1g5 deleted the dv/backup_code_done_keys branch June 12, 2025 16:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

skip-integration-tests Skip android tests.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants