Add place bookmarking logic, Remove all old code

This commit is contained in:
savsch 2024-12-20 00:26:53 +05:30
parent 3a97245120
commit f19352b451
3 changed files with 46 additions and 38 deletions

View file

@ -128,5 +128,7 @@ public interface NearbyParentFragmentContract {
void setCheckboxUnknown();
void setAdvancedQuery(String query);
void toggleBookmarkedStatus(Place place);
}
}

View file

@ -597,8 +597,7 @@ public class NearbyParentFragment extends CommonsDaggerSupportFragment
return Unit.INSTANCE;
},
(place, isBookmarked) -> {
updateMarker(isBookmarked, place, null);
binding.map.invalidate();
presenter.toggleBookmarkedStatus(place);
return Unit.INSTANCE;
},
commonPlaceClickActions,
@ -1795,21 +1794,6 @@ public class NearbyParentFragment extends CommonsDaggerSupportFragment
return binding.map == null ? null : getMapFocus();
}
/**
* Sets marker icon according to marker status. Sets title and distance.
*
* @param isBookmarked true if place is bookmarked
* @param place
* @param currentLatLng current location
*/
public void updateMarker(final boolean isBookmarked, final Place place,
@Nullable final LatLng currentLatLng) {
if (true) {
return; // TODO move this method to new overlay mangement logic
}
addMarkerToMap(place, isBookmarked);
}
/**
* Highlights nearest place when user clicks on home nearby banner
*
@ -1863,16 +1847,6 @@ public class NearbyParentFragment extends CommonsDaggerSupportFragment
);
}
/**
* Adds a marker representing a place to the map with optional bookmark icon.
*
* @param place The Place object containing information about the location.
* @param isBookMarked A Boolean flag indicating whether the place is bookmarked or not.
*/
private void addMarkerToMap(Place place, Boolean isBookMarked) {
binding.map.getOverlays().add(convertToMarker(place, isBookMarked));
}
public Marker convertToMarker(Place place, Boolean isBookMarked) {
Drawable icon = ContextCompat.getDrawable(getContext(), getIconFor(place, isBookMarked));
GeoPoint point = new GeoPoint(place.location.getLatitude(), place.location.getLongitude());
@ -2301,21 +2275,14 @@ public class NearbyParentFragment extends CommonsDaggerSupportFragment
@Override
public void onBottomSheetItemClick(@Nullable View view, int position) {
BottomSheetItem item = dataList.get(position);
boolean isBookmarked = bookmarkLocationDao.findBookmarkLocation(selectedPlace);
switch (item.getImageResourceId()) {
case R.drawable.ic_round_star_border_24px:
bookmarkLocationDao.updateBookmarkLocation(selectedPlace);
presenter.toggleBookmarkedStatus(selectedPlace);
updateBookmarkButtonImage(selectedPlace);
isBookmarked = bookmarkLocationDao.findBookmarkLocation(selectedPlace);
updateMarker(isBookmarked, selectedPlace, locationManager.getLastLocation());
binding.map.invalidate();
break;
case R.drawable.ic_round_star_filled_24px:
bookmarkLocationDao.updateBookmarkLocation(selectedPlace);
presenter.toggleBookmarkedStatus(selectedPlace);
updateBookmarkButtonImage(selectedPlace);
isBookmarked = bookmarkLocationDao.findBookmarkLocation(selectedPlace);
updateMarker(isBookmarked, selectedPlace, locationManager.getLastLocation());
binding.map.invalidate();
break;
case R.drawable.ic_directions_black_24dp:
Utils.handleGeoCoordinates(this.getContext(), selectedPlace.getLocation());

View file

@ -70,7 +70,9 @@ class NearbyParentFragmentPresenter
val skipDelayMs = 500L
}
suspend fun schedulePlacesUpdate(markerPlaceGroups: List<MarkerPlaceGroup>) =
private var bookmarkChangedPlaces = CopyOnWriteArrayList<Place>()
private suspend fun schedulePlacesUpdate(markerPlaceGroups: List<MarkerPlaceGroup>) =
withContext(Dispatchers.Main) {
if (markerPlaceGroups.isEmpty()) return@withContext
schedulePlacesUpdateJob?.cancel()
@ -83,6 +85,19 @@ class NearbyParentFragmentPresenter
}
}
override fun toggleBookmarkedStatus(place: Place?) {
if (place == null) return
val nowBookmarked = bookmarkLocationDao.updateBookmarkLocation(place)
bookmarkChangedPlaces.add(place)
val placeIndex =
NearbyController.markerLabelList.indexOfFirst { it.place.location == place.location }
NearbyController.markerLabelList[placeIndex] = MarkerPlaceGroup(
nowBookmarked,
NearbyController.markerLabelList[placeIndex].place
)
nearbyParentFragmentView.setFilterState()
}
override fun attachView(view: NearbyParentFragmentContract.View) {
this.nearbyParentFragmentView = view
}
@ -241,8 +256,12 @@ class NearbyParentFragmentPresenter
updatePlaceGroupsToControllerAndRender(nearbyPlaceGroups)
loadPlacesDataAyncJob = scope?.launch(Dispatchers.IO) {
clickedPlaces.clear() // clear past clicks
// clear past clicks and bookmarkChanged queues
clickedPlaces.clear()
bookmarkChangedPlaces.clear()
var clickedPlacesIndex = 0
var bookmarkChangedPlacesIndex = 0
val updatedGroups = nearbyPlaceGroups.toMutableList()
// first load cached places:
val indicesToUpdate = mutableListOf<Int>()
@ -333,6 +352,26 @@ class NearbyParentFragmentPresenter
}
}
}
// handle any bookmarks toggled
if (bookmarkChangedPlacesIndex < bookmarkChangedPlaces.size) {
val bookmarkChangedPlacesBacklog = hashMapOf<LatLng, Place>()
while (bookmarkChangedPlacesIndex < bookmarkChangedPlaces.size) {
bookmarkChangedPlacesBacklog.put(
bookmarkChangedPlaces[bookmarkChangedPlacesIndex].location,
bookmarkChangedPlaces[bookmarkChangedPlacesIndex]
)
++bookmarkChangedPlacesIndex
}
for ((index, group) in updatedGroups.withIndex()) {
if (bookmarkChangedPlacesBacklog.containsKey(group.place.location)) {
updatedGroups[index] = MarkerPlaceGroup(
bookmarkLocationDao
.findBookmarkLocation(updatedGroups[index].place),
updatedGroups[index].place
)
}
}
}
schedulePlacesUpdate(updatedGroups)
if (collectCount++ == totalBatches) {
break