package com.morihacky.android.rxjava.rxbus;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewCompat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import butterknife.ButterKnife;
import butterknife.InjectView;
import com.morihacky.android.rxjava.MainActivity;
import com.morihacky.android.rxjava.app.R;
import java.util.List;
import java.util.concurrent.TimeUnit;
import rx.Subscription;
import rx.android.observables.AndroidObservable;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.functions.Func1;

import static com.morihacky.android.rxjava.rxbus.RxBusFrag1.TapEvent;

public class RxBusFrag2
    extends Fragment {

  private RxBus _rxBus;
  private Subscription _subscription1_tapListen;
  private Subscription _subscription2_tapCollector;

  @InjectView(R.id.demo_rxbus_tap_txt) TextView _tapEventTxtShow;
  @InjectView(R.id.demo_rxbus_tap_count) TextView _tapEventCountShow;

  @Override
  public View onCreateView(LayoutInflater inflater,
                           @Nullable ViewGroup container,
                           @Nullable Bundle savedInstanceState) {
    View layout = inflater.inflate(R.layout.fragment_rxbus_frag2, container, false);
    ButterKnife.inject(this, layout);
    return layout;
  }

  @Override
  public void onActivityCreated(@Nullable Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    _rxBus = ((MainActivity) getActivity()).getRxBusSingleton();
  }

  @Override
  public void onStart() {
    super.onStart();

    _subscription1_tapListen = AndroidObservable.bindFragment(this, _rxBus.toObserverable())
        .subscribe(new Action1<Object>() {
          @Override
          public void call(Object event) {
            if (event instanceof TapEvent) {
              _showTapText();
            }
          }
        });

    _rxBus.toObserverable().buffer(1, TimeUnit.SECONDS).filter(new Func1<List<Object>, Boolean>() {
      @Override
      public Boolean call(List<Object> objects) {
        return objects != null && objects.size() > 0;
      }
    }).observeOn(AndroidSchedulers.mainThread()).subscribe(new Action1<List<Object>>() {
      @Override
      public void call(List<Object> taps) {
        _showTapCount(taps.size());
      }
    });
  }

  private void _showTapText() {
    _tapEventTxtShow.setVisibility(View.VISIBLE);
    _tapEventTxtShow.setAlpha(1f);
    ViewCompat.animate(_tapEventTxtShow).alphaBy(-1f).setDuration(400);
  }

  private void _showTapCount(int size) {
    _tapEventCountShow.setText(String.valueOf(size));
    _tapEventCountShow.setVisibility(View.VISIBLE);
    _tapEventCountShow.setScaleX(1f);
    _tapEventCountShow.setScaleY(1f);
    ViewCompat.animate(_tapEventCountShow)
        .scaleXBy(-1f)
        .scaleYBy(-1f)
        .setDuration(400)
        .setStartDelay(100);
  }

  @Override
  public void onStop() {
    super.onStop();
    _subscription1_tapListen.unsubscribe();
  }
}

@@ -533,16 +533,38 @@
 meUnit;%0A
+import rx.Observable;%0A
 import r
@@ -708,35 +708,8 @@
 on1;
-%0Aimport rx.functions.Func1;
 %0A%0Aim
@@ -1681,24 +1681,134 @@
 onStart();%0A%0A
+    // .share = publish + refcount%0A    Observable%3CObject%3E tapEventEmitter = _rxBus.toObserverable().share();%0A%0A
     _subscri
@@ -1867,41 +1867,36 @@
 is, 
-_rxBus.toObserverable())%0A
+tapEventEmitter)%0A   
         
 .sub
@@ -1891,16 +1891,18 @@
         
+  
 .subscri
@@ -1924,24 +1924,29 @@
 Object%3E() %7B%0A
+     
           @O
@@ -1959,24 +1959,29 @@
 e%0A          
+     
 public void 
@@ -2013,16 +2013,21 @@
         
+  
+   
 if (even
@@ -2057,32 +2057,37 @@
 %7B%0A              
+     
 _showTapText();%0A
@@ -2090,32 +2090,37 @@
 ();%0A            
+     
 %7D%0A          %7D%0A  
@@ -2107,34 +2107,44 @@
     %7D%0A          
+ 
-%7D%0A
+    %7D%0A     
         %7D);%0A%0A   
@@ -2148,238 +2148,156 @@
     
-_rxBus.toObserverable().buffer(1, TimeUnit.SECONDS).filter(new Func1%3CList%3CObject%3E, Boolean%3E() %7B%0A      @Override%0A      public Boolean call(List%3CObject%3E objects) %7B%0A        return objects != null && objects.size() %3E 0;%0A      %7D%0A    %7D)
+Observable%3CObject%3E debouncedEventEmitter = tapEventEmitter.debounce(1, TimeUnit.SECONDS);%0A    tapEventEmitter.buffer(debouncedEventEmitter)%0A        
 .obs
@@ -2334,16 +2334,25 @@
 hread())
+%0A        
 .subscri
@@ -2386,24 +2386,28 @@
 %3E() %7B%0A      
+    
 @Override%0A  
@@ -2406,24 +2406,28 @@
 rride%0A      
+  
+  
 public void 
@@ -2452,32 +2452,36 @@
 taps) %7B%0A        
+    
 _showTapCount(ta
@@ -2494,26 +2494,34 @@
 e());%0A      
+ 
-%7D%0A
+   %7D%0A    
     %7D);%0A  %7D%0A

use a debounced Buffer