Skip to content

Category suggestions for direct nearby uploads #1319

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
e2b54c0
Add categories to Wikidata query
misaochan Mar 16, 2018
2c6b6f9
Add TODO for category suggestions
misaochan Mar 16, 2018
8133ba1
Strip string for categories and add them to a Place
misaochan Mar 16, 2018
5e828d4
Save categories in PlaceRenderer and SingleUploadFragment
misaochan Mar 16, 2018
c1f4512
Add FIXME to PlaceRenderer
misaochan Mar 16, 2018
2099a99
Add Dagger injector to render()
misaochan Mar 16, 2018
efdfa38
Add FIXME for injector()
misaochan Mar 16, 2018
a0ecf1f
Switch to ApplicationlessInjection in PlaceRenderer.java
misaochan Mar 18, 2018
787425b
Change ImageUtil log to verbose to prevent logcat spam
misaochan Mar 18, 2018
945aa35
Add TODO for concat
misaochan Mar 18, 2018
d2b0ffc
Add directPrefs injection and obtain CategoryItem for direct cats
misaochan Mar 18, 2018
eb4646d
Tidy up CategorizationFragment
misaochan Mar 18, 2018
d0908ab
Put directCats on top of other categories
misaochan Mar 18, 2018
2249284
Really put directCats on top this time
misaochan Mar 18, 2018
efa7820
Comment out UploadController.java line to test with Prod categories
misaochan Mar 18, 2018
819ec79
Add check for empty direct categories
misaochan Mar 18, 2018
666b7a0
Set if-else block for direct Cats in CategorizationFragment
misaochan Mar 18, 2018
d6e848b
Remove comment from UploadController.java
misaochan Mar 18, 2018
29ccd58
Remove FIXMEs in PlaceRenderer
misaochan Mar 18, 2018
b600f51
Fix newlines
misaochan Mar 18, 2018
a2db562
Switch Commons categories to the end of the query to prevent cases wi…
misaochan Mar 18, 2018
eaed365
Remove TODO
misaochan Mar 18, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,16 @@ public class CategorizationFragment extends CommonsDaggerSupportFragment {

@Inject MediaWikiApi mwApi;
@Inject @Named("default_preferences") SharedPreferences prefs;
@Inject @Named("prefs") SharedPreferences prefsPrefs;
@Inject @Named("direct_nearby_upload_prefs") SharedPreferences directPrefs;
@Inject CategoryDao categoryDao;

private RVRendererAdapter<CategoryItem> categoriesAdapter;
private OnCategoriesSaveHandler onCategoriesSaveHandler;
private HashMap<String, ArrayList<String>> categoriesCache;
private List<CategoryItem> selectedCategories = new ArrayList<>();
private TitleTextWatcher textWatcher = new TitleTextWatcher();
private boolean hasDirectCategories = false;

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

public void hideKeyboard(View view) {
InputMethodManager inputMethodManager =(InputMethodManager)getActivity().getSystemService(Activity.INPUT_METHOD_SERVICE);
InputMethodManager inputMethodManager = (InputMethodManager) getActivity().getSystemService(Activity.INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), 0);
}

