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

Resolved OTP issue #2923

Merged
merged 3 commits into from
Dec 31, 2024
Merged
Changes from all 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
56 changes: 46 additions & 10 deletions app/src/org/commcare/views/connect/CustomOtpView.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,11 @@
import android.graphics.Color;
import android.graphics.drawable.GradientDrawable;
import android.text.Editable;
import android.text.InputFilter;
import android.text.InputType;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.util.Log;
import android.view.Gravity;
import android.view.KeyEvent;
import android.widget.EditText;
import android.widget.LinearLayout;

Expand Down Expand Up @@ -81,7 +80,6 @@ private EditText createOtpEditText(int index) {
editText.setGravity(Gravity.CENTER);
editText.setTextColor(isErrorState ? errorTextColor : textColor);
editText.setTextSize(textSize);
editText.setFilters(new InputFilter[]{new InputFilter.LengthFilter(1)});
editText.setInputType(InputType.TYPE_CLASS_NUMBER);
editText.setBackground(createBackgroundDrawable());
editText.setId(index);
Expand All @@ -98,38 +96,75 @@ private EditText createOtpEditText(int index) {
}
});

editText.setOnKeyListener((v, keyCode, event) -> {
if (keyCode == KeyEvent.KEYCODE_DEL && event.getAction() == KeyEvent.ACTION_DOWN) {
int edtIndex = index > 0 ? index - 1 : 0;

EditText edt = (EditText)getChildAt(edtIndex);
edt.setText("");
edt.requestFocus();
return true; // Consume the event
}
return false;
});

editText.addTextChangedListener(new TextWatcher() {
private boolean isSelfTriggered = false;

Comment on lines +112 to +113
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue

Ensure thread safety of the isSelfTriggered flag

The isSelfTriggered flag could potentially cause issues in multi-threaded scenarios. Consider using AtomicBoolean or synchronization.

-    private boolean isSelfTriggered = false;
+    private final AtomicBoolean isSelfTriggered = new AtomicBoolean(false);

Also applies to: 123-124

@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}

@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (isSelfTriggered) {
return;
}

setErrorState(false);

if (s.length() == 1) {
// Automatically move to the next EditText if current is filled
// Overwrite the existing value and move to the next EditText
if (index < digitCount - 1) {
getChildAt(index + 1).requestFocus();
EditText nextEditText = (EditText) getChildAt(index + 1);
nextEditText.setText(""); // Clear next field
nextEditText.requestFocus();
} else {
checkOtpCompletion();
}
if (otpChangedListener != null) {
String otp = getOtpValue();
otpChangedListener.onOtpChanged(otp);
}
} else {
if (otpChangedListener != null) {
String otp = getOtpValue();
otpChangedListener.onOtpChanged(otp);
} else if (s.length() > 1) {
// If more than one character, replace with the latest input
char lastChar = s.charAt(s.length() - 1);
String text = "";
if(Character.isDigit(lastChar)) {
text = String.valueOf(lastChar);
}
editText.setText(text);
} else if (otpChangedListener != null) {
String otp = getOtpValue();
otpChangedListener.onOtpChanged(otp);
}
}

@Override
public void afterTextChanged(Editable s) {
if (isSelfTriggered) return;

if (s.length() > 1) {
// Ensure only the last character is preserved
isSelfTriggered = true;
editText.setText(s.subSequence(s.length() - 1, s.length()));
editText.setSelection(1); // Place cursor after the character
isSelfTriggered = false;
}
}
});


return editText;
}

Expand Down Expand Up @@ -200,8 +235,9 @@ public void setOtp(String otp) {
if (i < otp.length()) {
editText.setText(String.valueOf(otp.charAt(i)));
} else {
editText.setText(""); // Clear remaining fields if OTP is shorter
editText.setText("");
}
editText.clearFocus(); // Ensure no field remains focused
}
Comment on lines +238 to 241
Copy link

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Enhance setOtp method with validation and notification

Consider these improvements:

  1. Validate that input contains only digits
  2. Notify listeners when OTP is set programmatically
 public void setOtp(String otp) {
     if (otp.length() > digitCount) {
         throw new IllegalArgumentException("OTP length exceeds the digit count");
     }
+    if (!otp.matches("\\d*")) {
+        throw new IllegalArgumentException("OTP must contain only digits");
+    }
 
     for (int i = 0; i < digitCount; i++) {
         EditText editText = (EditText) getChildAt(i);
         if (i < otp.length()) {
             editText.setText(String.valueOf(otp.charAt(i)));
         } else {
             editText.setText("");
         }
         editText.clearFocus();
     }
+    // Notify listeners
+    if (otpChangedListener != null) {
+        otpChangedListener.onOtpChanged(otp);
+    }
+    if (otp.length() == digitCount && otpCompleteListener != null) {
+        otpCompleteListener.onOtpComplete(otp);
+    }
 }

Committable suggestion skipped: line range outside the PR's diff.

}
}