-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathLocalSearchActivity.java
More file actions
170 lines (138 loc) · 5.89 KB
/
LocalSearchActivity.java
File metadata and controls
170 lines (138 loc) · 5.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package info.androidhive.rxjavasearch.view;
import android.graphics.Color;
import android.os.Build;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.appcompat.widget.Toolbar;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import com.jakewharton.rxbinding3.widget.RxTextView;
import com.jakewharton.rxbinding3.widget.TextViewTextChangeEvent;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import info.androidhive.rxjavasearch.R;
import info.androidhive.rxjavasearch.adapter.ContactsAdapterFilterable;
import info.androidhive.rxjavasearch.network.ApiClient;
import info.androidhive.rxjavasearch.network.ApiService;
import info.androidhive.rxjavasearch.network.model.Contact;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.observers.DisposableObserver;
import io.reactivex.observers.DisposableSingleObserver;
import io.reactivex.schedulers.Schedulers;
public class LocalSearchActivity extends AppCompatActivity implements ContactsAdapterFilterable.ContactsAdapterListener {
private static final String TAG = LocalSearchActivity.class.getSimpleName();
private CompositeDisposable disposable = new CompositeDisposable();
private ApiService apiService;
private ContactsAdapterFilterable mAdapter;
private List<Contact> contactsList = new ArrayList<>();
@BindView(R.id.input_search)
EditText inputSearch;
@BindView(R.id.recycler_view)
RecyclerView recyclerView;
private Unbinder unbinder;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_local_search);
unbinder = ButterKnife.bind(this);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
mAdapter = new ContactsAdapterFilterable(this, contactsList, this);
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
recyclerView.setLayoutManager(mLayoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));
recyclerView.setAdapter(mAdapter);
whiteNotificationBar(recyclerView);
apiService = ApiClient.getClient().create(ApiService.class);
disposable.add(RxTextView.textChangeEvents(inputSearch)
.skipInitialValue()
.debounce(300, TimeUnit.MILLISECONDS)
/*.filter(new Predicate<TextViewTextChangeEvent>() {
@Override
public boolean test(TextViewTextChangeEvent textViewTextChangeEvent) throws Exception {
return TextUtils.isEmpty(textViewTextChangeEvent.text().toString()) || textViewTextChangeEvent.text().toString().length() > 2;
}
})*/
.distinctUntilChanged()
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribeWith(searchContacts()));
// source: `gmail` or `linkedin`
// fetching all contacts on app launch
// only gmail will be fetched
fetchContacts("gmail");
}
private DisposableObserver<TextViewTextChangeEvent> searchContacts() {
return new DisposableObserver<TextViewTextChangeEvent>() {
@Override
public void onNext(TextViewTextChangeEvent textViewTextChangeEvent) {
Log.d(TAG, "Search query: " + textViewTextChangeEvent.getText());
mAdapter.getFilter().filter(textViewTextChangeEvent.getText());
}
@Override
public void onError(Throwable e) {
Log.e(TAG, "onError: " + e.getMessage());
}
@Override
public void onComplete() {
}
};
}
/**
* Fetching all contacts
*/
private void fetchContacts(String source) {
disposable.add(apiService
.getContacts(source, null)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribeWith(new DisposableSingleObserver<List<Contact>>() {
@Override
public void onSuccess(List<Contact> contacts) {
contactsList.clear();
contactsList.addAll(contacts);
mAdapter.notifyDataSetChanged();
}
@Override
public void onError(Throwable e) {
}
}));
}
@Override
protected void onDestroy() {
disposable.clear();
unbinder.unbind();
super.onDestroy();
}
@Override
public void onContactSelected(Contact contact) {
}
private void whiteNotificationBar(View view) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
int flags = view.getSystemUiVisibility();
flags |= View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
view.setSystemUiVisibility(flags);
getWindow().setStatusBarColor(Color.WHITE);
}
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == android.R.id.home) {
finish();
}
return super.onOptionsItemSelected(item);
}
}