Skip to content

Commit 4c192e4

Browse files
Merge pull request commons-app#1319 from misaochan/category-suggestions-new
Category suggestions for direct nearby uploads
2 parents 499591d + eaed365 commit 4c192e4

File tree

9 files changed

+59
-15
lines changed

9 files changed

+59
-15
lines changed

app/src/main/java/fr/free/nrw/commons/category/CategorizationFragment.java

+33-5
Original file line numberDiff line numberDiff line change
@@ -72,13 +72,16 @@ public class CategorizationFragment extends CommonsDaggerSupportFragment {
7272

7373
@Inject MediaWikiApi mwApi;
7474
@Inject @Named("default_preferences") SharedPreferences prefs;
75+
@Inject @Named("prefs") SharedPreferences prefsPrefs;
76+
@Inject @Named("direct_nearby_upload_prefs") SharedPreferences directPrefs;
7577
@Inject CategoryDao categoryDao;
7678

7779
private RVRendererAdapter<CategoryItem> categoriesAdapter;
7880
private OnCategoriesSaveHandler onCategoriesSaveHandler;
7981
private HashMap<String, ArrayList<String>> categoriesCache;
8082
private List<CategoryItem> selectedCategories = new ArrayList<>();
8183
private TitleTextWatcher textWatcher = new TitleTextWatcher();
84+
private boolean hasDirectCategories = false;
8285

8386
private final CategoriesAdapterFactory adapterFactory = new CategoriesAdapterFactory(item -> {
8487
if (item.isSelected()) {
@@ -127,7 +130,7 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container,
127130
}
128131

129132
public void hideKeyboard(View view) {
130-
InputMethodManager inputMethodManager =(InputMethodManager)getActivity().getSystemService(Activity.INPUT_METHOD_SERVICE);
133+
InputMethodManager inputMethodManager = (InputMethodManager) getActivity().getSystemService(Activity.INPUT_METHOD_SERVICE);
131134
inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), 0);
132135
}
133136

@@ -222,7 +225,7 @@ private void updateCategoryList(String filter) {
222225
.observeOn(AndroidSchedulers.mainThread())
223226
.subscribe(
224227
s -> categoriesAdapter.add(s),
225-
Timber::e,
228+
Timber::e,
226229
() -> {
227230
categoriesAdapter.notifyDataSetChanged();
228231
categoriesSearchInProgress.setVisibility(View.GONE);
@@ -257,9 +260,34 @@ private List<String> getStringList(List<CategoryItem> input) {
257260
}
258261

259262
private Observable<CategoryItem> defaultCategories() {
260-
return gpsCategories()
261-
.concatWith(titleCategories())
262-
.concatWith(recentCategories());
263+
264+
Observable<CategoryItem> directCat = directCategories();
265+
if (hasDirectCategories) {
266+
Timber.d("Image has direct Cat");
267+
return directCat
268+
.concatWith(gpsCategories())
269+
.concatWith(titleCategories())
270+
.concatWith(recentCategories());
271+
}
272+
else {
273+
Timber.d("Image has no direct Cat");
274+
return gpsCategories()
275+
.concatWith(titleCategories())
276+
.concatWith(recentCategories());
277+
}
278+
}
279+
280+
private Observable<CategoryItem> directCategories() {
281+
String directCategory = directPrefs.getString("Category", "");
282+
List<String> categoryList = new ArrayList<>();
283+
Timber.d("Direct category found: " + directCategory);
284+
285+
if (!directCategory.equals("")) {
286+
hasDirectCategories = true;
287+
categoryList.add(directCategory);
288+
Timber.d("DirectCat does not equal emptyString. Direct Cat list has " + categoryList);
289+
}
290+
return Observable.fromIterable(categoryList).map(name -> new CategoryItem(name, false));
263291
}
264292

265293
private Observable<CategoryItem> gpsCategories() {

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

+1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ public interface CommonsApplicationComponent extends AndroidInjector<Application
3939

4040
@Override
4141
void inject(ApplicationlessInjection instance);
42+
4243
void inject(PlaceRenderer placeRenderer);
4344

4445
@Component.Builder

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

+1
Original file line numberDiff line numberDiff line change
@@ -623,6 +623,7 @@ void storeSharedPrefs() {
623623
SharedPreferences.Editor editor = directPrefs.edit();
624624
editor.putString("Title", place.getName());
625625
editor.putString("Desc", place.getLongDescription());
626+
editor.putString("Category", place.getCategory());
626627
editor.apply();
627628
}
628629

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

+8-2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import java.net.URL;
99
import java.net.URLConnection;
1010
import java.util.ArrayList;
11+
import java.util.Arrays;
1112
import java.util.Collections;
1213
import java.util.List;
1314
import java.util.Locale;
@@ -100,13 +101,17 @@ private List<Place> getFromWikidataQuery(LatLng cur,
100101
}
101102

102103
String[] fields = line.split("\t");
104+
Timber.v("Fields: " + Arrays.toString(fields));
103105
String point = fields[0];
106+
String wikiDataLink = Utils.stripLocalizedString(fields[1]);
104107
String name = Utils.stripLocalizedString(fields[2]);
105108
String type = Utils.stripLocalizedString(fields[4]);
109+
String icon = fields[5];
106110
String wikipediaSitelink = Utils.stripLocalizedString(fields[7]);
107111
String commonsSitelink = Utils.stripLocalizedString(fields[8]);
108-
String wikiDataLink = Utils.stripLocalizedString(fields[1]);
109-
String icon = fields[5];
112+
String category = Utils.stripLocalizedString(fields[9]);
113+
114+
Timber.v("Name: " + name + ", type: " + type + ", category: " + category + ", wikipediaSitelink: " + wikipediaSitelink + ", commonsSitelink: " + commonsSitelink);
110115

111116
double latitude;
112117
double longitude;
@@ -128,6 +133,7 @@ private List<Place> getFromWikidataQuery(LatLng cur,
128133
type, // details
129134
Uri.parse(icon),
130135
new LatLng(latitude, longitude, 0),
136+
category,
131137
new Sitelinks.Builder()
132138
.setWikipediaLink(wikipediaSitelink)
133139
.setCommonsLink(commonsSitelink)

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

+5-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ public class Place {
1717
private final String longDescription;
1818
private final Uri secondaryImageUrl;
1919
public final LatLng location;
20+
private final String category;
2021

2122
public Bitmap image;
2223
public Bitmap secondaryImage;
@@ -25,12 +26,13 @@ public class Place {
2526

2627

2728
public Place(String name, Label label, String longDescription,
28-
Uri secondaryImageUrl, LatLng location, Sitelinks siteLinks) {
29+
Uri secondaryImageUrl, LatLng location, String category, Sitelinks siteLinks) {
2930
this.name = name;
3031
this.label = label;
3132
this.longDescription = longDescription;
3233
this.secondaryImageUrl = secondaryImageUrl;
3334
this.location = location;
35+
this.category = category;
3436
this.siteLinks = siteLinks;
3537
}
3638

@@ -42,6 +44,8 @@ public Label getLabel() {
4244

4345
public String getLongDescription() { return longDescription; }
4446

47+
public String getCategory() {return category; }
48+
4549
public void setDistance(String distance) {
4650
this.distance = distance;
4751
}

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

+4-2
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import fr.free.nrw.commons.CommonsApplication;
2929
import fr.free.nrw.commons.R;
3030
import fr.free.nrw.commons.contributions.ContributionController;
31+
import fr.free.nrw.commons.di.ApplicationlessInjection;
3132
import timber.log.Timber;
3233

3334
public class PlaceRenderer extends Renderer<Place> {
@@ -124,8 +125,8 @@ private void storeSharedPrefs() {
124125
Timber.d("directPrefs stored");
125126
editor.putString("Title", place.getName());
126127
editor.putString("Desc", place.getLongDescription());
128+
editor.putString("Category", place.getCategory());
127129
editor.apply();
128-
129130
}
130131

131132
private void closeLayout(LinearLayout buttonLayout){
@@ -138,7 +139,8 @@ private void openLayout(LinearLayout buttonLayout){
138139

139140
@Override
140141
public void render() {
141-
142+
ApplicationlessInjection.getInstance(getContext().getApplicationContext())
143+
.getCommonsApplicationComponent().inject(this);
142144
place = getContent();
143145
tvName.setText(place.name);
144146
String descriptionText = place.getLongDescription();

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

+2
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,8 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container,
114114
if (isNearbyUpload) {
115115
String imageTitle = directPrefs.getString("Title", "");
116116
String imageDesc = directPrefs.getString("Desc", "");
117+
String imageCats = directPrefs.getString("Category", "");
118+
Timber.d("Image title: " + imageTitle + ", image desc: " + imageDesc + ", image categories: " + imageCats);
117119
titleEdit.setText(imageTitle);
118120
descEdit.setText(imageDesc);
119121
}

app/src/main/java/fr/free/nrw/commons/utils/ImageUtils.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public static Result checkIfImageIsTooDark(BitmapRegionDecoder bitmapRegionDecod
5454

5555
while ((checkImageRightPosition <= loadImageWidth) && (checkImageLeftPosition < checkImageRightPosition)) {
5656
while ((checkImageBottomPosition <= loadImageHeight) && (checkImageTopPosition < checkImageBottomPosition)) {
57-
Timber.d("left: " + checkImageLeftPosition + " right: " + checkImageRightPosition + " top: " + checkImageTopPosition + " bottom: " + checkImageBottomPosition);
57+
Timber.v("left: " + checkImageLeftPosition + " right: " + checkImageRightPosition + " top: " + checkImageTopPosition + " bottom: " + checkImageBottomPosition);
5858

5959
Rect rect = new Rect(checkImageLeftPosition,checkImageTopPosition,checkImageRightPosition,checkImageBottomPosition);
6060
totalDividedRectangles++;

app/src/main/resources/queries/nearby_query.rq

+4-4
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ SELECT
88
(SAMPLE(COALESCE(?emoji0, ?emoji1)) as ?emoji)
99
?wikipediaArticle
1010
?commonsArticle
11+
(SAMPLE(?Commons_category) as ?Commons_category)
1112
WHERE {
1213
# Around given location...
1314
SERVICE wikibase:around {
@@ -23,6 +24,9 @@ SELECT
2324
OPTIONAL {?item rdfs:label ?item_label_preferred_language. FILTER (lang(?item_label_preferred_language) = "${LANG}")}
2425
OPTIONAL {?item rdfs:label ?item_label_any_language}
2526

27+
# Get Commons category (P373)
28+
OPTIONAL { ?item wdt:P373 ?Commons_category. }
29+
2630
# Get the class label in the preferred language of the user, or any other language if no label is available in that language.
2731
OPTIONAL {
2832
?item p:P31/ps:P31 ?classId.
@@ -35,10 +39,6 @@ SELECT
3539
# Get emoji
3640
OPTIONAL { ?classId wdt:P487 ?emoji0. }
3741
OPTIONAL { ?classId wdt:P279*/wdt:P487 ?emoji1. }
38-
OPTIONAL {
39-
?sitelink schema:about ?item .
40-
?sitelink schema:inLanguage "en"
41-
}
4242
OPTIONAL {
4343
?wikipediaArticle schema:about ?item ;
4444
schema:isPartOf <https://en.wikipedia.org/> .

0 commit comments

Comments
 (0)