TimeOutUtils.java 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. // 清空session和页面传值
  43. LocalStorage.getInstance().cleanSession(mainActivity);
  44. NavHostFragment.findNavController(fragment).navigate(fragmentAction);
  45. }
  46. }
  47. };
  48. }
  49. /**
  50. * 根据Fragment来跳转
  51. * @param fragment fragment
  52. * @return 跳转所对应的action id
  53. */
  54. private static int getFragmentAction(Fragment fragment) {
  55. if (fragment instanceof ChooseCabinetFragment) {
  56. return R.id.action_chooseCabinetFragment_to_mainFragment;
  57. } else if (fragment instanceof ExceptionFragment) {
  58. return R.id.actionExceptionFragment_to_mainFragment;
  59. } else if (fragment instanceof InputInfoFragment) {
  60. return R.id.action_inputInfoFragment_to_mainFragment;
  61. } else if (fragment instanceof SendFragment) {
  62. return R.id.action_sendFragment_to_mainFragment;
  63. } else if (fragment instanceof SendInfoConfirmFragment) {
  64. return R.id.action_sendInfoConfirmFragment_to_mainFragment;
  65. } else if (fragment instanceof SendMainFragment) {
  66. return R.id.action_sendMainFragment_to_mainFragment;
  67. } else if (fragment instanceof SendSuccessFragment) {
  68. return R.id.action_sendSuccessFragment_to_mainFragment;
  69. } else if (fragment instanceof TakeCodeFragment) {
  70. return R.id.action_takeCodeFragment_to_mainFragment;
  71. } else if (fragment instanceof TakeFragment) {
  72. return R.id.action_takeFragment_to_mainFragment;
  73. } else if (fragment instanceof TakeSuccessFragment) {
  74. return R.id.action_takeSuccessFragment_to_mainFragment;
  75. }
  76. return 0;
  77. }
  78. }