| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154 |
- package com.example.chemical.ui.common;
- import android.os.Bundle;
- import android.os.CountDownTimer;
- import androidx.annotation.Nullable;
- import androidx.viewbinding.ViewBinding;
- import com.blankj.utilcode.util.LogUtils;
- import com.example.chemical.ChemicalApp;
- import com.example.chemical.comm.Constants;
- import com.rc.core.ui.activity.RcBaseActivity;
- import com.rc.httpcore.bean.ConfigBean;
- import com.rc.httpcore.client.ApiRepository;
- import io.reactivex.functions.Consumer;
- public abstract class BaseCountDownActivity<VB extends ViewBinding> extends RcBaseActivity<VB> {
- // 公共配置倒计时长
- private int backTime = 60;
- // 页面自定义倒计时长
- private int cTime = 0;
- private CountDownTimer countDownTimer = new CountDownTimer(1000, 1000) {
- @Override
- public void onTick(long millisUntilFinished) {
- }
- @Override
- public void onFinish() {
- if (!isFinishing() && !isDestroyed()) {
- if (cTime > 0) {
- cTime--;
- if (cTime <= 0) {
- cdEnd();
- } else {
- invokeTime(cTime);
- }
- } else {
- backTime--;
- if (backTime <= 0) {
- cdEnd();
- } else {
- invokeTime(backTime);
- }
- }
- countDownTimer.start();
- }
- }
- };
- private void cdEnd() {
- // 下面逻辑是之前开发者的 很难理解为什么写
- if (BaseCountDownActivity.class instanceof UseActivity.class) {
- cdFinish();
- } else {
- UiManager.INSTANCE.switcher(BaseCountDownActivity.this, MainActivity.class);
- }
- }
- private void invokeTime(int cd) {
- if (!isDestroyed()) {
- cdTime(backTime);
- }
- }
- /**
- * 当前页最大倒计时
- */
- protected void setCTime(int cd) {
- cTime = cd;
- }
- protected abstract void cdTime(int cd);
- protected void cdFinish() {
- }
- /**
- * 重置时间
- */
- private void reSetCdTime() {
- ConfigBean configBean = ChemicalApp.confs;
- if (null != configBean) {
- backTime = configBean.getBackTime();
- Constants.INSTANCE.setOFFTIME_TIME(configBean.getOffTime());
- }
- }
- @Override
- protected void onCreate(@Nullable Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- // 重置时间
- reSetCdTime();
- }
- @Override
- protected void onResume() {
- super.onResume();
- if (null != countDownTimer) {
- countDownTimer.start();
- }
- }
- @Override
- protected void onPause() {
- super.onPause();
- if (null != countDownTimer) {
- countDownTimer.cancel();
- }
- }
- @Override
- protected void onDestroy() {
- super.onDestroy();
- stopCountDown();
- }
- @Override
- public void onUserInteraction() {
- super.onUserInteraction();
- reSetCdTime();
- }
- protected void stopCountDown() {
- if (null != countDownTimer) {
- countDownTimer.cancel();
- countDownTimer = null;
- }
- }
- protected void callLogoutApi(Consumer<Boolean> unit) {
- showLoading("退出中...", false);
- addDisposable(ApiRepository.INSTANCE.loginOut().subscribe(new Consumer<Boolean>() {
- @Override
- public void accept(Boolean aBoolean) throws Exception {
- dismissLoading();
- unit.accept(true);
- }
- }, new Consumer<Throwable>() {
- @Override
- public void accept(Throwable throwable) throws Exception {
- dismissLoading();
- unit.accept(false);
- }
- }));
- }
- }
|