|
| 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.Button; |
| 14 | +import android.widget.ListView; |
| 15 | +import android.widget.ProgressBar; |
| 16 | + |
| 17 | +import com.morihacky.android.rxjava.app.R; |
| 18 | + |
| 19 | +import java.util.ArrayList; |
| 20 | +import java.util.List; |
| 21 | +import java.util.concurrent.TimeUnit; |
| 22 | + |
| 23 | +import butterknife.ButterKnife; |
| 24 | +import butterknife.InjectView; |
| 25 | +import butterknife.OnClick; |
| 26 | +import rx.Observable; |
| 27 | +import rx.Observer; |
| 28 | +import rx.Subscriber; |
| 29 | +import rx.schedulers.Schedulers; |
| 30 | +import timber.log.Timber; |
| 31 | + |
| 32 | +public class ConcurrencyWithSchedulersDemoFragment |
| 33 | + extends Fragment { |
| 34 | + |
| 35 | + |
| 36 | +// @InjectView(R.id.btn_start_operation) Button _btnStart; |
| 37 | + @InjectView(R.id.progress_operation_running) ProgressBar _progress; |
| 38 | + @InjectView(R.id.list_threading_log) ListView _logsList; |
| 39 | + |
| 40 | + private LogAdapter _adapter; |
| 41 | + private List<String> _logs; |
| 42 | + |
| 43 | + |
| 44 | + // ----------------------------------------------------------------------------------- |
| 45 | + // Main Rx entities |
| 46 | + |
| 47 | + /** |
| 48 | + * Gets the Observable as emitted from BehaviorSubject |
| 49 | + * |
| 50 | + * It begins by emitting the item most recently emitted by source Observable |
| 51 | + * (or seed/default if none has yet been emitted - which is the case here) |
| 52 | + * |
| 53 | + * https://github.com/Netflix/RxJava/wiki/Subject#behaviorsubject |
| 54 | + */ |
| 55 | + private Observable<List<Integer>> _getObservable() { |
| 56 | + return Observable.create(new Observable.OnSubscribe<Integer>() { |
| 57 | + |
| 58 | + |
| 59 | + @Override |
| 60 | + public void call(Subscriber<? super Integer> observer) { |
| 61 | + observer.onNext(1); |
| 62 | + } |
| 63 | + }).buffer(2, TimeUnit.SECONDS); |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * Observer that handles the result List<Integer> from Observable |
| 68 | + * through the 3 important actions: |
| 69 | + * |
| 70 | + * 1. onCompleted |
| 71 | + * 2. onError |
| 72 | + * 3. onNext |
| 73 | + */ |
| 74 | + private Observer<List<Integer>> _getObserver() { |
| 75 | + return new Observer<List<Integer>>() { |
| 76 | + |
| 77 | + |
| 78 | + @Override |
| 79 | + public void onCompleted() { |
| 80 | + _mainThreadHandler.post(new Runnable() { |
| 81 | + |
| 82 | + |
| 83 | + @Override |
| 84 | + public void run() { |
| 85 | + _addLogToAdapter(String.format("%d taps", _counter)); |
| 86 | + _counter = 0; |
| 87 | + } |
| 88 | + }); |
| 89 | + } |
| 90 | + |
| 91 | + @Override |
| 92 | + public void onError(Throwable e) { |
| 93 | + Timber.e(e, "--------- Woops on error!"); |
| 94 | + } |
| 95 | + |
| 96 | + @Override |
| 97 | + public void onNext(List<Integer> integers) { |
| 98 | + for (int i : integers) { |
| 99 | + _counter += i; |
| 100 | + } |
| 101 | + |
| 102 | + Timber.d("--------- on next with a count of %d", _counter); |
| 103 | + onCompleted(); |
| 104 | + } |
| 105 | + }; |
| 106 | + } |
| 107 | + |
| 108 | + // ----------------------------------------------------------------------------------- |
| 109 | + // Method that help wiring up the example (irrelevant to RxJava) |
| 110 | + |
| 111 | + @Override |
| 112 | + public View onCreateView(LayoutInflater inflater, |
| 113 | + @Nullable ViewGroup container, |
| 114 | + @Nullable Bundle savedInstanceState) { |
| 115 | + View layout = inflater.inflate(R.layout.fragment_concurrency_schedulers, container, false); |
| 116 | + ButterKnife.inject(this, layout); |
| 117 | + return layout; |
| 118 | + } |
| 119 | + |
| 120 | + private void _setupLogAdapter() { |
| 121 | + _logs = new ArrayList<String>(); |
| 122 | + _adapter = new LogAdapter(getActivity(), new ArrayList<String>()); |
| 123 | + _logsListView.setAdapter(_adapter); |
| 124 | + } |
| 125 | + |
| 126 | + private void _addLogToAdapter(String logMsg) { |
| 127 | + _logs.add(0, logMsg); |
| 128 | + _adapter.clear(); |
| 129 | + _adapter.addAll(_logs); |
| 130 | + } |
| 131 | + |
| 132 | + private class LogAdapter |
| 133 | + extends ArrayAdapter<String> { |
| 134 | + |
| 135 | + public LogAdapter(Context context, List<String> logs) { |
| 136 | + super(context, R.layout.item_log, R.id.item_log, logs); |
| 137 | + } |
| 138 | + } |
| 139 | +} |
0 commit comments