Advertisement
Guest User

Untitled

a guest
Oct 15th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.59 KB | None | 0 0
  1. package ru.aeroflot.fragments;
  2.  
  3. import android.os.Bundle;
  4. import android.support.annotation.NonNull;
  5. import android.support.annotation.Nullable;
  6. import android.support.v4.app.Fragment;
  7. import android.view.LayoutInflater;
  8. import android.view.View;
  9. import android.view.ViewGroup;
  10.  
  11. import io.reactivex.disposables.CompositeDisposable;
  12. import io.reactivex.disposables.Disposable;
  13. import ru.aeroflot.AFLApplication;
  14. import ru.aeroflot.R;
  15. import ru.aeroflot.dialogs.AFLProgressDialog;
  16. import rx.Subscription;
  17. import rx.subscriptions.CompositeSubscription;
  18.  
  19. public class BaseRxFragment extends Fragment {
  20.  
  21. /** Список подписчиков типа Disposable */
  22. protected CompositeDisposable disposables;
  23. /** Список подписчиков типа Subscription */
  24. protected CompositeSubscription subscriptions;
  25. /** Диалог прогресса фоновой логики */
  26. protected AFLProgressDialog progressDialog;
  27.  
  28. /**
  29. * Добавляем подписчика в отслеживаемый список
  30. * @param disposable подписчик
  31. */
  32. protected void addDisposable( Disposable disposable ) {
  33. if ( disposables == null ) {
  34. disposables = new CompositeDisposable();
  35. }
  36. disposables.add( disposable );
  37. }
  38.  
  39. /**
  40. * Добавляем подписчика в отслеживаемый список
  41. * @param subscription подписчик
  42. */
  43. protected void addSubscription( Subscription subscription ) {
  44. if ( this.subscriptions == null || this.subscriptions.isUnsubscribed() ) {
  45. this.subscriptions = new CompositeSubscription();
  46. }
  47. this.subscriptions.add( subscription );
  48. }
  49.  
  50. /**
  51. * Отписка подписчиков типа Subscriptions
  52. */
  53. protected void unsubscribeSubscriptions() {
  54. if ( subscriptions != null ) {
  55. subscriptions.unsubscribe();
  56. }
  57. }
  58.  
  59. /**
  60. * Отписка подписчиков типа Disposables
  61. */
  62. protected void unsubscribeDisposables() {
  63. if ( disposables != null ) {
  64. disposables.clear();
  65. }
  66. }
  67.  
  68. @Override
  69. public void onCreate( @Nullable Bundle savedInstanceState ) {
  70. super.onCreate( savedInstanceState );
  71. progressDialog = new AFLProgressDialog( getActivity() );
  72. progressDialog.setMessage( getString( R.string.auth_progress_please_wait ) );
  73. progressDialog.setCancelable( true );
  74. progressDialog.setCanceledOnTouchOutside( false );
  75. progressDialog.setOnCancelListener( dialogInterface -> {
  76. unsubscribeDisposables();
  77. unsubscribeSubscriptions();
  78. } ); //TODO возможно следует использовать только по необходимости в наследниках
  79. }
  80.  
  81. @Override
  82. public void onStart() {
  83. super.onStart();
  84.  
  85. subscribeOnEvents();
  86. }
  87.  
  88. /**
  89. * Перехват событий
  90. */
  91. protected void subscribeOnEvents() {
  92. addSubscription( AFLApplication
  93. .getInstance()
  94. .getRxBus()
  95. .getEvents()
  96. .subscribe( this::handleRxEventLogic, this::handleRxThrowableLogic ) );
  97. }
  98.  
  99. protected void handleRxEventLogic( Object o ) {
  100. //Override this in child class
  101. hideProgress();
  102. }
  103.  
  104. protected void handleRxThrowableLogic( Throwable throwable ) {
  105. //Override this in child class
  106. hideProgress();
  107. throwable.printStackTrace();
  108. }
  109.  
  110. @Override
  111. public void onStop() {
  112. super.onStop();
  113.  
  114. unsubscribeDisposables();
  115. unsubscribeSubscriptions();
  116. }
  117.  
  118. protected void showProgress() {
  119. progressDialog.show();
  120. }
  121.  
  122. protected void hideProgress() {
  123. progressDialog.hide();
  124. }
  125.  
  126. protected boolean isInProgress() {
  127. return progressDialog != null && progressDialog.isShowing();
  128. }
  129. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement