Skip to content

Commit 91e7afc

Browse files
Add search example
1 parent fd135d5 commit 91e7afc

File tree

7 files changed

+171
-3
lines changed

7 files changed

+171
-3
lines changed

app/src/main/AndroidManifest.xml

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@
3030
<activity
3131
android:name=".ui.pagination.PaginationActivity"
3232
android:label="@string/pagination" />
33+
<activity
34+
android:name=".ui.search.SearchActivity"
35+
android:label="@string/search" />
3336
<activity
3437
android:name=".ui.operators.SimpleExampleActivity"
3538
android:label="@string/simple" />
@@ -120,9 +123,9 @@
120123
<activity
121124
android:name=".ui.operators.DelayExampleActivity"
122125
android:label="@string/delay" />
123-
<activity android:name=".ui.compose.ComposeOperatorExampleActivity"
124-
android:label="@string/compose"
125-
/>
126+
<activity
127+
android:name=".ui.compose.ComposeOperatorExampleActivity"
128+
android:label="@string/compose" />
126129
</application>
127130

128131
</manifest>

app/src/main/java/com/rxjava2/android/samples/ui/SelectionActivity.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import com.rxjava2.android.samples.ui.networking.NetworkingActivity;
1212
import com.rxjava2.android.samples.ui.pagination.PaginationActivity;
1313
import com.rxjava2.android.samples.ui.rxbus.RxBusActivity;
14+
import com.rxjava2.android.samples.ui.search.SearchActivity;
1415

1516
public class SelectionActivity extends AppCompatActivity {
1617

@@ -40,4 +41,9 @@ public void startPaginationActivity(View view) {
4041
public void startComposeOperator(View view) {
4142
startActivity(new Intent(SelectionActivity.this, ComposeOperatorExampleActivity.class));
4243
}
44+
45+
public void startSearchActivity(View view) {
46+
startActivity(new Intent(SelectionActivity.this, SearchActivity.class));
47+
}
48+
4349
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.rxjava2.android.samples.ui.search;
2+
3+
import android.widget.SearchView;
4+
5+
import io.reactivex.Observable;
6+
import io.reactivex.subjects.PublishSubject;
7+
8+
/**
9+
* Created by amitshekhar on 15/10/17.
10+
*/
11+
12+
public class RxSearchObservable {
13+
14+
private RxSearchObservable() {
15+
// no instance
16+
}
17+
18+
public static Observable<String> fromView(SearchView searchView) {
19+
20+
final PublishSubject<String> subject = PublishSubject.create();
21+
22+
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
23+
@Override
24+
public boolean onQueryTextSubmit(String s) {
25+
subject.onComplete();
26+
return true;
27+
}
28+
29+
@Override
30+
public boolean onQueryTextChange(String text) {
31+
subject.onNext(text);
32+
return true;
33+
}
34+
});
35+
36+
return subject;
37+
}
38+
}
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
package com.rxjava2.android.samples.ui.search;
2+
3+
import android.os.Bundle;
4+
import android.support.v7.app.AppCompatActivity;
5+
import android.widget.SearchView;
6+
import android.widget.TextView;
7+
8+
import com.rxjava2.android.samples.R;
9+
10+
import java.util.concurrent.TimeUnit;
11+
12+
import io.reactivex.Observable;
13+
import io.reactivex.ObservableSource;
14+
import io.reactivex.android.schedulers.AndroidSchedulers;
15+
import io.reactivex.annotations.NonNull;
16+
import io.reactivex.functions.Consumer;
17+
import io.reactivex.functions.Function;
18+
import io.reactivex.functions.Predicate;
19+
import io.reactivex.schedulers.Schedulers;
20+
21+
/**
22+
* Created by amitshekhar on 15/10/17.
23+
*/
24+
25+
public class SearchActivity extends AppCompatActivity {
26+
27+
public static final String TAG = SearchActivity.class.getSimpleName();
28+
private SearchView searchView;
29+
private TextView textViewResult;
30+
31+
@Override
32+
protected void onCreate(Bundle savedInstanceState) {
33+
super.onCreate(savedInstanceState);
34+
setContentView(R.layout.activity_search);
35+
searchView = (SearchView) findViewById(R.id.searchView);
36+
textViewResult = (TextView) findViewById(R.id.textViewResult);
37+
38+
setUpSearchObservable();
39+
}
40+
41+
private void setUpSearchObservable() {
42+
RxSearchObservable.fromView(searchView)
43+
.debounce(300, TimeUnit.MILLISECONDS)
44+
.filter(new Predicate<String>() {
45+
@Override
46+
public boolean test(String text) throws Exception {
47+
if (text.isEmpty()) {
48+
textViewResult.setText("");
49+
return false;
50+
} else {
51+
return true;
52+
}
53+
}
54+
})
55+
.distinctUntilChanged()
56+
.switchMap(new Function<String, ObservableSource<String>>() {
57+
@Override
58+
public ObservableSource<String> apply(String query) throws Exception {
59+
return dataFromNetwork(query);
60+
}
61+
})
62+
.subscribeOn(Schedulers.io())
63+
.observeOn(AndroidSchedulers.mainThread())
64+
.subscribe(new Consumer<String>() {
65+
@Override
66+
public void accept(String result) throws Exception {
67+
textViewResult.setText(result);
68+
}
69+
});
70+
}
71+
72+
/**
73+
* Simulation of network data
74+
*/
75+
private Observable<String> dataFromNetwork(final String query) {
76+
return Observable.just(true)
77+
.delay(2, TimeUnit.SECONDS)
78+
.map(new Function<Boolean, String>() {
79+
@Override
80+
public String apply(@NonNull Boolean value) throws Exception {
81+
return query;
82+
}
83+
});
84+
}
85+
86+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
3+
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
4+
android:layout_width="match_parent"
5+
android:layout_height="match_parent"
6+
android:orientation="vertical">
7+
8+
9+
<SearchView
10+
android:id="@+id/searchView"
11+
android:layout_width="match_parent"
12+
android:layout_height="48dp">
13+
14+
</SearchView>
15+
16+
<TextView
17+
android:layout_width="match_parent"
18+
android:layout_height="wrap_content"
19+
android:text="@string/result" />
20+
21+
<TextView
22+
android:id="@+id/textViewResult"
23+
android:layout_width="match_parent"
24+
android:layout_height="wrap_content" />
25+
26+
</LinearLayout>

app/src/main/res/layout/activity_selection.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,5 +50,12 @@
5050
android:text="@string/compose"
5151
android:textColor="@android:color/black" />
5252

53+
<Button
54+
android:layout_width="match_parent"
55+
android:layout_height="wrap_content"
56+
android:onClick="startSearchActivity"
57+
android:text="@string/search"
58+
android:textColor="@android:color/black" />
59+
5360
</LinearLayout>
5461
</ScrollView>

app/src/main/res/values/strings.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
<string name="operators">Operators</string>
55
<string name="networking">Networking</string>
66
<string name="pagination">Pagination</string>
7+
<string name="search">Search</string>
78
<string name="simple">Simple</string>
89
<string name="map">Map</string>
910
<string name="zip">Zip</string>
@@ -40,4 +41,5 @@
4041
<string name="click_me">Click Me</string>
4142
<string name="rx_bus">RxBus</string>
4243
<string name="compose">Compose for reusable</string>
44+
<string name="result">Result</string>
4345
</resources>

0 commit comments

Comments
 (0)