TimeOutUtils.java 3.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. package com.emato.ich.utils;
  2. import android.os.CountDownTimer;
  3. import android.widget.TextView;
  4. import androidx.fragment.app.Fragment;
  5. import androidx.navigation.fragment.NavHostFragment;
  6. import com.emato.ich.MainActivity;
  7. import com.emato.ich.R;
  8. import com.emato.ich.fragment.ChooseCabinetFragment;
  9. import com.emato.ich.fragment.ExceptionFragment;
  10. import com.emato.ich.fragment.InputInfoFragment;
  11. import com.emato.ich.fragment.SendFragment;
  12. import com.emato.ich.fragment.SendInfoConfirmFragment;
  13. import com.emato.ich.fragment.SendMainFragment;
  14. import com.emato.ich.fragment.SendSuccessFragment;
  15. import com.emato.ich.fragment.TakeCodeFragment;
  16. import com.emato.ich.fragment.TakeFragment;
  17. import com.emato.ich.fragment.TakeSuccessFragment;
  18. import com.emato.ich.local.LocalStorage;
  19. /**
  20. * 页面倒计时工具类
  21. */
  22. public class TimeOutUtils {
  23. /**
  24. * 倒计时
  25. * @param mainActivity activity
  26. * @param fragment fragment
  27. * @param textView 倒计时显示的控件
  28. * @param time 时间
  29. * @return 计时器
  30. */
  31. public static CountDownTimer timeout(MainActivity mainActivity, Fragment fragment, TextView textView, int time){
  32. // TODO 需要action 每一个Fragment都需要一个跳转Main的action 需要判断是哪个Fragment
  33. return new CountDownTimer(time * 1000, 1000) {
  34. @Override
  35. public void onTick(long millisUntilFinished) {
  36. textView.setText(String.valueOf((millisUntilFinished / 1000)));
  37. }
  38. @Override
  39. public void onFinish() {
  40. int fragmentAction = getFragmentAction(fragment);
  41. if (fragmentAction != 0) {
  42. NavHostFragment.findNavController(fragment).navigate(fragmentAction);
  43. // 清空session和页面传值, 异常处理有不同处理方式
  44. boolean isException = false;
  45. if ( LocalStorage.getInstance().getSession().getException() &&
  46. (fragment instanceof SendInfoConfirmFragment || fragment instanceof SendSuccessFragment)) {
  47. isException = true;
  48. }
  49. LocalStorage.getInstance().cleanSession(mainActivity, isException);
  50. fragment.onDestroy();
  51. }
  52. }
  53. };
  54. }
  55. /**
  56. * 根据Fragment来跳转
  57. * @param fragment fragment
  58. * @return 跳转所对应的action id
  59. */
  60. private static int getFragmentAction(Fragment fragment) {
  61. if (fragment instanceof ChooseCabinetFragment) {
  62. return R.id.action_chooseCabinetFragment_to_mainFragment;
  63. } else if (fragment instanceof ExceptionFragment) {
  64. return R.id.actionExceptionFragment_to_mainFragment;
  65. } else if (fragment instanceof InputInfoFragment) {
  66. return R.id.action_inputInfoFragment_to_mainFragment;
  67. } else if (fragment instanceof SendFragment) {
  68. return R.id.action_sendFragment_to_mainFragment;
  69. } else if (fragment instanceof SendInfoConfirmFragment) {
  70. return R.id.action_sendInfoConfirmFragment_to_mainFragment;
  71. } else if (fragment instanceof SendMainFragment) {
  72. return R.id.action_sendMainFragment_to_mainFragment;
  73. } else if (fragment instanceof SendSuccessFragment) {
  74. return R.id.action_sendSuccessFragment_to_mainFragment;
  75. } else if (fragment instanceof TakeCodeFragment) {
  76. return R.id.action_takeCodeFragment_to_mainFragment;
  77. } else if (fragment instanceof TakeFragment) {
  78. return R.id.action_takeFragment_to_mainFragment;
  79. } else if (fragment instanceof TakeSuccessFragment) {
  80. return R.id.action_takeSuccessFragment_to_mainFragment;
  81. }
  82. return 0;
  83. }
  84. }