From 32db7b3239378098dda16fa10aa9140708cab422 Mon Sep 17 00:00:00 2001 From: neslihanturan Date: Fri, 6 Sep 2019 17:05:03 +0300 Subject: [PATCH] Implement map marker click actions --- .../nearby/NearbyTestLayersFragment.java | 130 +++++++++++++++++- .../commons/nearby/SupportMapFragment.java | 89 ++++++++---- .../mvp/contract/NearbyMapContract.java | 6 +- .../NearbyParentFragmentContract.java | 4 + .../mvp/fragments/NearbyParentFragment.java | 18 ++- .../NearbyParentFragmentPresenter.java | 17 ++- 6 files changed, 232 insertions(+), 32 deletions(-) diff --git a/app/src/main/java/fr/free/nrw/commons/nearby/NearbyTestLayersFragment.java b/app/src/main/java/fr/free/nrw/commons/nearby/NearbyTestLayersFragment.java index 4d1c042ff..d9b0b1d97 100644 --- a/app/src/main/java/fr/free/nrw/commons/nearby/NearbyTestLayersFragment.java +++ b/app/src/main/java/fr/free/nrw/commons/nearby/NearbyTestLayersFragment.java @@ -14,7 +14,10 @@ import android.view.View; import android.view.ViewGroup; import android.view.animation.Animation; import android.view.animation.AnimationUtils; +import android.widget.ImageView; +import android.widget.LinearLayout; import android.widget.ProgressBar; +import android.widget.TextView; import androidx.annotation.NonNull; import androidx.annotation.Nullable; @@ -25,6 +28,7 @@ import com.google.android.material.bottomsheet.BottomSheetBehavior; import com.google.android.material.floatingactionbutton.FloatingActionButton; import com.google.android.material.snackbar.Snackbar; import com.mapbox.mapboxsdk.Mapbox; +import com.mapbox.mapboxsdk.annotations.Marker; import com.mapbox.mapboxsdk.camera.CameraPosition; import com.mapbox.mapboxsdk.camera.CameraUpdateFactory; import com.mapbox.mapboxsdk.geometry.LatLng; @@ -40,6 +44,9 @@ import butterknife.BindView; import butterknife.ButterKnife; import fr.free.nrw.commons.CommonsApplication; import fr.free.nrw.commons.R; +import fr.free.nrw.commons.Utils; +import fr.free.nrw.commons.bookmarks.locations.BookmarkLocationsDao; +import fr.free.nrw.commons.contributions.ContributionController; import fr.free.nrw.commons.contributions.MainActivity; import fr.free.nrw.commons.di.CommonsDaggerSupportFragment; import fr.free.nrw.commons.kvstore.JsonKvStore; @@ -83,6 +90,36 @@ public class NearbyTestLayersFragment extends CommonsDaggerSupportFragment imple @BindView(R.id.bottom_sheet_details) View bottomSheetDetails; + @BindView(R.id.bookmarkButtonImage) + ImageView bookmarkButtonImage; + + @BindView(R.id.bookmarkButton) + LinearLayout bookmarkButton; + + @BindView(R.id.wikipediaButton) + LinearLayout wikipediaButton; + + @BindView(R.id.wikidataButton) + LinearLayout wikidataButton; + + @BindView(R.id.directionsButton) + LinearLayout directionsButton; + + @BindView(R.id.commonsButton) + LinearLayout commonsButton; + + @BindView(R.id.description) + TextView description; + + @BindView(R.id.title) + TextView title; + + @BindView(R.id.category) + TextView distance; + + @BindView(R.id.icon) + ImageView icon; + @Inject LocationServiceManager locationManager; @@ -93,6 +130,12 @@ public class NearbyTestLayersFragment extends CommonsDaggerSupportFragment imple @Named("default_preferences") JsonKvStore applicationKvStore; + @Inject + BookmarkLocationsDao bookmarkLocationDao; + + @Inject + ContributionController controller; + private BottomSheetBehavior bottomSheetListBehavior; private BottomSheetBehavior bottomSheetDetailsBehavior; @@ -115,6 +158,8 @@ public class NearbyTestLayersFragment extends CommonsDaggerSupportFragment imple boolean isDarkTheme; boolean isFabOpen; boolean isBottomListSheetExpanded; + private Marker selectedMarker; + private Place selectedPlace; private final double CAMERA_TARGET_SHIFT_FACTOR_PORTRAIT = 0.06; private final double CAMERA_TARGET_SHIFT_FACTOR_LANDSCAPE = 0.04; @@ -321,7 +366,7 @@ public class NearbyTestLayersFragment extends CommonsDaggerSupportFragment imple * @param nearbyPlacesInfo This variable has place list information and distances. */ private void updateMapMarkers(NearbyController.NearbyPlacesInfo nearbyPlacesInfo) { - nearbyParentFragmentPresenter.updateMapMarkers(nearbyPlacesInfo); + nearbyParentFragmentPresenter.updateMapMarkers(nearbyPlacesInfo, selectedMarker); } @Override @@ -362,7 +407,6 @@ public class NearbyTestLayersFragment extends CommonsDaggerSupportFragment imple /** * Starts animation of fab plus (turning on opening) and other FABs - * @param isFabOpen state of FAB buttons, open when clicked on fab button, closed on other click */ @Override public void animateFABs() { @@ -442,4 +486,86 @@ public class NearbyTestLayersFragment extends CommonsDaggerSupportFragment imple mapFragment.getMapboxMap().animateCamera(CameraUpdateFactory.newCameraPosition(position), 1000); } + @Override + public void initViewPositions() { + + } + + @Override + public void hideBottomSheet() { + bottomSheetListBehavior.setState(BottomSheetBehavior.STATE_HIDDEN); + } + + @Override + public void displayBottomSheetWithInfo(Marker marker) { + this.selectedMarker = marker; + NearbyMarker nearbyMarker = (NearbyMarker) marker; + Place place = nearbyMarker.getNearbyBaseMarker().getPlace(); + passInfoToSheet(place); + bottomSheetListBehavior.setState(BottomSheetBehavior.STATE_HIDDEN); + bottomSheetDetailsBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED); + } + + /** + * Same bottom sheet carries information for all nearby places, so we need to pass information + * (title, description, distance and links) to view on nearby marker click + * @param place Place of clicked nearby marker + */ + private void passInfoToSheet(Place place) { + this.selectedPlace = place; + updateBookmarkButtonImage(this.selectedPlace); + + bookmarkButton.setOnClickListener(view -> { + boolean isBookmarked = bookmarkLocationDao.updateBookmarkLocation(this.selectedPlace); + updateBookmarkButtonImage(this.selectedPlace); + mapFragment.updateMarker(isBookmarked, this.selectedPlace, locationManager.getLastLocation()); + }); + + + //TODO move all this buttons into a custom bottom sheet + wikipediaButton.setVisibility(place.hasWikipediaLink()?View.VISIBLE:View.GONE); + wikipediaButton.setOnClickListener(view -> Utils.handleWebUrl(getContext(), this.selectedPlace.siteLinks.getWikipediaLink())); + + wikidataButton.setVisibility(place.hasWikidataLink()?View.VISIBLE:View.GONE); + wikidataButton.setOnClickListener(view -> Utils.handleWebUrl(getContext(), this.selectedPlace.siteLinks.getWikidataLink())); + + directionsButton.setOnClickListener(view -> Utils.handleGeoCoordinates(getActivity(), this.selectedPlace.getLocation())); + + commonsButton.setVisibility(this.selectedPlace.hasCommonsLink()?View.VISIBLE:View.GONE); + commonsButton.setOnClickListener(view -> Utils.handleWebUrl(getContext(), this.selectedPlace.siteLinks.getCommonsLink())); + + icon.setImageResource(this.selectedPlace.getLabel().getIcon()); + + title.setText(this.selectedPlace.name); + distance.setText(this.selectedPlace.distance); + description.setText(this.selectedPlace.getLongDescription()); + + fabCamera.setOnClickListener(view -> { + if (fabCamera.isShown()) { + Timber.d("Camera button tapped. Place: %s", this.selectedPlace.toString()); + // TODO storeSharedPrefs(); + controller.initiateCameraPick(getActivity()); + } + }); + + fabGallery.setOnClickListener(view -> { + if (fabGallery.isShown()) { + Timber.d("Gallery button tapped. Place: %s", this.selectedPlace.toString()); + //TODO storeSharedPrefs(); + controller.initiateGalleryPick(getActivity(), false); + } + }); + } + + private void updateBookmarkButtonImage(Place place) { + int bookmarkIcon; + if (bookmarkLocationDao.findBookmarkLocation(place)) { + bookmarkIcon = R.drawable.ic_round_star_filled_24px; + } else { + bookmarkIcon = R.drawable.ic_round_star_border_24px; + } + if (bookmarkButtonImage != null) { + bookmarkButtonImage.setImageResource(bookmarkIcon); + } + } } diff --git a/app/src/main/java/fr/free/nrw/commons/nearby/SupportMapFragment.java b/app/src/main/java/fr/free/nrw/commons/nearby/SupportMapFragment.java index 9ff714052..c947ee771 100644 --- a/app/src/main/java/fr/free/nrw/commons/nearby/SupportMapFragment.java +++ b/app/src/main/java/fr/free/nrw/commons/nearby/SupportMapFragment.java @@ -1,6 +1,7 @@ package fr.free.nrw.commons.nearby; import android.content.Context; +import android.graphics.Bitmap; import android.graphics.Color; import android.net.Uri; import android.os.Bundle; @@ -12,7 +13,9 @@ import android.view.ViewGroup; import androidx.annotation.NonNull; import androidx.annotation.Nullable; +import androidx.vectordrawable.graphics.drawable.VectorDrawableCompat; +import com.google.android.material.bottomsheet.BottomSheetBehavior; import com.google.android.material.floatingactionbutton.FloatingActionButton; import com.mapbox.mapboxsdk.annotations.Icon; import com.mapbox.mapboxsdk.annotations.IconFactory; @@ -39,9 +42,13 @@ import fr.free.nrw.commons.di.CommonsDaggerSupportFragment; import fr.free.nrw.commons.location.LatLng; import fr.free.nrw.commons.nearby.mvp.contract.NearbyMapContract; import fr.free.nrw.commons.nearby.mvp.contract.NearbyParentFragmentContract; +import fr.free.nrw.commons.nearby.mvp.presenter.NearbyParentFragmentPresenter; import fr.free.nrw.commons.utils.LocationUtils; +import fr.free.nrw.commons.utils.UiUtils; import timber.log.Timber; +import static fr.free.nrw.commons.utils.LengthUtils.formatDistanceBetween; + /** * Support Fragment wrapper around a map view. *

@@ -265,7 +272,9 @@ public class SupportMapFragment extends CommonsDaggerSupportFragment } @Override - public void updateMapMarkers(LatLng latLng, List placeList) { + public void updateMapMarkers(LatLng latLng, List placeList + , Marker selectedMarker + , NearbyParentFragmentPresenter nearbyParentFragmentPresenter) { Log.d("denemeTest","updateMapMarkers, curLatng:"+latLng); List customBaseMarkerOptions = NearbyController .loadAttractionsFromLocationToBaseMarkerOptions(latLng, // Curlatlang will be used to calculate distances @@ -281,7 +290,7 @@ public class SupportMapFragment extends CommonsDaggerSupportFragment .newCameraPosition(cameraPosition), 1000);*/ // TODO: set position depening to botom sheet position heere // We are trying to find nearby places around our custom searched area, thus custom parameter is nonnull - addNearbyMarkersToMapBoxMap(customBaseMarkerOptions); + addNearbyMarkersToMapBoxMap(customBaseMarkerOptions, selectedMarker, nearbyParentFragmentPresenter); // Re-enable mapbox gestures on custom location markers load mapboxMap.getUiSettings().setAllGesturesEnabled(true); updateMapToTrackPosition(latLng); @@ -373,7 +382,9 @@ public class SupportMapFragment extends CommonsDaggerSupportFragment } @Override - public void addNearbyMarkersToMapBoxMap(@Nullable List baseMarkerList) { + public void addNearbyMarkersToMapBoxMap(@Nullable List baseMarkerList + , Marker selectedMarker + , NearbyParentFragmentPresenter nearbyParentFragmentPresenter) { Log.d("denemeTest","add markers to map"); mapboxMap.addMarkers(baseMarkerList); mapboxMap.setOnInfoWindowCloseListener(marker -> { @@ -384,27 +395,7 @@ public class SupportMapFragment extends CommonsDaggerSupportFragment map.getMapAsync(mapboxMap -> { mapboxMap.addMarkers(baseMarkerList); //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; - }); - + setMapMarkerActions(selectedMarker, nearbyParentFragmentPresenter); }); } @@ -492,5 +483,55 @@ public class SupportMapFragment extends CommonsDaggerSupportFragment public void showPlaces() { } + + void setMapMarkerActions(Marker selected, NearbyParentFragmentPresenter nearbyParentFragmentPresenter) { + getMapboxMap().setOnInfoWindowCloseListener(marker -> { + if (marker == selected) { + nearbyParentFragmentPresenter.markerUnselected(); + } + }); + + getMapboxMap().setOnMarkerClickListener(marker -> { + + if (marker instanceof NearbyMarker) { + nearbyParentFragmentPresenter.markerSelected(marker); + } + return false; + }); + } + + public void updateMarker(boolean isBookmarked, Place place, LatLng curLatLng) { + + VectorDrawableCompat vectorDrawable; + if (isBookmarked) { + vectorDrawable = VectorDrawableCompat.create( + getContext().getResources(), R.drawable.ic_custom_bookmark_marker, getContext().getTheme() + ); + } else { + vectorDrawable = VectorDrawableCompat.create( + getContext().getResources(), R.drawable.ic_custom_map_marker, getContext().getTheme() + ); + } + for (Marker marker : mapboxMap.getMarkers()) { + if (marker.getTitle() != null && marker.getTitle().equals(place.getName())) { + + Bitmap icon = UiUtils.getBitmap(vectorDrawable); + String distance = formatDistanceBetween(curLatLng, place.location); + place.setDistance(distance); + + NearbyBaseMarker nearbyBaseMarker = new NearbyBaseMarker(); + nearbyBaseMarker.title(place.name); + nearbyBaseMarker.position( + new com.mapbox.mapboxsdk.geometry.LatLng( + place.location.getLatitude(), + place.location.getLongitude())); + nearbyBaseMarker.place(place); + nearbyBaseMarker.icon(IconFactory.getInstance(getContext()) + .fromBitmap(icon)); + marker.setIcon(IconFactory.getInstance(getContext()).fromBitmap(icon)); + } + } + } + } diff --git a/app/src/main/java/fr/free/nrw/commons/nearby/mvp/contract/NearbyMapContract.java b/app/src/main/java/fr/free/nrw/commons/nearby/mvp/contract/NearbyMapContract.java index 00678b74a..45b13e58c 100644 --- a/app/src/main/java/fr/free/nrw/commons/nearby/mvp/contract/NearbyMapContract.java +++ b/app/src/main/java/fr/free/nrw/commons/nearby/mvp/contract/NearbyMapContract.java @@ -6,6 +6,7 @@ import android.view.View; import com.google.android.material.floatingactionbutton.FloatingActionButton; import com.mapbox.mapboxsdk.annotations.BaseMarkerOptions; +import com.mapbox.mapboxsdk.annotations.Marker; import com.mapbox.mapboxsdk.maps.MapView; import com.mapbox.mapboxsdk.maps.MapboxMap; @@ -14,6 +15,7 @@ import java.util.List; import fr.free.nrw.commons.location.LatLng; import fr.free.nrw.commons.nearby.NearbyBaseMarker; import fr.free.nrw.commons.nearby.Place; +import fr.free.nrw.commons.nearby.mvp.presenter.NearbyParentFragmentPresenter; /** * This interface defines specific View and UserActions for map @@ -27,14 +29,14 @@ public interface NearbyMapContract { void showSearchThisAreaButton(); void showInformationBottomSheet(); void initViews(); - void updateMapMarkers(LatLng latLng, List placeList); + void updateMapMarkers(LatLng latLng, List placeList, Marker selectedMarker, NearbyParentFragmentPresenter nearbyParentFragmentPresenter); void updateMapToTrackPosition(LatLng curLatLng); void setListeners(); MapView setupMapView(Bundle savedInstanceState); void addCurrentLocationMarker(LatLng curLatLng); void setSearchThisAreaButtonVisibility(boolean visible); boolean isCurrentLocationMarkerVisible(); - void addNearbyMarkersToMapBoxMap(List baseMarkerOptions); + void addNearbyMarkersToMapBoxMap(List baseMarkerOptions, Marker marker, NearbyParentFragmentPresenter nearbyParentFragmentPresenter); void prepareViewsForSheetPosition(); void hideFABs(); void showFABs(); diff --git a/app/src/main/java/fr/free/nrw/commons/nearby/mvp/contract/NearbyParentFragmentContract.java b/app/src/main/java/fr/free/nrw/commons/nearby/mvp/contract/NearbyParentFragmentContract.java index 549d4c0d4..268ac2299 100644 --- a/app/src/main/java/fr/free/nrw/commons/nearby/mvp/contract/NearbyParentFragmentContract.java +++ b/app/src/main/java/fr/free/nrw/commons/nearby/mvp/contract/NearbyParentFragmentContract.java @@ -2,6 +2,7 @@ package fr.free.nrw.commons.nearby.mvp.contract; import android.view.View; +import com.mapbox.mapboxsdk.annotations.Marker; import com.mapbox.mapboxsdk.maps.MapboxMap; import fr.free.nrw.commons.kvstore.JsonKvStore; @@ -29,6 +30,9 @@ public interface NearbyParentFragmentContract { void setFABRecenterAction(android.view.View.OnClickListener onClickListener); void animateFABs(); void recenterMap(LatLng curLatLng); + void initViewPositions(); + void hideBottomSheet(); + void displayBottomSheetWithInfo(Marker marker); } interface UserActions { diff --git a/app/src/main/java/fr/free/nrw/commons/nearby/mvp/fragments/NearbyParentFragment.java b/app/src/main/java/fr/free/nrw/commons/nearby/mvp/fragments/NearbyParentFragment.java index 77f51b2b4..8a2e75087 100644 --- a/app/src/main/java/fr/free/nrw/commons/nearby/mvp/fragments/NearbyParentFragment.java +++ b/app/src/main/java/fr/free/nrw/commons/nearby/mvp/fragments/NearbyParentFragment.java @@ -22,6 +22,7 @@ import com.google.android.material.bottomsheet.BottomSheetBehavior; import com.google.android.material.snackbar.Snackbar; import com.google.gson.Gson; import com.mapbox.mapboxsdk.Mapbox; +import com.mapbox.mapboxsdk.annotations.Marker; import com.mapbox.mapboxsdk.camera.CameraPosition; import com.mapbox.mapboxsdk.maps.MapboxMap; import com.mapbox.mapboxsdk.maps.MapboxMapOptions; @@ -233,7 +234,7 @@ public class NearbyParentFragment extends CommonsDaggerSupportFragment * @param nearbyPlacesInfo This variable has place list information and distances. */ private void updateMapMarkers(NearbyController.NearbyPlacesInfo nearbyPlacesInfo) { - nearbyParentFragmentPresenter.updateMapMarkers(nearbyPlacesInfo); + nearbyParentFragmentPresenter.updateMapMarkers(nearbyPlacesInfo, null); } /** @@ -349,6 +350,21 @@ public class NearbyParentFragment extends CommonsDaggerSupportFragment } + @Override + public void initViewPositions() { + + } + + @Override + public void hideBottomSheet() { + + } + + @Override + public void displayBottomSheetWithInfo(Marker marker) { + + } + @Override public boolean isNetworkConnectionEstablished() { return NetworkUtils.isInternetConnectionEstablished(getActivity()); diff --git a/app/src/main/java/fr/free/nrw/commons/nearby/mvp/presenter/NearbyParentFragmentPresenter.java b/app/src/main/java/fr/free/nrw/commons/nearby/mvp/presenter/NearbyParentFragmentPresenter.java index 86fc7462b..f123dee94 100644 --- a/app/src/main/java/fr/free/nrw/commons/nearby/mvp/presenter/NearbyParentFragmentPresenter.java +++ b/app/src/main/java/fr/free/nrw/commons/nearby/mvp/presenter/NearbyParentFragmentPresenter.java @@ -3,6 +3,7 @@ package fr.free.nrw.commons.nearby.mvp.presenter; import android.util.Log; import android.view.View; +import com.mapbox.mapboxsdk.annotations.Marker; import com.mapbox.mapboxsdk.maps.MapboxMap; import fr.free.nrw.commons.kvstore.JsonKvStore; @@ -142,10 +143,10 @@ public class NearbyParentFragmentPresenter public void initializeMapOperations() { Log.d("denemeTest","initializeMapOperations"); + nearbyParentFragmentView.initViewPositions(); lockNearby(false); nearbyParentFragmentView.addNetworkBroadcastReceiver(); - //TODO: NETWORK RECEIVER IS NOT ASSIGNED Timber.d("Nearby map view is created and ready"); updateMapAndList(LOCATION_SIGNIFICANTLY_CHANGED, null); @@ -171,6 +172,16 @@ public class NearbyParentFragmentPresenter nearbyParentFragmentView.setFABRecenterAction(v -> { nearbyParentFragmentView.recenterMap(curLatLng); }); + + } + + public void markerUnselected() { + nearbyParentFragmentView.hideBottomSheet(); + } + + + public void markerSelected(Marker marker) { + nearbyParentFragmentView.displayBottomSheetWithInfo(marker); } @@ -269,8 +280,8 @@ public class NearbyParentFragmentPresenter * location where you are not at. * @param nearbyPlacesInfo This variable has place list information and distances. */ - public void updateMapMarkers(NearbyController.NearbyPlacesInfo nearbyPlacesInfo) { - nearbyMapFragmentView.updateMapMarkers(nearbyPlacesInfo.curLatLng, nearbyPlacesInfo.placeList); + public void updateMapMarkers(NearbyController.NearbyPlacesInfo nearbyPlacesInfo, Marker selectedMarker) { + nearbyMapFragmentView.updateMapMarkers(nearbyPlacesInfo.curLatLng, nearbyPlacesInfo.placeList, selectedMarker, this); nearbyMapFragmentView.updateMapToTrackPosition(nearbyPlacesInfo.curLatLng); }