Skip to content

Commit b7b87e0

Browse files
Jatin0312maskara
authored and
maskara
committed
Fixes issues commons-app#1228 added recenter button to nearby map
1 parent fc91fb8 commit b7b87e0

File tree

5 files changed

+323
-86
lines changed

5 files changed

+323
-86
lines changed

app/src/main/java/fr/free/nrw/commons/nearby/NearbyMapFragment.java

+63-36
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import android.animation.ObjectAnimator;
44
import android.animation.TypeEvaluator;
55
import android.animation.ValueAnimator;
6-
76
import android.content.Intent;
87
import android.content.SharedPreferences;
98
import android.content.pm.PackageManager;
@@ -15,8 +14,6 @@
1514
import android.support.design.widget.BottomSheetBehavior;
1615
import android.support.design.widget.CoordinatorLayout;
1716
import android.support.design.widget.FloatingActionButton;
18-
19-
import android.util.Log;
2017
import android.view.Gravity;
2118
import android.view.KeyEvent;
2219
import android.view.LayoutInflater;
@@ -82,6 +79,7 @@ public class NearbyMapFragment extends DaggerFragment {
8279
private FloatingActionButton fabPlus;
8380
private FloatingActionButton fabCamera;
8481
private FloatingActionButton fabGallery;
82+
private FloatingActionButton fabRecenter;
8583
private View transparentView;
8684
private TextView description;
8785
private TextView title;
@@ -93,7 +91,7 @@ public class NearbyMapFragment extends DaggerFragment {
9391
private TextView commonsButtonText;
9492
private TextView directionsButtonText;
9593

96-
private boolean isFabOpen=false;
94+
private boolean isFabOpen = false;
9795
private Animation rotate_backward;
9896
private Animation fab_close;
9997
private Animation fab_open;
@@ -109,8 +107,12 @@ public class NearbyMapFragment extends DaggerFragment {
109107
private boolean isBottomListSheetExpanded;
110108
private final double CAMERA_TARGET_SHIFT_FACTOR = 0.06;
111109

112-
@Inject @Named("prefs") SharedPreferences prefs;
113-
@Inject @Named("direct_nearby_upload_prefs") SharedPreferences directPrefs;
110+
@Inject
111+
@Named("prefs")
112+
SharedPreferences prefs;
113+
@Inject
114+
@Named("direct_nearby_upload_prefs")
115+
SharedPreferences directPrefs;
114116

115117
public NearbyMapFragment() {
116118
}
@@ -125,10 +127,12 @@ public void onCreate(Bundle savedInstanceState) {
125127
if (bundle != null) {
126128
String gsonPlaceList = bundle.getString("PlaceList");
127129
String gsonLatLng = bundle.getString("CurLatLng");
130+
Type listType = new TypeToken<List<Place>>() {
131+
}.getType();
128132
String gsonBoundaryCoordinates = bundle.getString("BoundaryCoord");
129-
Type listType = new TypeToken<List<Place>>() {}.getType();
130133
List<Place> placeList = gson.fromJson(gsonPlaceList, listType);
131-
Type curLatLngType = new TypeToken<fr.free.nrw.commons.location.LatLng>() {}.getType();
134+
Type curLatLngType = new TypeToken<fr.free.nrw.commons.location.LatLng>() {
135+
}.getType();
132136
Type gsonBoundaryCoordinatesType = new TypeToken<fr.free.nrw.commons.location.LatLng[]>() {}.getType();
133137
curLatLng = gson.fromJson(gsonLatLng, curLatLngType);
134138
baseMarkerOptions = NearbyController
@@ -163,16 +167,15 @@ public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
163167
this.getView().requestFocus();
164168
this.getView().setOnKeyListener((v, keyCode, event) -> {
165169
if (keyCode == KeyEvent.KEYCODE_BACK) {
166-
if(bottomSheetDetailsBehavior.getState() == BottomSheetBehavior
170+
if (bottomSheetDetailsBehavior.getState() == BottomSheetBehavior
167171
.STATE_EXPANDED) {
168172
bottomSheetDetailsBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED);
169173
return true;
170-
}
171-
else if (bottomSheetDetailsBehavior.getState() == BottomSheetBehavior
174+
} else if (bottomSheetDetailsBehavior.getState() == BottomSheetBehavior
172175
.STATE_COLLAPSED) {
173176
bottomSheetDetailsBehavior.setState(BottomSheetBehavior.STATE_HIDDEN);
174177
mapView.getMapAsync(MapboxMap::deselectMarkers);
175-
selected=null;
178+
selected = null;
176179
return true;
177180
}
178181
}
@@ -301,11 +304,12 @@ private void initViews() {
301304
fabPlus = getActivity().findViewById(R.id.fab_plus);
302305
fabCamera = getActivity().findViewById(R.id.fab_camera);
303306
fabGallery = getActivity().findViewById(R.id.fab_galery);
307+
fabRecenter = getActivity().findViewById(R.id.fab_recenter);
304308

305309
fab_open = AnimationUtils.loadAnimation(getActivity(), R.anim.fab_open);
306-
fab_close = AnimationUtils.loadAnimation(getActivity(),R.anim.fab_close);
307-
rotate_forward = AnimationUtils.loadAnimation(getActivity(),R.anim.rotate_forward);
308-
rotate_backward = AnimationUtils.loadAnimation(getActivity(),R.anim.rotate_backward);
310+
fab_close = AnimationUtils.loadAnimation(getActivity(), R.anim.fab_close);
311+
rotate_forward = AnimationUtils.loadAnimation(getActivity(), R.anim.rotate_forward);
312+
rotate_backward = AnimationUtils.loadAnimation(getActivity(), R.anim.rotate_backward);
309313

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

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

332336
bottomSheetDetails.setOnClickListener(view -> {
333-
if(bottomSheetDetailsBehavior.getState() == BottomSheetBehavior.STATE_COLLAPSED) {
337+
if (bottomSheetDetailsBehavior.getState() == BottomSheetBehavior.STATE_COLLAPSED) {
334338
bottomSheetDetailsBehavior.setState(BottomSheetBehavior.STATE_EXPANDED);
335-
}
336-
else{
339+
} else {
337340
bottomSheetDetailsBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED);
338341
}
339342
});
340343

344+
fabRecenter.setOnClickListener(view -> {
345+
if (curLatLng != null) {
346+
mapView.getMapAsync(mapboxMap -> {
347+
CameraPosition position = new CameraPosition.Builder()
348+
.target(new LatLng(curLatLng.getLatitude(), curLatLng.getLongitude())) // Sets the new camera position
349+
.zoom(11) // Sets the zoom
350+
.build(); // Creates a CameraPosition from the builder
351+
352+
mapboxMap.animateCamera(CameraUpdateFactory
353+
.newCameraPosition(position), 1000);
354+
355+
});
356+
}
357+
});
358+
341359
bottomSheetDetailsBehavior.setBottomSheetCallback(new BottomSheetBehavior
342360
.BottomSheetCallback() {
343361
@Override
@@ -351,7 +369,7 @@ public void onSlide(@NonNull View bottomSheet, float slideOffset) {
351369
transparentView.setAlpha(slideOffset);
352370
if (slideOffset == 1) {
353371
transparentView.setClickable(true);
354-
} else if (slideOffset == 0){
372+
} else if (slideOffset == 0) {
355373
transparentView.setClickable(false);
356374
}
357375
}
@@ -362,7 +380,7 @@ public void onSlide(@NonNull View bottomSheet, float slideOffset) {
362380
.BottomSheetCallback() {
363381
@Override
364382
public void onStateChanged(@NonNull View bottomSheet, int newState) {
365-
if (newState == BottomSheetBehavior.STATE_EXPANDED){
383+
if (newState == BottomSheetBehavior.STATE_EXPANDED) {
366384
bottomSheetDetailsBehavior.setState(BottomSheetBehavior.STATE_HIDDEN);
367385
updateMapCameraAccordingToBottomSheet(true);
368386
} else {
@@ -448,27 +466,35 @@ private void addNearbyMarkerstoMapBoxMap() {
448466

449467
mapboxMap.addMarkers(baseMarkerOptions);
450468
mapboxMap.setOnInfoWindowCloseListener(marker -> {
451-
if (marker == selected){
469+
if (marker == selected) {
452470
bottomSheetDetailsBehavior.setState(BottomSheetBehavior.STATE_HIDDEN);
453471
}
454472
});
473+
mapView.getMapAsync(mapboxMap -> {
474+
mapboxMap.addMarkers(baseMarkerOptions);
475+
fabRecenter.setVisibility(View.VISIBLE);
476+
mapboxMap.setOnInfoWindowCloseListener(marker -> {
477+
if (marker == selected) {
478+
bottomSheetDetailsBehavior.setState(BottomSheetBehavior.STATE_HIDDEN);
479+
}
480+
});
481+
482+
mapboxMap.setOnMarkerClickListener(marker -> {
483+
if (marker instanceof NearbyMarker) {
484+
this.selected = marker;
485+
NearbyMarker nearbyMarker = (NearbyMarker) marker;
486+
Place place = nearbyMarker.getNearbyBaseMarker().getPlace();
487+
passInfoToSheet(place);
488+
bottomSheetListBehavior.setState(BottomSheetBehavior.STATE_HIDDEN);
489+
bottomSheetDetailsBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED);
490+
}
491+
return false;
492+
});
455493

456-
mapboxMap.setOnMarkerClickListener(marker -> {
457-
if (marker instanceof NearbyMarker) {
458-
this.selected = marker;
459-
NearbyMarker nearbyMarker = (NearbyMarker) marker;
460-
Place place = nearbyMarker.getNearbyBaseMarker().getPlace();
461-
passInfoToSheet(place);
462-
bottomSheetListBehavior.setState(BottomSheetBehavior.STATE_HIDDEN);
463-
bottomSheetDetailsBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED);
464-
}
465-
return false;
466494
});
467-
468495
}
469496

470497

471-
472498
/**
473499
* Creates a series of points that create a circle on the map.
474500
* Takes the center latitude, center longitude of the circle,
@@ -653,7 +679,7 @@ public void onRequestPermissionsResult(int requestCode, @NonNull String[] permis
653679
}
654680
}
655681
break;
656-
682+
657683
// 3 = "Write external storage" allowed when camera selected
658684
case 3: {
659685
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
@@ -684,6 +710,7 @@ private void openWebView(Uri link) {
684710
}
685711

686712
private void animateFAB(boolean isFabOpen) {
713+
this.isFabOpen = !isFabOpen;
687714
if (fabPlus.isShown()){
688715
if (isFabOpen) {
689716
fabPlus.startAnimation(rotate_backward);
@@ -702,14 +729,14 @@ private void animateFAB(boolean isFabOpen) {
702729
}
703730
}
704731

705-
private void closeFabs(boolean isFabOpen){
732+
private void closeFabs ( boolean isFabOpen){
706733
if (isFabOpen) {
707734
fabPlus.startAnimation(rotate_backward);
708735
fabCamera.startAnimation(fab_close);
709736
fabGallery.startAnimation(fab_close);
710737
fabCamera.hide();
711738
fabGallery.hide();
712-
this.isFabOpen=!isFabOpen;
739+
this.isFabOpen = !isFabOpen;
713740
}
714741
}
715742

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<vector xmlns:android="http://schemas.android.com/apk/res/android"
2+
android:width="24dp"
3+
android:height="24dp"
4+
android:viewportWidth="24.0"
5+
android:viewportHeight="24.0">
6+
<path
7+
android:fillColor="#FF000000"
8+
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"/>
9+
</vector>

0 commit comments

Comments
 (0)