Add markers filter

This commit is contained in:
Mieszko Wrzeszczyński 2018-11-20 22:29:07 +01:00
parent 892216cc0c
commit b02ec917ed
8 changed files with 127 additions and 97 deletions

View File

@ -20,7 +20,6 @@ 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;
@ -38,9 +37,7 @@ 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;
@ -50,21 +47,17 @@ public abstract class BaseActivity
extends AppCompatActivity
implements BottomNavigationView.OnNavigationItemSelectedListener {
String tag = getClass().getName();
protected static final int REQUEST_PERMISSIONS = 100;
private final static int REQUEST_CODE_ASK_PERMISSIONS = 1;
private static final String[] REQUIRED_SDK_PERMISSIONS = new String[] {
Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_LOCATION_EXTRA_COMMANDS };
private static final String[] REQUIRED_SDK_PERMISSIONS = new String[]{
Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_LOCATION_EXTRA_COMMANDS};
public DrawerLayout drawerLayout;
protected BottomNavigationView navigationView;
protected NavigationView drawerNavigationView;
protected DrawerLayout sideDrawer;
protected Toolbar toolbar;
protected boolean isTutor;
String tag = getClass().getName();
private ActionBarDrawerToggle actionBarDrawerToggle;
private SharingFragment sharingFragment;
@ -84,13 +77,13 @@ public abstract class BaseActivity
item -> {
String itemName = (String) item.getTitle();
Intent launchIntent;
if (itemName.equals(getResources().getString(R.string.navigation_item_whitelist))) {
if (itemName.equals(getResources().getString(R.string.navigation_item_whitelist))) {
/* launchIntent = new Intent(getApplicationContext(), WhitelistActivity.class);
startActivity(launchIntent);*/
} else if (itemName.equals(getResources().getString(R.string.navigation_item_blacklist))) {
/* launchIntent = new Intent(getApplicationContext(), BlacklistActivity.class);
startActivity(launchIntent);*/
} else if (itemName.equals(getResources().getString(R.string.navigation_item_profile))) {
} else if (itemName.equals(getResources().getString(R.string.navigation_item_profile))) {
/* launchIntent = new Intent(getApplicationContext(), ProfileActivity.class);
startActivity(launchIntent);*/
} else if (itemName.equals(getResources().getString(R.string.navigation_item_settings))) {
@ -98,12 +91,12 @@ public abstract class BaseActivity
startActivity(launchIntent);
} else if (itemName.equals(getResources().getString(R.string.navigation_item_logout))) {
if(PrefUtils.isBackgroundLocationServiceRunning(getApplicationContext())) {
if (PrefUtils.isBackgroundLocationServiceRunning(getApplicationContext())) {
stopBackgroundLocalizationTask();
}
storeBackgroundLocationStatus(getApplication(),false);
PrefUtils.storeIsLoggedIn(getApplicationContext(),false);
storeBackgroundLocationStatus(getApplication(), false);
PrefUtils.storeIsLoggedIn(getApplicationContext(), false);
Intent i = getBaseContext().getPackageManager()
.getLaunchIntentForPackage(getBaseContext().getPackageName());
@ -130,14 +123,8 @@ public abstract class BaseActivity
if (!isTutor) {
navigationView.findViewById(R.id.nav_profile).setVisibility(View.GONE);
}
}
protected void checkPermissions() {
final List<String> missingPermissions = new ArrayList<String>();
@ -201,9 +188,9 @@ public abstract class BaseActivity
Boolean shouldServiceRun = PrefUtils.isEnableSharingLocalization(getApplicationContext())
&& !PrefUtils.isBackgroundLocationServiceRunning(getApplicationContext());
if (shouldServiceRun){
if (shouldServiceRun) {
startBackgroundLocalizationTask();
} else if(PrefUtils.isBackgroundLocationServiceRunning(getApplicationContext()) &&
} else if (PrefUtils.isBackgroundLocationServiceRunning(getApplicationContext()) &&
!PrefUtils.isEnableSharingLocalization(getApplicationContext())) {
stopBackgroundLocalizationTask();
}
@ -263,8 +250,16 @@ public abstract class BaseActivity
MenuItem myActionMenuItem = menu.findItem(R.id.action_search);
searchView = (SearchView) myActionMenuItem.getActionView();
searchView.setOnQueryTextFocusChangeListener((v, hasFocus) -> {
if (!hasFocus && activeFragment.equals(ActiveFragment.NONE)) {
restoreMapMarkers();
}
});
RxSearchObservable.fromView(searchView)
.map(String::toLowerCase)
.filter(t -> !t.trim().isEmpty())
.debounce(300, TimeUnit.MILLISECONDS)
.distinctUntilChanged()
.subscribeOn(Schedulers.io())
@ -277,9 +272,16 @@ public abstract class BaseActivity
private void executeSearch(String input) {
if (activeFragment.equals(ActiveFragment.USER_LIST)) {
((UsersListFragment) userListFragment).searchUser(input);
} else if (activeFragment.equals(ActiveFragment.NONE)) {
searchUser(input);
}
}
public void searchUser(String textToSearch) {
}
public void restoreMapMarkers() {
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
@ -289,7 +291,6 @@ public abstract class BaseActivity
return super.onOptionsItemSelected(item);
}
@Override
protected void onStart() {
super.onStart();
@ -311,13 +312,13 @@ public abstract class BaseActivity
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
navigationView.postDelayed(() -> {
int itemId = item.getItemId();
if (itemId == R.id.nav_map) {
removeFragment(sharingFragment);
removeFragment(userListFragment);
activeFragment = ActiveFragment.NONE;
} else if (itemId == R.id.nav_profile) {
loadUserSettingsFragment();
} else if (itemId == R.id.nav_user_list) {

View File

@ -6,7 +6,6 @@ import android.animation.ValueAnimator;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.location.Location;
import android.os.Bundle;
import android.os.Handler;
@ -40,9 +39,6 @@ import com.mapbox.mapboxsdk.location.modes.RenderMode;
import com.mapbox.mapboxsdk.maps.MapView;
import com.mapbox.mapboxsdk.maps.MapboxMap;
import com.mapbox.mapboxsdk.maps.OnMapReadyCallback;
import com.mapbox.mapboxsdk.style.layers.CircleLayer;
import com.mapbox.mapboxsdk.style.layers.Layer;
import com.mapbox.mapboxsdk.style.sources.VectorSource;
import com.uam.wmi.findmytutor.R;
import com.uam.wmi.findmytutor.model.Coordinate;
import com.uam.wmi.findmytutor.model.User;
@ -51,32 +47,20 @@ import com.uam.wmi.findmytutor.service.CoordinateService;
import com.uam.wmi.findmytutor.service.UserService;
import com.uam.wmi.findmytutor.utils.PrefUtils;
import com.uam.wmi.findmytutor.utils.RestApiHelper;
import com.uam.wmi.findmytutor.utils.SharingLevel;
import com.uam.wmi.findmytutor.utils.mapUtils;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.functions.Predicate;
import io.reactivex.observers.DisposableSingleObserver;
import io.reactivex.schedulers.Schedulers;
import okhttp3.ResponseBody;
import timber.log.Timber;
import java.util.Set;
import static com.mapbox.mapboxsdk.style.layers.Property.NONE;
import static com.mapbox.mapboxsdk.style.layers.Property.VISIBLE;
import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.circleColor;
import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.circleRadius;
import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.visibility;
import static java.util.stream.Collectors.toList;
public class MapActivity extends BaseActivity
@ -105,6 +89,7 @@ public class MapActivity extends BaseActivity
private int bearingParam = 180;
private int tiltParam = 30;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
@ -137,7 +122,6 @@ public class MapActivity extends BaseActivity
handleBackgroundTaskLifeCycle();
}
@Override
public void onMapReady(MapboxMap mapboxMap) {
MapActivity.this.mapboxMap = mapboxMap;
@ -185,19 +169,17 @@ public class MapActivity extends BaseActivity
alertDialog.show();
}
private void handleError(Throwable error) {
showError(error);
}
private void showError(Throwable e) {
String message;
if (e instanceof HttpException) {
ResponseBody responseBody = ((HttpException) e).response().errorBody();
message = RestApiHelper.getErrorMessage(responseBody);
}else {
} else {
message = "Network Error!";
}
@ -312,37 +294,6 @@ public class MapActivity extends BaseActivity
}
private void addStaticLayer() {
// Toggle layer button
final FloatingActionButton button = findViewById(R.id.toggleMarkerLayerButton);
button.setVisibility(View.VISIBLE);
button.setOnClickListener(view -> {
Layer layer = mapboxMap.getLayer("museums");
if (layer != null) {
if (VISIBLE.equals(layer.getVisibility().getValue())) {
layer.setProperties(visibility(NONE));
} else {
layer.setProperties(visibility(VISIBLE));
}
}
});
// TODO here we create static layer, we are able to hide/show (but we cannot put markers inthere)
VectorSource museums = new VectorSource("museums_source", "mapbox://mapbox.2opop9hr");
mapboxMap.addSource(museums);
CircleLayer museumsLayer = new CircleLayer("museums", "museums_source");
museumsLayer.setSourceLayer("museum-cusco");
museumsLayer.setProperties(
visibility(VISIBLE),
circleRadius(8f),
circleColor(Color.argb(255, 55, 148, 179))
);
mapboxMap.addLayer(museumsLayer);
}
private void fetchTopCoords() {
disposable.add(
@ -354,44 +305,37 @@ public class MapActivity extends BaseActivity
@Override
public void onSuccess(List<Coordinate> coordsList) {
if(coordsList.isEmpty()) {
Log.e(tag, "200 empty []");
if (coordsList.isEmpty() && droppedMarker == null) {
Timber.e("200 empty []");
mapboxMap.clear();
return;
}
ArrayList<String> tmp = new ArrayList<>();
ArrayList<String> tmp = new ArrayList<>();
for (Coordinate coordinate : coordsList) {
Log.e(tag, coordinate.toString());
if (coordinate.getDisplayMode().equals(SharingLevel.PRESENCE.toString())) {
Log.e(tag, "presence user omitted");
} else {
tmp.add(coordinate.getUserId());
}
tmp.add(coordinate.getUserId());
}
Set<String> currentCoordsIds = new HashSet<>(tmp);
if (previousCoordsIds.isEmpty()){
if (previousCoordsIds.isEmpty()) {
previousCoordsIds.addAll(currentCoordsIds);
} else {
// here we clear + it returns bool if sth was removed
if (previousCoordsIds.removeAll(currentCoordsIds)) {
for (String toRemoveId: previousCoordsIds) {
Log.e(tag+ "delete: " , "removing: " + toRemoveId + ": " + markerHash.get(toRemoveId));
for (String toRemoveId : previousCoordsIds) {
Log.e(tag + "delete: ", "removing: " + toRemoveId + ": " + markerHash.get(toRemoveId));
mapboxMap.removeMarker(markerHash.get(toRemoveId));
markerHash.remove(toRemoveId);
coordsMap.remove(toRemoveId);
}
} else {
// TODO double check when some markers api will change
Log.e(tag+ "delete: ","nothing to remove");
Log.e(tag + "delete: ", "nothing to remove");
}
}
for (Coordinate element : coordsList) {
String id = element.getUserId();
String newLabel = element.getLabel();
@ -430,13 +374,16 @@ public class MapActivity extends BaseActivity
}
} else {
Log.e(tag, "Marker Added: " + id);
coordsMap.put(id, element);
Marker marker = mapboxMap.addMarker(new MarkerOptions()
MarkerOptions markerOptions = new MarkerOptions()
.title(id)
.position(new LatLng(element.getLatitude(), element.getLongitude())));
.position(new LatLng(element.getLatitude(), element.getLongitude()));
Marker marker = mapboxMap.addMarker(markerOptions);
markerHash.put(id, marker);
}
@ -603,4 +550,51 @@ public class MapActivity extends BaseActivity
// finish();
// }
}
@Override
public void searchUser(String textToSearch) {
getUserFromApi(textToSearch);
}
private void getUserFromApi(String userNameToSearch) {
disposable.add(
userService.apiUsersGet()
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.map(tutors -> Stream.of(tutors).filter(t ->
t.toSearchAbleUserName().toLowerCase().contains(userNameToSearch.toLowerCase())).toList())
.subscribeWith(new DisposableSingleObserver<List<User>>() {
@Override
public void onSuccess(List<User> users) {
filterMarkers(users);
}
@Override
public void onError(Throwable e) {
showError(e);
}
}));
}
private void filterMarkers(List<User> users) {
restoreMapMarkers();
Icon icon1 = IconFactory.getInstance(MapActivity.this).fromResource(R.drawable.custom_marker);
List<Marker> markersToSet = Stream.of(mapboxMap.getMarkers())
.filter(m -> Stream.of(users).anyMatch(u -> u.getId().equals(m.getTitle())))
.toList();
for (Marker marker : markersToSet) {
marker.setIcon(icon1);
}
}
public void restoreMapMarkers() {
Icon icon = IconFactory.getInstance(MapActivity.this).fromResource(R.drawable.red_marker);
for (Marker marker : mapboxMap.getMarkers()) {
marker.setIcon(icon);
}
}
}

View File

@ -712,6 +712,15 @@ public class User extends BaseResponse {
return sb.toString();
}
public String toSearchAbleUserName(){
StringBuilder sb = new StringBuilder();
sb.append(getFirstName());
sb.append(getLastName());
return sb.toString();
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();

View File

@ -1,7 +1,7 @@
package com.uam.wmi.findmytutor.utils;
public class Consts {
public final static Double presenceLatitude = 52.467491;
public final static Double presencelongitude = 16.926782;
public final static Double presenceLatitude = 65.600244;
public final static Double presencelongitude = 480.032153;
public final static String presenceApproximatedName = "unknown";
}

View File

@ -0,0 +1,23 @@
package com.uam.wmi.findmytutor.utils;
import android.support.annotation.NonNull;
import com.mapbox.mapboxsdk.annotations.InfoWindow;
import com.mapbox.mapboxsdk.annotations.Marker;
import com.mapbox.mapboxsdk.annotations.MarkerOptions;
import com.mapbox.mapboxsdk.maps.MapView;
public class MapboxMarker extends Marker {
public MapboxMarker( MarkerOptions markerOptions ){
super(markerOptions);
}
/*
@NonNull
private InfoWindow showInfoWindow(InfoWindow iw, MapView mapView) {
iw.open(mapView, this, this.getPosition(), 0, 0);
this.infoWindowShown = true;
return iw;
}*/
}

View File

@ -29,8 +29,11 @@ public class RxSearchObservable {
subject.onNext(text);
return false;
}
});
return subject;
}
}

Binary file not shown.

Before

Width:  |  Height:  |  Size: 923 B

After

Width:  |  Height:  |  Size: 444 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 3.1 KiB

After

Width:  |  Height:  |  Size: 1.3 KiB