-
-
Notifications
You must be signed in to change notification settings - Fork 28
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
Resolved OTP issue #2923
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
||
|
@@ -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); | ||
|
@@ -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; | ||
|
||
@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; | ||
} | ||
|
||
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Enhance setOtp method with validation and notification Consider these improvements:
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);
+ }
}
|
||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ensure thread safety of the isSelfTriggered flag
The
isSelfTriggered
flag could potentially cause issues in multi-threaded scenarios. Consider using AtomicBoolean or synchronization.Also applies to: 123-124