Skip to content

Commit 7a5774e

Browse files
authored
Fixes commons-app#4281 "Wrong language pre-selected in Nearby upload" (commons-app#4285)
* location added * tests * changes requested * comments * Test fixed, minor improvement
1 parent 91a5aa1 commit 7a5774e

File tree

9 files changed

+88
-33
lines changed

9 files changed

+88
-33
lines changed

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@ Place fromCursor(Cursor cursor) {
157157
builder.setCommonsLink(cursor.getString(cursor.getColumnIndex(Table.COLUMN_COMMONS_LINK)));
158158

159159
return new Place(
160+
cursor.getString(cursor.getColumnIndex(Table.COLUMN_LANGUAGE)),
160161
cursor.getString(cursor.getColumnIndex(Table.COLUMN_NAME)),
161162
Label.fromText((cursor.getString(cursor.getColumnIndex(Table.COLUMN_LABEL_TEXT)))),
162163
cursor.getString(cursor.getColumnIndex(Table.COLUMN_DESCRIPTION)),
@@ -171,6 +172,7 @@ Place fromCursor(Cursor cursor) {
171172
private ContentValues toContentValues(Place bookmarkLocation) {
172173
ContentValues cv = new ContentValues();
173174
cv.put(BookmarkLocationsDao.Table.COLUMN_NAME, bookmarkLocation.getName());
175+
cv.put(BookmarkLocationsDao.Table.COLUMN_LANGUAGE, bookmarkLocation.getLanguage());
174176
cv.put(BookmarkLocationsDao.Table.COLUMN_DESCRIPTION, bookmarkLocation.getLongDescription());
175177
cv.put(BookmarkLocationsDao.Table.COLUMN_CATEGORY, bookmarkLocation.getCategory());
176178
cv.put(BookmarkLocationsDao.Table.COLUMN_LABEL_TEXT, bookmarkLocation.getLabel().getText());
@@ -189,6 +191,7 @@ public static class Table {
189191
public static final String TABLE_NAME = "bookmarksLocations";
190192

191193
static final String COLUMN_NAME = "location_name";
194+
static final String COLUMN_LANGUAGE = "location_lang";
192195
static final String COLUMN_DESCRIPTION = "location_description";
193196
static final String COLUMN_LAT = "location_lat";
194197
static final String COLUMN_LONG = "location_long";
@@ -205,6 +208,7 @@ public static class Table {
205208
// NOTE! KEEP IN SAME ORDER AS THEY ARE DEFINED UP THERE. HELPS HARD CODE COLUMN INDICES.
206209
public static final String[] ALL_FIELDS = {
207210
COLUMN_NAME,
211+
COLUMN_LANGUAGE,
208212
COLUMN_DESCRIPTION,
209213
COLUMN_CATEGORY,
210214
COLUMN_LABEL_TEXT,
@@ -223,6 +227,7 @@ public static class Table {
223227

224228
static final String CREATE_TABLE_STATEMENT = "CREATE TABLE " + TABLE_NAME + " ("
225229
+ COLUMN_NAME + " STRING PRIMARY KEY,"
230+
+ COLUMN_LANGUAGE + " STRING,"
226231
+ COLUMN_DESCRIPTION + " STRING,"
227232
+ COLUMN_CATEGORY + " STRING,"
228233
+ COLUMN_LABEL_TEXT + " STRING,"
@@ -287,6 +292,13 @@ public static void onUpdate(SQLiteDatabase db, int from, int to) {
287292
Timber.e(exception);
288293
}
289294
}
295+
if (from == 13){
296+
try {
297+
db.execSQL("ALTER TABLE bookmarksLocations ADD COLUMN location_lang STRING;");
298+
} catch (SQLiteException exception){
299+
Timber.e(exception);
300+
}
301+
}
290302
}
291303
}
292304
}

app/src/main/java/fr/free/nrw/commons/data/DBOpenHelper.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
public class DBOpenHelper extends SQLiteOpenHelper {
1414

1515
private static final String DATABASE_NAME = "commons.db";
16-
private static final int DATABASE_VERSION = 13;
16+
private static final int DATABASE_VERSION = 14;
1717
public static final String CONTRIBUTIONS_TABLE = "contributions";
1818
private final String DROP_TABLE_STATEMENT="DROP TABLE IF EXISTS %s";
1919

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

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
*/
1919
public class Place implements Parcelable {
2020

21+
public final String language;
2122
public final String name;
2223
private final Label label;
2324
private final String longDescription;
@@ -30,7 +31,8 @@ public class Place implements Parcelable {
3031
public final Sitelinks siteLinks;
3132

3233

33-
public Place(String name, Label label, String longDescription, LatLng location, String category, Sitelinks siteLinks, String pic, String destroyed) {
34+
public Place(String language,String name, Label label, String longDescription, LatLng location, String category, Sitelinks siteLinks, String pic, String destroyed) {
35+
this.language = language;
3436
this.name = name;
3537
this.label = label;
3638
this.longDescription = longDescription;
@@ -40,8 +42,8 @@ public Place(String name, Label label, String longDescription, LatLng location,
4042
this.pic = (pic == null) ? "":pic;
4143
this.destroyed = (destroyed == null) ? "":destroyed;
4244
}
43-
4445
public Place(Parcel in) {
46+
this.language = in.readString();
4547
this.name = in.readString();
4648
this.label = (Label) in.readSerializable();
4749
this.longDescription = in.readString();
@@ -53,14 +55,14 @@ public Place(Parcel in) {
5355
String destroyedString = in.readString();
5456
this.destroyed = (destroyedString == null) ? "":destroyedString;
5557
}
56-
5758
public static Place from(NearbyResultItem item) {
5859
String itemClass = item.getClassName().getValue();
5960
String classEntityId = "";
6061
if(!StringUtils.isBlank(itemClass)) {
6162
classEntityId = itemClass.replace("http://www.wikidata.org/entity/", "");
6263
}
6364
return new Place(
65+
item.getLabel().getLanguage(),
6466
item.getLabel().getValue(),
6567
Label.fromText(classEntityId), // list
6668
item.getClassLabel().getValue(), // details
@@ -75,6 +77,14 @@ public static Place from(NearbyResultItem item) {
7577
item.getDestroyed().getValue());
7678
}
7779

80+
/**
81+
* Gets the language of the caption ie name.
82+
* @return language
83+
*/
84+
public String getLanguage() {
85+
return language;
86+
}
87+
7888
/**
7989
* Gets the name of the place
8090
* @return name
@@ -176,6 +186,7 @@ public int hashCode() {
176186
public String toString() {
177187
return "Place{" +
178188
"name='" + name + '\'' +
189+
", lang='" + language + '\'' +
179190
", label='" + label + '\'' +
180191
", longDescription='" + longDescription + '\'' +
181192
", location='" + location + '\'' +
@@ -194,6 +205,7 @@ public int describeContents() {
194205

195206
@Override
196207
public void writeToParcel(Parcel dest, int flags) {
208+
dest.writeString(language);
197209
dest.writeString(name);
198210
dest.writeSerializable(label);
199211
dest.writeString(longDescription);

app/src/main/java/fr/free/nrw/commons/nearby/model/ResultTuple.kt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,21 @@
11
package fr.free.nrw.commons.nearby.model
22

3+
import com.google.gson.annotations.SerializedName
4+
35
class ResultTuple {
6+
@SerializedName("xml:lang")
7+
val language: String
48
val type: String
59
val value: String
610

7-
constructor(type: String, value: String) {
11+
constructor(lang: String, type: String, value: String) {
12+
this.language = lang
813
this.type = type
914
this.value = value
1015
}
1116

1217
constructor() {
18+
language = ""
1319
type = ""
1420
value = ""
1521
}

app/src/main/java/fr/free/nrw/commons/upload/UploadMediaDetail.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package fr.free.nrw.commons.upload
22

33
import fr.free.nrw.commons.nearby.Place
4-
import java.util.*
54

65
/**
76
* Holds a description of an item being uploaded by [UploadActivity]
@@ -20,7 +19,7 @@ data class UploadMediaDetail constructor(
2019
fun javaCopy() = copy()
2120

2221
constructor(place: Place) : this(
23-
Locale.getDefault().language,
22+
place.language,
2423
place.longDescription,
2524
place.name
2625
)

app/src/main/java/fr/free/nrw/commons/upload/UploadMediaDetailAdapter.java

Lines changed: 35 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -174,31 +174,46 @@ public void onNothingSelected(AdapterView<?> adapterView) {
174174
}
175175
});
176176

177-
if (description.getSelectedLanguageIndex() == -1) {
178-
if (!TextUtils.isEmpty(savedLanguageValue)) {
179-
// If user has chosen a default language from settings activity savedLanguageValue is not null
180-
spinnerDescriptionLanguages.setSelection(languagesAdapter.getIndexOfLanguageCode(savedLanguageValue));
181-
} else {
182-
//Checking whether Language Code attribute is null or not.
183-
if(uploadMediaDetails.get(position).getLanguageCode() != null){
184-
//If it is not null that means it is fetching details from the previous upload (i.e. when user has pressed copy previous caption & description)
185-
//hence providing same language code for the current upload.
186-
spinnerDescriptionLanguages.setSelection(languagesAdapter
187-
.getIndexOfLanguageCode(uploadMediaDetails.get(position).getLanguageCode()), true);
177+
178+
if(description.getCaptionText().isEmpty() == false
179+
&& languagesAdapter.getIndexOfLanguageCode(description.getLanguageCode()) != -1){
180+
// If the user selects a nearby pin or location bookmark to upload a picture and language is present in spinner we set the language.
181+
spinnerDescriptionLanguages.setSelection(languagesAdapter.getIndexOfLanguageCode(description.getLanguageCode()));
182+
}
183+
else {
184+
// This is a contribution upload or the language from description is not present in spinner.
185+
if (description.getSelectedLanguageIndex() == -1) {
186+
if (!TextUtils.isEmpty(savedLanguageValue)) {
187+
// If user has chosen a default language from settings activity savedLanguageValue is not null
188+
spinnerDescriptionLanguages
189+
.setSelection(
190+
languagesAdapter.getIndexOfLanguageCode(savedLanguageValue));
188191
} else {
189-
if (position == 0) {
190-
int defaultLocaleIndex = languagesAdapter
191-
.getIndexOfUserDefaultLocale(spinnerDescriptionLanguages.getContext());
192-
spinnerDescriptionLanguages.setSelection(defaultLocaleIndex, true);
192+
//Checking whether Language Code attribute is null or not.
193+
if (uploadMediaDetails.get(position).getLanguageCode() != null) {
194+
//If it is not null that means it is fetching details from the previous upload (i.e. when user has pressed copy previous caption & description)
195+
//hence providing same language code for the current upload.
196+
spinnerDescriptionLanguages.setSelection(languagesAdapter
197+
.getIndexOfLanguageCode(
198+
uploadMediaDetails.get(position).getLanguageCode()), true);
193199
} else {
194-
spinnerDescriptionLanguages.setSelection(0,true);
200+
if (position == 0) {
201+
int defaultLocaleIndex = languagesAdapter
202+
.getIndexOfUserDefaultLocale(
203+
spinnerDescriptionLanguages.getContext());
204+
spinnerDescriptionLanguages.setSelection(defaultLocaleIndex, true);
205+
} else {
206+
spinnerDescriptionLanguages.setSelection(0, true);
207+
}
195208
}
196209
}
197-
}
198210

199-
} else {
200-
spinnerDescriptionLanguages.setSelection(description.getSelectedLanguageIndex());
201-
selectedLanguages.put(spinnerDescriptionLanguages, description.getLanguageCode());
211+
} else {
212+
spinnerDescriptionLanguages
213+
.setSelection(description.getSelectedLanguageIndex());
214+
selectedLanguages
215+
.put(spinnerDescriptionLanguages, description.getLanguageCode());
216+
}
202217
}
203218
}
204219
}

app/src/test/kotlin/ModelFunctions.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ fun depictSearchItem(
7676

7777
fun place(
7878
name: String = "name",
79+
lang: String = "en",
7980
label: Label? = null,
8081
longDescription: String = "longDescription",
8182
latLng: LatLng? = null,
@@ -84,7 +85,7 @@ fun place(
8485
pic: String = "pic",
8586
destroyed: String = "destroyed"
8687
): Place {
87-
return Place(name, label, longDescription, latLng, category, siteLinks, pic, destroyed)
88+
return Place(lang, name, label, longDescription, latLng, category, siteLinks, pic, destroyed)
8889
}
8990

9091
fun entityId(wikiBaseEntityValue: WikiBaseEntityValue = wikiBaseEntityValue()) =

app/src/test/kotlin/fr/free/nrw/commons/bookmarks/locations/BookMarkLocationDaoTest.kt

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import org.robolectric.annotation.Config
2626
@Config(sdk = [21], application = TestCommonsApplication::class)
2727
class BookMarkLocationDaoTest {
2828
private val columns = arrayOf(COLUMN_NAME,
29+
COLUMN_LANGUAGE,
2930
COLUMN_DESCRIPTION,
3031
COLUMN_CATEGORY,
3132
COLUMN_LABEL_TEXT,
@@ -61,7 +62,7 @@ class BookMarkLocationDaoTest {
6162
builder.setCommonsLink("commonsLink")
6263

6364

64-
examplePlaceBookmark = Place("placeName", exampleLabel, "placeDescription"
65+
examplePlaceBookmark = Place("en", "placeName", exampleLabel, "placeDescription"
6566
, exampleLocation, "placeCategory", builder.build(),"picName","placeDestroyed")
6667
testObject = BookmarkLocationsDao { client }
6768
}
@@ -86,6 +87,7 @@ class BookMarkLocationDaoTest {
8687
createCursor(1).let { cursor ->
8788
cursor.moveToFirst()
8889
testObject.fromCursor(cursor).let {
90+
assertEquals("en", it.language)
8991
assertEquals("placeName", it.name)
9092
assertEquals(Label.FOREST, it.label)
9193
assertEquals("placeDescription", it.longDescription)
@@ -149,8 +151,9 @@ class BookMarkLocationDaoTest {
149151
assertTrue(testObject.updateBookmarkLocation(examplePlaceBookmark))
150152
verify(client).insert(eq(BASE_URI), captor.capture())
151153
captor.firstValue.let { cv ->
152-
assertEquals(12, cv.size())
154+
assertEquals(13, cv.size())
153155
assertEquals(examplePlaceBookmark.name, cv.getAsString(COLUMN_NAME))
156+
assertEquals(examplePlaceBookmark.language, cv.getAsString(COLUMN_LANGUAGE))
154157
assertEquals(examplePlaceBookmark.longDescription, cv.getAsString(COLUMN_DESCRIPTION))
155158
assertEquals(examplePlaceBookmark.label.text, cv.getAsString(COLUMN_LABEL_TEXT))
156159
assertEquals(examplePlaceBookmark.category, cv.getAsString(COLUMN_CATEGORY))
@@ -262,10 +265,17 @@ class BookMarkLocationDaoTest {
262265
verify(database).execSQL("ALTER TABLE bookmarksLocations ADD COLUMN location_destroyed STRING;")
263266
}
264267

268+
@Test
269+
fun migrateTableVersionFrom_v13_to_v14() {
270+
onUpdate(database, 13, 14)
271+
verify(database).execSQL("ALTER TABLE bookmarksLocations ADD COLUMN location_lang STRING;")
272+
}
273+
274+
265275
private fun createCursor(rowCount: Int) = MatrixCursor(columns, rowCount).apply {
266276

267277
for (i in 0 until rowCount) {
268-
addRow(listOf("placeName", "placeDescription","placeCategory", exampleLabel.text, exampleLabel.icon,
278+
addRow(listOf("placeName", "en", "placeDescription", "placeCategory", exampleLabel.text, exampleLabel.icon,
269279
exampleUri, builder.build().wikipediaLink, builder.build().wikidataLink, builder.build().commonsLink,
270280
exampleLocation.latitude, exampleLocation.longitude, "picName", "placeDestroyed"))
271281
}

app/src/test/kotlin/fr/free/nrw/commons/bookmarks/locations/BookmarkLocationControllerTest.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ class BookmarkLocationControllerTest {
3333
private val mockBookmarkList: List<Place>
3434
private get() {
3535
val list = ArrayList<Place>()
36-
list.add(Place("a place",null,"a description",null,"a cat",null,null,null))
37-
list.add(Place("another place",null,"another description",null,"another cat",null,null,null))
36+
list.add(Place("en","a place",null,"a description",null,"a cat",null,null,null))
37+
list.add(Place("en","another place",null,"another description",null,"another cat",null,null,null))
3838
return list
3939
}
4040

0 commit comments

Comments
 (0)