@@ -506,6 +506,93 @@ class NearbyParentFragmentPresenter
506506 }
507507 }
508508
509+ /* *
510+ * Creates a HashMap mapping place locations (LatLng) to the associated places.
511+ *
512+ * @param places The list of Places used to create HashMap entries
513+ * @param startIndex Only Places with indices between startIndex and places.lastIndex
514+ * (inclusive) will be used to create the HashMap. This can be useful for avoiding redundant
515+ * computation when calling this method on the same places list multiple times.
516+ *
517+ * @return The HashMap containing (Place's LatLng, Place) entries.
518+ */
519+ private fun getLocationPlaceMap (places : List <Place >, startIndex : Int ): HashMap <LatLng , Place > {
520+ val map = hashMapOf<LatLng , Place >()
521+
522+ for (i in startIndex.. places.lastIndex) {
523+ map[places[i].location] = places[i]
524+ }
525+
526+ return map
527+ }
528+
529+ /* *
530+ * Ensures the correct bookmark boolean value for Places whose bookmarks have changed.
531+ * This bookmark information is retrieved from the bookmarkLocationDao.
532+ *
533+ * @param updatedGroups The MarkerPlaceGroups which will be displayed on the map to the user.
534+ * @param bookmarkChangedPlacesIndex Only Places at indices greater than or equal to this number
535+ * in bookmarkChangedPlaces will be handled. This can be useful for avoiding redundant
536+ * computation when calling this method multiple times.
537+ *
538+ * @return The updated bookmarkChangedPlacesIndex used in future calls to this method.
539+ */
540+ private suspend fun handleBookmarksToggled (
541+ updatedGroups : MutableList <MarkerPlaceGroup >,
542+ bookmarkChangedPlacesIndex : Int
543+ ): Int {
544+ var i = bookmarkChangedPlacesIndex
545+ if (i < bookmarkChangedPlaces.size) {
546+ val bookmarkChangedPlacesBacklog = getLocationPlaceMap(bookmarkChangedPlaces, i)
547+
548+ i + = bookmarkChangedPlacesBacklog.size
549+ for ((index, group) in updatedGroups.withIndex()) {
550+ if (bookmarkChangedPlacesBacklog.containsKey(group.place.location)) {
551+ updatedGroups[index] = MarkerPlaceGroup (
552+ bookmarkLocationDao
553+ .findBookmarkLocation(updatedGroups[index].place.name),
554+ updatedGroups[index].place
555+ )
556+ }
557+ }
558+ }
559+
560+ return i
561+ }
562+
563+ /* *
564+ * Ensures any clicked Places are updated using existing Place data
565+ * and not by data from regular/batched WikiData server responses.
566+ *
567+ * @param updatedGroups The MarkerPlaceGroups which will be displayed on the map to the user.
568+ * @param clickedPlacesIndex Only Places at indices greater than or equal to this number
569+ * in clickedPlaces will be handled. This can be useful for avoiding redundant computation when
570+ * calling this method multiple times.
571+ *
572+ * @return The updated clickedPlacesIndex used in future calls to this method.
573+ */
574+ private fun handlePlacesClicked (
575+ updatedGroups : MutableList <MarkerPlaceGroup >,
576+ clickedPlacesIndex : Int
577+ ): Int {
578+ var i = clickedPlacesIndex
579+ if (i < clickedPlaces.size) {
580+ val clickedPlacesBacklog = getLocationPlaceMap(clickedPlaces, i)
581+
582+ i + = clickedPlacesBacklog.size
583+ for ((index, group) in updatedGroups.withIndex()) {
584+ if (clickedPlacesBacklog.containsKey(group.place.location)) {
585+ updatedGroups[index] = MarkerPlaceGroup (
586+ updatedGroups[index].isBookmarked,
587+ clickedPlacesBacklog[group.place.location]
588+ )
589+ }
590+ }
591+ }
592+
593+ return i
594+ }
595+
509596 /* *
510597 * Load the places' details from cache and Wikidata query, and update these details on the map
511598 * as and when they arrive.
@@ -526,8 +613,6 @@ class NearbyParentFragmentPresenter
526613 // clear past clicks and bookmarkChanged queues
527614 clickedPlaces.clear()
528615 bookmarkChangedPlaces.clear()
529- var clickedPlacesIndex = 0
530- var bookmarkChangedPlacesIndex = 0
531616
532617 val updatedGroups = nearbyPlaceGroups.toMutableList()
533618 // first load cached places:
@@ -546,50 +631,17 @@ class NearbyParentFragmentPresenter
546631 updatedGroups, collectResults)
547632
548633 var collectCount = 0
634+ var clickedPlacesIndex = 0
635+ var bookmarkChangedPlacesIndex = 0
549636 while (collectCount < indicesToUpdate.size) {
550637 val resultList = collectResults.receive()
551638
552639 processResults(resultList, updatedGroups)
640+
641+ clickedPlacesIndex = handlePlacesClicked(updatedGroups, clickedPlacesIndex)
553642
554- // handle any places clicked
555- if (clickedPlacesIndex < clickedPlaces.size) {
556- val clickedPlacesBacklog = hashMapOf<LatLng , Place >()
557- while (clickedPlacesIndex < clickedPlaces.size) {
558- clickedPlacesBacklog.put(
559- clickedPlaces[clickedPlacesIndex].location,
560- clickedPlaces[clickedPlacesIndex]
561- )
562- ++ clickedPlacesIndex
563- }
564- for ((index, group) in updatedGroups.withIndex()) {
565- if (clickedPlacesBacklog.containsKey(group.place.location)) {
566- updatedGroups[index] = MarkerPlaceGroup (
567- updatedGroups[index].isBookmarked,
568- clickedPlacesBacklog[group.place.location]
569- )
570- }
571- }
572- }
573- // handle any bookmarks toggled
574- if (bookmarkChangedPlacesIndex < bookmarkChangedPlaces.size) {
575- val bookmarkChangedPlacesBacklog = hashMapOf<LatLng , Place >()
576- while (bookmarkChangedPlacesIndex < bookmarkChangedPlaces.size) {
577- bookmarkChangedPlacesBacklog.put(
578- bookmarkChangedPlaces[bookmarkChangedPlacesIndex].location,
579- bookmarkChangedPlaces[bookmarkChangedPlacesIndex]
580- )
581- ++ bookmarkChangedPlacesIndex
582- }
583- for ((index, group) in updatedGroups.withIndex()) {
584- if (bookmarkChangedPlacesBacklog.containsKey(group.place.location)) {
585- updatedGroups[index] = MarkerPlaceGroup (
586- bookmarkLocationDao
587- .findBookmarkLocation(updatedGroups[index].place.name),
588- updatedGroups[index].place
589- )
590- }
591- }
592- }
643+ bookmarkChangedPlacesIndex = handleBookmarksToggled(updatedGroups,
644+ bookmarkChangedPlacesIndex)
593645 schedulePlacesUpdate(updatedGroups)
594646 collectCount + = resultList.size
595647 }
0 commit comments