From 55ff68b729931df5bf56bc42ee37dbc05b5e0a09 Mon Sep 17 00:00:00 2001 From: Jason Whitmore Date: Fri, 2 May 2025 16:25:33 -0700 Subject: [PATCH 1/5] ViewPagerAdapter.java: create new constructor with behavior parameter Before this commit, ViewPagerAdapter only had one constructor which used the default behavior where more than the current fragment would be in the Resume state. This commit adds a constructor where the behavior parameter can be specified. --- .../java/fr/free/nrw/commons/ViewPagerAdapter.java | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/app/src/main/java/fr/free/nrw/commons/ViewPagerAdapter.java b/app/src/main/java/fr/free/nrw/commons/ViewPagerAdapter.java index 5ca20372a..b887aaf99 100644 --- a/app/src/main/java/fr/free/nrw/commons/ViewPagerAdapter.java +++ b/app/src/main/java/fr/free/nrw/commons/ViewPagerAdapter.java @@ -18,6 +18,17 @@ public class ViewPagerAdapter extends FragmentPagerAdapter { super(manager); } + /** + * Constructs a ViewPagerAdapter with a specified Fragment Manager and Fragment resume behavior. + * + * @param manager The FragmentManager + * @param behavior An integer which represents the behavior of non visible fragments. See + * FragmentPagerAdapter.java for options. + */ + public ViewPagerAdapter(FragmentManager manager, int behavior) { + super(manager, behavior); + } + /** * This method returns the fragment of the viewpager at a particular position * @param position From c44a610db993cc5ee5e76b470dad0445e9fab188 Mon Sep 17 00:00:00 2001 From: Jason Whitmore Date: Fri, 2 May 2025 16:34:07 -0700 Subject: [PATCH 2/5] ExploreFragment.java: modify onCreateView to use new ViewPagerAdapter constructor Before this change, onCreateView would use the old ViewPagerAdapter constructor, which had a default behavior where fragments not currently visible would be in the Resume state. After this change, the new ViewPagerAdapter constructor is used with a non-default behavior where only the visible fragment would be in the Resume state. This has performance benefits for most users since the Fragments will only be active when they want to view them. --- .../java/fr/free/nrw/commons/explore/ExploreFragment.java | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/fr/free/nrw/commons/explore/ExploreFragment.java b/app/src/main/java/fr/free/nrw/commons/explore/ExploreFragment.java index 223d028dc..b31c34b67 100644 --- a/app/src/main/java/fr/free/nrw/commons/explore/ExploreFragment.java +++ b/app/src/main/java/fr/free/nrw/commons/explore/ExploreFragment.java @@ -12,6 +12,7 @@ import android.view.ViewGroup; import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.fragment.app.Fragment; +import androidx.fragment.app.FragmentPagerAdapter; import androidx.viewpager.widget.ViewPager.OnPageChangeListener; import fr.free.nrw.commons.R; import fr.free.nrw.commons.ViewPagerAdapter; @@ -69,7 +70,9 @@ public class ExploreFragment extends CommonsDaggerSupportFragment { loadNearbyMapData(); binding = FragmentExploreBinding.inflate(inflater, container, false); - viewPagerAdapter = new ViewPagerAdapter(getChildFragmentManager()); + viewPagerAdapter = new ViewPagerAdapter(getChildFragmentManager(), + FragmentPagerAdapter.BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT); + binding.viewPager.setAdapter(viewPagerAdapter); binding.viewPager.setId(R.id.viewPager); binding.tabLayout.setupWithViewPager(binding.viewPager); From b31a01c3ee419c93e7ebb26fc455d8baf722604b Mon Sep 17 00:00:00 2001 From: Jason Whitmore Date: Fri, 2 May 2025 16:49:12 -0700 Subject: [PATCH 3/5] ExploreMapFragment.java: remove redundant method call Before this commit, performMapReadyActions() was called in both onViewCreated and onResume. In effect, the method is called twice before the user can even see the map, leading to performance issues. After this commit, the call in onViewCreated is removed. The method is now called only once when the user switches to the ExploreMapFragment. --- .../java/fr/free/nrw/commons/explore/map/ExploreMapFragment.java | 1 - 1 file changed, 1 deletion(-) diff --git a/app/src/main/java/fr/free/nrw/commons/explore/map/ExploreMapFragment.java b/app/src/main/java/fr/free/nrw/commons/explore/map/ExploreMapFragment.java index 60758ac20..3041c1e33 100644 --- a/app/src/main/java/fr/free/nrw/commons/explore/map/ExploreMapFragment.java +++ b/app/src/main/java/fr/free/nrw/commons/explore/map/ExploreMapFragment.java @@ -221,7 +221,6 @@ public class ExploreMapFragment extends CommonsDaggerSupportFragment binding.mapView.getController().setZoom(ZOOM_LEVEL); } - performMapReadyActions(); binding.mapView.getOverlays().add(new MapEventsOverlay(new MapEventsReceiver() { @Override From df8585d877600d8ad73177ffd0003e05b2f4cbd7 Mon Sep 17 00:00:00 2001 From: Jason Whitmore Date: Fri, 2 May 2025 17:26:28 -0700 Subject: [PATCH 4/5] ExploreMapFragment.java: remove method call that causes recursive loop Before this commit, there was a call loop that included onScroll and animateTo on the map. These two method calls caused performance issues in ExploreMapFragment. This commit breaks the call loop by removing moveCameraToPosition from getMapCenter. Also, the removed code did not fit the purpose of getMapCenter. --- .../fr/free/nrw/commons/explore/map/ExploreMapFragment.java | 3 --- 1 file changed, 3 deletions(-) diff --git a/app/src/main/java/fr/free/nrw/commons/explore/map/ExploreMapFragment.java b/app/src/main/java/fr/free/nrw/commons/explore/map/ExploreMapFragment.java index 3041c1e33..b5406b770 100644 --- a/app/src/main/java/fr/free/nrw/commons/explore/map/ExploreMapFragment.java +++ b/app/src/main/java/fr/free/nrw/commons/explore/map/ExploreMapFragment.java @@ -961,9 +961,6 @@ public class ExploreMapFragment extends CommonsDaggerSupportFragment -0.07483536015053005, 1f); } } - if (!isCameFromNearbyMap()) { - moveCameraToPosition(new GeoPoint(latLnge.getLatitude(), latLnge.getLongitude())); - } return latLnge; } From a6b0ecc0b210cb11f0dc40b41acc23d8ff0785c0 Mon Sep 17 00:00:00 2001 From: Jason Whitmore Date: Fri, 2 May 2025 18:34:22 -0700 Subject: [PATCH 5/5] ExploreMapFragment.java: fix performMapReadyActions to center to user's last known location Before this commit, fixing a previous bug caused performMapReadyActions to center the map on a default location rather than the available last known location. This commit adds code which will attempt to get the last known user location. If it cannot be retrieved, the default location is used. The map now centers properly. --- .../free/nrw/commons/explore/map/ExploreMapFragment.java | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/fr/free/nrw/commons/explore/map/ExploreMapFragment.java b/app/src/main/java/fr/free/nrw/commons/explore/map/ExploreMapFragment.java index b5406b770..f7dafcc8d 100644 --- a/app/src/main/java/fr/free/nrw/commons/explore/map/ExploreMapFragment.java +++ b/app/src/main/java/fr/free/nrw/commons/explore/map/ExploreMapFragment.java @@ -340,7 +340,12 @@ public class ExploreMapFragment extends CommonsDaggerSupportFragment !locationPermissionsHelper.checkLocationPermission(getActivity())) { isPermissionDenied = true; } - lastKnownLocation = MapUtils.getDefaultLatLng(); + + lastKnownLocation = getLastLocation(); + + if (lastKnownLocation == null) { + lastKnownLocation = MapUtils.getDefaultLatLng(); + } // if we came from 'Show in Explore' in Nearby, load Nearby map center and zoom if (isCameFromNearbyMap()) {