From 0fe7388bdeef0154610483b2d18b8003b70c85f5 Mon Sep 17 00:00:00 2001 From: savsch Date: Tue, 17 Dec 2024 23:16:05 +0530 Subject: [PATCH] temporary fixes part four --- .../fragments/NearbyParentFragment.java | 16 +- .../nearby/helper/JustExperimenting.kt | 137 +++++++++++++----- 2 files changed, 117 insertions(+), 36 deletions(-) diff --git a/app/src/main/java/fr/free/nrw/commons/nearby/fragments/NearbyParentFragment.java b/app/src/main/java/fr/free/nrw/commons/nearby/fragments/NearbyParentFragment.java index b03d4f777..25f084b58 100644 --- a/app/src/main/java/fr/free/nrw/commons/nearby/fragments/NearbyParentFragment.java +++ b/app/src/main/java/fr/free/nrw/commons/nearby/fragments/NearbyParentFragment.java @@ -1639,7 +1639,21 @@ public class NearbyParentFragment extends CommonsDaggerSupportFragment }); } - private void savePlaceToDatabase(Place place) { + public Place getPlaceFromRepository(String entityID) { + return placesRepository.fetchPlace(entityID); + } + + public List getPlacesFromController(List places) { + List results = new ArrayList(); + try { + results = nearbyController.getPlaces(places); + } catch (Exception e) { + Timber.tag("Nearby Pin Details").e(e); + } + return results; + } + + public void savePlaceToDatabase(Place place) { compositeDisposable.add(placesRepository .save(place) .subscribeOn(Schedulers.io()) diff --git a/app/src/main/java/fr/free/nrw/commons/nearby/helper/JustExperimenting.kt b/app/src/main/java/fr/free/nrw/commons/nearby/helper/JustExperimenting.kt index 68c25255d..09b487c31 100644 --- a/app/src/main/java/fr/free/nrw/commons/nearby/helper/JustExperimenting.kt +++ b/app/src/main/java/fr/free/nrw/commons/nearby/helper/JustExperimenting.kt @@ -3,9 +3,11 @@ package fr.free.nrw.commons.nearby.helper import androidx.lifecycle.DefaultLifecycleObserver import androidx.lifecycle.LifecycleOwner import androidx.lifecycle.lifecycleScope +import fr.free.nrw.commons.location.LatLng import fr.free.nrw.commons.nearby.MarkerPlaceGroup import fr.free.nrw.commons.nearby.Place import fr.free.nrw.commons.nearby.fragments.NearbyParentFragment +import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Job import kotlinx.coroutines.channels.Channel @@ -14,10 +16,11 @@ import kotlinx.coroutines.ensureActive import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.collectLatest import kotlinx.coroutines.launch +import kotlinx.coroutines.withContext import org.osmdroid.views.overlay.Marker import timber.log.Timber import java.util.ArrayList -import java.util.concurrent.CopyOnWriteArraySet +import java.util.concurrent.CopyOnWriteArrayList class JustExperimenting(frag: NearbyParentFragment) { private val scope = frag.viewLifecycleOwner.lifecycleScope @@ -29,7 +32,7 @@ class JustExperimenting(frag: NearbyParentFragment) { private var markersState = MutableStateFlow(emptyList()) private val markerBaseDataChannel = Channel>(Channel.CONFLATED) - private val clickedPlaces = CopyOnWriteArraySet() + private val clickedPlaces = CopyOnWriteArrayList() fun handlePlaceClicked(place: Place) { clickedPlaces.add(place) } @@ -38,17 +41,19 @@ class JustExperimenting(frag: NearbyParentFragment) { markerBaseDataChannel.send(es) } fun updateMarkersState(markers: List){ + Timber.tag("nearbyperformancefixes").d("should be here in a bit") markersState.value = markers } init { scope.launch(Dispatchers.Default) { markersState.collectLatest { + Timber.tag("nearbyperformancefixes").d("here lol") if (it.isEmpty()) { return@collectLatest } - if (skippedCount++ < skipLimit) { - delay(skipDelayMs) - } +// if (skippedCount++ < skipLimit) { +// delay(skipDelayMs) +// } skippedCount = 0 Timber.tag("temptagtwo").d("here: ${it.size}") frag.replaceMarkerOverlays(it) @@ -59,36 +64,7 @@ class JustExperimenting(frag: NearbyParentFragment) { for(markerBaseDataList in markerBaseDataChannel) { loadPinDetailsJob?.cancel() loadPinDetailsJob = launch { - - // make sure the grey pins are loaded immediately: - skippedCount = skipLimit - updateMarkersState( - markerBaseDataList.map { - frag.convertToMarker(it.place, it.isBookmarked) - } - ) - - // now load the pin details: - clickedPlaces.clear() - var clickedPlacesIndex = 0 - markerBaseDataList.sortBy { - it.place.getDistanceInDouble(frag.mapFocus) - } - val updatedMarkers = ArrayList(markerBaseDataList.size) - markerBaseDataList.forEach { - updatedMarkers.add(frag.convertToMarker(it.place, it.isBookmarked)) - } - - val batchSize = 3 - var currentIndex = 0 - val endIndex = markerBaseDataList.lastIndex - while (currentIndex <= endIndex) { - ensureActive() - - val placesToProcess = HashMap() -// while(currentIndex<=endIndex && ) - ++currentIndex // remove this, added just for testing - } + loadPinsDetails(frag, markerBaseDataList, this) } } } @@ -100,6 +76,97 @@ class JustExperimenting(frag: NearbyParentFragment) { }) } + private suspend fun loadPinsDetails( + frag: NearbyParentFragment, + markerBaseDataList: ArrayList, + scope: CoroutineScope + ) { + // make sure the grey pins are loaded immediately: + skippedCount = skipLimit + updateMarkersState( + markerBaseDataList.map { + frag.convertToMarker(it.place, it.isBookmarked) + } + ) + + // now load the pin details: + clickedPlaces.clear() + var clickedPlacesIndex = 0 + markerBaseDataList.sortBy { + it.place.getDistanceInDouble(frag.mapFocus) + } + val updatedMarkers = ArrayList(markerBaseDataList.size) + markerBaseDataList.forEach { + updatedMarkers.add(frag.convertToMarker(it.place, it.isBookmarked)) + } + + val batchSize = 3 + var currentIndex = 0 + val endIndex = markerBaseDataList.lastIndex + Timber.tag("nearbyperformancefixes").d("loaded %d gray pins", endIndex+1) + while (currentIndex <= endIndex) { + Timber.tag("nearbyperformancefixes").d("loading pins from %d", currentIndex) + scope.ensureActive() + val toUpdateMarkersFrom = currentIndex + + val placesToFetch = mutableListOf() + while (currentIndex<=endIndex && placesToFetch.size < batchSize) { + val existingPlace = markerBaseDataList[currentIndex].place + if (existingPlace.name != "") { + ++currentIndex + continue + } + val repoPlace = withContext(Dispatchers.IO) { + frag.getPlaceFromRepository(existingPlace.entityID) + } + if (repoPlace != null && repoPlace.name != ""){ + markerBaseDataList[currentIndex] = + MarkerPlaceGroup(markerBaseDataList[currentIndex].isBookmarked, repoPlace) + ++currentIndex + continue + } + placesToFetch.add(currentIndex) + ++currentIndex + } + if (placesToFetch.isNotEmpty()) { + val fetchedPlaces = withContext(Dispatchers.IO) { + frag.getPlacesFromController(placesToFetch.map { + markerBaseDataList[it].place + }) + } + scope.ensureActive() + for (fetchedPlace in fetchedPlaces) { + for (index in placesToFetch) { // nesting okay here as batch size is small + val existingPlace = markerBaseDataList[index].place + if (existingPlace.siteLinks.wikidataLink == fetchedPlace.siteLinks.wikidataLink){ + fetchedPlace.location = existingPlace.location + fetchedPlace.distance = existingPlace.distance + fetchedPlace.isMonument = existingPlace.isMonument + markerBaseDataList[index] = MarkerPlaceGroup(markerBaseDataList[index].isBookmarked, fetchedPlace) + frag.savePlaceToDatabase(fetchedPlace) + } + } + } + } + for (i in toUpdateMarkersFrom..() + while (clickedPlacesIndex < clickedPlaces.size) { + clickedPlacesBacklog.put(clickedPlaces[clickedPlacesIndex].location, clickedPlaces[clickedPlacesIndex]) + } + for (i in currentIndex..endIndex) { + if (clickedPlacesBacklog.containsKey(markerBaseDataList[i].place.location)) { + markerBaseDataList[i] = MarkerPlaceGroup(markerBaseDataList[i].isBookmarked, clickedPlacesBacklog[markerBaseDataList[i].place.location]) + updatedMarkers[i] = frag.convertToMarker(markerBaseDataList[i].place, markerBaseDataList[i].isBookmarked) + } + } + } + updateMarkersState(updatedMarkers) + } + } + private fun performCleanup() { markerBaseDataChannel.close() }