Skip to content

Commit 2290545

Browse files
authored
Suggest and auto fill title and description based on image location (commons-app#3323)
* Suggest and auto fill title and description based on image location * with java docs
1 parent afdeaae commit 2290545

File tree

7 files changed

+92
-3
lines changed

7 files changed

+92
-3
lines changed

app/src/main/java/fr/free/nrw/commons/nearby/NearbyPlaces.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ List<Place> radiusExpander(LatLng curLatLng, String lang, boolean returnClosestR
9191
* @return list of places obtained
9292
* @throws IOException if query fails
9393
*/
94-
private List<Place> getFromWikidataQuery(LatLng cur, String lang, double radius) throws IOException {
94+
public List<Place> getFromWikidataQuery(LatLng cur, String lang, double radius) throws IOException {
9595
return okHttpJsonApiClient.getNearbyPlaces(cur, lang, radius).blockingSingle();
9696
}
9797
}

app/src/main/java/fr/free/nrw/commons/repository/UploadRemoteDataSource.java

+28-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package fr.free.nrw.commons.repository;
22

3+
import java.io.IOException;
34
import java.util.Comparator;
45
import java.util.List;
6+
import java.util.Locale;
57

68
import javax.inject.Inject;
79
import javax.inject.Singleton;
@@ -10,6 +12,8 @@
1012
import fr.free.nrw.commons.category.CategoryItem;
1113
import fr.free.nrw.commons.contributions.Contribution;
1214
import fr.free.nrw.commons.filepicker.UploadableFile;
15+
import fr.free.nrw.commons.location.LatLng;
16+
import fr.free.nrw.commons.nearby.NearbyPlaces;
1317
import fr.free.nrw.commons.nearby.Place;
1418
import fr.free.nrw.commons.upload.SimilarImageInterface;
1519
import fr.free.nrw.commons.upload.UploadController;
@@ -24,16 +28,21 @@
2428
@Singleton
2529
public class UploadRemoteDataSource {
2630

31+
private static final double NEARBY_RADIUS_IN_KILO_METERS = 0.1; //100 meters
32+
2733
private UploadModel uploadModel;
2834
private UploadController uploadController;
2935
private CategoriesModel categoriesModel;
36+
private NearbyPlaces nearbyPlaces;
3037

3138
@Inject
3239
public UploadRemoteDataSource(UploadModel uploadModel, UploadController uploadController,
33-
CategoriesModel categoriesModel) {
40+
CategoriesModel categoriesModel,
41+
NearbyPlaces nearbyPlaces) {
3442
this.uploadModel = uploadModel;
3543
this.uploadController = uploadController;
3644
this.categoriesModel = categoriesModel;
45+
this.nearbyPlaces = nearbyPlaces;
3746
}
3847

3948
/**
@@ -176,4 +185,22 @@ public Observable<UploadItem> preProcessImage(UploadableFile uploadableFile, Pla
176185
public Single<Integer> getImageQuality(UploadItem uploadItem, boolean shouldValidateTitle) {
177186
return uploadModel.getImageQuality(uploadItem, shouldValidateTitle);
178187
}
188+
189+
/**
190+
* gets nearby places matching with upload item's GPS location
191+
*
192+
* @param latitude
193+
* @param longitude
194+
* @return
195+
*/
196+
public Place getNearbyPlaces(double latitude, double longitude) {
197+
try {
198+
List<Place> fromWikidataQuery = nearbyPlaces.getFromWikidataQuery(new LatLng(latitude, longitude, 0.0f),
199+
Locale.getDefault().getLanguage(),
200+
NEARBY_RADIUS_IN_KILO_METERS);
201+
return fromWikidataQuery.size() > 0 ? fromWikidataQuery.get(0) : null;
202+
} catch (IOException e) {
203+
return null;
204+
}
205+
}
179206
}

app/src/main/java/fr/free/nrw/commons/repository/UploadRepository.java

+10
Original file line numberDiff line numberDiff line change
@@ -262,4 +262,14 @@ public String getValue(String key, String value) {
262262
public void setSelectedLicense(String licenseName) {
263263
localDataSource.setSelectedLicense(licenseName);
264264
}
265+
266+
/**
267+
* Returns nearest place matching the passed latitude and longitude
268+
* @param decLatitude
269+
* @param decLongitude
270+
* @return
271+
*/
272+
public Place checkNearbyPlaces(double decLatitude, double decLongitude) {
273+
return remoteDataSource.getNearbyPlaces(decLatitude, decLongitude);
274+
}
265275
}

app/src/main/java/fr/free/nrw/commons/upload/mediaDetails/UploadMediaDetailFragment.java

+28
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package fr.free.nrw.commons.upload.mediaDetails;
22

3+
import android.annotation.SuppressLint;
34
import android.content.Context;
45
import android.os.Bundle;
56
import android.text.TextUtils;
@@ -25,6 +26,7 @@
2526
import org.apache.commons.lang3.StringUtils;
2627

2728
import java.util.ArrayList;
29+
import java.util.Arrays;
2830
import java.util.List;
2931
import java.util.Locale;
3032

@@ -297,6 +299,32 @@ public void onImageProcessed(UploadItem uploadItem, Place place) {
297299
setDescriptionsInAdapter(descriptions);
298300
}
299301

302+
/**
303+
* Shows popup if any nearby location needing pictures matches uploadable picture's GPS location
304+
* @param uploadItem
305+
* @param place
306+
*/
307+
@SuppressLint("StringFormatInvalid")
308+
@Override
309+
public void onNearbyPlaceFound(UploadItem uploadItem, Place place) {
310+
DialogUtil.showAlertDialog(getActivity(),
311+
getString(R.string.upload_nearby_place_found_title),
312+
String.format(Locale.getDefault(),
313+
getString(R.string.upload_nearby_place_found_description),
314+
place.getName()),
315+
() -> {
316+
317+
},
318+
() -> {
319+
etTitle.setText(place.getName());
320+
Description description = new Description();
321+
description.setLanguageCode("en");
322+
description.setDescriptionText(place.getLongDescription());
323+
descriptions = Arrays.asList(description);
324+
setDescriptionsInAdapter(descriptions);
325+
});
326+
}
327+
300328
@Override
301329
public void showProgress(boolean shouldShow) {
302330
callback.showProgress(shouldShow);

app/src/main/java/fr/free/nrw/commons/upload/mediaDetails/UploadMediaDetailsContract.java

+2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ interface View extends SimilarImageInterface {
1919

2020
void onImageProcessed(UploadItem uploadItem, Place place);
2121

22+
void onNearbyPlaceFound(UploadItem uploadItem, Place place);
23+
2224
void showProgress(boolean shouldShow);
2325

2426
void onImageValidationSuccess();

app/src/main/java/fr/free/nrw/commons/upload/mediaDetails/UploadMediaPresenter.java

+20-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import fr.free.nrw.commons.upload.UploadModel.UploadItem;
1515
import fr.free.nrw.commons.upload.mediaDetails.UploadMediaDetailsContract.UserActionListener;
1616
import fr.free.nrw.commons.upload.mediaDetails.UploadMediaDetailsContract.View;
17+
import io.reactivex.Observable;
1718
import io.reactivex.Scheduler;
1819
import io.reactivex.disposables.CompositeDisposable;
1920
import io.reactivex.disposables.Disposable;
@@ -81,13 +82,31 @@ public void receiveImage(UploadableFile uploadableFile, String source, Place pla
8182
{
8283
view.onImageProcessed(uploadItem, place);
8384
GPSExtractor gpsCoords = uploadItem.getGpsCoords();
84-
view.showMapWithImageCoordinates((gpsCoords != null && gpsCoords.imageCoordsExists) ? true : false);
85+
view.showMapWithImageCoordinates(gpsCoords != null && gpsCoords.imageCoordsExists);
8586
view.showProgress(false);
87+
if (gpsCoords != null && gpsCoords.imageCoordsExists) {
88+
checkNearbyPlaces(uploadItem);
89+
}
8690
},
8791
throwable -> Timber.e(throwable, "Error occurred in processing images"));
8892
compositeDisposable.add(uploadItemDisposable);
8993
}
9094

95+
/**
96+
* This method checks for the nearest location that needs images and suggests it to the user.
97+
* @param uploadItem
98+
*/
99+
private void checkNearbyPlaces(UploadItem uploadItem) {
100+
Disposable checkNearbyPlaces = Observable.fromCallable(() -> repository
101+
.checkNearbyPlaces(uploadItem.getGpsCoords().getDecLatitude(),
102+
uploadItem.getGpsCoords().getDecLongitude()))
103+
.subscribeOn(ioScheduler)
104+
.observeOn(mainThreadScheduler)
105+
.subscribe(place -> view.onNearbyPlaceFound(uploadItem, place),
106+
throwable -> Timber.e(throwable, "Error occurred in processing images"));
107+
compositeDisposable.add(checkNearbyPlaces);
108+
}
109+
91110
/**
92111
* asks the repository to verify image quality
93112
*

app/src/main/res/values/strings.xml

+3
Original file line numberDiff line numberDiff line change
@@ -588,4 +588,7 @@ Upload your first media by tapping on the add button.</string>
588588
<string name="place_type">Place type:</string>
589589
<string name="nearby_search_hint">Bridge, museum, hotel etc.</string>
590590
<string name="you_must_reset_your_passsword">Something went wrong with login, you must reset your password !!</string>
591+
592+
<string name="upload_nearby_place_found_title">Nearby Place Found</string>
593+
<string name="upload_nearby_place_found_description">Is this a photo of Place %1$s?</string>
591594
</resources>

0 commit comments

Comments
 (0)