Implementation of Auxiliary Class for Obtaining Verification Code Based on RxJava Framework
- 2021-09-12 02:15:09
- OfStack
This article example for everyone to share RxJava to achieve access to verification code auxiliary class specific code, for your reference, the specific content is as follows
Application scenario:
1-like projects have the function of obtaining verification code, and may be used in more than one place. Its logic is current:
... Monitor the validity of mobile phone number or email address in the input box in real time
... When legal, you can click the Get Verification Code button
When the button is clicked, the interface is called to request the verification code. At the same time, the button becomes unclickable and the countdown is displayed
... Execute normal logic when the request is successful, cancel the countdown when the request fails, and restore the button state
Design thought
... Objects involved in the whole function: 1 EditText for inputting account information, 1 Button for obtaining verification code, and 1 Rxjava manager for managing the life cycle of Rx events (different management schemes can be paired by themselves)
... Initialize monitoring EditText input, initialize anti-shake button click event
... the logic in handling events
Develop two abstract methods, a method for obtaining verification codes and a method for verification rules
Code
package txcap.com.bigtime.utils;
import android.widget.Button;
import android.widget.EditText;
import com.jakewharton.rxbinding.view.RxView;
import com.jakewharton.rxbinding.widget.RxTextView;
import java.util.concurrent.TimeUnit;
import rx.Observable;
import rx.Subscriber;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action0;
import rx.functions.Action1;
import txcap.com.bigtime.Constant;
import txcap.com.bigtime.R;
import txcap.com.library.core.app.BaseApplication;
import txcap.com.library.core.rx.RxManager;
import txcap.com.library.utils.file.ResUtil;
/**
* @author gaojigong
* @version V1.0
* @Description: Tools for processing verification codes
* @date 17/3/22
*/
public abstract class CheckCodeUtil {
private RxManager mRxManager;
private Button btnGetCheckCode;
private EditText edtAccount;
// During the verification code acquisition, the button is always in the countdown state and is not affected by the input box event
protected boolean countDowning = false;
// Used to cancel the countdown
private Subscription intervalSub;
public CheckCodeUtil(RxManager mRxManager, Button btnGetCheckCode, EditText edtAccount) {
this.mRxManager = mRxManager;
this.btnGetCheckCode = btnGetCheckCode;
this.edtAccount = edtAccount;
init();
}
private void init() {
RxView.enabled(btnGetCheckCode).call(false);
Observable<CharSequence> observableAccount = RxTextView.textChanges(edtAccount);
// The account number input is unreasonable. You can't click to get the verification code
mRxManager.add(observableAccount
.subscribeOn(AndroidSchedulers.mainThread())
.subscribe(new Action1<CharSequence>() {
@Override
public void call(CharSequence charSequence) {
if(!countDowning){// This reduces repetitive execution
if (etCheck(charSequence.toString())) {
RxView.enabled(btnGetCheckCode).call(true);
} else {
RxView.enabled(btnGetCheckCode).call(false);
}
}
}
}));
// Click to get the verification code
mRxManager.add(
RxView.clicks(btnGetCheckCode)
.throttleFirst(Constant.CLICK_THROTTLE, TimeUnit.SECONDS)
.subscribe(new Action1<Void>() {
@Override
public void call(Void aVoid) {
getCheckCode();
}
})
);
}
/**
* Get verification code
*/
private void getCheckCode() {
intervalSub = Observable.interval(0,1,TimeUnit.SECONDS, AndroidSchedulers.mainThread())
.take(Constant.CHECK_CODE_SECOND)
.doOnSubscribe(new Action0() {
@Override
public void call() {
getCodeNumber();
// Button becomes unclickable
RxView.enabled(btnGetCheckCode).call(false);
countDowning = true;
}
})
.subscribe(new Subscriber<Long>() {
@Override
public void onCompleted() {
countDowning = false;
RxTextView.text(btnGetCheckCode).call(ResUtil.getResString(BaseApplication.getAppContext(),R.string.get_check_code));
if(etCheck(edtAccount.getText().toString())){
RxView.enabled(btnGetCheckCode).call(true);
}
}
@Override
public void onError(Throwable e) {
countDowning = false;
RxTextView.text(btnGetCheckCode).call(ResUtil.getResString(BaseApplication.getAppContext(),R.string.get_check_code));
if(etCheck(edtAccount.getText().toString())){
RxView.enabled(btnGetCheckCode).call(true);
}
}
@Override
public void onNext(Long aLong) {
RxTextView.text(btnGetCheckCode).call((Constant.CHECK_CODE_SECOND - aLong)+"s");
}
});
mRxManager.add(intervalSub);
}
/**
* Get verification code
*/
public abstract void getCodeNumber();
/**
* Verification rule
* @param str
* @return
*/
public abstract boolean etCheck(String str);
public void getCodeError(){
if(null != intervalSub){
if(!intervalSub.isUnsubscribed()){
intervalSub.unsubscribe();
RxTextView.text(btnGetCheckCode).call(ResUtil.getResString(BaseApplication.getAppContext(),R.string.get_check_code));
RxView.enabled(btnGetCheckCode).call(true);
countDowning = false;
}
}
}
}
Use instances
@BindView(R.id.edt_account)
EditText edtAccount;
@BindView(R.id.btn_get_check_code)
Button btnGetCheckCode;
private CheckCodeUtil checkCodeUtil;
checkCodeUtil = new CheckCodeUtil(mRxManager,btnGetCheckCode,edtAccount) {
@Override
public void getCodeNumber() {
mPresenter.getCode(edtAccount.getText().toString());
}
@Override
public boolean etCheck(String str) {
return StrCheckUtil.checkedAccount(str);
}
};
@Override
public void getCodeFailed(String message) {
showToastLong(" Failed to get verification code :" + message);
checkCodeUtil.getCodeError();
}
Characteristic
... Reducing code redundancy
... Convenient maintenance of requirements