From 755b216507786f32999f9141536f78d3f1379298 Mon Sep 17 00:00:00 2001 From: Devarsh Mavani Date: Wed, 16 Feb 2022 19:43:23 +0530 Subject: [PATCH] Show user current location (#4825) --- .../commons/media/MediaDetailFragment.java | 15 +++++++++--- .../media/MediaDetailFragmentUnitTests.kt | 24 ++++++++++++++++++- 2 files changed, 35 insertions(+), 4 deletions(-) diff --git a/app/src/main/java/fr/free/nrw/commons/media/MediaDetailFragment.java b/app/src/main/java/fr/free/nrw/commons/media/MediaDetailFragment.java index 7f335f604..72b2dd485 100644 --- a/app/src/main/java/fr/free/nrw/commons/media/MediaDetailFragment.java +++ b/app/src/main/java/fr/free/nrw/commons/media/MediaDetailFragment.java @@ -80,6 +80,7 @@ import fr.free.nrw.commons.description.DescriptionEditHelper; import fr.free.nrw.commons.di.CommonsDaggerSupportFragment; import fr.free.nrw.commons.explore.depictions.WikidataItemDetailsActivity; import fr.free.nrw.commons.kvstore.JsonKvStore; +import fr.free.nrw.commons.location.LocationServiceManager; import fr.free.nrw.commons.nearby.Label; import fr.free.nrw.commons.profile.ProfileActivity; import fr.free.nrw.commons.ui.widget.HtmlTextView; @@ -117,6 +118,9 @@ public class MediaDetailFragment extends CommonsDaggerSupportFragment implements private boolean isWikipediaButtonDisplayed; private Callback callback; + @Inject + LocationServiceManager locationManager; + public static MediaDetailFragment forMedia(int index, boolean editable, boolean isCategoryImage, boolean isWikipediaButtonDisplayed) { MediaDetailFragment mf = new MediaDetailFragment(); @@ -826,9 +830,14 @@ public class MediaDetailFragment extends CommonsDaggerSupportFragment implements defaultLatitude = media.getCoordinates().getLatitude(); defaultLongitude = media.getCoordinates().getLongitude(); } else { - String[] lastLocation = applicationKvStore.getString(LAST_LOCATION,(defaultLatitude + "," + defaultLongitude)).split(","); - defaultLatitude = Double.parseDouble(lastLocation[0]); - defaultLongitude = Double.parseDouble(lastLocation[1]); + if(locationManager.getLastLocation()!=null) { + defaultLatitude = locationManager.getLastLocation().getLatitude(); + defaultLongitude = locationManager.getLastLocation().getLongitude(); + } else { + String[] lastLocation = applicationKvStore.getString(LAST_LOCATION,(defaultLatitude + "," + defaultLongitude)).split(","); + defaultLatitude = Double.parseDouble(lastLocation[0]); + defaultLongitude = Double.parseDouble(lastLocation[1]); + } } startActivityForResult(new LocationPicker.IntentBuilder() diff --git a/app/src/test/kotlin/fr/free/nrw/commons/media/MediaDetailFragmentUnitTests.kt b/app/src/test/kotlin/fr/free/nrw/commons/media/MediaDetailFragmentUnitTests.kt index 3143d2efc..87de19ccd 100644 --- a/app/src/test/kotlin/fr/free/nrw/commons/media/MediaDetailFragmentUnitTests.kt +++ b/app/src/test/kotlin/fr/free/nrw/commons/media/MediaDetailFragmentUnitTests.kt @@ -28,6 +28,7 @@ import fr.free.nrw.commons.category.CategoryEditSearchRecyclerViewAdapter import fr.free.nrw.commons.explore.SearchActivity import fr.free.nrw.commons.kvstore.JsonKvStore import fr.free.nrw.commons.location.LatLng +import fr.free.nrw.commons.location.LocationServiceManager import fr.free.nrw.commons.ui.widget.HtmlTextView import org.junit.Assert import org.junit.Before @@ -66,6 +67,9 @@ class MediaDetailFragmentUnitTests { private lateinit var view: View private lateinit var context: Context + @Mock + private lateinit var locationManager: LocationServiceManager + @Mock private lateinit var categoryEditSearchRecyclerViewAdapter: CategoryEditSearchRecyclerViewAdapter @@ -180,6 +184,7 @@ class MediaDetailFragmentUnitTests { Whitebox.setInternalState(fragment, "categoryContainer", linearLayout) Whitebox.setInternalState(fragment, "categorySearchView", searchView) Whitebox.setInternalState(fragment, "mediaDiscussion", textView) + Whitebox.setInternalState(fragment, "locationManager", locationManager) Whitebox.setInternalState( fragment, "categoryEditSearchRecyclerViewAdapter", @@ -239,11 +244,13 @@ class MediaDetailFragmentUnitTests { @Test @Throws(Exception::class) - fun testOnUpdateCoordinatesClicked() { + fun testOnUpdateCoordinatesClickedCurrentLocationNull() { `when`(media.coordinates).thenReturn(null) + `when`(locationManager.lastLocation).thenReturn(null) `when`(applicationKvStore.getString(LAST_LOCATION)).thenReturn("37.773972,-122.431297") fragment.onUpdateCoordinatesClicked() Mockito.verify(media, Mockito.times(1)).coordinates + Mockito.verify(locationManager, Mockito.times(1)).lastLocation val shadowActivity: ShadowActivity = shadowOf(activity) val startedIntent = shadowActivity.nextStartedActivity val shadowIntent: ShadowIntent = shadowOf(startedIntent) @@ -263,6 +270,21 @@ class MediaDetailFragmentUnitTests { Assert.assertEquals(shadowIntent.intentClass, LocationPickerActivity::class.java) } + @Test + @Throws(Exception::class) + fun testOnUpdateCoordinatesClickedCurrentLocationNotNull() { + `when`(media.coordinates).thenReturn(null) + `when`(locationManager.lastLocation).thenReturn(LatLng(-0.000001, -0.999999, 0f)) + `when`(applicationKvStore.getString(LAST_LOCATION)).thenReturn("37.773972,-122.431297") + + fragment.onUpdateCoordinatesClicked() + Mockito.verify(locationManager, Mockito.times(3)).lastLocation + val shadowActivity: ShadowActivity = shadowOf(activity) + val startedIntent = shadowActivity.nextStartedActivity + val shadowIntent: ShadowIntent = shadowOf(startedIntent) + Assert.assertEquals(shadowIntent.intentClass, LocationPickerActivity::class.java) + } + @Test @Throws(Exception::class) fun testOnResume() {