| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311 |
- package com.datarecovery.master.module.login;
- import android.accounts.NetworkErrorException;
- import android.text.TextUtils;
- import androidx.annotation.NonNull;
- import androidx.lifecycle.LiveData;
- import androidx.lifecycle.MutableLiveData;
- import com.atmob.app.lib.base.BaseViewModel;
- import com.atmob.app.lib.livedata.SingleLiveEvent;
- import com.atmob.common.logging.AtmobLog;
- import com.atmob.common.runtime.ContextUtil;
- import com.datarecovery.master.R;
- import com.datarecovery.master.data.api.response.LoginResponse;
- import com.datarecovery.master.data.consts.ErrorCode;
- import com.datarecovery.master.data.consts.EventId;
- import com.datarecovery.master.data.repositories.AccountRepository;
- import com.datarecovery.master.handler.EventHelper;
- import com.datarecovery.master.sdk.quicklogin.QuickLoginHelper;
- import com.datarecovery.master.utils.BoxingUtil;
- import com.datarecovery.master.utils.Maps;
- import com.datarecovery.master.utils.RxHttpHandler;
- import com.datarecovery.master.utils.ToastUtil;
- import com.netease.nis.quicklogin.helper.UnifyUiConfig;
- import com.netease.nis.quicklogin.listener.QuickLoginPreMobileListener;
- import java.util.concurrent.TimeUnit;
- import java.util.regex.Matcher;
- import java.util.regex.Pattern;
- import javax.inject.Inject;
- import atmob.reactivex.rxjava3.core.SingleObserver;
- import atmob.reactivex.rxjava3.disposables.Disposable;
- import atmob.rxjava.utils.RxJavaUtil;
- import dagger.hilt.android.lifecycle.HiltViewModel;
- @HiltViewModel
- public class LoginViewModel extends BaseViewModel {
- private final String TAG = "LoginViewModel";
- private final SingleLiveEvent<Boolean> showLoading = new SingleLiveEvent<>();
- private final MutableLiveData<String> phoneNum = new MutableLiveData<>();
- private final MutableLiveData<Boolean> isRequestCodeCountdown = new MutableLiveData<>(false);
- private final MutableLiveData<Boolean> isCheckedAgreement = new MutableLiveData<>(false);
- private final MutableLiveData<String> requestCodeCountdown = new MutableLiveData<>();
- private final MutableLiveData<String> verificationCode = new MutableLiveData<>();
- private final MutableLiveData<CharSequence> agreeText = new MutableLiveData<>();
- private final SingleLiveEvent<?> finishEvent = new SingleLiveEvent<>();
- private final AccountRepository accountRepository;
- private Disposable getCodeCountdownDisposable;
- private String reportId;
- @Inject
- public LoginViewModel(AccountRepository accountRepository) {
- this.accountRepository = accountRepository;
- init();
- }
- public LiveData<Boolean> getShowLoading() {
- return showLoading;
- }
- public MutableLiveData<String> getPhoneNum() {
- return phoneNum;
- }
- public MutableLiveData<String> getVerificationCode() {
- return verificationCode;
- }
- public MutableLiveData<Boolean> getIsCheckedAgreement() {
- return isCheckedAgreement;
- }
- public LiveData<Boolean> getIsRequestCodeCountdown() {
- return isRequestCodeCountdown;
- }
- public LiveData<?> getFinishEvent() {
- return finishEvent;
- }
- public LiveData<String> getRequestCodeCountdown() {
- return requestCodeCountdown;
- }
- public LiveData<CharSequence> getAgreeText() {
- return agreeText;
- }
- public void onBackClick() {
- finishEvent.call();
- }
- private void init() {
- getPreMobileNumber();
- }
- private void getPreMobileNumber() {
- QuickLoginHelper.openQuickLoginActivity(new QuickLoginHelper.QuickLoginListener() {
- @Override
- public void onGetTokenSuccess(String YDToken, String accessToken) {
- quickLogin(YDToken, accessToken);
- }
- @Override
- public void onGetTokenError(String msg) {
- ToastUtil.show(R.string.quick_login_fail, ToastUtil.LENGTH_SHORT);
- }
- @Override
- public void onOpenQuickLoginActivity() {
- }
- });
- }
- private void quickLogin(String YDToken, String accessToken) {
- accountRepository.quickLogin(YDToken, accessToken)
- .subscribe(new SingleObserver<LoginResponse>() {
- @Override
- public void onSubscribe(@atmob.reactivex.rxjava3.annotations.NonNull Disposable d) {
- addDisposable(d);
- }
- @Override
- public void onSuccess(@atmob.reactivex.rxjava3.annotations.NonNull LoginResponse loginResponse) {
- ToastUtil.show(R.string.login_success, ToastUtil.LENGTH_SHORT);
- finishEvent.call();
- }
- @Override
- public void onError(@atmob.reactivex.rxjava3.annotations.NonNull Throwable e) {
- if (e instanceof NetworkErrorException) {
- ToastUtil.show(R.string.net_error, ToastUtil.LENGTH_SHORT);
- return;
- }
- if (e instanceof RxHttpHandler.ServerErrorException) {
- RxHttpHandler.ServerErrorException serverErrorException = (RxHttpHandler.ServerErrorException) e;
- ToastUtil.show(serverErrorException.getMsg(), ToastUtil.LENGTH_SHORT);
- } else {
- AtmobLog.e(TAG, "QuickLogin Error: " + e.getMessage());
- ToastUtil.show(R.string.login_failed_toast, ToastUtil.LENGTH_SHORT);
- }
- }
- });
- }
- public void onGetCodeClick() {
- EventHelper.report(EventId.hf200011);
- String phoneNumText = phoneNum.getValue();
- if (isPhone(phoneNumText)) {
- doRequestVerificationCode(phoneNumText);
- } else {
- EventHelper.report(EventId.hf1000305, Maps.asMap(EventId.EVENT_ID, "hf11034"));
- ToastUtil.show(R.string.login_phone_num_11, ToastUtil.LENGTH_SHORT);
- }
- }
- private void doRequestVerificationCode(String phoneNumText) {
- if (BoxingUtil.boxing(isRequestCodeCountdown.getValue())) {
- ToastUtil.show(R.string.login_request_code_frequently_toast, ToastUtil.LENGTH_SHORT);
- return;
- }
- accountRepository.requestUserCode(phoneNumText)
- .subscribe(new SingleObserver<Object>() {
- @Override
- public void onSubscribe(@NonNull Disposable d) {
- addDisposable(d);
- showLoading.setValue(true);
- }
- @Override
- public void onSuccess(@NonNull Object o) {
- startGetCodeCountdown();
- showLoading.setValue(false);
- ToastUtil.show(R.string.login_verification_code_request_success_toast, ToastUtil.LENGTH_SHORT);
- }
- @Override
- public void onError(@NonNull Throwable e) {
- showLoading.setValue(false);
- if (e instanceof AccountRepository.RequestCodeTooOftenException) {
- EventHelper.report(EventId.hf1000305, Maps.asMap(EventId.EVENT_ID, "hf11022"));
- ToastUtil.show(R.string.login_request_code_frequently_toast, ToastUtil.LENGTH_SHORT);
- return;
- }
- e.printStackTrace();
- stopGetCodeCountdown();
- EventHelper.report(EventId.hf1000305, Maps.asMap(EventId.EVENT_ID, "hf11022"));
- ToastUtil.show(R.string.login_verification_code_request_failed_toast, ToastUtil.LENGTH_SHORT);
- }
- });
- }
- private void stopGetCodeCountdown() {
- if (getCodeCountdownDisposable != null && !getCodeCountdownDisposable.isDisposed()) {
- getCodeCountdownDisposable.dispose();
- }
- isRequestCodeCountdown.setValue(false);
- }
- private void startGetCodeCountdown() {
- if (getCodeCountdownDisposable != null && !getCodeCountdownDisposable.isDisposed()) {
- getCodeCountdownDisposable.dispose();
- }
- isRequestCodeCountdown.setValue(true);
- getCodeCountdownDisposable = RxJavaUtil.interval(0, 1, 60, TimeUnit.SECONDS,
- index -> requestCodeCountdown.setValue(String.valueOf(60 - index)), () -> isRequestCodeCountdown.setValue(false));
- addDisposable(getCodeCountdownDisposable);
- }
- public void onLoginClick() {
- EventHelper.report(EventId.hf1000302);
- String phoneNumText = phoneNum.getValue();
- if (!isPhone(phoneNumText)) {
- ToastUtil.show(R.string.login_phone_num_11, ToastUtil.LENGTH_SHORT);
- return;
- }
- String code = verificationCode.getValue();
- if (TextUtils.isEmpty(code)) {
- ToastUtil.show(R.string.login_code_hint, ToastUtil.LENGTH_SHORT);
- return;
- }
- if (!BoxingUtil.boxing(isCheckedAgreement.getValue())) {
- EventHelper.report(EventId.hf1000304, Maps.asMap(EventId.EVENT_ID, "hf11019"));
- ToastUtil.show(R.string.login_please_agree, ToastUtil.LENGTH_SHORT);
- return;
- }
- accountRepository.login(phoneNum.getValue(), verificationCode.getValue())
- .subscribe(new SingleObserver<Object>() {
- @Override
- public void onSubscribe(@NonNull Disposable d) {
- addDisposable(d);
- showLoading.setValue(true);
- }
- @Override
- public void onSuccess(@NonNull Object o) {
- showLoading.setValue(false);
- ToastUtil.show(R.string.login_success, ToastUtil.LENGTH_SHORT);
- finishEvent.call();
- EventHelper.report(EventId.hf1000303);
- }
- @Override
- public void onError(@NonNull Throwable e) {
- showLoading.setValue(false);
- if (e instanceof AccountRepository.LoginTooOftenException) {
- EventHelper.report(EventId.hf1000304, Maps.asMap(EventId.EVENT_ID, "hf11022"));
- ToastUtil.show(R.string.login_too_often_toast, ToastUtil.LENGTH_SHORT);
- return;
- }
- if (e instanceof NetworkErrorException) {
- EventHelper.report(EventId.hf1000304, Maps.asMap(EventId.EVENT_ID, "hf11019"));
- ToastUtil.show(R.string.net_error, ToastUtil.LENGTH_SHORT);
- return;
- }
- RxHttpHandler.ServerErrorException serverErrorException
- = e instanceof RxHttpHandler.ServerErrorException ? ((RxHttpHandler.ServerErrorException) e) : null;
- if (serverErrorException != null) {
- if (serverErrorException.getCode() == ErrorCode.ERROR_CODE_VERIFICATION_CODE_ERROR) {
- EventHelper.report(EventId.hf1000304, Maps.asMap(EventId.EVENT_ID, "hf11021"));
- ToastUtil.show(R.string.login_verification_code_error_toast, ToastUtil.LENGTH_SHORT);
- } else {
- EventHelper.report(EventId.hf1000304, Maps.asMap(EventId.EVENT_ID, "hf11022"));
- ToastUtil.show(serverErrorException.getMsg(), ToastUtil.LENGTH_SHORT);
- }
- } else {
- EventHelper.report(EventId.hf1000304, Maps.asMap(EventId.EVENT_ID, "hf11022"));
- ToastUtil.show(R.string.login_failed_toast, ToastUtil.LENGTH_SHORT);
- }
- }
- });
- }
- public boolean isCanLogin(String phoneNum, String verificationCode) {
- return isPhone(phoneNum) && verificationCode != null && verificationCode.length() > 0;
- }
- private boolean isPhone(String phoneNumText) {
- Pattern phonePattern = Pattern.compile("^1\\d{10}$");
- if (phoneNumText == null) {
- return false;
- }
- phoneNumText = phoneNumText.trim();
- if (TextUtils.isEmpty(phoneNumText)) {
- return false;
- }
- Matcher matcher = phonePattern.matcher(phoneNumText);
- return matcher.matches();
- }
- public void setReportId(String reportId) {
- if (!TextUtils.isEmpty(this.reportId)) {
- return;
- }
- this.reportId = reportId;
- EventHelper.report(EventId.hf1000301, Maps.asMap(EventId.EVENT_ID, reportId));
- }
- }
|