temporary fixes part four

This commit is contained in:
savsch 2024-12-17 23:16:05 +05:30
parent 422ba8dcfa
commit 0fe7388bde
2 changed files with 117 additions and 36 deletions

View file

@ -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<Place> getPlacesFromController(List<Place> places) {
List<Place> results = new ArrayList<Place>();
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())

View file

@ -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<Marker>())
private val markerBaseDataChannel = Channel<ArrayList<MarkerPlaceGroup>>(Channel.CONFLATED)
private val clickedPlaces = CopyOnWriteArraySet<Place>()
private val clickedPlaces = CopyOnWriteArrayList<Place>()
fun handlePlaceClicked(place: Place) {
clickedPlaces.add(place)
}
@ -38,17 +41,19 @@ class JustExperimenting(frag: NearbyParentFragment) {
markerBaseDataChannel.send(es)
}
fun updateMarkersState(markers: List<Marker>){
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,7 +64,23 @@ class JustExperimenting(frag: NearbyParentFragment) {
for(markerBaseDataList in markerBaseDataChannel) {
loadPinDetailsJob?.cancel()
loadPinDetailsJob = launch {
loadPinsDetails(frag, markerBaseDataList, this)
}
}
}
frag.viewLifecycleOwner.lifecycle.addObserver(object: DefaultLifecycleObserver {
override fun onDestroy(owner: LifecycleOwner) {
performCleanup()
}
})
}
private suspend fun loadPinsDetails(
frag: NearbyParentFragment,
markerBaseDataList: ArrayList<MarkerPlaceGroup>,
scope: CoroutineScope
) {
// make sure the grey pins are loaded immediately:
skippedCount = skipLimit
updateMarkersState(
@ -82,23 +103,69 @@ class JustExperimenting(frag: NearbyParentFragment) {
val batchSize = 3
var currentIndex = 0
val endIndex = markerBaseDataList.lastIndex
Timber.tag("nearbyperformancefixes").d("loaded %d gray pins", endIndex+1)
while (currentIndex <= endIndex) {
ensureActive()
Timber.tag("nearbyperformancefixes").d("loading pins from %d", currentIndex)
scope.ensureActive()
val toUpdateMarkersFrom = currentIndex
val placesToProcess = HashMap<Int, Place>()
// while(currentIndex<=endIndex && )
++currentIndex // remove this, added just for testing
val placesToFetch = mutableListOf<Int>()
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
}
frag.viewLifecycleOwner.lifecycle.addObserver(object: DefaultLifecycleObserver {
override fun onDestroy(owner: LifecycleOwner) {
performCleanup()
}
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..<currentIndex) {
updatedMarkers[i] = frag.convertToMarker(markerBaseDataList[i].place, markerBaseDataList[i].isBookmarked)
}
if(clickedPlacesIndex < clickedPlaces.size) {
val clickedPlacesBacklog = hashMapOf<LatLng, Place>()
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()