BaseCountDownActivity.java 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. package com.example.chemical.ui.common;
  2. import android.os.Bundle;
  3. import android.os.CountDownTimer;
  4. import androidx.annotation.Nullable;
  5. import androidx.viewbinding.ViewBinding;
  6. import com.blankj.utilcode.util.LogUtils;
  7. import com.example.chemical.ChemicalApp;
  8. import com.example.chemical.comm.Constants;
  9. import com.rc.core.ui.activity.RcBaseActivity;
  10. import com.rc.httpcore.bean.ConfigBean;
  11. import com.rc.httpcore.client.ApiRepository;
  12. import io.reactivex.functions.Consumer;
  13. public abstract class BaseCountDownActivity<VB extends ViewBinding> extends RcBaseActivity<VB> {
  14. // 公共配置倒计时长
  15. private int backTime = 60;
  16. // 页面自定义倒计时长
  17. private int cTime = 0;
  18. private CountDownTimer countDownTimer = new CountDownTimer(1000, 1000) {
  19. @Override
  20. public void onTick(long millisUntilFinished) {
  21. }
  22. @Override
  23. public void onFinish() {
  24. if (!isFinishing() && !isDestroyed()) {
  25. if (cTime > 0) {
  26. cTime--;
  27. if (cTime <= 0) {
  28. cdEnd();
  29. } else {
  30. invokeTime(cTime);
  31. }
  32. } else {
  33. backTime--;
  34. if (backTime <= 0) {
  35. cdEnd();
  36. } else {
  37. invokeTime(backTime);
  38. }
  39. }
  40. countDownTimer.start();
  41. }
  42. }
  43. };
  44. private void cdEnd() {
  45. // 下面逻辑是之前开发者的 很难理解为什么写
  46. if (BaseCountDownActivity.class instanceof UseActivity.class) {
  47. cdFinish();
  48. } else {
  49. UiManager.INSTANCE.switcher(BaseCountDownActivity.this, MainActivity.class);
  50. }
  51. }
  52. private void invokeTime(int cd) {
  53. if (!isDestroyed()) {
  54. cdTime(backTime);
  55. }
  56. }
  57. /**
  58. * 当前页最大倒计时
  59. */
  60. protected void setCTime(int cd) {
  61. cTime = cd;
  62. }
  63. protected abstract void cdTime(int cd);
  64. protected void cdFinish() {
  65. }
  66. /**
  67. * 重置时间
  68. */
  69. private void reSetCdTime() {
  70. ConfigBean configBean = ChemicalApp.confs;
  71. if (null != configBean) {
  72. backTime = configBean.getBackTime();
  73. Constants.INSTANCE.setOFFTIME_TIME(configBean.getOffTime());
  74. }
  75. }
  76. @Override
  77. protected void onCreate(@Nullable Bundle savedInstanceState) {
  78. super.onCreate(savedInstanceState);
  79. // 重置时间
  80. reSetCdTime();
  81. }
  82. @Override
  83. protected void onResume() {
  84. super.onResume();
  85. if (null != countDownTimer) {
  86. countDownTimer.start();
  87. }
  88. }
  89. @Override
  90. protected void onPause() {
  91. super.onPause();
  92. if (null != countDownTimer) {
  93. countDownTimer.cancel();
  94. }
  95. }
  96. @Override
  97. protected void onDestroy() {
  98. super.onDestroy();
  99. stopCountDown();
  100. }
  101. @Override
  102. public void onUserInteraction() {
  103. super.onUserInteraction();
  104. reSetCdTime();
  105. }
  106. protected void stopCountDown() {
  107. if (null != countDownTimer) {
  108. countDownTimer.cancel();
  109. countDownTimer = null;
  110. }
  111. }
  112. protected void callLogoutApi(Consumer<Boolean> unit) {
  113. showLoading("退出中...", false);
  114. addDisposable(ApiRepository.INSTANCE.loginOut().subscribe(new Consumer<Boolean>() {
  115. @Override
  116. public void accept(Boolean aBoolean) throws Exception {
  117. dismissLoading();
  118. unit.accept(true);
  119. }
  120. }, new Consumer<Throwable>() {
  121. @Override
  122. public void accept(Throwable throwable) throws Exception {
  123. dismissLoading();
  124. unit.accept(false);
  125. }
  126. }));
  127. }
  128. }