Compare commits

...

5 commits

Author SHA1 Message Date
Jason Whitmore
a6b0ecc0b2 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.
2025-05-02 18:34:22 -07:00
Jason Whitmore
df8585d877 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.
2025-05-02 17:26:28 -07:00
Jason Whitmore
b31a01c3ee 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.
2025-05-02 16:49:12 -07:00
Jason Whitmore
c44a610db9 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.
2025-05-02 16:34:07 -07:00
Jason Whitmore
55ff68b729 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.
2025-05-02 16:25:33 -07:00
3 changed files with 21 additions and 6 deletions

View file

@ -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

View file

@ -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);

View file

@ -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
@ -341,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()) {
@ -962,9 +966,6 @@ public class ExploreMapFragment extends CommonsDaggerSupportFragment
-0.07483536015053005, 1f);
}
}
if (!isCameFromNearbyMap()) {
moveCameraToPosition(new GeoPoint(latLnge.getLatitude(), latLnge.getLongitude()));
}
return latLnge;
}