cancel button improved

handle runnable for tmp sending manual location
This commit is contained in:
Adam Domagalski 2018-11-20 22:56:30 +01:00
parent e1496ce65c
commit 70001e36c3
3 changed files with 80 additions and 69 deletions

View File

@ -94,9 +94,13 @@ public class MapActivity extends BaseActivity
private int mInterval = 10000; private int mInterval = 10000;
private Handler mHandler = new Handler(); private Handler mHandler = new Handler();
private Runnable mStatusChecker; private Runnable mStatusChecker;
private Handler manualLocHandler = new Handler();
private Runnable manualLocStatusChecker;
private MapView mapView; private MapView mapView;
private MapboxMap mapboxMap; private MapboxMap mapboxMap;
private Button selectLocationButton;
private Marker droppedMarker; private Marker droppedMarker;
private Marker tmpLocalMarker;
private Coordinate droppedMarkercoordinate; private Coordinate droppedMarkercoordinate;
private HashMap<String, Coordinate> coordsMap = new HashMap<>(); private HashMap<String, Coordinate> coordsMap = new HashMap<>();
private HashMap<String, Marker> markerHash = new HashMap<>(); private HashMap<String, Marker> markerHash = new HashMap<>();
@ -129,8 +133,10 @@ public class MapActivity extends BaseActivity
} }
}; };
Bundle extras = getIntent().getExtras(); Bundle extras = getIntent().getExtras();
selectLocationButton = findViewById(R.id.select_location_button);
Mapbox.getInstance(this, getString(R.string.access_token)); Mapbox.getInstance(this, getString(R.string.access_token));
mapView = findViewById(R.id.mapView); mapView = findViewById(R.id.mapView);
@ -147,8 +153,25 @@ public class MapActivity extends BaseActivity
MapActivity.this.mapboxMap = mapboxMap; MapActivity.this.mapboxMap = mapboxMap;
mStatusChecker.run(); mStatusChecker.run();
enableLocationPlugin(); enableLocationPlugin();
mapboxMap.setOnMarkerClickListener(marker -> { mapboxMap.setOnMarkerClickListener(marker -> {
createMarkerModal(marker.getTitle()); String id = marker.getTitle();
if (id.equals(myID)) {
Log.e(tag+"Manual", "my own marker from API");
Button rmButton = findViewById(R.id.remove_location_button);
rmButton.setVisibility(View.VISIBLE);
rmButton.setOnClickListener(view -> {
Log.e(tag+"Manual", "manual coords sending stopped");
// TODO to remove after BGserv
manualLocHandler.removeCallbacks(manualLocStatusChecker);
rmButton.setVisibility(View.GONE);
});
} else {
createMarkerModal(id);
}
return true; return true;
}); });
@ -210,34 +233,28 @@ public class MapActivity extends BaseActivity
// Manual loc selection on long tap // Manual loc selection on long tap
private void setOnMapLongClickListener() { private void setOnMapLongClickListener() {
final boolean[] cancel = {false};
Button selectLocationButton = findViewById(R.id.select_location_button);
mapboxMap.addOnMapLongClickListener((LatLng latLng) -> { mapboxMap.addOnMapLongClickListener((LatLng latLng) -> {
selectLocationButton.setVisibility(View.VISIBLE); selectLocationButton.setVisibility(View.VISIBLE);
Icon icon = IconFactory.getInstance(MapActivity.this).fromResource(R.drawable.green_marker); Icon icon = IconFactory.getInstance(MapActivity.this).fromResource(R.drawable.green_marker);
if (droppedMarker == null) { if (tmpLocalMarker == null) {
droppedMarker = mapboxMap.addMarker(new MarkerOptions() tmpLocalMarker = mapboxMap.addMarker(new MarkerOptions()
.position(latLng) .position(latLng)
.icon(icon) .icon(icon)
.title("My Loc") .title("My Loc")
.setSnippet("Snipecik")); .setSnippet("Snipecik"));
} else if (!cancel[0]) { } else {
ValueAnimator markerAnimator = ObjectAnimator.ofObject(droppedMarker, "position", ValueAnimator markerAnimator = ObjectAnimator.ofObject(tmpLocalMarker, "position",
new mapUtils.LatLngEvaluator(), droppedMarker.getPosition(), latLng); new mapUtils.LatLngEvaluator(), tmpLocalMarker.getPosition(), latLng);
markerAnimator.setDuration(2000); markerAnimator.setDuration(2000);
markerAnimator.start(); markerAnimator.start();
} }
selectLocationButton.setOnClickListener((View view) -> { selectLocationButton.setOnClickListener((View view) -> {
if (tmpLocalMarker != null) {
if (!cancel[0] && droppedMarker != null) {
// Toast instructing user to tap on the mapboxMap // Toast instructing user to tap on the mapboxMap
// TODO PUT MANUAL CORD // TODO PUT MANUAL CORD
try { try {
droppedMarkercoordinate = new Coordinate( droppedMarkercoordinate = new Coordinate(
@ -250,68 +267,57 @@ public class MapActivity extends BaseActivity
PrefUtils.getLocationLevel(getApplicationContext()) PrefUtils.getLocationLevel(getApplicationContext())
); );
Log.e(tag, "tutajMANUALid: " + PrefUtils.getUserId(getApplicationContext())); // TODO remove after BG sending
Log.e(tag, "tutajMANUALid: " + myID); manualLocStatusChecker = () -> {
try {
Log.e(tag+"Manual", "sending manual coords");
// TODO ^^^ wrapper to removve
disposable.add(
coordinateService
.postCoordinate(droppedMarkercoordinate)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribeWith(new DisposableSingleObserver<Coordinate>() {
@SuppressLint("LongLogTag")
@Override
public void onSuccess(Coordinate coord) {
Log.e(tag + "POST", String.valueOf(coord));
}
disposable.add( @SuppressLint("LongLogTag")
coordinateService @Override
.postCoordinate(droppedMarkercoordinate) public void onError(Throwable e) {
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribeWith(new DisposableSingleObserver<Coordinate>() {
@SuppressLint("LongLogTag")
@Override
public void onSuccess(Coordinate coord) {
Log.e(tag + "POST", String.valueOf(coord));
}
@SuppressLint("LongLogTag") Log.e(tag + "onError", e.getMessage());
@Override
public void onError(Throwable e) {
Log.e(tag + "onError", e.getMessage()); if (e instanceof HttpException) {
ResponseBody responseBody = ((HttpException) e).response().errorBody();
Log.e(tag + "onError", RestApiHelper.getErrorMessage(responseBody));
}
}
}));
// TODO \/\/\/\/\/ wrapper to removve
} finally {
manualLocHandler.postDelayed(manualLocStatusChecker, mInterval);
}
};
manualLocStatusChecker.run();
if (e instanceof HttpException) {
ResponseBody responseBody = ((HttpException) e).response().errorBody();
Log.e(tag + "onError", RestApiHelper.getErrorMessage(responseBody));
}
}
}));
} catch (IllegalArgumentException e) { } catch (IllegalArgumentException e) {
Timber.e(String.valueOf(e)); Timber.e(String.valueOf(e));
} }
Toast.makeText( Toast.makeText(
MapActivity.this, MapActivity.this,
"Manual Locations selected!" + latLng, "Manual Locations selected!" + latLng,
Toast.LENGTH_LONG Toast.LENGTH_LONG
).show(); ).show();
selectLocationButton.setBackgroundColor(
ContextCompat.getColor(MapActivity.this, R.color.colorAccent));
selectLocationButton.setText("Remove Manual location");
selectLocationButton.setVisibility(View.VISIBLE);
cancel[0] = true;
} else {
// TODO REMOVE Manual Location
mapboxMap.removeMarker(droppedMarker);
droppedMarker = null;
droppedMarkercoordinate = null;
Toast.makeText(
MapActivity.this,
"REMOVED!!" + latLng,
Toast.LENGTH_LONG
).show();
cancel[0] = false;
selectLocationButton.setVisibility(View.GONE); selectLocationButton.setVisibility(View.GONE);
selectLocationButton.setText(R.string.select_a_location); mapboxMap.removeMarker(tmpLocalMarker);
selectLocationButton.setBackgroundColor( tmpLocalMarker = null;
ContextCompat.getColor(MapActivity.this, R.color.colorPrimary));
} }
}); });
}); });
@ -362,18 +368,12 @@ public class MapActivity extends BaseActivity
@Override @Override
public void onSuccess(List<Coordinate> coordsList) { public void onSuccess(List<Coordinate> coordsList) {
if (droppedMarker != null) { if (coordsList.isEmpty()) {
Timber.e("coordsList.add(droppedMarkercoordinate);");
coordsList.add(droppedMarkercoordinate);
}
if (coordsList.isEmpty() && droppedMarker == null) {
Timber.e("200 empty []"); Timber.e("200 empty []");
mapboxMap.clear(); mapboxMap.clear();
return; return;
} }
ArrayList<String> tmp = new ArrayList<>(); ArrayList<String> tmp = new ArrayList<>();
for (Coordinate coordinate : coordsList) { for (Coordinate coordinate : coordsList) {
tmp.add(coordinate.getUserId()); tmp.add(coordinate.getUserId());
@ -408,9 +408,6 @@ public class MapActivity extends BaseActivity
if (coordinate != null) { if (coordinate != null) {
Log.e(tag, "Coordin: " + coordinate.getLatitude() + " | " + coordinate.getLongitude()); Log.e(tag, "Coordin: " + coordinate.getLatitude() + " | " + coordinate.getLongitude());
// Log.e(tag, "Element: " + element.getLatitude()+" | " + element.getLongitude());
// Log.e(tag, "isEqual: " + coordinate.getLatitude().equals(element.getLatitude()));
// Log.e(tag, "isEqual: " + coordinate.getLongitude().equals(element.getLongitude()));
boolean statement = coordinate.getLatitude().equals(element.getLatitude()) || coordinate.getLongitude().equals(element.getLongitude()); boolean statement = coordinate.getLatitude().equals(element.getLatitude()) || coordinate.getLongitude().equals(element.getLongitude());
Log.e(tag, "diff || diff: " + !statement); Log.e(tag, "diff || diff: " + !statement);
@ -576,6 +573,8 @@ public class MapActivity extends BaseActivity
super.onDestroy(); super.onDestroy();
mapView.onDestroy(); mapView.onDestroy();
mHandler.removeCallbacks(mStatusChecker); mHandler.removeCallbacks(mStatusChecker);
// TODO remove after BG sending
manualLocHandler.removeCallbacks(manualLocStatusChecker);
disposable.dispose(); disposable.dispose();
} }

View File

@ -111,6 +111,17 @@
android:textColor="@android:color/white" android:textColor="@android:color/white"
android:visibility="gone" /> android:visibility="gone" />
<Button
android:id="@+id/remove_location_button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_margin="8dp"
android:background="@color/colorAccent"
android:text="@string/remove_manual_location"
android:textColor="@android:color/white"
android:visibility="gone" />
</LinearLayout> </LinearLayout>
</android.support.constraint.ConstraintLayout> </android.support.constraint.ConstraintLayout>

View File

@ -195,5 +195,6 @@
<string name="launch_activity" /> <string name="launch_activity" />
<string name="remove_location_updates" /> <string name="remove_location_updates" />
<string name="map_activity_label">MapActivity</string> <string name="map_activity_label">MapActivity</string>
<string name="remove_manual_location">Remove Manual location</string>
</resources> </resources>