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); getMenuInflater().inflate(R.menu.menu_main, menu);
infoMenuItem = menu.findItem(R.id.action_info); infoMenuItem = menu.findItem(R.id.action_info);
MenuItem myActionMenuItem = menu.findItem(R.id.action_search); MenuItem myActionMenuItem = menu.findItem(R.id.action_search);
searchView = (SearchView) myActionMenuItem.getActionView(); searchView = (SearchView) myActionMenuItem.getActionView();
@ -295,11 +294,17 @@ public abstract class BaseActivity
adjustMapToSearch(defaultMapZoom); adjustMapToSearch(defaultMapZoom);
} }
if (!hasFocus && activeFragment.equals(ActiveFragment.USER_LIST)) {
((UsersListFragment) userListFragment).restoreUsersList();
}
if(hasFocus && activeFragment.equals(ActiveFragment.NONE)){ if(hasFocus && activeFragment.equals(ActiveFragment.NONE)){
adjustMapToSearch(searchMapZoom); adjustMapToSearch(searchMapZoom);
} }
}); });
RxSearchObservable.fromView(searchView) RxSearchObservable.fromView(searchView)
.skip(0) .skip(0)
.map(String::toLowerCase) .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.LinearLayoutManager;
import android.support.v7.widget.RecyclerView; import android.support.v7.widget.RecyclerView;
import android.util.Log; import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.Menu; import android.view.Menu;
import android.view.MenuInflater; import android.view.MenuInflater;
@ -160,11 +161,7 @@ public class UsersListFragment extends Fragment {
} }
public void searchUser(String textToSearch) { public void searchUser(String textToSearch) {
searchTutorInBackend(textToSearch);
tutorsFiltered.clear();
tutorsFiltered.addAll(Stream.of(tutorsList).filter(t ->
t.toSearchAbleString().toLowerCase().contains(textToSearch.toLowerCase())).toList());
mAdapter.notifyDataSetChanged();
} }
private void showNoteDialog(final User user) { 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) { private int sortByUserName(User t1, User t2) {
return plCollator.compare(t1.getLastName(), t2.getLastName()); return plCollator.compare(t1.getLastName(), t2.getLastName());
} }
@ -288,13 +310,45 @@ public class UsersListFragment extends Fragment {
ResponseBody responseBody = ((HttpException) e).response().errorBody(); ResponseBody responseBody = ((HttpException) e).response().errorBody();
message = RestApiHelper.getErrorMessage(responseBody); message = RestApiHelper.getErrorMessage(responseBody);
} else { } else {
message = "Network Error !"; message = getString(R.string.network_err);
} }
Snackbar.make(coordinatorLayout, message, Snackbar.LENGTH_LONG) Snackbar snackbar = Snackbar.make(coordinatorLayout, message, Snackbar.LENGTH_LONG);
.show(); 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() { private void toggleEmptyNotes() {
if (tutorsList.size() > 0) { if (tutorsList.size() > 0) {
noNotesView.setVisibility(View.GONE); noNotesView.setVisibility(View.GONE);

View File

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

View File

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

View File

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

View File

@ -257,6 +257,9 @@
<string name="no_offline_users">Brak użytkowników offline.</string> <string name="no_offline_users">Brak użytkowników offline.</string>
<string name="only_online_users">Tylko użytkownicy online</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> </resources>

View File

@ -423,5 +423,7 @@
<string name="no_online_users">Currently, there are no\nonline users.</string> <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="no_offline_users">Currently, there are no\noffline users.</string>
<string name="only_online_users">Only online 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> </resources>