Skip to content

Commit f19352b

Browse files
committed
Add place bookmarking logic, Remove all old code
1 parent 3a97245 commit f19352b

File tree

3 files changed

+46
-38
lines changed

3 files changed

+46
-38
lines changed

app/src/main/java/fr/free/nrw/commons/nearby/contract/NearbyParentFragmentContract.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,5 +128,7 @@ void filterByMarkerType(List<Label> selectedLabels, int state, boolean filterFor
128128
void setCheckboxUnknown();
129129

130130
void setAdvancedQuery(String query);
131+
132+
void toggleBookmarkedStatus(Place place);
131133
}
132134
}

app/src/main/java/fr/free/nrw/commons/nearby/fragments/NearbyParentFragment.java

Lines changed: 3 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -597,8 +597,7 @@ private void initRvNearbyList() {
597597
return Unit.INSTANCE;
598598
},
599599
(place, isBookmarked) -> {
600-
updateMarker(isBookmarked, place, null);
601-
binding.map.invalidate();
600+
presenter.toggleBookmarkedStatus(place);
602601
return Unit.INSTANCE;
603602
},
604603
commonPlaceClickActions,
@@ -1795,21 +1794,6 @@ public LatLng getCameraTarget() {
17951794
return binding.map == null ? null : getMapFocus();
17961795
}
17971796

