mirror of
https://github.com/commons-app/apps-android-commons.git
synced 2025-10-26 12:23:58 +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")
|
return depictsInterface.searchForDepicts(query, "$limit", language, language, "$offset")
|
||||||
.map { it.search.joinToString("|") { searchItem -> searchItem.id } }
|
.map { it.search.joinToString("|") { searchItem -> searchItem.id } }
|
||||||
.flatMap(::getEntities)
|
.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>> {
|
fun toDepictions(sparqlResponse: Observable<SparqlResponse>): Observable<List<DepictedItem>> {
|
||||||
return sparqlResponse.map { it.results.bindings.joinToString("|", transform = Binding::id) }
|
return sparqlResponse.map { it.results.bindings.joinToString("|", transform = Binding::id) }
|
||||||
.flatMap { getEntities(it).toObservable() }
|
.flatMap { getEntities(it).toObservable() }
|
||||||
.map { it.entities()?.values?.map(::DepictedItem) ?: emptyList() }
|
.map { it.entities().values.map(::DepictedItem) }
|
||||||
}
|
}
|
||||||
|
|
||||||
companion object {
|
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.explore.depictions.DepictsClient
|
||||||
import fr.free.nrw.commons.nearby.Place
|
import fr.free.nrw.commons.nearby.Place
|
||||||
import io.reactivex.Flowable
|
import io.reactivex.Flowable
|
||||||
|
import io.reactivex.Single
|
||||||
import io.reactivex.processors.BehaviorProcessor
|
import io.reactivex.processors.BehaviorProcessor
|
||||||
|
import timber.log.Timber
|
||||||
import javax.inject.Inject
|
import javax.inject.Inject
|
||||||
import javax.inject.Singleton
|
import javax.inject.Singleton
|
||||||
|
|
||||||
|
|
@ -15,7 +17,6 @@ class DepictModel @Inject constructor(private val depictsClient: DepictsClient)
|
||||||
|
|
||||||
val nearbyPlaces: BehaviorProcessor<List<Place>> = BehaviorProcessor.createDefault(emptyList())
|
val nearbyPlaces: BehaviorProcessor<List<Place>> = BehaviorProcessor.createDefault(emptyList())
|
||||||
|
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
private const val SEARCH_DEPICTS_LIMIT = 25
|
private const val SEARCH_DEPICTS_LIMIT = 25
|
||||||
}
|
}
|
||||||
|
|
@ -24,23 +25,24 @@ class DepictModel @Inject constructor(private val depictsClient: DepictsClient)
|
||||||
* Search for depictions
|
* Search for depictions
|
||||||
*/
|
*/
|
||||||
fun searchAllEntities(query: String): Flowable<List<DepictedItem>> {
|
fun searchAllEntities(query: String): Flowable<List<DepictedItem>> {
|
||||||
if (query.isBlank()) {
|
return if (query.isBlank())
|
||||||
return nearbyPlaces.switchMap { places: List<Place> ->
|
nearbyPlaces.switchMap { places: List<Place> ->
|
||||||
depictsClient.getEntities(
|
depictsClient.getEntities(places.toIds())
|
||||||
places.mapNotNull { it.wikiDataEntityId }.joinToString("|")
|
|
||||||
)
|
|
||||||
.map {
|
.map {
|
||||||
it.entities()!!.values.mapIndexed { index, entity ->
|
it.entities()
|
||||||
DepictedItem(entity, places[index])
|
.values
|
||||||
}
|
.mapIndexed { index, entity -> DepictedItem(entity, places[index]) }
|
||||||
}.toFlowable()
|
}
|
||||||
|
.onErrorResumeWithEmptyList()
|
||||||
|
.toFlowable()
|
||||||
}
|
}
|
||||||
}
|
else
|
||||||
return networkItems(query)
|
networkItems(query)
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun networkItems(query: String): Flowable<List<DepictedItem>> {
|
private fun networkItems(query: String): Flowable<List<DepictedItem>> {
|
||||||
return depictsClient.searchForDepictions(query, SEARCH_DEPICTS_LIMIT, 0)
|
return depictsClient.searchForDepictions(query, SEARCH_DEPICTS_LIMIT, 0)
|
||||||
|
.onErrorResumeWithEmptyList()
|
||||||
.toFlowable()
|
.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) }
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -7,6 +7,7 @@ import java.util.Collections;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import org.apache.commons.lang3.StringUtils;
|
import org.apache.commons.lang3.StringUtils;
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
import org.wikipedia.dataclient.mwapi.MwResponse;
|
import org.wikipedia.dataclient.mwapi.MwResponse;
|
||||||
import org.wikipedia.json.PostProcessingTypeAdapter;
|
import org.wikipedia.json.PostProcessingTypeAdapter;
|
||||||
|
|
||||||
|
|
@ -15,8 +16,9 @@ public class Entities extends MwResponse implements PostProcessingTypeAdapter.Po
|
||||||
@Nullable private Map<String, Entity> entities;
|
@Nullable private Map<String, Entity> entities;
|
||||||
private int success;
|
private int success;
|
||||||
|
|
||||||
@Nullable public Map<String, Entity> entities() {
|
@NotNull
|
||||||
return entities;
|
public Map<String, Entity> entities() {
|
||||||
|
return entities != null ? entities : Collections.emptyMap();
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getSuccess() {
|
public int getSuccess() {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue