|
| 1 | +package com.morihacky.android.rxjava; |
| 2 | + |
| 3 | +import android.content.Context; |
| 4 | +import android.os.Bundle; |
| 5 | +import android.os.Handler; |
| 6 | +import android.os.Looper; |
| 7 | +import android.support.annotation.Nullable; |
| 8 | +import android.support.v4.app.Fragment; |
| 9 | +import android.view.LayoutInflater; |
| 10 | +import android.view.View; |
| 11 | +import android.view.ViewGroup; |
| 12 | +import android.widget.ArrayAdapter; |
| 13 | +import android.widget.ListView; |
| 14 | + |
| 15 | +import com.morihacky.android.rxjava.app.R; |
| 16 | + |
| 17 | +import java.util.ArrayList; |
| 18 | +import java.util.List; |
| 19 | + |
| 20 | +import butterknife.ButterKnife; |
| 21 | +import butterknife.InjectView; |
| 22 | +import butterknife.OnTextChanged; |
| 23 | +import rx.Observable; |
| 24 | +import rx.Observer; |
| 25 | +import rx.Subscriber; |
| 26 | +import rx.Subscription; |
| 27 | +import rx.android.observables.AndroidObservable; |
| 28 | +import rx.android.schedulers.AndroidSchedulers; |
| 29 | +import rx.schedulers.Schedulers; |
| 30 | +import rx.subjects.BehaviorSubject; |
| 31 | +import timber.log.Timber; |
| 32 | + |
| 33 | + |
| 34 | +/** |
| 35 | + * The reason we use a Subject for tracking the search query is because it emits observables. |
| 36 | + * Because a Subject subscribes to an Observable, it will trigger that Observable to begin emitting items |
| 37 | + * (if that Observable is "cold" — that is, if it waits for a subscription before it begins to emit items). |
| 38 | + * This can have the effect of making the resulting Subject a "hot" Observable variant of the original "cold" Observable. |
| 39 | + * |
| 40 | + * This allows us to create the subject and subscription one time onActivity creation. Subsequently |
| 41 | + * we send in Observables to the Subject's subscriber onTextChanged |
| 42 | + * |
| 43 | + * Otherwise we would have to create the subscription onTextChanged every single time, which is |
| 44 | + * wasteful (not really since we anyway unsubscribe at the end). |
| 45 | + * less-elegant (as a concept for sure, but not for simplicity vs. the 3 step basic subscription creation) |
| 46 | + * doesn't allow us to debounce/throttle/sample ? |
| 47 | + * |
| 48 | + */ |
| 49 | +public class BehaviorSubjectSearchEmitterFragment |
| 50 | + extends Fragment { |
| 51 | + |
| 52 | + @InjectView(R.id.list_threading_log) ListView _logsList; |
| 53 | + |
| 54 | + private LogAdapter _adapter; |
| 55 | + private List<String> _logs; |
| 56 | + |
| 57 | + private Subscription _subscription; |
| 58 | + private BehaviorSubject<Observable<String>> _searchTextEmitterSubject; |
| 59 | + |
| 60 | + @Override |
| 61 | + public void onActivityCreated(@Nullable Bundle savedInstanceState) { |
| 62 | + super.onActivityCreated(savedInstanceState); |
| 63 | + _setupLogger(); |
| 64 | + |
| 65 | + _searchTextEmitterSubject = BehaviorSubject.create(Observable.<String>empty()); |
| 66 | + |
| 67 | + _subscription = AndroidObservable.bindFragment(BehaviorSubjectSearchEmitterFragment.this, |
| 68 | + Observable.switchOnNext(_searchTextEmitterSubject)) |
| 69 | + .subscribeOn(Schedulers.io()) |
| 70 | + .observeOn(AndroidSchedulers.mainThread()) |
| 71 | + .subscribe(_getSearchObserver()); |
| 72 | + |
| 73 | + // .debounce(400, TimeUnit.MILLISECONDS, Schedulers.io()) |
| 74 | + } |
| 75 | + |
| 76 | + @OnTextChanged(R.id.input_txt_subject) |
| 77 | + public void onTextEntered(CharSequence charsEntered) { |
| 78 | + Timber.d("---------- text entered %s", charsEntered); |
| 79 | + _searchTextEmitterSubject.onNext(_getASearchObservableFor(charsEntered.toString())); |
| 80 | + } |
| 81 | + |
| 82 | + // ----------------------------------------------------------------------------------- |
| 83 | + // Main Rx entities |
| 84 | + |
| 85 | + private Observable<String> _getASearchObservableFor(final String searchText) { |
| 86 | + return Observable.create(new Observable.OnSubscribe<String>() { |
| 87 | + |
| 88 | + |
| 89 | + @Override |
| 90 | + public void call(Subscriber<? super String> subscriber) { |
| 91 | + subscriber.onNext(searchText); |
| 92 | + subscriber.onCompleted(); |
| 93 | + } |
| 94 | + }); |
| 95 | + } |
| 96 | + |
| 97 | + private Observer<String> _getSearchObserver() { |
| 98 | + return new Observer<String>() { |
| 99 | + |
| 100 | + |
| 101 | + @Override |
| 102 | + public void onCompleted() { |
| 103 | + Timber.d("--------- onComplete"); |
| 104 | + } |
| 105 | + |
| 106 | + @Override |
| 107 | + public void onError(Throwable e) { |
| 108 | + Timber.e(e, "--------- Woops on error!"); |
| 109 | + _log(String.format("Dang error. check your logs")); |
| 110 | + } |
| 111 | + |
| 112 | + @Override |
| 113 | + public void onNext(String searchText) { |
| 114 | + Timber.d("--------- onNext"); |
| 115 | + _log(String.format("You searched for %s", searchText)); |
| 116 | + } |
| 117 | + }; |
| 118 | + } |
| 119 | + |
| 120 | + @Override |
| 121 | + public void onDestroy() { |
| 122 | + super.onDestroy(); |
| 123 | + _subscription.unsubscribe(); |
| 124 | + } |
| 125 | + |
| 126 | + // ----------------------------------------------------------------------------------- |
| 127 | + // Method that help wiring up the example (irrelevant to RxJava) |
| 128 | + |
| 129 | + @Override |
| 130 | + public View onCreateView(LayoutInflater inflater, |
| 131 | + @Nullable ViewGroup container, |
| 132 | + @Nullable Bundle savedInstanceState) { |
| 133 | + View layout = inflater.inflate(R.layout.fragment_subject, container, false); |
| 134 | + ButterKnife.inject(this, layout); |
| 135 | + return layout; |
| 136 | + } |
| 137 | + |
| 138 | + private void _setupLogger() { |
| 139 | + _logs = new ArrayList<String>(); |
| 140 | + _adapter = new LogAdapter(getActivity(), new ArrayList<String>()); |
| 141 | + _logsList.setAdapter(_adapter); |
| 142 | + } |
| 143 | + |
| 144 | + private void _log(String logMsg) { |
| 145 | + |
| 146 | + if (_isCurrentlyOnMainThread()) { |
| 147 | + _logs.add(0, logMsg + " (main thread) "); |
| 148 | + _adapter.clear(); |
| 149 | + _adapter.addAll(_logs); |
| 150 | + |
| 151 | + } else { |
| 152 | + _logs.add(0, logMsg + " (NOT main thread) "); |
| 153 | + |
| 154 | + // You can only do below stuff on main thread. |
| 155 | + new Handler(Looper.getMainLooper()).post(new Runnable() { |
| 156 | + |
| 157 | + |
| 158 | + @Override |
| 159 | + public void run() { |
| 160 | + _adapter.clear(); |
| 161 | + _adapter.addAll(_logs); |
| 162 | + } |
| 163 | + }); |
| 164 | + } |
| 165 | + } |
| 166 | + |
| 167 | + private boolean _isCurrentlyOnMainThread() { |
| 168 | + return Looper.myLooper() == Looper.getMainLooper(); |
| 169 | + } |
| 170 | + |
| 171 | + private class LogAdapter |
| 172 | + extends ArrayAdapter<String> { |
| 173 | + |
| 174 | + public LogAdapter(Context context, List<String> logs) { |
| 175 | + super(context, R.layout.item_log, R.id.item_log, logs); |
| 176 | + } |
| 177 | + } |
| 178 | +} |
0 commit comments