|
| 1 | +package com.morihacky.android.rxjava.rxbus; |
| 2 | + |
| 3 | +import android.os.Bundle; |
| 4 | +import android.support.annotation.Nullable; |
| 5 | +import android.support.v4.app.Fragment; |
| 6 | +import android.support.v4.view.ViewCompat; |
| 7 | +import android.view.LayoutInflater; |
| 8 | +import android.view.View; |
| 9 | +import android.view.ViewGroup; |
| 10 | +import android.widget.TextView; |
| 11 | +import butterknife.ButterKnife; |
| 12 | +import butterknife.InjectView; |
| 13 | +import com.morihacky.android.rxjava.MainActivity; |
| 14 | +import com.morihacky.android.rxjava.app.R; |
| 15 | +import java.util.List; |
| 16 | +import java.util.concurrent.TimeUnit; |
| 17 | +import rx.Observable; |
| 18 | +import rx.android.schedulers.AndroidSchedulers; |
| 19 | +import rx.functions.Action1; |
| 20 | +import rx.functions.Func1; |
| 21 | +import rx.observables.ConnectableObservable; |
| 22 | +import rx.subscriptions.CompositeSubscription; |
| 23 | + |
| 24 | +import static rx.android.observables.AndroidObservable.bindFragment; |
| 25 | + |
| 26 | +public class RxBusDemo_Bottom3Fragment |
| 27 | + extends Fragment { |
| 28 | + |
| 29 | + private RxBus _rxBus; |
| 30 | + private CompositeSubscription _subscriptions; |
| 31 | + |
| 32 | + @InjectView(R.id.demo_rxbus_tap_txt) TextView _tapEventTxtShow; |
| 33 | + @InjectView(R.id.demo_rxbus_tap_count) TextView _tapEventCountShow; |
| 34 | + |
| 35 | + @Override |
| 36 | + public View onCreateView(LayoutInflater inflater, |
| 37 | + @Nullable ViewGroup container, |
| 38 | + @Nullable Bundle savedInstanceState) { |
| 39 | + View layout = inflater.inflate(R.layout.fragment_rxbus_bottom, container, false); |
| 40 | + ButterKnife.inject(this, layout); |
| 41 | + return layout; |
| 42 | + } |
| 43 | + |
| 44 | + @Override |
| 45 | + public void onActivityCreated(@Nullable Bundle savedInstanceState) { |
| 46 | + super.onActivityCreated(savedInstanceState); |
| 47 | + _rxBus = ((MainActivity) getActivity()).getRxBusSingleton(); |
| 48 | + } |
| 49 | + |
| 50 | + @Override |
| 51 | + public void onStart() { |
| 52 | + super.onStart(); |
| 53 | + _subscriptions = new CompositeSubscription(); |
| 54 | + |
| 55 | + ConnectableObservable<Object> tapEventEmitter = _rxBus.toObserverable().publish(); |
| 56 | + |
| 57 | + _subscriptions// |
| 58 | + .add(bindFragment(this, tapEventEmitter)// |
| 59 | + .subscribe(new Action1<Object>() { |
| 60 | + @Override |
| 61 | + public void call(Object event) { |
| 62 | + if (event instanceof RxBusDemoFragment.TapEvent) { |
| 63 | + _showTapText(); |
| 64 | + } |
| 65 | + } |
| 66 | + })); |
| 67 | + |
| 68 | + _subscriptions// |
| 69 | + .add(tapEventEmitter.publish(new Func1<Observable<Object>, Observable<List<Object>>>() { |
| 70 | + @Override |
| 71 | + public Observable<List<Object>> call(Observable<Object> stream) { |
| 72 | + return stream.buffer(stream.debounce(1, TimeUnit.SECONDS)); |
| 73 | + } |
| 74 | + }).observeOn(AndroidSchedulers.mainThread()).subscribe(new Action1<List<Object>>() { |
| 75 | + @Override |
| 76 | + public void call(List<Object> taps) { |
| 77 | + _showTapCount(taps.size()); |
| 78 | + } |
| 79 | + })); |
| 80 | + |
| 81 | + _subscriptions.add(tapEventEmitter.connect()); |
| 82 | + } |
| 83 | + |
| 84 | + @Override |
| 85 | + public void onStop() { |
| 86 | + super.onStop(); |
| 87 | + _subscriptions.clear(); |
| 88 | + } |
| 89 | + |
| 90 | + // ----------------------------------------------------------------------------------- |
| 91 | + // Helper to show the text via an animation |
| 92 | + |
| 93 | + private void _showTapText() { |
| 94 | + _tapEventTxtShow.setVisibility(View.VISIBLE); |
| 95 | + _tapEventTxtShow.setAlpha(1f); |
| 96 | + ViewCompat.animate(_tapEventTxtShow).alphaBy(-1f).setDuration(400); |
| 97 | + } |
| 98 | + |
| 99 | + private void _showTapCount(int size) { |
| 100 | + _tapEventCountShow.setText(String.valueOf(size)); |
| 101 | + _tapEventCountShow.setVisibility(View.VISIBLE); |
| 102 | + _tapEventCountShow.setScaleX(1f); |
| 103 | + _tapEventCountShow.setScaleY(1f); |
| 104 | + ViewCompat.animate(_tapEventCountShow) |
| 105 | + .scaleXBy(-1f) |
| 106 | + .scaleYBy(-1f) |
| 107 | + .setDuration(800) |
| 108 | + .setStartDelay(100); |
| 109 | + } |
| 110 | +} |
0 commit comments