Expand Down Expand Up @@ -222,7 +225,7 @@ private void updateCategoryList(String filter) {
.observeOn(AndroidSchedulers.mainThread())
.subscribe(
s -> categoriesAdapter.add(s),
Timber::e,
Timber::e,
() -> {
categoriesAdapter.notifyDataSetChanged();
categoriesSearchInProgress.setVisibility(View.GONE);
Expand Down Expand Up @@ -257,9 +260,34 @@ private List<String> getStringList(List<CategoryItem> input) {
}

private Observable<CategoryItem> defaultCategories() {
return gpsCategories()
.concatWith(titleCategories())
.concatWith(recentCategories());

Observable<CategoryItem> directCat = directCategories();
if (hasDirectCategories) {
Timber.d("Image has direct Cat");
return directCat
.concatWith(gpsCategories())
.concatWith(titleCategories())
.concatWith(recentCategories());
}
else {
Timber.d("Image has no direct Cat");
return gpsCategories()
.concatWith(titleCategories())
.concatWith(recentCategories());
}
}

private Observable<CategoryItem> directCategories() {
String directCategory = directPrefs.getString("Category", "");
List<String> categoryList = new ArrayList<>();
Timber.d("Direct category found: " + directCategory);

if (!directCategory.equals("")) {
hasDirectCategories = true;
categoryList.add(directCategory);
Timber.d("DirectCat does not equal emptyString. Direct Cat list has " + categoryList);
}
return Observable.fromIterable(categoryList).map(name -> new CategoryItem(name, false));
}

private Observable<CategoryItem> gpsCategories() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public interface CommonsApplicationComponent extends AndroidInjector<Application

@Override
void inject(ApplicationlessInjection instance);

void inject(PlaceRenderer placeRenderer);

@Component.Builder
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -580,6 +580,7 @@ void storeSharedPrefs() {
SharedPreferences.Editor editor = directPrefs.edit();
editor.putString("Title", place.getName());
editor.putString("Desc", place.getLongDescription());
editor.putString("Category", place.getCategory());
editor.apply();
}

Expand Down
10 changes: 8 additions & 2 deletions app/src/main/java/fr/free/nrw/commons/nearby/NearbyPlaces.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Locale;
Expand Down Expand Up @@ -100,13 +101,17 @@ private List<Place> getFromWikidataQuery(LatLng cur,
}

String[] fields = line.split("\t");
Timber.v("Fields: " + Arrays.toString(fields));
String point = fields[0];
String wikiDataLink = Utils.stripLocalizedString(fields[1]);
String name = Utils.stripLocalizedString(fields[2]);
String type = Utils.stripLocalizedString(fields[4]);
String icon = fields[5];
String wikipediaSitelink = Utils.stripLocalizedString(fields[7]);
String commonsSitelink = Utils.stripLocalizedString(fields[8]);
String wikiDataLink = Utils.stripLocalizedString(fields[1]);
String icon = fields[5];
String category = Utils.stripLocalizedString(fields[9]);

Timber.v("Name: " + name + ", type: " + type + ", category: " + category + ", wikipediaSitelink: " + wikipediaSitelink + ", commonsSitelink: " + commonsSitelink);

double latitude;
double longitude;
Expand All @@ -128,6 +133,7 @@ private List<Place> getFromWikidataQuery(LatLng cur,
type, // details
Uri.parse(icon),
new LatLng(latitude, longitude, 0),
category,
new Sitelinks.Builder()
.setWikipediaLink(wikipediaSitelink)
.setCommonsLink(commonsSitelink)
Expand Down
6 changes: 5 additions & 1 deletion app/src/main/java/fr/free/nrw/commons/nearby/Place.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public class Place {
private final String longDescription;
private final Uri secondaryImageUrl;
public final LatLng location;
private final String category;

public Bitmap image;
public Bitmap secondaryImage;
Expand All @@ -25,12 +26,13 @@ public class Place {


public Place(String name, Label label, String longDescription,
Uri secondaryImageUrl, LatLng location, Sitelinks siteLinks) {
Uri secondaryImageUrl, LatLng location, String category, Sitelinks siteLinks) {
this.name = name;
this.label = label;
this.longDescription = longDescription;
this.secondaryImageUrl = secondaryImageUrl;
this.location = location;
this.category = category;
this.siteLinks = siteLinks;
}

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

public String getLongDescription() { return longDescription; }

public String getCategory() {return category; }

public void setDistance(String distance) {
this.distance = distance;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import fr.free.nrw.commons.CommonsApplication;
import fr.free.nrw.commons.R;
import fr.free.nrw.commons.contributions.ContributionController;
import fr.free.nrw.commons.di.ApplicationlessInjection;
import timber.log.Timber;

public class PlaceRenderer extends Renderer<Place> {
Expand Down Expand Up @@ -124,8 +125,8 @@ private void storeSharedPrefs() {
Timber.d("directPrefs stored");
editor.putString("Title", place.getName());
editor.putString("Desc", place.getLongDescription());
editor.putString("Category", place.getCategory());
editor.apply();

}

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

@Override
public void render() {

ApplicationlessInjection.getInstance(getContext().getApplicationContext())
.getCommonsApplicationComponent().inject(this);
place = getContent();
tvName.setText(place.name);
String descriptionText = place.getLongDescription();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,8 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container,
if (isNearbyUpload) {
String imageTitle = directPrefs.getString("Title", "");
String imageDesc = directPrefs.getString("Desc", "");
String imageCats = directPrefs.getString("Category", "");
Timber.d("Image title: " + imageTitle + ", image desc: " + imageDesc + ", image categories: " + imageCats);
titleEdit.setText(imageTitle);
descEdit.setText(imageDesc);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public static Result checkIfImageIsTooDark(BitmapRegionDecoder bitmapRegionDecod

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

Rect rect = new Rect(checkImageLeftPosition,checkImageTopPosition,checkImageRightPosition,checkImageBottomPosition);
totalDividedRectangles++;
Expand Down
8 changes: 4 additions & 4 deletions app/src/main/resources/queries/nearby_query.rq
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ SELECT
(SAMPLE(COALESCE(?emoji0, ?emoji1)) as ?emoji)
?wikipediaArticle
?commonsArticle
(SAMPLE(?Commons_category) as ?Commons_category)
WHERE {
# Around given location...
SERVICE wikibase:around {
Expand All @@ -23,6 +24,9 @@ SELECT
OPTIONAL {?item rdfs:label ?item_label_preferred_language. FILTER (lang(?item_label_preferred_language) = "${LANG}")}
OPTIONAL {?item rdfs:label ?item_label_any_language}

# Get Commons category (P373)
OPTIONAL { ?item wdt:P373 ?Commons_category. }

# Get the class label in the preferred language of the user, or any other language if no label is available in that language.
OPTIONAL {
?item p:P31/ps:P31 ?classId.
Expand All @@ -35,10 +39,6 @@ SELECT
# Get emoji
OPTIONAL { ?classId wdt:P487 ?emoji0. }
OPTIONAL { ?classId wdt:P279*/wdt:P487 ?emoji1. }
OPTIONAL {
?sitelink schema:about ?item .
?sitelink schema:inLanguage "en"
}
OPTIONAL {
?wikipediaArticle schema:about ?item ;
schema:isPartOf <https://en.wikipedia.org/> .
Expand Down