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,
boolean filterForAllNoneType);
void updateMapMarkersToController(List<BaseMarker> baseMarkers);
void searchViewGainedFocus();
void setCheckboxUnknown();
@ -131,5 +129,7 @@ public interface NearbyParentFragmentContract {
void setAdvancedQuery(String query);
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.core.content.ContextCompat;
import androidx.core.content.FileProvider;
import androidx.lifecycle.LifecycleCoroutineScope;
import androidx.lifecycle.LifecycleOwnerKt;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.GridLayoutManager;
@ -208,6 +209,7 @@ public class NearbyParentFragment extends CommonsDaggerSupportFragment
private boolean isNetworkErrorOccurred;
private Snackbar snackbar;
private View view;
private LifecycleCoroutineScope scope;
private NearbyParentFragmentPresenter presenter;
private boolean isDarkTheme;
private boolean isFABsExpanded;
@ -231,10 +233,8 @@ public class NearbyParentFragment extends CommonsDaggerSupportFragment
private Place nearestPlace;
private volatile boolean stopQuery;
private boolean isSearchInProgress = false;
private final Handler searchHandler = new Handler();
private Runnable searchRunnable;
private static final long SCROLL_DELAY = 800; // Delay for debounce of onscroll, in milliseconds.
private LatLng updatedLatLng;
private boolean searchable;
@ -341,6 +341,7 @@ public class NearbyParentFragment extends CommonsDaggerSupportFragment
view = binding.getRoot();
initNetworkBroadCastReceiver();
scope = LifecycleOwnerKt.getLifecycleScope(getViewLifecycleOwner());
presenter = new NearbyParentFragmentPresenter(bookmarkLocationDao, placesRepository, nearbyController);
progressDialog = new ProgressDialog(getActivity());
progressDialog.setCancelable(false);
@ -471,28 +472,7 @@ public class NearbyParentFragment extends CommonsDaggerSupportFragment
binding.map.addMapListener(new MapListener() {
@Override
public boolean onScroll(ScrollEvent event) {
// 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);
presenter.handleMapScrolled(scope);
return true;
}
@ -1477,8 +1457,7 @@ public class NearbyParentFragment extends CommonsDaggerSupportFragment
*/
private void updateMapMarkers(final List<Place> nearbyPlaces, final LatLng curLatLng,
final boolean shouldUpdateSelectedMarker) {
presenter.updateMapMarkers(nearbyPlaces, curLatLng,
LifecycleOwnerKt.getLifecycleScope(getViewLifecycleOwner()));
presenter.updateMapMarkers(nearbyPlaces, curLatLng, scope);
}

View file

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