From d735e1c57d6737d3cad8616a59f9fd189c24779a Mon Sep 17 00:00:00 2001 From: Jason Whitmore Date: Tue, 18 Feb 2025 17:18:06 -0800 Subject: [PATCH] NearbyParentFragmentPresenter.kt: change loadPlacesDataAsync to retry HTTP requests after failure Before this commit, when an HTTP request failed, the entire batch of Places would not get updated, even if it was only one Place in the batch that caused the failure. This commit changes the code such that upon HTTP request failure, new HTTP requests are sent with one Place per request. This allows more Places to be fetched from the server. --- .../NearbyParentFragmentPresenter.kt | 30 +++++++++++++++---- 1 file changed, 25 insertions(+), 5 deletions(-) diff --git a/app/src/main/java/fr/free/nrw/commons/nearby/presenter/NearbyParentFragmentPresenter.kt b/app/src/main/java/fr/free/nrw/commons/nearby/presenter/NearbyParentFragmentPresenter.kt index 21892cff1..ed84751b0 100644 --- a/app/src/main/java/fr/free/nrw/commons/nearby/presenter/NearbyParentFragmentPresenter.kt +++ b/app/src/main/java/fr/free/nrw/commons/nearby/presenter/NearbyParentFragmentPresenter.kt @@ -25,9 +25,12 @@ import kotlinx.coroutines.Job import kotlinx.coroutines.channels.Channel import kotlinx.coroutines.delay import kotlinx.coroutines.ensureActive +import kotlinx.coroutines.job import kotlinx.coroutines.launch import kotlinx.coroutines.withContext +import okhttp3.internal.wait import timber.log.Timber +import java.io.IOException import java.lang.reflect.InvocationHandler import java.lang.reflect.Method import java.lang.reflect.Proxy @@ -379,13 +382,32 @@ class NearbyParentFragmentPresenter ) } catch (e: Exception) { Timber.tag("NearbyPinDetails").e(e) - collectResults.send(indices.map { Pair(it, updatedGroups[it]) }) + //HTTP request failed. Try individual places + for (i in indices) { + launch { + val onePlaceBatch = mutableListOf>() + try { + val fetchedPlace = nearbyController.getPlaces( + mutableListOf(updatedGroups[i].place) + ) + + onePlaceBatch.add(Pair(i, MarkerPlaceGroup( + bookmarkLocationDao.findBookmarkLocation( + fetchedPlace[0]),fetchedPlace[0]))) + } catch (e: Exception) { + Timber.tag("NearbyPinDetails").e(e) + onePlaceBatch.add(Pair(i, updatedGroups[i])) + } + collectResults.send(onePlaceBatch) + } + } } } } } var collectCount = 0 - for (resultList in collectResults) { + while (collectCount < indicesToUpdate.size) { + val resultList = collectResults.receive() for ((index, fetchedPlaceGroup) in resultList) { val existingPlace = updatedGroups[index].place val finalPlaceGroup = MarkerPlaceGroup( @@ -442,9 +464,7 @@ class NearbyParentFragmentPresenter } } schedulePlacesUpdate(updatedGroups) - if (++collectCount == totalBatches) { - break - } + collectCount += resultList.size } collectResults.close() }