Nearby: Move handling map scroll to presenter

This commit is contained in:
savsch 2024-12-26 12:23:40 +05:30
parent 84b6b76402
commit b12f247042
3 changed files with 26 additions and 39 deletions

View file

@ -122,8 +122,6 @@ public interface NearbyParentFragmentContract {
void filterByMarkerType(List<Label> selectedLabels, int state, boolean filterForPlaceState, void filterByMarkerType(List<Label> selectedLabels, int state, boolean filterForPlaceState,
boolean filterForAllNoneType); boolean filterForAllNoneType);
void updateMapMarkersToController(List<BaseMarker> baseMarkers);
void searchViewGainedFocus(); void searchViewGainedFocus();
void setCheckboxUnknown(); void setCheckboxUnknown();
@ -131,5 +129,7 @@ public interface NearbyParentFragmentContract {
void setAdvancedQuery(String query); void setAdvancedQuery(String query);
void toggleBookmarkedStatus(Place place); void toggleBookmarkedStatus(Place place);
void handleMapScrolled(LifecycleCoroutineScope scope);
} }
} }

View file

@ -55,6 +55,7 @@ import androidx.appcompat.app.AlertDialog.Builder;
import androidx.constraintlayout.widget.ConstraintLayout; import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.core.content.ContextCompat; import androidx.core.content.ContextCompat;
import androidx.core.content.FileProvider; import androidx.core.content.FileProvider;
import androidx.lifecycle.LifecycleCoroutineScope;
import androidx.lifecycle.LifecycleOwnerKt; import androidx.lifecycle.LifecycleOwnerKt;
import androidx.recyclerview.widget.DividerItemDecoration; import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.GridLayoutManager; import androidx.recyclerview.widget.GridLayoutManager;
@ -208,6 +209,7 @@ public class NearbyParentFragment extends CommonsDaggerSupportFragment
private boolean isNetworkErrorOccurred; private boolean isNetworkErrorOccurred;
private Snackbar snackbar; private Snackbar snackbar;
private View view; private View view;
private LifecycleCoroutineScope scope;
private NearbyParentFragmentPresenter presenter; private NearbyParentFragmentPresenter presenter;
private boolean isDarkTheme; private boolean isDarkTheme;
private boolean isFABsExpanded; private boolean isFABsExpanded;
@ -231,10 +233,8 @@ public class NearbyParentFragment extends CommonsDaggerSupportFragment
private Place nearestPlace; private Place nearestPlace;
private volatile boolean stopQuery; private volatile boolean stopQuery;
private boolean isSearchInProgress = false;
private final Handler searchHandler = new Handler(); private final Handler searchHandler = new Handler();
private Runnable searchRunnable; private Runnable searchRunnable;
private static final long SCROLL_DELAY = 800; // Delay for debounce of onscroll, in milliseconds.
private LatLng updatedLatLng; private LatLng updatedLatLng;
private boolean searchable; private boolean searchable;
@ -341,6 +341,7 @@ public class NearbyParentFragment extends CommonsDaggerSupportFragment
view = binding.getRoot(); view = binding.getRoot();
initNetworkBroadCastReceiver(); initNetworkBroadCastReceiver();
scope = LifecycleOwnerKt.getLifecycleScope(getViewLifecycleOwner());
presenter = new NearbyParentFragmentPresenter(bookmarkLocationDao, placesRepository, nearbyController); presenter = new NearbyParentFragmentPresenter(bookmarkLocationDao, placesRepository, nearbyController);
progressDialog = new ProgressDialog(getActivity()); progressDialog = new ProgressDialog(getActivity());
progressDialog.setCancelable(false); progressDialog.setCancelable(false);
@ -471,28 +472,7 @@ public class NearbyParentFragment extends CommonsDaggerSupportFragment
binding.map.addMapListener(new MapListener() { binding.map.addMapListener(new MapListener() {
@Override @Override
public boolean onScroll(ScrollEvent event) { public boolean onScroll(ScrollEvent event) {
presenter.handleMapScrolled(scope);
// Remove any pending search runnables
searchHandler.removeCallbacks(searchRunnable);
// Set a runnable to call the Search after a delay
searchRunnable = new Runnable() {
@Override
public void run() {
if (!isSearchInProgress) {
isSearchInProgress = true; // search executing flag
// Start Search
try {
presenter.searchInTheArea();
} finally {
isSearchInProgress = false;
}
}
}
};
// post runnable with configured SCROLL_DELAY
searchHandler.postDelayed(searchRunnable, SCROLL_DELAY);
return true; return true;
} }
@ -1477,8 +1457,7 @@ public class NearbyParentFragment extends CommonsDaggerSupportFragment
*/ */
private void updateMapMarkers(final List<Place> nearbyPlaces, final LatLng curLatLng, private void updateMapMarkers(final List<Place> nearbyPlaces, final LatLng curLatLng,
final boolean shouldUpdateSelectedMarker) { final boolean shouldUpdateSelectedMarker) {
presenter.updateMapMarkers(nearbyPlaces, curLatLng, presenter.updateMapMarkers(nearbyPlaces, curLatLng, scope);
LifecycleOwnerKt.getLifecycleScope(getViewLifecycleOwner()));
} }

View file

@ -52,6 +52,9 @@ class NearbyParentFragmentPresenter
private var nearbyParentFragmentView: NearbyParentFragmentContract.View = DUMMY private var nearbyParentFragmentView: NearbyParentFragmentContract.View = DUMMY
private var placeSearchJob: Job? = null
private var isSearchInProgress = false
private val clickedPlaces = CopyOnWriteArrayList<Place>() private val clickedPlaces = CopyOnWriteArrayList<Place>()
/** /**
@ -489,17 +492,21 @@ class NearbyParentFragmentPresenter
} }
} }
@MainThread @Override
override fun updateMapMarkersToController(baseMarkers: MutableList<BaseMarker>) { override fun handleMapScrolled(scope: LifecycleCoroutineScope?) {
NearbyController.markerLabelList.clear() scope ?: return
for (i in baseMarkers.indices) { placeSearchJob?.cancel()
val nearbyBaseMarker = baseMarkers[i] placeSearchJob = scope.launch {
NearbyController.markerLabelList.add( delay(SCROLL_DELAY)
MarkerPlaceGroup( if (!isSearchInProgress) {
bookmarkLocationDao.findBookmarkLocation(nearbyBaseMarker.place), isSearchInProgress = true; // search executing flag
nearbyBaseMarker.place // Start Search
) try {
) searchInTheArea();
} finally {
isSearchInProgress = false;
}
}
} }
} }
@ -575,6 +582,7 @@ class NearbyParentFragmentPresenter
} }
companion object { companion object {
private const val SCROLL_DELAY = 800L; // Delay for debounce of onscroll, in milliseconds.
private val DUMMY = Proxy.newProxyInstance( private val DUMMY = Proxy.newProxyInstance(
NearbyParentFragmentContract.View::class.java.getClassLoader(), NearbyParentFragmentContract.View::class.java.getClassLoader(),
arrayOf<Class<*>>(NearbyParentFragmentContract.View::class.java), arrayOf<Class<*>>(NearbyParentFragmentContract.View::class.java),