| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 5512ca0 commit adffc8e
10 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -8,7 +8,7 @@ | |||
| 8 | 8 | android:label="@string/app_name" | |
| 9 | 9 | android:theme="@style/AppTheme" > | |
| 10 | 10 | <activity | |
| 11 | - android:name="com.morihacky.android.rxjava.DemoActivity" | ||
| 11 | + android:name="com.morihacky.android.rxjava.MainActivity" | ||
| 12 | 12 | android:label="@string/app_name" > | |
| 13 | 13 | <intent-filter> | |
| 14 | 14 | <action android:name="android.intent.action.MAIN" /> | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -27,10 +27,10 @@ | |||
| 27 | 27 | import rx.schedulers.Schedulers; | |
| 28 | 28 | import timber.log.Timber; | |
| 29 | 29 | ||
| 30 | - public class DemoAccumulateEventFragment | ||
| 30 | + public class BufferDemoFragment | ||
| 31 | 31 | extends Fragment { | |
| 32 | 32 | ||
| 33 | - @InjectView(R.id.accumulated_event_list) ListView _logsListView; | ||
| 33 | + @InjectView(R.id.list_threading_log) ListView _logsList; | ||
| 34 | 34 | ||
| 35 | 35 | private LogAdapter _adapter; | |
| 36 | 36 | private List<String> _logs; | |
@@ -50,7 +50,7 @@ public void onActivityCreated(@Nullable Bundle savedInstanceState) { | |||
| 50 | 50 | } | |
| 51 | 51 | ||
| 52 | 52 | ||
| 53 | - @OnClick(R.id.accumulated_event_btn) | ||
| 53 | + @OnClick(R.id.btn_start_operation) | ||
| 54 | 54 | public void onButtonTapped() { | |
| 55 | 55 | // BehaviorSubject takes in Observable inputs. | |
| 56 | 56 | // So send 1 tap as an observable | |
@@ -129,15 +129,15 @@ public void onNext(List<Integer> integers) { | |||
| 129 | 129 | public View onCreateView(LayoutInflater inflater, | |
| 130 | 130 | @Nullable ViewGroup container, | |
| 131 | 131 | @Nullable Bundle savedInstanceState) { | |
| 132 | - View layout = inflater.inflate(R.layout.fragment_accumulate, container, false); | ||
| 132 | + View layout = inflater.inflate(R.layout.fragment_buffer, container, false); | ||
| 133 | 133 | ButterKnife.inject(this, layout); | |
| 134 | 134 | return layout; | |
| 135 | 135 | } | |
| 136 | 136 | ||
| 137 | 137 | private void _setupLogAdapter() { | |
| 138 | 138 | _logs = new ArrayList<String>(); | |
| 139 | 139 | _adapter = new LogAdapter(getActivity(), new ArrayList<String>()); | |
| 140 | - _logsListView.setAdapter(_adapter); | ||
| 140 | + _logsList.setAdapter(_adapter); | ||
| 141 | 141 | } | |
| 142 | 142 | ||
| 143 | 143 | private void _addLogToAdapter(String logMsg) { | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,139 @@ | |||
| 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 | + } | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -7,19 +7,19 @@ | |||
| 7 | 7 | ||
| 8 | 8 | import timber.log.Timber; | |
| 9 | 9 | ||
| 10 | - public class DemoActivity | ||
| 10 | + public class MainActivity | ||
| 11 | 11 | extends FragmentActivity { | |
| 12 | 12 | ||
| 13 | 13 | @Override | |
| 14 | 14 | protected void onCreate(Bundle savedInstanceState) { | |
| 15 | 15 | super.onCreate(savedInstanceState); | |
| 16 | - setContentView(R.layout.activity_demo); | ||
| 16 | + setContentView(R.layout.activity_main); | ||
| 17 | 17 | ||
| 18 | 18 | Timber.plant(new Timber.DebugTree()); | |
| 19 | 19 | ||
| 20 | 20 | getSupportFragmentManager().beginTransaction() | |
| 21 | 21 | .addToBackStack(this.toString()) | |
| 22 | - .replace(R.id.activity_container, new DemoFragment(), this.toString()) | ||
| 22 | + .replace(R.id.activity_main, new MainFragment(), this.toString()) | ||
| 23 | 23 | .commit(); | |
| 24 | 24 | } | |
| 25 | 25 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -12,25 +12,37 @@ | |||
| 12 | 12 | import butterknife.ButterKnife; | |
| 13 | 13 | import butterknife.OnClick; | |
| 14 | 14 | ||
| 15 | - public class DemoFragment | ||
| 15 | + public class MainFragment | ||
| 16 | 16 | extends Fragment { | |
| 17 | 17 | ||
| 18 | 18 | @Override | |
| 19 | 19 | public View onCreateView(LayoutInflater inflater, | |
| 20 | 20 | @Nullable ViewGroup container, | |
| 21 | 21 | @Nullable Bundle savedInstanceState) { | |
| 22 | - View layout = inflater.inflate(R.layout.fragment_demo, container, false); | ||
| 22 | + View layout = inflater.inflate(R.layout.fragment_main, container, false); | ||
| 23 | 23 | ButterKnife.inject(this, layout); | |
| 24 | 24 | return layout; | |
| 25 | 25 | } | |
| 26 | 26 | ||
| 27 | - @OnClick(R.id.demo_accumulate_event) | ||
| 28 | - public void demoAccumulationOfEvents() { | ||
| 27 | + @OnClick(R.id.btn_demo_schedulers) | ||
| 28 | + public void demoConcurrencyWithSchedulers() { | ||
| 29 | 29 | getActivity().getSupportFragmentManager() | |
| 30 | 30 | .beginTransaction() | |
| 31 | 31 | .addToBackStack(this.toString()) | |
| 32 | - .replace(R.id.activity_container, | ||
| 33 | - new DemoAccumulateEventFragment(), | ||
| 32 | + .replace(R.id.activity_main, | ||
| 33 | + new ConcurrencyWithSchedulersDemoFragment(), | ||
| 34 | + this.toString()) | ||
| 35 | + .commit(); | ||
| 36 | + } | ||
| 37 | + | ||
| 38 | + | ||
| 39 | + @OnClick(R.id.btn_demo_buffer) | ||
| 40 | + public void demoBuffer() { | ||
| 41 | + getActivity().getSupportFragmentManager() | ||
| 42 | + .beginTransaction() | ||
| 43 | + .addToBackStack(this.toString()) | ||
| 44 | + .replace(R.id.activity_main, | ||
| 45 | + new BufferDemoFragment(), | ||
| 34 | 46 | this.toString()) | |
| 35 | 47 | .commit(); | |
| 36 | 48 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,5 +1,5 @@ | |||
| 1 | 1 | <FrameLayout | |
| 2 | - android:id="@+id/activity_container" | ||
| 2 | + android:id="@+id/activity_main" | ||
| 3 | 3 | android:layout_height="match_parent" | |
| 4 | 4 | android:layout_width="match_parent" | |
| 5 | 5 | xmlns:android="http://schemas.android.com/apk/res/android" /> | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -11,10 +11,10 @@ | |||
| 11 | 11 | android:layout_width="match_parent" | |
| 12 | 12 | android:padding="10dp" | |
| 13 | 13 | android:gravity="center" | |
| 14 | - android:text="@string/demo_accumulate_msg"/> | ||
| 14 | + android:text="@string/msg_demo_buffer"/> | ||
| 15 | 15 | ||
| 16 | 16 | <Button | |
| 17 | - android:id="@+id/accumulated_event_btn" | ||
| 17 | + android:id="@+id/btn_start_operation" | ||
| 18 | 18 | android:layout_height="wrap_content" | |
| 19 | 19 | android:layout_width="match_parent" | |
| 20 | 20 | android:layout_marginLeft="90dp" | |
@@ -23,7 +23,7 @@ | |||
| 23 | 23 | android:text="tap me"/> | |
| 24 | 24 | ||
| 25 | 25 | <ListView | |
| 26 | - android:id="@+id/accumulated_event_list" | ||
| 26 | + android:id="@+id/list_threading_log" | ||
| 27 | 27 | android:layout_height="match_parent" | |
| 28 | 28 | android:layout_width="match_parent"/> | |
| 29 | 29 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,41 @@ | |||
| 1 | + <?xml version="1.0" encoding="utf-8"?> | ||
| 2 | + | ||
| 3 | + <LinearLayout | ||
| 4 | + android:orientation="vertical" | ||
| 5 | + android:layout_height="match_parent" | ||
| 6 | + android:layout_width="match_parent" | ||
| 7 | + xmlns:android="http://schemas.android.com/apk/res/android"> | ||
| 8 | + | ||
| 9 | + <TextView | ||
| 10 | + android:layout_height="wrap_content" | ||
| 11 | + android:layout_width="match_parent" | ||
| 12 | + android:padding="10dp" | ||
| 13 | + android:gravity="center" | ||
| 14 | + android:text="@string/msg_demo_concurrency_schedulers"/> | ||
| 15 | + | ||
| 16 | + | ||
| 17 | + <LinearLayout | ||
| 18 | + android:layout_height="wrap_content" | ||
| 19 | + android:layout_width="match_parent"> | ||
| 20 | + | ||
| 21 | + <Button | ||
| 22 | + android:id="@+id/btn_start_operation" | ||
| 23 | + android:layout_height="wrap_content" | ||
| 24 | + android:layout_width="wrap_content" | ||
| 25 | + android:layout_marginLeft="20dp" | ||
| 26 | + android:textSize="16sp" | ||
| 27 | + android:text="Start long running operation"/> | ||
| 28 | + | ||
| 29 | + <ProgressBar | ||
| 30 | + android:id="@+id/progress_operation_running" | ||
| 31 | + android:visibility="gone" | ||
| 32 | + android:layout_height="wrap_content" | ||
| 33 | + android:layout_width="wrap_content" | ||
| 34 | + android:layout_marginLeft="20dp"/> | ||
| 35 | + </LinearLayout> | ||
| 36 | + | ||
| 37 | + <ListView | ||
| 38 | + android:id="@+id/list_threading_log" | ||
| 39 | + android:layout_height="match_parent" | ||
| 40 | + android:layout_width="match_parent"/> | ||
| 41 | + </LinearLayout> | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -11,15 +11,21 @@ | |||
| 11 | 11 | tools:context=".DemoActivity"> | |
| 12 | 12 | ||
| 13 | 13 | <Button | |
| 14 | - android:id="@+id/demo_long_bg" | ||
| 14 | + android:id="@+id/btn_demo_schedulers" | ||
| 15 | 15 | android:layout_height="wrap_content" | |
| 16 | 16 | android:layout_width="wrap_content" | |
| 17 | - android:text="@string/demo_type_long_bg"/> | ||
| 17 | + android:text="@string/btn_demo_schedulers"/> | ||
| 18 | 18 | ||
| 19 | 19 | <Button | |
| 20 | - android:id="@+id/demo_accumulate_event" | ||
| 20 | + android:id="@+id/btn_demo_buffer" | ||
| 21 | 21 | android:layout_height="wrap_content" | |
| 22 | 22 | android:layout_width="wrap_content" | |
| 23 | - android:text="@string/demo_type_accumulate"/> | ||
| 23 | + android:text="@string/btn_demo_buffer"/> | ||
| 24 | + | ||
| 25 | + <Button | ||
| 26 | + android:id="@+id/btn_demo_throttle" | ||
| 27 | + android:layout_height="wrap_content" | ||
| 28 | + android:layout_width="wrap_content" | ||
| 29 | + android:text="@string/btn_demo_throttle"/> | ||
| 24 | 30 | ||
| 25 | 31 | </LinearLayout> | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -4,8 +4,12 @@ | |||
| 4 | 4 | <string name="app_name">Android-RxJava</string> | |
| 5 | 5 | <string name="hello_world">Hello world!</string> | |
| 6 | 6 | <string name="action_settings">Settings</string> | |
| 7 | - <string name="demo_type_long_bg">long operation in bg (schedulers)</string> | ||
| 8 | - <string name="demo_type_accumulate">accumulate events (instant search)</string> | ||
| 9 | - <string name="demo_accumulate_msg">This is a demonstration of how events are accumulated. Tap the button below repetitively and you will notice in the logs that button taps are collected over a span of 2s and printed below.</string> | ||
| 7 | + | ||
| 8 | + <string name="msg_demo_buffer">This is a demo of how events can be accumulated using the "buffer" operation. Tap the button below repetitively and you will notice in the logs that button taps are collected over a span of 2s and printed below.</string> | ||
| 9 | + <string name="msg_demo_concurrency_schedulers">This is a demo of how long running operations can be offloaded to a background thread. After the operation is done, we resume back on the main thread. All using RxJava!</string> | ||
| 10 | + | ||
| 11 | + <string name="btn_demo_schedulers">bg work (schedulers & concurrency)</string> | ||
| 12 | + <string name="btn_demo_buffer">accumulate calls (buffer)</string> | ||
| 13 | + <string name="btn_demo_throttle">instant search (throttle)</string> | ||
| 10 | 14 | ||
| 11 | 15 | </resources> | |
| Back | FazBrowse Home | New Git URL |
0 commit comments