Improve user search

This commit is contained in:
Mieszko Wrzeszczyński 2018-11-20 00:08:18 +01:00
parent d5114a8587
commit 50a427313f
4 changed files with 72 additions and 28 deletions

View File

@ -1,6 +1,7 @@
package com.uam.wmi.findmytutor.activity;
import android.Manifest;
import android.annotation.SuppressLint;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.content.Intent;
@ -19,6 +20,7 @@ import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.SearchView;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
@ -29,10 +31,17 @@ import com.uam.wmi.findmytutor.R;
import com.uam.wmi.findmytutor.service.BackgroundLocalizationService;
import com.uam.wmi.findmytutor.utils.ActiveFragment;
import com.uam.wmi.findmytutor.utils.PrefUtils;
import com.uam.wmi.findmytutor.utils.RxSearchObservable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.TimeUnit;
import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;
import static com.uam.wmi.findmytutor.utils.PrefUtils.storeBackgroundLocationStatus;
@ -61,7 +70,9 @@ public abstract class BaseActivity
private Fragment userListFragment;
private ActiveFragment activeFragment = ActiveFragment.NONE;
private SearchView searchView;
@SuppressLint("CheckResult")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
@ -121,6 +132,9 @@ public abstract class BaseActivity
}
}
@ -241,41 +255,31 @@ public abstract class BaseActivity
actionBarDrawerToggle.onConfigurationChanged(newConfig);
}
@SuppressLint("CheckResult")
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
MenuItem myActionMenuItem = menu.findItem(R.id.action_search);
final SearchView searchView = (SearchView) myActionMenuItem.getActionView();
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String input) {
if (activeFragment.equals(ActiveFragment.USER_LIST)) {
executeUserListSearch(input);
}
searchView = (SearchView) myActionMenuItem.getActionView();
return false;
}
@Override
public boolean onQueryTextChange(String input) {
if (activeFragment.equals(ActiveFragment.USER_LIST)) {
executeUserListSearch(input);
}
return true;
}
});
RxSearchObservable.fromView(searchView)
.map(String::toLowerCase)
.debounce(300, TimeUnit.MILLISECONDS)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(this::executeSearch);
return true;
}
private void executeUserListSearch(String input) {
((UsersListFragment) userListFragment).searchUser(input);
private void executeSearch(String input) {
if (activeFragment.equals(ActiveFragment.USER_LIST)) {
((UsersListFragment) userListFragment).searchUser(input);
}
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (actionBarDrawerToggle.onOptionsItemSelected(item)) {

View File

@ -349,8 +349,6 @@ public class MapActivity extends BaseActivity
coordinateService.getOnlineCoordinates()
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.map(tutors -> Stream.of(tutors).
filterNot(t -> t.getDisplayMode().equals(SharingLevel.PRESENCE.toString())).toList())
.subscribeWith(new DisposableSingleObserver<List<Coordinate>>() {
@Override
@ -362,12 +360,18 @@ public class MapActivity extends BaseActivity
return;
}
ArrayList<String> tmp = new ArrayList<>();
for (Coordinate coordinate : coordsList) {
tmp.add(coordinate.getUserId());
Log.e(tag, coordinate.toString());
if (coordinate.getDisplayMode().equals(SharingLevel.PRESENCE.toString())) {
Log.e(tag, "presence user omitted");
} else {
tmp.add(coordinate.getUserId());
}
}
Set<String> currentCoordsIds = new HashSet<>(tmp);
if (previousCoordsIds.isEmpty()){
previousCoordsIds.addAll(currentCoordsIds);
@ -388,7 +392,6 @@ public class MapActivity extends BaseActivity
for (Coordinate element : coordsList) {
String id = element.getUserId();
String newLabel = element.getLabel();

View File

@ -9,6 +9,7 @@ import android.support.v7.app.AlertDialog;
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.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

View File

@ -0,0 +1,36 @@
package com.uam.wmi.findmytutor.utils;
import android.support.v7.widget.SearchView;
import io.reactivex.Observable;
import io.reactivex.subjects.PublishSubject;
public class RxSearchObservable {
private RxSearchObservable() {
// no instance
}
public static Observable<String> fromView(SearchView searchView) {
final PublishSubject<String> subject = PublishSubject.create();
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String s) {
subject.onComplete();
return false;
}
@Override
public boolean onQueryTextChange(String text) {
subject.onNext(text);
return false;
}
});
return subject;
}
}