From 7bcaab2442739c09012e16329d072de6bf84fa98 Mon Sep 17 00:00:00 2001 From: Pratham Pahariya <54663429+Pratham2305@users.noreply.github.com> Date: Wed, 19 Jan 2022 16:58:38 +0530 Subject: [PATCH] Fixes #53 - Suggest depicted country based on EXIF latitude/longitude (#4756) * Fix - Suggest depicted country based on EXIF latitude/longitude * Added null check and moved country suggestion to the end of the list --- app/build.gradle | 2 ++ .../commons/repository/UploadRepository.java | 8 ++--- .../structure/depictions/DepictModel.kt | 32 +++++++++++++------ 3 files changed, 28 insertions(+), 14 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index 8dbfa3649..e103a1b88 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -148,6 +148,8 @@ dependencies { //Glide implementation 'com.github.bumptech.glide:glide:4.12.0' annotationProcessor 'com.github.bumptech.glide:compiler:4.12.0' + + implementation("io.github.coordinates2country:coordinates2country-android:1.2") { exclude group: 'com.google.android', module: 'android' } } android { diff --git a/app/src/main/java/fr/free/nrw/commons/repository/UploadRepository.java b/app/src/main/java/fr/free/nrw/commons/repository/UploadRepository.java index e51fb4969..f91519ef8 100644 --- a/app/src/main/java/fr/free/nrw/commons/repository/UploadRepository.java +++ b/app/src/main/java/fr/free/nrw/commons/repository/UploadRepository.java @@ -255,7 +255,7 @@ public class UploadRepository { */ public Flowable> searchAllEntities(String query) { - return depictModel.searchAllEntities(query); + return depictModel.searchAllEntities(query, this); } /** @@ -265,14 +265,14 @@ public class UploadRepository { * @return a single that provides the depictions */ public Single> getPlaceDepictions() { - final Set places = new HashSet<>(); + final Set qids = new HashSet<>(); for (final UploadItem item : getUploads()) { final Place place = item.getPlace(); if (place != null) { - places.add(place); + qids.add(place.getWikiDataEntityId()); } } - return depictModel.getPlaceDepictions(new ArrayList<>(places)); + return depictModel.getPlaceDepictions(new ArrayList<>(qids)); } /** diff --git a/app/src/main/java/fr/free/nrw/commons/upload/structure/depictions/DepictModel.kt b/app/src/main/java/fr/free/nrw/commons/upload/structure/depictions/DepictModel.kt index 3bb3b4fbb..3caa2934d 100644 --- a/app/src/main/java/fr/free/nrw/commons/upload/structure/depictions/DepictModel.kt +++ b/app/src/main/java/fr/free/nrw/commons/upload/structure/depictions/DepictModel.kt @@ -2,6 +2,8 @@ package fr.free.nrw.commons.upload.structure.depictions import fr.free.nrw.commons.explore.depictions.DepictsClient import fr.free.nrw.commons.nearby.Place +import fr.free.nrw.commons.repository.UploadRepository +import io.github.coordinates2country.Coordinates2Country import io.reactivex.Flowable import io.reactivex.Observable import io.reactivex.Single @@ -25,27 +27,37 @@ class DepictModel @Inject constructor(private val depictsClient: DepictsClient) /** * Search for depictions */ - fun searchAllEntities(query: String): Flowable> { - return if (query.isBlank()) + fun searchAllEntities(query: String, repository: UploadRepository): Flowable> { + return if (query.isBlank()) { nearbyPlaces.switchMap { places: List -> - getPlaceDepictions(places).toFlowable() + val qids = mutableSetOf() + for(place in places) { + place.wikiDataEntityId?.let { qids.add(it) } + } + repository.uploads.forEach { item -> + if(item.gpsCoords != null && item.gpsCoords.imageCoordsExists) { + Coordinates2Country.countryQID(item.gpsCoords.decLatitude, + item.gpsCoords.decLongitude)?.let { qids.add("Q$it") } + } + } + getPlaceDepictions(ArrayList(qids)).toFlowable() } - else + } else networkItems(query) } /** - * Provides [DepictedItem] instances via a [Single] for a given list of [Place], providing an - * empty list if no places are provided or if there is an error + * Provides [DepictedItem] instances via a [Single] for a given list of ids, providing an + * empty list if no places/country are provided or if there is an error */ - fun getPlaceDepictions(places: List): Single> = - places.toIds().let { ids -> + fun getPlaceDepictions(qids: List): Single> = + qids.toIds().let { ids -> if (ids.isNotEmpty()) depictsClient.getEntities(ids) .map{ it.entities() .values - .mapIndexed { index, entity -> DepictedItem(entity, places[index])} + .mapIndexed { index, entity -> DepictedItem(entity)} } .onErrorResumeWithEmptyList() else Single.just(emptyList()) @@ -75,7 +87,7 @@ class DepictModel @Inject constructor(private val depictsClient: DepictsClient) } -private fun List.toIds() = mapNotNull { it.wikiDataEntityId }.joinToString("|") +private fun List.toIds() = mapNotNull { it }.joinToString("|") private fun Single>.onErrorResumeWithEmptyList() = onErrorResumeNext { t: Throwable -> Single.just(emptyList()).also { Timber.e(t) }