Add search on backend

This commit is contained in:
Mieszko Wrzeszczyński 2019-01-02 18:23:03 +01:00
parent be7a0cec0a
commit 44e392c027
7 changed files with 81 additions and 9 deletions

View File

@ -285,7 +285,6 @@ public abstract class BaseActivity
getMenuInflater().inflate(R.menu.menu_main, menu);
infoMenuItem = menu.findItem(R.id.action_info);
MenuItem myActionMenuItem = menu.findItem(R.id.action_search);
searchView = (SearchView) myActionMenuItem.getActionView();
@ -295,11 +294,17 @@ public abstract class BaseActivity
adjustMapToSearch(defaultMapZoom);
}
if (!hasFocus && activeFragment.equals(ActiveFragment.USER_LIST)) {
((UsersListFragment) userListFragment).restoreUsersList();
}
if(hasFocus && activeFragment.equals(ActiveFragment.NONE)){
adjustMapToSearch(searchMapZoom);
}
});
RxSearchObservable.fromView(searchView)
.skip(0)
.map(String::toLowerCase)

View File

@ -10,6 +10,7 @@ import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
@ -160,11 +161,7 @@ public class UsersListFragment extends Fragment {
}
public void searchUser(String textToSearch) {
tutorsFiltered.clear();
tutorsFiltered.addAll(Stream.of(tutorsList).filter(t ->
t.toSearchAbleString().toLowerCase().contains(textToSearch.toLowerCase())).toList());
mAdapter.notifyDataSetChanged();
searchTutorInBackend(textToSearch);
}
private void showNoteDialog(final User user) {
@ -276,6 +273,31 @@ public class UsersListFragment extends Fragment {
}
private void searchTutorInBackend(String searchString) {
disposable.add(
userService.searchUser(searchString)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribeWith(new DisposableSingleObserver<List<User>>() {
@Override
public void onSuccess(List<User> users) {
tutorsFiltered.clear();
tutorsFiltered.addAll(users);
mAdapter.notifyDataSetChanged();
}
@Override
public void onError(Throwable e) {
showSearchError(e);
}
}));
}
public void restoreUsersList(){
fetchAllTutors();
}
private int sortByUserName(User t1, User t2) {
return plCollator.compare(t1.getLastName(), t2.getLastName());
}
@ -288,13 +310,45 @@ public class UsersListFragment extends Fragment {
ResponseBody responseBody = ((HttpException) e).response().errorBody();
message = RestApiHelper.getErrorMessage(responseBody);
} else {
message = "Network Error !";
message = getString(R.string.network_err);
}
Snackbar.make(coordinatorLayout, message, Snackbar.LENGTH_LONG)
.show();
Snackbar snackbar = Snackbar.make(coordinatorLayout, message, Snackbar.LENGTH_LONG);
View view = snackbar.getView();
CoordinatorLayout.LayoutParams params=(CoordinatorLayout.LayoutParams)view.getLayoutParams();
params.gravity = Gravity.TOP;
view.setLayoutParams(params);
snackbar.show();
}
private void showSearchError(Throwable e) {
String message;
Log.e(TAG, String.valueOf(e));
if (e instanceof HttpException) {
ResponseBody responseBody = ((HttpException) e).response().errorBody();
message = RestApiHelper.getErrorMessage(responseBody);
} else {
message = getString(R.string.search_null);
}
/* Snackbar.make(coordinatorLayout, message, Snackbar.LENGTH_LONG)
.show();
*/
Snackbar snackbar = Snackbar.make(coordinatorLayout, message, Snackbar.LENGTH_LONG);
View view = snackbar.getView();
CoordinatorLayout.LayoutParams params=(CoordinatorLayout.LayoutParams)view.getLayoutParams();
params.gravity = Gravity.TOP;
view.setLayoutParams(params);
snackbar.show();
}
private void toggleEmptyNotes() {
if (tutorsList.size() > 0) {
noNotesView.setVisibility(View.GONE);

View File

@ -37,6 +37,9 @@ public interface UserService {
@GET("api/users/tutors/offline")
Single <List<User>> getAllOfflineTutors();
@GET("api/users/tutors/search/{searchString}")
Single <List<User>> searchUser(@Path("searchString") String searchString );
@POST("api/users")
Completable createUser(@Body User user);

View File

@ -21,6 +21,7 @@ public class RxSearchObservable {
@Override
public boolean onQueryTextSubmit(String s) {
subject.onNext(s);
searchView.clearFocus();
return false;
}
@ -31,6 +32,8 @@ public class RxSearchObservable {
}
});

View File

@ -6,6 +6,8 @@
android:id="@+id/action_search"
android:icon="@drawable/ic_menu_search"
app:showAsAction="always"
android:focusable="true"
android:focusableInTouchMode="true"
app:actionViewClass="android.support.v7.widget.SearchView"
android:title="@string/search"/>
<item

View File

@ -257,6 +257,9 @@
<string name="no_offline_users">Brak użytkowników offline.</string>
<string name="only_online_users">Tylko użytkownicy online</string>
<string name="network_err">Błąd sieci!</string>
<string name="search_null">Brak wyników!</string>
</resources>

View File

@ -423,5 +423,7 @@
<string name="no_online_users">Currently, there are no\nonline users.</string>
<string name="no_offline_users">Currently, there are no\noffline users.</string>
<string name="only_online_users">Only online users</string>
<string name="network_err">Network Error !</string>
<string name="search_null">Search response is empty!</string>
</resources>