123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108 |
- package com.emato.ich.utils;
- import android.os.CountDownTimer;
- import android.widget.TextView;
- import androidx.fragment.app.Fragment;
- import androidx.navigation.fragment.NavHostFragment;
- import com.emato.ich.MainActivity;
- import com.emato.ich.R;
- import com.emato.ich.fragment.ChooseCabinetFragment;
- import com.emato.ich.fragment.ExceptionFragment;
- import com.emato.ich.fragment.InputInfoFragment;
- import com.emato.ich.fragment.InputInfoKeyBoardFragment;
- import com.emato.ich.fragment.SendFragment;
- import com.emato.ich.fragment.SendInfoConfirmFragment;
- import com.emato.ich.fragment.SendKeyBoardFragment;
- import com.emato.ich.fragment.SendMainFragment;
- import com.emato.ich.fragment.SendSuccessFragment;
- import com.emato.ich.fragment.TakeCodeFragment;
- import com.emato.ich.fragment.TakeFragment;
- import com.emato.ich.fragment.TakeSuccessFragment;
- import com.emato.ich.local.LocalStorage;
- /**
- * 页面倒计时工具类
- */
- public class TimeOutUtils {
- /**
- * 倒计时
- * @param mainActivity activity
- * @param fragment fragment
- * @param textView 倒计时显示的控件
- * @param time 时间
- * @return 计时器
- */
- public static CountDownTimer timeout(MainActivity mainActivity, Fragment fragment, TextView textView, int time){
- // TODO 需要action 每一个Fragment都需要一个跳转Main的action 需要判断是哪个Fragment
- return new CountDownTimer(time * 1000, 1000) {
- @Override
- public void onTick(long millisUntilFinished) {
- textView.setText(String.valueOf((millisUntilFinished / 1000)));
- if (millisUntilFinished <= 1999) {
- int fragmentAction = getFragmentAction(fragment);
- if (fragmentAction != 0) {
- NavUtils.navigate(fragment, fragmentAction);
- fragment.onDestroy();
- }
- }
- }
- @Override
- public void onFinish() {
- int fragmentAction = getFragmentAction(fragment);
- if (fragmentAction != 0) {
- // 清空session和页面传值, 异常处理有不同处理方式
- boolean isException = false;
- if ( LocalStorage.getInstance().getSession().getException() &&
- (fragment instanceof SendInfoConfirmFragment || fragment instanceof SendSuccessFragment)) {
- isException = true;
- }
- LocalStorage.getInstance().cleanSession(mainActivity, isException);
- fragment.onDestroy();
- }
- }
- };
- }
- /**
- * 根据Fragment来跳转
- * @param fragment fragment
- * @return 跳转所对应的action id
- */
- private static int getFragmentAction(Fragment fragment) {
- if (fragment instanceof ChooseCabinetFragment) {
- return R.id.action_chooseCabinetFragment_to_mainFragment;
- } else if (fragment instanceof ExceptionFragment) {
- return R.id.actionExceptionFragment_to_mainFragment;
- } else if (fragment instanceof InputInfoFragment) {
- return R.id.action_InputInfoKeyBoardFragment_to_mainFragment;
- } else if (fragment instanceof InputInfoKeyBoardFragment) {
- return R.id.action_InputInfoKeyBoardFragment_to_mainFragment;
- }else if (fragment instanceof SendFragment) {
- return R.id.action_sendFragment_to_mainFragment;
- } else if (fragment instanceof SendKeyBoardFragment) {
- return R.id.action_sendFragment_to_mainFragment;
- }else if (fragment instanceof SendInfoConfirmFragment) {
- return R.id.action_sendInfoConfirmFragment_to_mainFragment;
- } else if (fragment instanceof SendMainFragment) {
- return R.id.action_sendMainFragment_to_mainFragment;
- } else if (fragment instanceof SendSuccessFragment) {
- return R.id.action_sendSuccessFragment_to_mainFragment;
- } else if (fragment instanceof TakeCodeFragment) {
- return R.id.action_takeCodeFragment_to_mainFragment;
- } else if (fragment instanceof TakeFragment) {
- return R.id.action_takeFragment_to_mainFragment;
- } else if (fragment instanceof TakeSuccessFragment) {
- return R.id.action_takeSuccessFragment_to_mainFragment;
- }
- return 0;
- }
- }
|