Replace temporary experimental fixes part 3

This commit is contained in:
savsch 2024-12-19 20:55:16 +05:30
parent fcc427e724
commit 73c16aea41
2 changed files with 54 additions and 9 deletions

View file

@ -356,7 +356,7 @@ public class NearbyParentFragment extends CommonsDaggerSupportFragment
// justExperimenting = new JustExperimenting(this); // justExperimenting = new JustExperimenting(this);
initNetworkBroadCastReceiver(); initNetworkBroadCastReceiver();
presenter = new NearbyParentFragmentPresenter(bookmarkLocationDao, placesRepository); presenter = new NearbyParentFragmentPresenter(bookmarkLocationDao, placesRepository, nearbyController);
progressDialog = new ProgressDialog(getActivity()); progressDialog = new ProgressDialog(getActivity());
progressDialog.setCancelable(false); progressDialog.setCancelable(false);
progressDialog.setMessage("Saving in progress..."); progressDialog.setMessage("Saving in progress...");

View file

@ -24,15 +24,21 @@ import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job import kotlinx.coroutines.Job
import kotlinx.coroutines.channels.Channel import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.delay import kotlinx.coroutines.delay
import kotlinx.coroutines.ensureActive
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext import kotlinx.coroutines.withContext
import org.osmdroid.views.overlay.Marker
import timber.log.Timber import timber.log.Timber
import java.lang.reflect.InvocationHandler import java.lang.reflect.InvocationHandler
import java.lang.reflect.Method import java.lang.reflect.Method
import java.lang.reflect.Proxy import java.lang.reflect.Proxy
class NearbyParentFragmentPresenter class NearbyParentFragmentPresenter
(val bookmarkLocationDao: BookmarkLocationsDao, val placesRepository: PlacesRepository) : (
val bookmarkLocationDao: BookmarkLocationsDao,
val placesRepository: PlacesRepository,
val nearbyController: NearbyController
) :
NearbyParentFragmentContract.UserActions, NearbyParentFragmentContract.UserActions,
WikidataP18EditListener, LocationUpdateListener { WikidataP18EditListener, LocationUpdateListener {
@ -48,7 +54,7 @@ class NearbyParentFragmentPresenter
private var loadPlacesDataAyncJob: Job? = null private var loadPlacesDataAyncJob: Job? = null
private object LoadPlacesAsyncOptions { private object LoadPlacesAsyncOptions {
val batchSize = 3 val batchSize = 3
val connectionCount = 5 val connectionCount = 3
} }
private var schedulePlacesUpdateJob: Job? = null private var schedulePlacesUpdateJob: Job? = null
@ -247,7 +253,9 @@ class NearbyParentFragmentPresenter
schedulePlacesUpdate(updatedGroups) schedulePlacesUpdate(updatedGroups)
} }
val fetchPlacesChannel = Channel<List<Int>>(Channel.UNLIMITED); val fetchPlacesChannel = Channel<List<Int>>(Channel.UNLIMITED);
var totalBatches = 0
for (i in indicesToUpdate.indices step LoadPlacesAsyncOptions.batchSize) { for (i in indicesToUpdate.indices step LoadPlacesAsyncOptions.batchSize) {
++totalBatches
fetchPlacesChannel.send( fetchPlacesChannel.send(
indicesToUpdate.slice( indicesToUpdate.slice(
i until (i + LoadPlacesAsyncOptions.batchSize).coerceAtMost( i until (i + LoadPlacesAsyncOptions.batchSize).coerceAtMost(
@ -256,9 +264,50 @@ class NearbyParentFragmentPresenter
) )
) )
} }
fetchPlacesChannel.close()
val collectResults = Channel<List<Pair<Int, MarkerPlaceGroup>>>(totalBatches);
repeat(LoadPlacesAsyncOptions.connectionCount) { repeat(LoadPlacesAsyncOptions.connectionCount) {
launch(Dispatchers.IO) { launch(Dispatchers.IO) {
for (indices in fetchPlacesChannel) {
ensureActive()
try {
val fetchedPlaces =
nearbyController.getPlaces(indices.map { updatedGroups[it].place })
collectResults.send(
fetchedPlaces.mapIndexed { index, place ->
Pair(indices[index], MarkerPlaceGroup(
bookmarkLocationDao.findBookmarkLocation(place),
place
))
}
)
} catch (e: Exception) {
Timber.tag("NearbyPinDetails").e(e);
collectResults.send(indices.map { Pair(it, updatedGroups[it]) })
}
}
}
}
var collectCount = 0
for (resultList in collectResults) {
for ((index, fetchedPlaceGroup) in resultList) {
val existingPlace = updatedGroups[index].place
val finalPlaceGroup = MarkerPlaceGroup(
fetchedPlaceGroup.isBookmarked,
fetchedPlaceGroup.place.apply {
location = existingPlace.location
distance = existingPlace.distance
isMonument = existingPlace.isMonument
}
)
updatedGroups[index] = finalPlaceGroup
launch {
placesRepository.save(finalPlaceGroup.place)
}
}
schedulePlacesUpdate(updatedGroups)
if (collectCount++ == totalBatches) {
break
} }
} }
} }
@ -394,11 +443,7 @@ class NearbyParentFragmentPresenter
mylocation.setLongitude(nearbyParentFragmentView.getLastMapFocus().longitude) mylocation.setLongitude(nearbyParentFragmentView.getLastMapFocus().longitude)
val distance = mylocation.distanceTo(dest_location) val distance = mylocation.distanceTo(dest_location)
return if (distance > 2000.0 * 3 / 4) { return (distance <= 2000.0 * 3 / 4)
false
} else {
true
}
} }
fun onMapReady() { fun onMapReady() {