mirror of
https://github.com/commons-app/apps-android-commons.git
synced 2025-10-26 12:23:58 +01:00
Merge remote-tracking branch 'origin/master' into macgills/3468-adapter-delegates
This commit is contained in:
commit
9f2dcb8c5e
4 changed files with 30 additions and 25 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) }
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -85,7 +85,7 @@ class DepictsClient @Inject constructor(
|
|||
}
|
||||
}
|
||||
.flatMap { getEntities(it).toObservable() }
|
||||
.map { it.entities()?.values?.map(::DepictedItem) ?: emptyList() }
|
||||
.map { it.entities().values.map(::DepictedItem) }
|
||||
}
|
||||
|
||||
companion object {
|
||||
|
|
|
|||
|
|
@ -9,7 +9,6 @@ import fr.free.nrw.commons.wikidata.WikidataDisambiguationItems
|
|||
import io.reactivex.Flowable
|
||||
import io.reactivex.Scheduler
|
||||
import io.reactivex.disposables.CompositeDisposable
|
||||
import io.reactivex.functions.BiFunction
|
||||
import io.reactivex.processors.PublishProcessor
|
||||
import timber.log.Timber
|
||||
import java.lang.reflect.Proxy
|
||||
|
|
@ -59,12 +58,8 @@ class DepictsPresenter @Inject constructor(
|
|||
)
|
||||
}
|
||||
|
||||
private fun searchResultsWithTerm(it: String): Flowable<Pair<List<DepictedItem>, String>> {
|
||||
return Flowable.zip(
|
||||
searchResults(it),
|
||||
Flowable.just(it),
|
||||
BiFunction { results: List<DepictedItem>, term: String -> Pair(results, term) }
|
||||
)
|
||||
private fun searchResultsWithTerm(term: String): Flowable<Pair<List<DepictedItem>, String>> {
|
||||
return searchResults(term).map { Pair(it, term) }
|
||||
}
|
||||
|
||||
private fun searchResults(it: String): Flowable<List<DepictedItem>> {
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
||||
|
|
@ -13,8 +15,7 @@ import javax.inject.Singleton
|
|||
@Singleton
|
||||
class DepictModel @Inject constructor(private val depictsClient: DepictsClient) {
|
||||
|
||||
var nearbyPlaces: BehaviorProcessor<List<Place>> = BehaviorProcessor.createDefault(emptyList())
|
||||
|
||||
val nearbyPlaces: BehaviorProcessor<List<Place>> = BehaviorProcessor.createDefault(emptyList())
|
||||
|
||||
companion object {
|
||||
private const val SEARCH_DEPICTS_LIMIT = 25
|
||||
|
|
@ -24,28 +25,35 @@ 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()
|
||||
}
|
||||
|
||||
fun cleanUp() {
|
||||
nearbyPlaces = BehaviorProcessor.createDefault(emptyList())
|
||||
nearbyPlaces.offer(emptyList())
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
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.Map;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.wikipedia.dataclient.mwapi.MwResponse;
|
||||
import org.wikipedia.json.PostProcessingTypeAdapter;
|
||||
|
||||
|
|
@ -15,8 +16,9 @@ public class Entities extends MwResponse implements PostProcessingTypeAdapter.Po
|
|||
@Nullable private Map<String, Entity> entities;
|
||||
private int success;
|
||||
|
||||
@Nullable public Map<String, Entity> entities() {
|
||||
return entities;
|
||||
@NotNull
|
||||
public Map<String, Entity> entities() {
|
||||
return entities != null ? entities : Collections.emptyMap();
|
||||
}
|
||||
|
||||
public int getSuccess() {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue