mirror of
https://github.com/commons-app/apps-android-commons.git
synced 2025-10-27 04:43:54 +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")
|
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) }
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -85,7 +85,7 @@ class DepictsClient @Inject constructor(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
.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 {
|
||||||
|
|
|
||||||
|
|
@ -9,7 +9,6 @@ import fr.free.nrw.commons.wikidata.WikidataDisambiguationItems
|
||||||
import io.reactivex.Flowable
|
import io.reactivex.Flowable
|
||||||
import io.reactivex.Scheduler
|
import io.reactivex.Scheduler
|
||||||
import io.reactivex.disposables.CompositeDisposable
|
import io.reactivex.disposables.CompositeDisposable
|
||||||
import io.reactivex.functions.BiFunction
|
|
||||||
import io.reactivex.processors.PublishProcessor
|
import io.reactivex.processors.PublishProcessor
|
||||||
import timber.log.Timber
|
import timber.log.Timber
|
||||||
import java.lang.reflect.Proxy
|
import java.lang.reflect.Proxy
|
||||||
|
|
@ -59,12 +58,8 @@ class DepictsPresenter @Inject constructor(
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun searchResultsWithTerm(it: String): Flowable<Pair<List<DepictedItem>, String>> {
|
private fun searchResultsWithTerm(term: String): Flowable<Pair<List<DepictedItem>, String>> {
|
||||||
return Flowable.zip(
|
return searchResults(term).map { Pair(it, term) }
|
||||||
searchResults(it),
|
|
||||||
Flowable.just(it),
|
|
||||||
BiFunction { results: List<DepictedItem>, term: String -> Pair(results, term) }
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun searchResults(it: String): Flowable<List<DepictedItem>> {
|
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.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
|
||||||
|
|
||||||
|
|
@ -13,8 +15,7 @@ import javax.inject.Singleton
|
||||||
@Singleton
|
@Singleton
|
||||||
class DepictModel @Inject constructor(private val depictsClient: DepictsClient) {
|
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 {
|
companion object {
|
||||||
private const val SEARCH_DEPICTS_LIMIT = 25
|
private const val SEARCH_DEPICTS_LIMIT = 25
|
||||||
|
|
@ -24,28 +25,35 @@ 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()
|
||||||
}
|
}
|
||||||
|
|
||||||
fun cleanUp() {
|
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.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