mirror of
				https://github.com/commons-app/apps-android-commons.git
				synced 2025-10-30 22:34:02 +01:00 
			
		
		
		
	#3745 Upload depiction suggestions not following text input - on upstream error emit empty list (#3751)
This commit is contained in:
		
							parent
							
								
									affcd8a6d7
								
							
						
					
					
						commit
						e597a7c96f
					
				
					 3 changed files with 26 additions and 16 deletions
				
			
		|  | @ -40,7 +40,7 @@ class DepictsClient @Inject constructor( | |||
|         return depictsInterface.searchForDepicts(query, "$limit", language, language, "$offset") | ||||
|             .map { it.search.joinToString("|") { searchItem -> searchItem.id } } | ||||
|             .flatMap(::getEntities) | ||||
|             .map { it.entities()?.values?.map(::DepictedItem) ?: emptyList() } | ||||
|             .map { it.entities().values.map(::DepictedItem) } | ||||
|     } | ||||
| 
 | ||||
|     /** | ||||
|  | @ -81,7 +81,7 @@ class DepictsClient @Inject constructor( | |||
|     fun toDepictions(sparqlResponse: Observable<SparqlResponse>): Observable<List<DepictedItem>> { | ||||
|         return sparqlResponse.map { it.results.bindings.joinToString("|", transform = Binding::id) } | ||||
|             .flatMap { getEntities(it).toObservable() } | ||||
|             .map { it.entities()?.values?.map(::DepictedItem) ?: emptyList() } | ||||
|             .map { it.entities().values.map(::DepictedItem) } | ||||
|     } | ||||
| 
 | ||||
|     companion object { | ||||
|  |  | |||
|  | @ -3,7 +3,9 @@ package fr.free.nrw.commons.upload.structure.depictions | |||
| import fr.free.nrw.commons.explore.depictions.DepictsClient | ||||
| import fr.free.nrw.commons.nearby.Place | ||||
| import io.reactivex.Flowable | ||||
| import io.reactivex.Single | ||||
| import io.reactivex.processors.BehaviorProcessor | ||||
| import timber.log.Timber | ||||
| import javax.inject.Inject | ||||
| import javax.inject.Singleton | ||||
| 
 | ||||
|  | @ -15,7 +17,6 @@ class DepictModel @Inject constructor(private val depictsClient: DepictsClient) | |||
| 
 | ||||
|     val nearbyPlaces: BehaviorProcessor<List<Place>> = BehaviorProcessor.createDefault(emptyList()) | ||||
| 
 | ||||
| 
 | ||||
|     companion object { | ||||
|         private const val SEARCH_DEPICTS_LIMIT = 25 | ||||
|     } | ||||
|  | @ -24,23 +25,24 @@ class DepictModel @Inject constructor(private val depictsClient: DepictsClient) | |||
|      * Search for depictions | ||||
|      */ | ||||
|     fun searchAllEntities(query: String): Flowable<List<DepictedItem>> { | ||||
|         if (query.isBlank()) { | ||||
|             return nearbyPlaces.switchMap { places: List<Place> -> | ||||
|                 depictsClient.getEntities( | ||||
|                     places.mapNotNull { it.wikiDataEntityId }.joinToString("|") | ||||
|                 ) | ||||
|         return if (query.isBlank()) | ||||
|             nearbyPlaces.switchMap { places: List<Place> -> | ||||
|                 depictsClient.getEntities(places.toIds()) | ||||
|                     .map { | ||||
|                         it.entities()!!.values.mapIndexed { index, entity -> | ||||
|                             DepictedItem(entity, places[index]) | ||||
|                         } | ||||
|                     }.toFlowable() | ||||
|                         it.entities() | ||||
|                             .values | ||||
|                             .mapIndexed { index, entity -> DepictedItem(entity, places[index]) } | ||||
|                     } | ||||
|                     .onErrorResumeWithEmptyList() | ||||
|                     .toFlowable() | ||||
|             } | ||||
|         } | ||||
|         return networkItems(query) | ||||
|         else | ||||
|             networkItems(query) | ||||
|     } | ||||
| 
 | ||||
|     private fun networkItems(query: String): Flowable<List<DepictedItem>> { | ||||
|         return depictsClient.searchForDepictions(query, SEARCH_DEPICTS_LIMIT, 0) | ||||
|             .onErrorResumeWithEmptyList() | ||||
|             .toFlowable() | ||||
|     } | ||||
| 
 | ||||
|  | @ -49,3 +51,9 @@ class DepictModel @Inject constructor(private val depictsClient: DepictsClient) | |||
|     } | ||||
| 
 | ||||
| } | ||||
| 
 | ||||
| private fun List<Place>.toIds() = mapNotNull { it.wikiDataEntityId }.joinToString("|") | ||||
| 
 | ||||
| private fun <T> Single<List<T>>.onErrorResumeWithEmptyList() = onErrorResumeNext { t: Throwable -> | ||||
|     Single.just(emptyList<T>()).also { Timber.e(t) } | ||||
| } | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Seán Mac Gillicuddy
						Seán Mac Gillicuddy