From a396821c2c68086a71dead273d8cb46b3398b015 Mon Sep 17 00:00:00 2001 From: neslihanturan Date: Mon, 2 Sep 2019 20:10:00 +0300 Subject: [PATCH] Initialize map operations if map ready and tab is selected --- .../nearby/NearbyTestLayersFragment.java | 26 ++++ .../commons/nearby/SupportMapFragment.java | 127 +++++++++++++++++- .../NearbyParentFragmentPresenter.java | 14 +- 3 files changed, 161 insertions(+), 6 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 09a21eadf..09a4c02cd 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 @@ -30,6 +30,9 @@ import fr.free.nrw.commons.location.LocationServiceManager; import fr.free.nrw.commons.nearby.mvp.contract.NearbyParentFragmentContract; import fr.free.nrw.commons.nearby.mvp.presenter.NearbyParentFragmentPresenter; import fr.free.nrw.commons.utils.PermissionUtils; +import io.reactivex.Observable; +import io.reactivex.android.schedulers.AndroidSchedulers; +import io.reactivex.schedulers.Schedulers; import timber.log.Timber; import static fr.free.nrw.commons.contributions.ContributionsFragment.CONTRIBUTION_LIST_FRAGMENT_TAG; @@ -41,6 +44,9 @@ public class NearbyTestLayersFragment extends CommonsDaggerSupportFragment imple @Inject LocationServiceManager locationManager; + @Inject + NearbyController nearbyController; + NearbyParentFragmentPresenter nearbyParentFragmentPresenter; SupportMapFragment mapFragment; @@ -153,7 +159,27 @@ public class NearbyTestLayersFragment extends CommonsDaggerSupportFragment imple @Override public void populatePlaces(fr.free.nrw.commons.location.LatLng curlatLng, fr.free.nrw.commons.location.LatLng searchLatLng) { + compositeDisposable.add(Observable.fromCallable(() -> nearbyController + .loadAttractionsFromLocation(curlatLng, searchLatLng, false, true)) + .subscribeOn(Schedulers.io()) + .observeOn(AndroidSchedulers.mainThread()) + .subscribe(this::updateMapMarkers, + throwable -> { + Timber.d(throwable); + //showErrorMessage(getString(R.string.error_fetching_nearby_places)); + // TODO solve first unneeded method call here + //progressBar.setVisibility(View.GONE); + //nearbyParentFragmentPresenter.lockNearby(false); + })); + } + /** + * Populates places for custom location, should be used for finding nearby places around a + * location where you are not at. + * @param nearbyPlacesInfo This variable has place list information and distances. + */ + private void updateMapMarkers(NearbyController.NearbyPlacesInfo nearbyPlacesInfo) { + nearbyParentFragmentPresenter.updateMapMarkers(nearbyPlacesInfo); } @Override 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 63bad0eeb..f4f968a1e 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,9 +1,11 @@ package fr.free.nrw.commons.nearby; import android.content.Context; +import android.graphics.Color; import android.net.Uri; import android.os.Bundle; import android.util.AttributeSet; +import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; @@ -12,6 +14,12 @@ import androidx.annotation.NonNull; import androidx.annotation.Nullable; import com.google.android.material.floatingactionbutton.FloatingActionButton; +import com.mapbox.mapboxsdk.annotations.Icon; +import com.mapbox.mapboxsdk.annotations.IconFactory; +import com.mapbox.mapboxsdk.annotations.Marker; +import com.mapbox.mapboxsdk.annotations.MarkerOptions; +import com.mapbox.mapboxsdk.annotations.PolygonOptions; +import com.mapbox.mapboxsdk.camera.CameraPosition; import com.mapbox.mapboxsdk.maps.MapFragment; import com.mapbox.mapboxsdk.maps.MapView; import com.mapbox.mapboxsdk.maps.MapboxMap; @@ -22,11 +30,17 @@ import com.mapbox.mapboxsdk.utils.MapFragmentUtils; import java.util.ArrayList; import java.util.List; +import javax.inject.Inject; + import dagger.android.support.DaggerFragment; +import fr.free.nrw.commons.R; +import fr.free.nrw.commons.bookmarks.locations.BookmarkLocationsDao; 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.utils.LocationUtils; +import timber.log.Timber; /** * Support Fragment wrapper around a map view. @@ -46,6 +60,9 @@ public class SupportMapFragment extends CommonsDaggerSupportFragment implements OnMapReadyCallback, NearbyMapContract.View{ + @Inject + BookmarkLocationsDao bookmarkLocationDao; + private final List mapReadyCallbackList = new ArrayList<>(); private MapFragment.OnMapViewReadyCallback mapViewReadyCallback; private MapboxMap mapboxMap; @@ -249,12 +266,30 @@ public class SupportMapFragment extends CommonsDaggerSupportFragment @Override public void updateMapMarkers(LatLng latLng, List placeList) { - + Log.d("denemeTest","updateMapMarkers, curLatng:"+latLng); + List customBaseMarkerOptions = NearbyController + .loadAttractionsFromLocationToBaseMarkerOptions(latLng, // Curlatlang will be used to calculate distances + placeList, + getActivity(), + bookmarkLocationDao.getAllBookmarksLocations()); + mapboxMap.clear(); + // TODO: set search latlang here + CameraPosition cameraPosition = new CameraPosition.Builder().target + (LocationUtils.commonsLatLngToMapBoxLatLng(latLng)).build(); + mapboxMap.setCameraPosition(cameraPosition); + /*mapboxMap.animateCamera(CameraUpdateFactory + .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); + // Re-enable mapbox gestures on custom location markers load + mapboxMap.getUiSettings().setAllGesturesEnabled(true); + updateMapToTrackPosition(latLng); } @Override public void updateMapToTrackPosition(LatLng curLatLng) { - + addCurrentLocationMarker(curLatLng); } @Override @@ -267,11 +302,66 @@ public class SupportMapFragment extends CommonsDaggerSupportFragment return null; } + /** + * Adds a marker for the user's current position. Adds a + * circle which uses the accuracy * 2, to draw a circle + * which represents the user's position with an accuracy + * of 95%. + * + * Should be called only on creation of mapboxMap, there + * is other method to update markers location with users + * move. + */ @Override public void addCurrentLocationMarker(LatLng curLatLng) { + Log.d("denemeTest","addCurrentLocationMarker"); + Timber.d("addCurrentLocationMarker is called"); + Icon icon = IconFactory.getInstance(getContext()).fromResource(R.drawable.current_location_marker); + + MarkerOptions currentLocationMarkerOptions = new MarkerOptions() + .position(new com.mapbox.mapboxsdk.geometry.LatLng(curLatLng.getLatitude(), curLatLng.getLongitude())); + currentLocationMarkerOptions.setIcon(icon); // Set custom icon + + Marker currentLocationMarker = mapboxMap.addMarker(currentLocationMarkerOptions); + + List circle = createCircleArray(curLatLng.getLatitude(), curLatLng.getLongitude(), + curLatLng.getAccuracy() * 2, 100); + + PolygonOptions currentLocationPolygonOptions = new PolygonOptions() + .addAll(circle) + .strokeColor(Color.parseColor("#55000000")) + .fillColor(Color.parseColor("#11000000")); + mapboxMap.addPolygon(currentLocationPolygonOptions); } + //TODO: go to util + /** + * Creates a series of points that create a circle on the map. + * Takes the center latitude, center longitude of the circle, + * the radius in meter and the number of nodes of the circle. + * + * @return List List of LatLng points of the circle. + */ + private List createCircleArray( + double centerLat, double centerLong, float radius, int nodes) { + List circle = new ArrayList<>(); + float radiusKilometer = radius / 1000; + double radiusLong = radiusKilometer + / (111.320 * Math.cos(centerLat * Math.PI / 180)); + double radiusLat = radiusKilometer / 110.574; + + for (int i = 0; i < nodes; i++) { + double theta = ((double) i / (double) nodes) * (2 * Math.PI); + double nodeLongitude = centerLong + radiusLong * Math.cos(theta); + double nodeLatitude = centerLat + radiusLat * Math.sin(theta); + circle.add(new com.mapbox.mapboxsdk.geometry.LatLng(nodeLatitude, nodeLongitude)); + } + return circle; + } + + + @Override public void setSearchThisAreaButtonVisibility(boolean visible) { @@ -283,8 +373,39 @@ public class SupportMapFragment extends CommonsDaggerSupportFragment } @Override - public void addNearbyMarkersToMapBoxMap(List baseMarkerOptions) { + public void addNearbyMarkersToMapBoxMap(@Nullable List baseMarkerList) { + Log.d("denemeTest","add markers to map"); + mapboxMap.addMarkers(baseMarkerList); + mapboxMap.setOnInfoWindowCloseListener(marker -> { + /*if (marker == selected) { + bottomSheetDetailsBehavior.setState(BottomSheetBehavior.STATE_HIDDEN); + }*/ + }); + 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; + }); + + }); } @Override 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 60c6b019d..0c31a88df 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 @@ -115,7 +115,8 @@ public class NearbyParentFragmentPresenter // We will know when we went offline and online again //nearbyParentFragmentView.addNetworkBroadcastReceiver(); //nearbyMapFragmentView.setupMapView(null); - nearbyOperationsInitialized(); + //nearbyOperationsInitialized(); + initializeMapOperations(); } @@ -139,10 +140,11 @@ public class NearbyParentFragmentPresenter } public void initializeMapOperations() { - Log.d("deneme2","initializeMapOperations"); + Log.d("denemeTest","initializeMapOperations"); lockNearby(false); nearbyParentFragmentView.addNetworkBroadcastReceiver(); + //TODO: NETWORK RECEIVER IS NOT ASSIGNED Timber.d("Nearby map view is created and ready"); updateMapAndList(LOCATION_SIGNIFICANTLY_CHANGED, null); @@ -187,14 +189,16 @@ public class NearbyParentFragmentPresenter */ @Override public void updateMapAndList(LocationServiceManager.LocationChangeType locationChangeType, LatLng cameraTarget) { + Log.d("denemeTest","updateMapAndList"); if (isNearbyLocked) { - + Log.d("denemeTest","isNearbyLocked"); Timber.d("Nearby is locked, so updateMapAndList returns"); return; } if (!nearbyParentFragmentView.isNetworkConnectionEstablished()) { Timber.d("Network connection is not established"); + Log.d("denemeTest","nearbyParentFragmentView.isNetworkConnectionEstablished()"); return; } @@ -218,6 +222,7 @@ public class NearbyParentFragmentPresenter */ if (locationChangeType.equals(LOCATION_SIGNIFICANTLY_CHANGED) || locationChangeType.equals(MAP_UPDATED)) { + Log.d("denemeTest","1"); nearbyParentFragmentView.populatePlaces(lastLocation, lastLocation); nearbyParentFragmentView.setSearchThisAreaProgressVisibility(false); //nearbyMapFragmentView.updateMapToTrackPosition(curLatLng); @@ -225,10 +230,13 @@ public class NearbyParentFragmentPresenter // TODO dont forget map updated state after an wikidata item is updated } else if (locationChangeType.equals(SEARCH_CUSTOM_AREA)) { + Log.d("denemeTest","2"); + nearbyParentFragmentView.populatePlaces(lastLocation, cameraTarget); nearbyParentFragmentView.setSearchThisAreaProgressVisibility(false); searchingThisArea = false; } else { // Means location changed slightly, ie user is walking or driving. + Log.d("denemeTest","3"); nearbyMapFragmentView.updateMapToTrackPosition(curLatLng); searchingThisArea = false; }