Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package ru.aeroflot.fragments;
- import android.os.Bundle;
- import android.support.annotation.NonNull;
- import android.support.annotation.Nullable;
- import android.support.v4.app.Fragment;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- import io.reactivex.disposables.CompositeDisposable;
- import io.reactivex.disposables.Disposable;
- import ru.aeroflot.AFLApplication;
- import ru.aeroflot.R;
- import ru.aeroflot.dialogs.AFLProgressDialog;
- import rx.Subscription;
- import rx.subscriptions.CompositeSubscription;
- public class BaseRxFragment extends Fragment {
- /** Список подписчиков типа Disposable */
- protected CompositeDisposable disposables;
- /** Список подписчиков типа Subscription */
- protected CompositeSubscription subscriptions;
- /** Диалог прогресса фоновой логики */
- protected AFLProgressDialog progressDialog;
- /**
- * Добавляем подписчика в отслеживаемый список
- * @param disposable подписчик
- */
- protected void addDisposable( Disposable disposable ) {
- if ( disposables == null ) {
- disposables = new CompositeDisposable();
- }
- disposables.add( disposable );
- }
- /**
- * Добавляем подписчика в отслеживаемый список
- * @param subscription подписчик
- */
- protected void addSubscription( Subscription subscription ) {
- if ( this.subscriptions == null || this.subscriptions.isUnsubscribed() ) {
- this.subscriptions = new CompositeSubscription();
- }
- this.subscriptions.add( subscription );
- }
- /**
- * Отписка подписчиков типа Subscriptions
- */
- protected void unsubscribeSubscriptions() {
- if ( subscriptions != null ) {
- subscriptions.unsubscribe();
- }
- }
- /**
- * Отписка подписчиков типа Disposables
- */
- protected void unsubscribeDisposables() {
- if ( disposables != null ) {
- disposables.clear();
- }
- }
- @Override
- public void onCreate( @Nullable Bundle savedInstanceState ) {
- super.onCreate( savedInstanceState );
- progressDialog = new AFLProgressDialog( getActivity() );
- progressDialog.setMessage( getString( R.string.auth_progress_please_wait ) );
- progressDialog.setCancelable( true );
- progressDialog.setCanceledOnTouchOutside( false );
- progressDialog.setOnCancelListener( dialogInterface -> {
- unsubscribeDisposables();
- unsubscribeSubscriptions();
- } ); //TODO возможно следует использовать только по необходимости в наследниках
- }
- @Override
- public void onStart() {
- super.onStart();
- subscribeOnEvents();
- }
- /**
- * Перехват событий
- */
- protected void subscribeOnEvents() {
- addSubscription( AFLApplication
- .getInstance()
- .getRxBus()
- .getEvents()
- .subscribe( this::handleRxEventLogic, this::handleRxThrowableLogic ) );
- }
- protected void handleRxEventLogic( Object o ) {
- //Override this in child class
- hideProgress();
- }
- protected void handleRxThrowableLogic( Throwable throwable ) {
- //Override this in child class
- hideProgress();
- throwable.printStackTrace();
- }
- @Override
- public void onStop() {
- super.onStop();
- unsubscribeDisposables();
- unsubscribeSubscriptions();
- }
- protected void showProgress() {
- progressDialog.show();
- }
- protected void hideProgress() {
- progressDialog.hide();
- }
- protected boolean isInProgress() {
- return progressDialog != null && progressDialog.isShowing();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement