LoginViewModel.java 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. package com.datarecovery.master.module.login;
  2. import android.accounts.NetworkErrorException;
  3. import android.text.TextUtils;
  4. import androidx.annotation.NonNull;
  5. import androidx.lifecycle.LiveData;
  6. import androidx.lifecycle.MutableLiveData;
  7. import com.atmob.app.lib.base.BaseViewModel;
  8. import com.atmob.app.lib.livedata.SingleLiveEvent;
  9. import com.atmob.common.logging.AtmobLog;
  10. import com.atmob.common.runtime.ContextUtil;
  11. import com.datarecovery.master.R;
  12. import com.datarecovery.master.data.api.response.LoginResponse;
  13. import com.datarecovery.master.data.consts.ErrorCode;
  14. import com.datarecovery.master.data.consts.EventId;
  15. import com.datarecovery.master.data.repositories.AccountRepository;
  16. import com.datarecovery.master.handler.EventHelper;
  17. import com.datarecovery.master.sdk.quicklogin.QuickLoginHelper;
  18. import com.datarecovery.master.utils.BoxingUtil;
  19. import com.datarecovery.master.utils.Maps;
  20. import com.datarecovery.master.utils.RxHttpHandler;
  21. import com.datarecovery.master.utils.ToastUtil;
  22. import com.netease.nis.quicklogin.helper.UnifyUiConfig;
  23. import com.netease.nis.quicklogin.listener.QuickLoginPreMobileListener;
  24. import java.util.concurrent.TimeUnit;
  25. import java.util.regex.Matcher;
  26. import java.util.regex.Pattern;
  27. import javax.inject.Inject;
  28. import atmob.reactivex.rxjava3.core.SingleObserver;
  29. import atmob.reactivex.rxjava3.disposables.Disposable;
  30. import atmob.rxjava.utils.RxJavaUtil;
  31. import dagger.hilt.android.lifecycle.HiltViewModel;
  32. @HiltViewModel
  33. public class LoginViewModel extends BaseViewModel {
  34. private final String TAG = "LoginViewModel";
  35. private final SingleLiveEvent<Boolean> showLoading = new SingleLiveEvent<>();
  36. private final MutableLiveData<String> phoneNum = new MutableLiveData<>();
  37. private final MutableLiveData<Boolean> isRequestCodeCountdown = new MutableLiveData<>(false);
  38. private final MutableLiveData<Boolean> isCheckedAgreement = new MutableLiveData<>(false);
  39. private final MutableLiveData<String> requestCodeCountdown = new MutableLiveData<>();
  40. private final MutableLiveData<String> verificationCode = new MutableLiveData<>();
  41. private final MutableLiveData<CharSequence> agreeText = new MutableLiveData<>();
  42. private final SingleLiveEvent<?> finishEvent = new SingleLiveEvent<>();
  43. private final AccountRepository accountRepository;
  44. private Disposable getCodeCountdownDisposable;
  45. private String reportId;
  46. @Inject
  47. public LoginViewModel(AccountRepository accountRepository) {
  48. this.accountRepository = accountRepository;
  49. init();
  50. }
  51. public LiveData<Boolean> getShowLoading() {
  52. return showLoading;
  53. }
  54. public MutableLiveData<String> getPhoneNum() {
  55. return phoneNum;
  56. }
  57. public MutableLiveData<String> getVerificationCode() {
  58. return verificationCode;
  59. }
  60. public MutableLiveData<Boolean> getIsCheckedAgreement() {
  61. return isCheckedAgreement;
  62. }
  63. public LiveData<Boolean> getIsRequestCodeCountdown() {
  64. return isRequestCodeCountdown;
  65. }
  66. public LiveData<?> getFinishEvent() {
  67. return finishEvent;
  68. }
  69. public LiveData<String> getRequestCodeCountdown() {
  70. return requestCodeCountdown;
  71. }
  72. public LiveData<CharSequence> getAgreeText() {
  73. return agreeText;
  74. }
  75. public void onBackClick() {
  76. finishEvent.call();
  77. }
  78. private void init() {
  79. getPreMobileNumber();
  80. }
  81. private void getPreMobileNumber() {
  82. QuickLoginHelper.openQuickLoginActivity(new QuickLoginHelper.QuickLoginListener() {
  83. @Override
  84. public void onGetTokenSuccess(String YDToken, String accessToken) {
  85. quickLogin(YDToken, accessToken);
  86. }
  87. @Override
  88. public void onGetTokenError(String msg) {
  89. ToastUtil.show(R.string.quick_login_fail, ToastUtil.LENGTH_SHORT);
  90. }
  91. @Override
  92. public void onOpenQuickLoginActivity() {
  93. }
  94. });
  95. }
  96. private void quickLogin(String YDToken, String accessToken) {
  97. accountRepository.quickLogin(YDToken, accessToken)
  98. .subscribe(new SingleObserver<LoginResponse>() {
  99. @Override
  100. public void onSubscribe(@atmob.reactivex.rxjava3.annotations.NonNull Disposable d) {
  101. addDisposable(d);
  102. }
  103. @Override
  104. public void onSuccess(@atmob.reactivex.rxjava3.annotations.NonNull LoginResponse loginResponse) {
  105. ToastUtil.show(R.string.login_success, ToastUtil.LENGTH_SHORT);
  106. finishEvent.call();
  107. }
  108. @Override
  109. public void onError(@atmob.reactivex.rxjava3.annotations.NonNull Throwable e) {
  110. if (e instanceof NetworkErrorException) {
  111. ToastUtil.show(R.string.net_error, ToastUtil.LENGTH_SHORT);
  112. return;
  113. }
  114. if (e instanceof RxHttpHandler.ServerErrorException) {
  115. RxHttpHandler.ServerErrorException serverErrorException = (RxHttpHandler.ServerErrorException) e;
  116. ToastUtil.show(serverErrorException.getMsg(), ToastUtil.LENGTH_SHORT);
  117. } else {
  118. AtmobLog.e(TAG, "QuickLogin Error: " + e.getMessage());
  119. ToastUtil.show(R.string.login_failed_toast, ToastUtil.LENGTH_SHORT);
  120. }
  121. }
  122. });
  123. }
  124. public void onGetCodeClick() {
  125. EventHelper.report(EventId.hf200011);
  126. String phoneNumText = phoneNum.getValue();
  127. if (isPhone(phoneNumText)) {
  128. doRequestVerificationCode(phoneNumText);
  129. } else {
  130. EventHelper.report(EventId.hf1000305, Maps.asMap(EventId.EVENT_ID, "hf11034"));
  131. ToastUtil.show(R.string.login_phone_num_11, ToastUtil.LENGTH_SHORT);
  132. }
  133. }
  134. private void doRequestVerificationCode(String phoneNumText) {
  135. if (BoxingUtil.boxing(isRequestCodeCountdown.getValue())) {
  136. ToastUtil.show(R.string.login_request_code_frequently_toast, ToastUtil.LENGTH_SHORT);
  137. return;
  138. }
  139. accountRepository.requestUserCode(phoneNumText)
  140. .subscribe(new SingleObserver<Object>() {
  141. @Override
  142. public void onSubscribe(@NonNull Disposable d) {
  143. addDisposable(d);
  144. showLoading.setValue(true);
  145. }
  146. @Override
  147. public void onSuccess(@NonNull Object o) {
  148. startGetCodeCountdown();
  149. showLoading.setValue(false);
  150. ToastUtil.show(R.string.login_verification_code_request_success_toast, ToastUtil.LENGTH_SHORT);
  151. }
  152. @Override
  153. public void onError(@NonNull Throwable e) {
  154. showLoading.setValue(false);
  155. if (e instanceof AccountRepository.RequestCodeTooOftenException) {
  156. EventHelper.report(EventId.hf1000305, Maps.asMap(EventId.EVENT_ID, "hf11022"));
  157. ToastUtil.show(R.string.login_request_code_frequently_toast, ToastUtil.LENGTH_SHORT);
  158. return;
  159. }
  160. e.printStackTrace();
  161. stopGetCodeCountdown();
  162. EventHelper.report(EventId.hf1000305, Maps.asMap(EventId.EVENT_ID, "hf11022"));
  163. ToastUtil.show(R.string.login_verification_code_request_failed_toast, ToastUtil.LENGTH_SHORT);
  164. }
  165. });
  166. }
  167. private void stopGetCodeCountdown() {
  168. if (getCodeCountdownDisposable != null && !getCodeCountdownDisposable.isDisposed()) {
  169. getCodeCountdownDisposable.dispose();
  170. }
  171. isRequestCodeCountdown.setValue(false);
  172. }
  173. private void startGetCodeCountdown() {
  174. if (getCodeCountdownDisposable != null && !getCodeCountdownDisposable.isDisposed()) {
  175. getCodeCountdownDisposable.dispose();
  176. }
  177. isRequestCodeCountdown.setValue(true);
  178. getCodeCountdownDisposable = RxJavaUtil.interval(0, 1, 60, TimeUnit.SECONDS,
  179. index -> requestCodeCountdown.setValue(String.valueOf(60 - index)), () -> isRequestCodeCountdown.setValue(false));
  180. addDisposable(getCodeCountdownDisposable);
  181. }
  182. public void onLoginClick() {
  183. EventHelper.report(EventId.hf1000302);
  184. String phoneNumText = phoneNum.getValue();
  185. if (!isPhone(phoneNumText)) {
  186. ToastUtil.show(R.string.login_phone_num_11, ToastUtil.LENGTH_SHORT);
  187. return;
  188. }
  189. String code = verificationCode.getValue();
  190. if (TextUtils.isEmpty(code)) {
  191. ToastUtil.show(R.string.login_code_hint, ToastUtil.LENGTH_SHORT);
  192. return;
  193. }
  194. if (!BoxingUtil.boxing(isCheckedAgreement.getValue())) {
  195. EventHelper.report(EventId.hf1000304, Maps.asMap(EventId.EVENT_ID, "hf11019"));
  196. ToastUtil.show(R.string.login_please_agree, ToastUtil.LENGTH_SHORT);
  197. return;
  198. }
  199. accountRepository.login(phoneNum.getValue(), verificationCode.getValue())
  200. .subscribe(new SingleObserver<Object>() {
  201. @Override
  202. public void onSubscribe(@NonNull Disposable d) {
  203. addDisposable(d);
  204. showLoading.setValue(true);
  205. }
  206. @Override
  207. public void onSuccess(@NonNull Object o) {
  208. showLoading.setValue(false);
  209. ToastUtil.show(R.string.login_success, ToastUtil.LENGTH_SHORT);
  210. finishEvent.call();
  211. EventHelper.report(EventId.hf1000303);
  212. }
  213. @Override
  214. public void onError(@NonNull Throwable e) {
  215. showLoading.setValue(false);
  216. if (e instanceof AccountRepository.LoginTooOftenException) {
  217. EventHelper.report(EventId.hf1000304, Maps.asMap(EventId.EVENT_ID, "hf11022"));
  218. ToastUtil.show(R.string.login_too_often_toast, ToastUtil.LENGTH_SHORT);
  219. return;
  220. }
  221. if (e instanceof NetworkErrorException) {
  222. EventHelper.report(EventId.hf1000304, Maps.asMap(EventId.EVENT_ID, "hf11019"));
  223. ToastUtil.show(R.string.net_error, ToastUtil.LENGTH_SHORT);
  224. return;
  225. }
  226. RxHttpHandler.ServerErrorException serverErrorException
  227. = e instanceof RxHttpHandler.ServerErrorException ? ((RxHttpHandler.ServerErrorException) e) : null;
  228. if (serverErrorException != null) {
  229. if (serverErrorException.getCode() == ErrorCode.ERROR_CODE_VERIFICATION_CODE_ERROR) {
  230. EventHelper.report(EventId.hf1000304, Maps.asMap(EventId.EVENT_ID, "hf11021"));
  231. ToastUtil.show(R.string.login_verification_code_error_toast, ToastUtil.LENGTH_SHORT);
  232. } else {
  233. EventHelper.report(EventId.hf1000304, Maps.asMap(EventId.EVENT_ID, "hf11022"));
  234. ToastUtil.show(serverErrorException.getMsg(), ToastUtil.LENGTH_SHORT);
  235. }
  236. } else {
  237. EventHelper.report(EventId.hf1000304, Maps.asMap(EventId.EVENT_ID, "hf11022"));
  238. ToastUtil.show(R.string.login_failed_toast, ToastUtil.LENGTH_SHORT);
  239. }
  240. }
  241. });
  242. }
  243. public boolean isCanLogin(String phoneNum, String verificationCode) {
  244. return isPhone(phoneNum) && verificationCode != null && verificationCode.length() > 0;
  245. }
  246. private boolean isPhone(String phoneNumText) {
  247. Pattern phonePattern = Pattern.compile("^1\\d{10}$");
  248. if (phoneNumText == null) {
  249. return false;
  250. }
  251. phoneNumText = phoneNumText.trim();
  252. if (TextUtils.isEmpty(phoneNumText)) {
  253. return false;
  254. }
  255. Matcher matcher = phonePattern.matcher(phoneNumText);
  256. return matcher.matches();
  257. }
  258. public void setReportId(String reportId) {
  259. if (!TextUtils.isEmpty(this.reportId)) {
  260. return;
  261. }
  262. this.reportId = reportId;
  263. EventHelper.report(EventId.hf1000301, Maps.asMap(EventId.EVENT_ID, reportId));
  264. }
  265. }