1798-
/**
1799-
* Sets marker icon according to marker status. Sets title and distance.
1800-
*
1801-
* @param isBookmarked true if place is bookmarked
1802-
* @param place
1803-
* @param currentLatLng current location
1804-
*/
1805-
public void updateMarker(final boolean isBookmarked, final Place place,
1806-
@Nullable final LatLng currentLatLng) {
1807-
if (true) {
1808-
return; // TODO move this method to new overlay mangement logic
1809-
}
1810-
addMarkerToMap(place, isBookmarked);
1811-
}
1812-
18131797
/**
18141798
* Highlights nearest place when user clicks on home nearby banner
18151799
*
@@ -1863,16 +1847,6 @@ private void highlightNearestPlace(final Place nearestPlace) {
18631847
);
18641848
}
18651849

1866-
/**
1867-
* Adds a marker representing a place to the map with optional bookmark icon.
1868-
*
1869-
* @param place The Place object containing information about the location.
1870-
* @param isBookMarked A Boolean flag indicating whether the place is bookmarked or not.
1871-
*/
1872-
private void addMarkerToMap(Place place, Boolean isBookMarked) {
1873-
binding.map.getOverlays().add(convertToMarker(place, isBookMarked));
1874-
}
1875-
18761850
public Marker convertToMarker(Place place, Boolean isBookMarked) {
18771851
Drawable icon = ContextCompat.getDrawable(getContext(), getIconFor(place, isBookMarked));
18781852
GeoPoint point = new GeoPoint(place.location.getLatitude(), place.location.getLongitude());
@@ -2301,21 +2275,14 @@ private void moveCameraToPosition(GeoPoint geoPoint) {
23012275
@Override
23022276
public void onBottomSheetItemClick(@Nullable View view, int position) {
23032277
BottomSheetItem item = dataList.get(position);
2304-
boolean isBookmarked = bookmarkLocationDao.findBookmarkLocation(selectedPlace);
23052278
switch (item.getImageResourceId()) {
23062279
case R.drawable.ic_round_star_border_24px:
2307-
bookmarkLocationDao.updateBookmarkLocation(selectedPlace);
2280+
presenter.toggleBookmarkedStatus(selectedPlace);
23082281
updateBookmarkButtonImage(selectedPlace);
2309-
isBookmarked = bookmarkLocationDao.findBookmarkLocation(selectedPlace);
2310-
updateMarker(isBookmarked, selectedPlace, locationManager.getLastLocation());
2311-
binding.map.invalidate();
23122282
break;
23132283
case R.drawable.ic_round_star_filled_24px:
2314-
bookmarkLocationDao.updateBookmarkLocation(selectedPlace);
2284+
presenter.toggleBookmarkedStatus(selectedPlace);
23152285
updateBookmarkButtonImage(selectedPlace);
2316-
isBookmarked = bookmarkLocationDao.findBookmarkLocation(selectedPlace);
2317-
updateMarker(isBookmarked, selectedPlace, locationManager.getLastLocation());
2318-
binding.map.invalidate();
23192286
break;
23202287
case R.drawable.ic_directions_black_24dp:
23212288
Utils.handleGeoCoordinates(this.getContext(), selectedPlace.getLocation());

app/src/main/java/fr/free/nrw/commons/nearby/presenter/NearbyParentFragmentPresenter.kt

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,9 @@ class NearbyParentFragmentPresenter
7070
val skipDelayMs = 500L
7171
}
7272

73-
suspend fun schedulePlacesUpdate(markerPlaceGroups: List<MarkerPlaceGroup>) =
73+
private var bookmarkChangedPlaces = CopyOnWriteArrayList<Place>()
74+
75+
private suspend fun schedulePlacesUpdate(markerPlaceGroups: List<MarkerPlaceGroup>) =
7476
withContext(Dispatchers.Main) {
7577
if (markerPlaceGroups.isEmpty()) return@withContext
7678
schedulePlacesUpdateJob?.cancel()
@@ -83,6 +85,19 @@ class NearbyParentFragmentPresenter
8385
}
8486
}
8587

88+
override fun toggleBookmarkedStatus(place: Place?) {
89+
if (place == null) return
90+
val nowBookmarked = bookmarkLocationDao.updateBookmarkLocation(place)
91+
bookmarkChangedPlaces.add(place)
92+
val placeIndex =
93+
NearbyController.markerLabelList.indexOfFirst { it.place.location == place.location }
94+
NearbyController.markerLabelList[placeIndex] = MarkerPlaceGroup(
95+
nowBookmarked,
96+
NearbyController.markerLabelList[placeIndex].place
97+
)
98+
nearbyParentFragmentView.setFilterState()
99+
}
100+
86101
override fun attachView(view: NearbyParentFragmentContract.View) {
87102
this.nearbyParentFragmentView = view
88103
}
@@ -241,8 +256,12 @@ class NearbyParentFragmentPresenter
241256
updatePlaceGroupsToControllerAndRender(nearbyPlaceGroups)
242257

243258
loadPlacesDataAyncJob = scope?.launch(Dispatchers.IO) {
244-
clickedPlaces.clear() // clear past clicks
259+
// clear past clicks and bookmarkChanged queues
260+
clickedPlaces.clear()
261+
bookmarkChangedPlaces.clear()
245262
var clickedPlacesIndex = 0
263+
var bookmarkChangedPlacesIndex = 0
264+
246265
val updatedGroups = nearbyPlaceGroups.toMutableList()
247266
// first load cached places:
248267
val indicesToUpdate = mutableListOf<Int>()
@@ -333,6 +352,26 @@ class NearbyParentFragmentPresenter
333352
}
334353
}
335354
}
355+
// handle any bookmarks toggled
356+
if (bookmarkChangedPlacesIndex < bookmarkChangedPlaces.size) {
357+
val bookmarkChangedPlacesBacklog = hashMapOf<LatLng, Place>()
358+
while (bookmarkChangedPlacesIndex < bookmarkChangedPlaces.size) {
359+
bookmarkChangedPlacesBacklog.put(
360+
bookmarkChangedPlaces[bookmarkChangedPlacesIndex].location,
361+
bookmarkChangedPlaces[bookmarkChangedPlacesIndex]
362+
)
363+
++bookmarkChangedPlacesIndex
364+
}
365+
for ((index, group) in updatedGroups.withIndex()) {
366+
if (bookmarkChangedPlacesBacklog.containsKey(group.place.location)) {
367+
updatedGroups[index] = MarkerPlaceGroup(
368+
bookmarkLocationDao
369+
.findBookmarkLocation(updatedGroups[index].place),
370+
updatedGroups[index].place
371+
)
372+
}
373+
}
374+
}
336375
schedulePlacesUpdate(updatedGroups)
337376
if (collectCount++ == totalBatches) {
338377
break

0 commit comments

Comments
 (0)