Skip to content

Commit 2d63f35

Browse files
authored
Made Split to Nearby Query into a fast query for coordinates + a details query for each pin (#5731)
* Splitted the query * Made changes to the query * Improvised query * Improvised query by dividing in the batches * Fixed failing tests * Improved batches * Improved sorting * Fixes issue caused by search this area button * Fixed failing tests * Fixed unnecessary reloads on onResume * Fixed few pins not loading on changing apps * Improved zoom level and fixed the pins not loading from the center * Removed toggle chips and changed pin's color * Fixed wikidata url * Fixed unit tests * Implemented retry with delay of 5000ms * Fixed exception issue and pins issue * Added change color icon to pin * Improved pin clicking * Removed search this area button * Implemented caching of places * Fixed unit test * Factorized methods * Changed primary key from location to entity id * Fixed tests * Fixed conflicts * Fixed unit test * Fixed unit test * Fixed the bug * Fixed issue with pin loading on the first launch * Updated javadocs * Temporary commit - only for testing * Replaced Temporary commit * Temporary commit - Added jcenter * Made minor changes * Fixed unit tests * Fixed unit tests * Fixed minor bug
1 parent ba6c8fe commit 2d63f35

39 files changed

+1127
-794
lines changed

app/src/main/java/fr/free/nrw/commons/bookmarks/locations/BookmarkLocationsDao.java

+41-41
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,11 @@ public List<Place> getAllBookmarksLocations() {
4646
ContentProviderClient db = clientProvider.get();
4747
try {
4848
cursor = db.query(
49-
BookmarkLocationsContentProvider.BASE_URI,
50-
Table.ALL_FIELDS,
51-
null,
52-
new String[]{},
53-
null);
49+
BookmarkLocationsContentProvider.BASE_URI,
50+
Table.ALL_FIELDS,
51+
null,
52+
new String[]{},
53+
null);
5454
while (cursor != null && cursor.moveToNext()) {
5555
items.add(fromCursor(cursor));
5656
}
@@ -126,11 +126,11 @@ public boolean findBookmarkLocation(Place bookmarkLocation) {
126126
ContentProviderClient db = clientProvider.get();
127127
try {
128128
cursor = db.query(
129-
BookmarkLocationsContentProvider.BASE_URI,
130-
Table.ALL_FIELDS,
131-
Table.COLUMN_NAME + "=?",
132-
new String[]{bookmarkLocation.name},
133-
null);
129+
BookmarkLocationsContentProvider.BASE_URI,
130+
Table.ALL_FIELDS,
131+
Table.COLUMN_NAME + "=?",
132+
new String[]{bookmarkLocation.name},
133+
null);
134134
if (cursor != null && cursor.moveToFirst()) {
135135
return true;
136136
}
@@ -149,7 +149,7 @@ public boolean findBookmarkLocation(Place bookmarkLocation) {
149149
@NonNull
150150
Place fromCursor(final Cursor cursor) {
151151
final LatLng location = new LatLng(cursor.getDouble(cursor.getColumnIndex(Table.COLUMN_LAT)),
152-
cursor.getDouble(cursor.getColumnIndex(Table.COLUMN_LONG)), 1F);
152+
cursor.getDouble(cursor.getColumnIndex(Table.COLUMN_LONG)), 1F);
153153

154154
final Sitelinks.Builder builder = new Sitelinks.Builder();
155155
builder.setWikipediaLink(cursor.getString(cursor.getColumnIndex(Table.COLUMN_WIKIPEDIA_LINK)));
@@ -207,40 +207,40 @@ public static class Table {
207207

208208
// NOTE! KEEP IN SAME ORDER AS THEY ARE DEFINED UP THERE. HELPS HARD CODE COLUMN INDICES.
209209
public static final String[] ALL_FIELDS = {
210-
COLUMN_NAME,
211-
COLUMN_LANGUAGE,
212-
COLUMN_DESCRIPTION,
213-
COLUMN_CATEGORY,
214-
COLUMN_LABEL_TEXT,
215-
COLUMN_LABEL_ICON,
216-
COLUMN_LAT,
217-
COLUMN_LONG,
218-
COLUMN_IMAGE_URL,
219-
COLUMN_WIKIPEDIA_LINK,
220-
COLUMN_WIKIDATA_LINK,
221-
COLUMN_COMMONS_LINK,
222-
COLUMN_PIC,
223-
COLUMN_EXISTS,
210+
COLUMN_NAME,
211+
COLUMN_LANGUAGE,
212+
COLUMN_DESCRIPTION,
213+
COLUMN_CATEGORY,
214+
COLUMN_LABEL_TEXT,
215+
COLUMN_LABEL_ICON,
216+
COLUMN_LAT,
217+
COLUMN_LONG,
218+
COLUMN_IMAGE_URL,
219+
COLUMN_WIKIPEDIA_LINK,
220+
COLUMN_WIKIDATA_LINK,
221+
COLUMN_COMMONS_LINK,
222+
COLUMN_PIC,
223+
COLUMN_EXISTS,
224224
};
225225

226226
static final String DROP_TABLE_STATEMENT = "DROP TABLE IF EXISTS " + TABLE_NAME;
227227

228228
static final String CREATE_TABLE_STATEMENT = "CREATE TABLE " + TABLE_NAME + " ("
229-
+ COLUMN_NAME + " STRING PRIMARY KEY,"
230-
+ COLUMN_LANGUAGE + " STRING,"
231-
+ COLUMN_DESCRIPTION + " STRING,"
232-
+ COLUMN_CATEGORY + " STRING,"
233-
+ COLUMN_LABEL_TEXT + " STRING,"
234-
+ COLUMN_LABEL_ICON + " INTEGER,"
235-
+ COLUMN_LAT + " DOUBLE,"
236-
+ COLUMN_LONG + " DOUBLE,"
237-
+ COLUMN_IMAGE_URL + " STRING,"
238-
+ COLUMN_WIKIPEDIA_LINK + " STRING,"
239-
+ COLUMN_WIKIDATA_LINK + " STRING,"
240-
+ COLUMN_COMMONS_LINK + " STRING,"
241-
+ COLUMN_PIC + " STRING,"
242-
+ COLUMN_EXISTS + " STRING"
243-
+ ");";
229+
+ COLUMN_NAME + " STRING PRIMARY KEY,"
230+
+ COLUMN_LANGUAGE + " STRING,"
231+
+ COLUMN_DESCRIPTION + " STRING,"
232+
+ COLUMN_CATEGORY + " STRING,"
233+
+ COLUMN_LABEL_TEXT + " STRING,"
234+
+ COLUMN_LABEL_ICON + " INTEGER,"
235+
+ COLUMN_LAT + " DOUBLE,"
236+
+ COLUMN_LONG + " DOUBLE,"
237+
+ COLUMN_IMAGE_URL + " STRING,"
238+
+ COLUMN_WIKIPEDIA_LINK + " STRING,"
239+
+ COLUMN_WIKIDATA_LINK + " STRING,"
240+
+ COLUMN_COMMONS_LINK + " STRING,"
241+
+ COLUMN_PIC + " STRING,"
242+
+ COLUMN_EXISTS + " STRING"
243+
+ ");";
244244

245245
public static void onCreate(SQLiteDatabase db) {
246246
db.execSQL(CREATE_TABLE_STATEMENT);
@@ -308,4 +308,4 @@ public static void onUpdate(final SQLiteDatabase db, int from, final int to) {
308308
}
309309
}
310310
}
311-
}
311+
}

app/src/main/java/fr/free/nrw/commons/db/AppDatabase.kt

+4-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import androidx.room.TypeConverters
66
import fr.free.nrw.commons.contributions.Contribution
77
import fr.free.nrw.commons.contributions.ContributionDao
88
import fr.free.nrw.commons.customselector.database.*
9+
import fr.free.nrw.commons.nearby.Place
10+
import fr.free.nrw.commons.nearby.PlaceDao
911
import fr.free.nrw.commons.review.ReviewDao
1012
import fr.free.nrw.commons.review.ReviewEntity
1113
import fr.free.nrw.commons.upload.depicts.Depicts
@@ -15,10 +17,11 @@ import fr.free.nrw.commons.upload.depicts.DepictsDao
1517
* The database for accessing the respective DAOs
1618
*
1719
*/
18-
@Database(entities = [Contribution::class, Depicts::class, UploadedStatus::class, NotForUploadStatus::class, ReviewEntity::class], version = 16, exportSchema = false)
20+
@Database(entities = [Contribution::class, Depicts::class, UploadedStatus::class, NotForUploadStatus::class, ReviewEntity::class, Place::class], version = 18, exportSchema = false)
1921
@TypeConverters(Converters::class)
2022
abstract class AppDatabase : RoomDatabase() {
2123
abstract fun contributionDao(): ContributionDao
24+
abstract fun PlaceDao(): PlaceDao
2225
abstract fun DepictsDao(): DepictsDao;
2326
abstract fun UploadedStatusDao(): UploadedStatusDao;
2427
abstract fun NotForUploadStatusDao(): NotForUploadStatusDao

app/src/main/java/fr/free/nrw/commons/db/Converters.java

+14
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@
88
import fr.free.nrw.commons.contributions.ChunkInfo;
99
import fr.free.nrw.commons.di.ApplicationlessInjection;
1010
import fr.free.nrw.commons.location.LatLng;
11+
import fr.free.nrw.commons.nearby.Sitelinks;
1112
import fr.free.nrw.commons.upload.WikidataPlace;
1213
import fr.free.nrw.commons.upload.structure.depictions.DepictedItem;
14+
import java.lang.reflect.Type;
1315
import java.util.Date;
1416
import java.util.List;
1517
import java.util.Map;
@@ -134,6 +136,18 @@ public static List<DepictedItem> stringToList(String depictedItems) {
134136
return readObjectWithTypeToken(depictedItems, new TypeToken<List<DepictedItem>>() {});
135137
}
136138

139+
@TypeConverter
140+
public static Sitelinks sitelinksFromString(String value) {
141+
Type type = new TypeToken<Sitelinks>() {}.getType();
142+
return new Gson().fromJson(value, type);
143+
}
144+
145+
@TypeConverter
146+
public static String fromSitelinks(Sitelinks sitelinks) {
147+
Gson gson = new Gson();
148+
return gson.toJson(sitelinks);
149+
}
150+
137151
private static String writeObjectToString(Object object) {
138152
return object == null ? null : getGson().toJson(object);
139153
}

app/src/main/java/fr/free/nrw/commons/di/CommonsApplicationModule.java

+6
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import fr.free.nrw.commons.db.AppDatabase;
2525
import fr.free.nrw.commons.kvstore.JsonKvStore;
2626
import fr.free.nrw.commons.location.LocationServiceManager;
27+
import fr.free.nrw.commons.nearby.PlaceDao;
2728
import fr.free.nrw.commons.review.ReviewDao;
2829
import fr.free.nrw.commons.settings.Prefs;
2930
import fr.free.nrw.commons.upload.UploadController;
@@ -275,6 +276,11 @@ public ContributionDao providesContributionsDao(AppDatabase appDatabase) {
275276
return appDatabase.contributionDao();
276277
}
277278

279+
@Provides
280+
public PlaceDao providesPlaceDao(AppDatabase appDatabase) {
281+
return appDatabase.PlaceDao();
282+
}
283+
278284
/**
279285
* Get the reference of DepictsDao class.
280286
*/

app/src/main/java/fr/free/nrw/commons/mwapi/OkHttpJsonApiClient.java

+48
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,54 @@ public List<Place> getNearbyPlaces(
397397
throw new Exception(response.message());
398398
}
399399

400+
/**
401+
* Retrieves a list of places based on the provided list of places and language.
402+
*
403+
* @param placeList A list of Place objects for which to fetch information.
404+
* @param language The language code to use for the query.
405+
* @return A list of Place objects with additional information retrieved from Wikidata, or null
406+
* if an error occurs.
407+
* @throws IOException If there is an issue with reading the resource file or executing the HTTP
408+
* request.
409+
*/
410+
@Nullable
411+
public List<Place> getPlaces(
412+
final List<Place> placeList, final String language) throws IOException {
413+
final String wikidataQuery = FileUtils.readFromResource("/queries/query_for_item.rq");
414+
String qids = "";
415+
for (final Place place : placeList) {
416+
qids += "\n" + ("wd:" + place.getWikiDataEntityId());
417+
}
418+
final String query = wikidataQuery
419+
.replace("${ENTITY}", qids)
420+
.replace("${LANG}", language);
421+
final HttpUrl.Builder urlBuilder = HttpUrl
422+
.parse(sparqlQueryUrl)
423+
.newBuilder()
424+
.addQueryParameter("query", query)
425+
.addQueryParameter("format", "json");
426+
427+
final Request request = new Request.Builder()
428+
.url(urlBuilder.build())
429+
.build();
430+
431+
try (Response response = okHttpClient.newCall(request).execute()) {
432+
if (response.isSuccessful()) {
433+
final String json = response.body().string();
434+
final NearbyResponse nearbyResponse = gson.fromJson(json, NearbyResponse.class);
435+
final List<NearbyResultItem> bindings = nearbyResponse.getResults().getBindings();
436+
final List<Place> places = new ArrayList<>();
437+
for (final NearbyResultItem item : bindings) {
438+
final Place placeFromNearbyItem = Place.from(item);
439+
places.add(placeFromNearbyItem);
440+
}
441+
return places;
442+
} else {
443+
throw new IOException("Unexpected response code: " + response.code());
444+
}
445+
}
446+
}
447+
400448
/**
401449
* Make API Call to get Places
402450
*

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

+11
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,17 @@ public String getPlacesAsGPX(LatLng currentLocation) throws Exception {
131131
);
132132
}
133133

134+
/**
135+
* Retrieves a list of places based on the provided list of places and language.
136+
*
137+
* @param placeList A list of Place objects for which to fetch information.
138+
* @return A list of Place objects obtained from the Wikidata query.
139+
* @throws Exception If an error occurs during the retrieval process.
140+
*/
141+
public List<Place> getPlaces(List<Place> placeList) throws Exception {
142+
return nearbyPlaces.getPlaces(placeList, Locale.getDefault().getLanguage());
143+
}
144+
134145
public static LatLng calculateNorthEast(double latitude, double longitude, double distance) {
135146
double lat1 = Math.toRadians(latitude);
136147
double deltaLat = distance * 0.008;

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

+16
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,22 @@ public List<Place> getFromWikidataQuery(
120120
customQuery);
121121
}
122122

123+
/**
124+
* Retrieves a list of places based on the provided list of places and language.
125+
*
126+
* This method fetches place information from a Wikidata query using the specified language.
127+
*
128+
* @param placeList A list of Place objects for which to fetch information.
129+
* @param lang The language code to use for the query.
130+
* @return A list of Place objects obtained from the Wikidata query.
131+
* @throws Exception If an error occurs during the retrieval process.
132+
*/
133+
public List<Place> getPlaces(final List<Place> placeList,
134+
final String lang) throws Exception {
135+
return okHttpJsonApiClient
136+
.getPlaces(placeList, lang);
137+
}
138+
123139
/**
124140
* Runs the Wikidata query to retrieve the KML String
125141
*

0 commit comments

Comments
 (0)