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 extends RcBaseActivity { // 公共配置倒计时长 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 unit) { showLoading("退出中...", false); addDisposable(ApiRepository.INSTANCE.loginOut().subscribe(new Consumer() { @Override public void accept(Boolean aBoolean) throws Exception { dismissLoading(); unit.accept(true); } }, new Consumer() { @Override public void accept(Throwable throwable) throws Exception { dismissLoading(); unit.accept(false); } })); } }