mirror of
https://github.com/commons-app/apps-android-commons.git
synced 2025-10-28 21:33:53 +01:00
temporary fixes part four
This commit is contained in:
parent
422ba8dcfa
commit
0fe7388bde
2 changed files with 117 additions and 36 deletions
|
|
@ -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())
|
||||
|
|
|
|||
|
|
@ -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,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<Marker>(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<Int, Place>()
|
||||
// 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<MarkerPlaceGroup>,
|
||||
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<Marker>(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<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
|
||||
}
|
||||
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()
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue