Skip to content

Commit

Permalink
Fixes issues commons-app#1228 added recenter button to nearby map
Browse files Browse the repository at this point in the history
  • Loading branch information
Jatin0312 authored and maskara committed Mar 21, 2018
1 parent fc91fb8 commit b7b87e0
Show file tree
Hide file tree
Showing 5 changed files with 323 additions and 86 deletions.
99 changes: 63 additions & 36 deletions app/src/main/java/fr/free/nrw/commons/nearby/NearbyMapFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import android.animation.ObjectAnimator;
import android.animation.TypeEvaluator;
import android.animation.ValueAnimator;

import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
Expand All @@ -15,8 +14,6 @@
import android.support.design.widget.BottomSheetBehavior;
import android.support.design.widget.CoordinatorLayout;
import android.support.design.widget.FloatingActionButton;

import android.util.Log;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
Expand Down Expand Up @@ -82,6 +79,7 @@ public class NearbyMapFragment extends DaggerFragment {
private FloatingActionButton fabPlus;
private FloatingActionButton fabCamera;
private FloatingActionButton fabGallery;
private FloatingActionButton fabRecenter;
private View transparentView;
private TextView description;
private TextView title;
Expand All @@ -93,7 +91,7 @@ public class NearbyMapFragment extends DaggerFragment {
private TextView commonsButtonText;
private TextView directionsButtonText;

private boolean isFabOpen=false;
private boolean isFabOpen = false;
private Animation rotate_backward;
private Animation fab_close;
private Animation fab_open;
Expand All @@ -109,8 +107,12 @@ public class NearbyMapFragment extends DaggerFragment {
private boolean isBottomListSheetExpanded;
private final double CAMERA_TARGET_SHIFT_FACTOR = 0.06;

@Inject @Named("prefs") SharedPreferences prefs;
@Inject @Named("direct_nearby_upload_prefs") SharedPreferences directPrefs;
@Inject
@Named("prefs")
SharedPreferences prefs;
@Inject
@Named("direct_nearby_upload_prefs")
SharedPreferences directPrefs;

public NearbyMapFragment() {
}
Expand All @@ -125,10 +127,12 @@ public void onCreate(Bundle savedInstanceState) {
if (bundle != null) {
String gsonPlaceList = bundle.getString("PlaceList");
String gsonLatLng = bundle.getString("CurLatLng");
Type listType = new TypeToken<List<Place>>() {
}.getType();
String gsonBoundaryCoordinates = bundle.getString("BoundaryCoord");
Type listType = new TypeToken<List<Place>>() {}.getType();
List<Place> placeList = gson.fromJson(gsonPlaceList, listType);
Type curLatLngType = new TypeToken<fr.free.nrw.commons.location.LatLng>() {}.getType();
Type curLatLngType = new TypeToken<fr.free.nrw.commons.location.LatLng>() {
}.getType();
Type gsonBoundaryCoordinatesType = new TypeToken<fr.free.nrw.commons.location.LatLng[]>() {}.getType();
curLatLng = gson.fromJson(gsonLatLng, curLatLngType);
baseMarkerOptions = NearbyController
Expand Down Expand Up @@ -163,16 +167,15 @@ public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
this.getView().requestFocus();
this.getView().setOnKeyListener((v, keyCode, event) -> {
if (keyCode == KeyEvent.KEYCODE_BACK) {
if(bottomSheetDetailsBehavior.getState() == BottomSheetBehavior
if (bottomSheetDetailsBehavior.getState() == BottomSheetBehavior
.STATE_EXPANDED) {
bottomSheetDetailsBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED);
return true;
}
else if (bottomSheetDetailsBehavior.getState() == BottomSheetBehavior
} else if (bottomSheetDetailsBehavior.getState() == BottomSheetBehavior
.STATE_COLLAPSED) {
bottomSheetDetailsBehavior.setState(BottomSheetBehavior.STATE_HIDDEN);
mapView.getMapAsync(MapboxMap::deselectMarkers);
selected=null;
selected = null;
return true;
}
}
Expand Down Expand Up @@ -301,11 +304,12 @@ private void initViews() {
fabPlus = getActivity().findViewById(R.id.fab_plus);
fabCamera = getActivity().findViewById(R.id.fab_camera);
fabGallery = getActivity().findViewById(R.id.fab_galery);
fabRecenter = getActivity().findViewById(R.id.fab_recenter);

fab_open = AnimationUtils.loadAnimation(getActivity(), R.anim.fab_open);
fab_close = AnimationUtils.loadAnimation(getActivity(),R.anim.fab_close);
rotate_forward = AnimationUtils.loadAnimation(getActivity(),R.anim.rotate_forward);
rotate_backward = AnimationUtils.loadAnimation(getActivity(),R.anim.rotate_backward);
fab_close = AnimationUtils.loadAnimation(getActivity(), R.anim.fab_close);
rotate_forward = AnimationUtils.loadAnimation(getActivity(), R.anim.rotate_forward);
rotate_backward = AnimationUtils.loadAnimation(getActivity(), R.anim.rotate_backward);

transparentView = getActivity().findViewById(R.id.transparentView);

Expand All @@ -330,14 +334,28 @@ private void setListeners() {
fabPlus.setOnClickListener(view -> animateFAB(isFabOpen));

bottomSheetDetails.setOnClickListener(view -> {
if(bottomSheetDetailsBehavior.getState() == BottomSheetBehavior.STATE_COLLAPSED) {
if (bottomSheetDetailsBehavior.getState() == BottomSheetBehavior.STATE_COLLAPSED) {
bottomSheetDetailsBehavior.setState(BottomSheetBehavior.STATE_EXPANDED);
}
else{
} else {
bottomSheetDetailsBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED);
}
});

fabRecenter.setOnClickListener(view -> {
if (curLatLng != null) {
mapView.getMapAsync(mapboxMap -> {
CameraPosition position = new CameraPosition.Builder()
.target(new LatLng(curLatLng.getLatitude(), curLatLng.getLongitude())) // Sets the new camera position
.zoom(11) // Sets the zoom
.build(); // Creates a CameraPosition from the builder

mapboxMap.animateCamera(CameraUpdateFactory
.newCameraPosition(position), 1000);

});
}
});

bottomSheetDetailsBehavior.setBottomSheetCallback(new BottomSheetBehavior
.BottomSheetCallback() {
@Override
Expand All @@ -351,7 +369,7 @@ public void onSlide(@NonNull View bottomSheet, float slideOffset) {
transparentView.setAlpha(slideOffset);
if (slideOffset == 1) {
transparentView.setClickable(true);
} else if (slideOffset == 0){
} else if (slideOffset == 0) {
transparentView.setClickable(false);
}
}
Expand All @@ -362,7 +380,7 @@ public void onSlide(@NonNull View bottomSheet, float slideOffset) {
.BottomSheetCallback() {
@Override
public void onStateChanged(@NonNull View bottomSheet, int newState) {
if (newState == BottomSheetBehavior.STATE_EXPANDED){
if (newState == BottomSheetBehavior.STATE_EXPANDED) {
bottomSheetDetailsBehavior.setState(BottomSheetBehavior.STATE_HIDDEN);
updateMapCameraAccordingToBottomSheet(true);
} else {
Expand Down Expand Up @@ -448,27 +466,35 @@ private void addNearbyMarkerstoMapBoxMap() {

mapboxMap.addMarkers(baseMarkerOptions);
mapboxMap.setOnInfoWindowCloseListener(marker -> {
if (marker == selected){
if (marker == selected) {
bottomSheetDetailsBehavior.setState(BottomSheetBehavior.STATE_HIDDEN);
}
});
mapView.getMapAsync(mapboxMap -> {
mapboxMap.addMarkers(baseMarkerOptions);
fabRecenter.setVisibility(View.VISIBLE);
mapboxMap.setOnInfoWindowCloseListener(marker -> {
if (marker == selected) {
bottomSheetDetailsBehavior.setState(BottomSheetBehavior.STATE_HIDDEN);
}
});

mapboxMap.setOnMarkerClickListener(marker -> {
if (marker instanceof NearbyMarker) {
this.selected = marker;
NearbyMarker nearbyMarker = (NearbyMarker) marker;
Place place = nearbyMarker.getNearbyBaseMarker().getPlace();
passInfoToSheet(place);
bottomSheetListBehavior.setState(BottomSheetBehavior.STATE_HIDDEN);
bottomSheetDetailsBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED);
}
return false;
});

mapboxMap.setOnMarkerClickListener(marker -> {
if (marker instanceof NearbyMarker) {
this.selected = marker;
NearbyMarker nearbyMarker = (NearbyMarker) marker;
Place place = nearbyMarker.getNearbyBaseMarker().getPlace();
passInfoToSheet(place);
bottomSheetListBehavior.setState(BottomSheetBehavior.STATE_HIDDEN);
bottomSheetDetailsBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED);
}
return false;
});

}



/**
* Creates a series of points that create a circle on the map.
* Takes the center latitude, center longitude of the circle,
Expand Down Expand Up @@ -653,7 +679,7 @@ public void onRequestPermissionsResult(int requestCode, @NonNull String[] permis
}
}
break;

// 3 = "Write external storage" allowed when camera selected
case 3: {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Expand Down Expand Up @@ -684,6 +710,7 @@ private void openWebView(Uri link) {
}

private void animateFAB(boolean isFabOpen) {
this.isFabOpen = !isFabOpen;
if (fabPlus.isShown()){
if (isFabOpen) {
fabPlus.startAnimation(rotate_backward);
Expand All @@ -702,14 +729,14 @@ private void animateFAB(boolean isFabOpen) {
}
}

private void closeFabs(boolean isFabOpen){
private void closeFabs ( boolean isFabOpen){
if (isFabOpen) {
fabPlus.startAnimation(rotate_backward);
fabCamera.startAnimation(fab_close);
fabGallery.startAnimation(fab_close);
fabCamera.hide();
fabGallery.hide();
this.isFabOpen=!isFabOpen;
this.isFabOpen = !isFabOpen;
}
}

Expand Down
9 changes: 9 additions & 0 deletions app/src/main/res/drawable/ic_my_location_black_24dp.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24.0"
android:viewportHeight="24.0">
<path
android:fillColor="#FF000000"
android:pathData="M12,8c-2.21,0 -4,1.79 -4,4s1.79,4 4,4 4,-1.79 4,-4 -1.79,-4 -4,-4zM20.94,11c-0.46,-4.17 -3.77,-7.48 -7.94,-7.94L13,1h-2v2.06C6.83,3.52 3.52,6.83 3.06,11L1,11v2h2.06c0.46,4.17 3.77,7.48 7.94,7.94L11,23h2v-2.06c4.17,-0.46 7.48,-3.77 7.94,-7.94L23,13v-2h-2.06zM12,19c-3.87,0 -7,-3.13 -7,-7s3.13,-7 7,-7 7,3.13 7,7 -3.13,7 -7,7z"/>
</vector>
Loading

0 comments on commit b7b87e0

Please sign in to comment.