Skip to content

Commit 3b1cf20

Browse files
ejeggmaskara
authored and
maskara
committed
Add some category stuff back in to review (commons-app#1538)
* Use standalone category extraction code in MediaDataExtractor * Add categories to category review page
1 parent 2cedf44 commit 3b1cf20

File tree

7 files changed

+72
-26
lines changed

7 files changed

+72
-26
lines changed

app/src/main/java/fr/free/nrw/commons/MediaDataExtractor.java

+2-16
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import fr.free.nrw.commons.location.LatLng;
2626
import fr.free.nrw.commons.mwapi.MediaResult;
2727
import fr.free.nrw.commons.mwapi.MediaWikiApi;
28+
import fr.free.nrw.commons.utils.MediaDataExtractorUtil;
2829
import timber.log.Timber;
2930

3031
/**
@@ -72,28 +73,13 @@ public void fetch(String filename, LicenseList licenseList) throws IOException {
7273
MediaResult result = mediaWikiApi.fetchMediaByFilename(filename);
7374

7475
// In-page category links are extracted from source, as XML doesn't cover [[links]]
75-
extractCategories(result.getWikiSource());
76+
categories = MediaDataExtractorUtil.extractCategories(result.getWikiSource());
7677

7778
// Description template info is extracted from preprocessor XML
7879
processWikiParseTree(result.getParseTreeXmlSource(), licenseList);
7980
fetched = true;
8081
}
8182

82-
/**
83-
* We could fetch all category links from API, but we actually only want the ones
84-
* directly in the page source so they're editable. In the future this may change.
85-
*
86-
* @param source wikitext source code
87-
*/
88-
private void extractCategories(String source) {
89-
Pattern regex = Pattern.compile("\\[\\[\\s*Category\\s*:([^]]*)\\s*\\]\\]", Pattern.CASE_INSENSITIVE);
90-
Matcher matcher = regex.matcher(source);
91-
while (matcher.find()) {
92-
String cat = matcher.group(1).trim();
93-
categories.add(cat);
94-
}
95-
}
96-
9783
private void processWikiParseTree(String source, LicenseList licenseList) throws IOException {
9884
Document doc;
9985
try {

app/src/main/java/fr/free/nrw/commons/review/ReviewActivity.java

+14
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import com.viewpagerindicator.CirclePageIndicator;
2121

2222
import java.io.IOException;
23+
import java.util.ArrayList;
2324
import java.util.List;
2425

2526
import javax.inject.Inject;
@@ -30,7 +31,9 @@
3031
import fr.free.nrw.commons.R;
3132
import fr.free.nrw.commons.Utils;
3233
import fr.free.nrw.commons.auth.AuthenticatedActivity;
34+
import fr.free.nrw.commons.mwapi.MediaResult;
3335
import fr.free.nrw.commons.mwapi.MediaWikiApi;
36+
import fr.free.nrw.commons.utils.MediaDataExtractorUtil;
3437
import io.reactivex.Observable;
3538
import io.reactivex.android.schedulers.AndroidSchedulers;
3639
import io.reactivex.schedulers.Schedulers;
@@ -125,8 +128,19 @@ private void updateImage(Media result) {
125128
reviewController.onImageRefreshed(result.getFilename()); //file name is updated
126129
reviewPagerAdapter.updateFilename();
127130
pager.setCurrentItem(0);
131+
Observable.fromCallable(() -> {
132+
MediaResult media = mwApi.fetchMediaByFilename("File:" + result.getFilename());
133+
return MediaDataExtractorUtil.extractCategories(media.getWikiSource());
134+
})
135+
.subscribeOn(Schedulers.io())
136+
.observeOn(AndroidSchedulers.mainThread())
137+
.subscribe(this::updateCategories);
128138
}
129139

140+
private void updateCategories(ArrayList<String> categories) {
141+
reviewController.onCategoriesRefreshed(categories);
142+
reviewPagerAdapter.updateCategories();
143+
}
130144

131145
/**
132146
* References ReviewPagerAdapter to null before the activity is destroyed

app/src/main/java/fr/free/nrw/commons/review/ReviewController.java

+8
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,23 @@
11
package fr.free.nrw.commons.review;
22

3+
import java.util.ArrayList;
4+
35
/**
46
* Created by root on 19.05.2018.
57
*/
68

79
public class ReviewController implements ReviewActivity.ReviewCallback {
810
public static String fileName;
11+
protected static ArrayList<String> categories;
912

1013
@Override
1114
public void onImageRefreshed(String fileName) {
1215
ReviewController.fileName = fileName;
16+
ReviewController.categories = new ArrayList<>();
17+
}
18+
19+
public void onCategoriesRefreshed(ArrayList<String> categories) {
20+
ReviewController.categories = categories;
1321
}
1422

1523
@Override

app/src/main/java/fr/free/nrw/commons/review/ReviewImageFragment.java

+22-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import android.app.AlertDialog;
44
import android.os.Bundle;
5+
import android.text.TextUtils;
56
import android.view.LayoutInflater;
67
import android.view.View;
78
import android.view.ViewGroup;
@@ -10,6 +11,8 @@
1011

1112
import com.facebook.drawee.view.SimpleDraweeView;
1213

14+
import java.util.ArrayList;
15+
1316
import fr.free.nrw.commons.R;
1417
import fr.free.nrw.commons.Utils;
1518
import fr.free.nrw.commons.di.CommonsDaggerSupportFragment;
@@ -26,7 +29,8 @@ public class ReviewImageFragment extends CommonsDaggerSupportFragment {
2629

2730
private int position;
2831
private String fileName;
29-
private View textView;
32+
private String catString;
33+
private View catsView;
3034
private SimpleDraweeView simpleDraweeView;
3135

3236
public void update(int position, String fileName) {
@@ -38,6 +42,13 @@ public void update(int position, String fileName) {
3842
}
3943
}
4044

45+
public void updateCategories(Iterable<String> categories) {
46+
catString = TextUtils.join(", ", categories);
47+
if (catsView != null) {
48+
((TextView) catsView).setText(catString);
49+
}
50+
}
51+
4152
@Override
4253
public void onCreate(Bundle savedInstanceState) {
4354
super.onCreate(savedInstanceState);
@@ -49,14 +60,16 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container,
4960
position = getArguments().getInt("position");
5061
View layoutView = inflater.inflate(R.layout.fragment_review_image, container,
5162
false);
52-
textView = layoutView.findViewById(R.id.reviewQuestion);
63+
View textView = layoutView.findViewById(R.id.reviewQuestion);
64+
catsView = layoutView.findViewById(R.id.reviewCategories);
5365
String question;
5466
switch(position) {
5567
case COPYRIGHT:
5668
question = getString(R.string.review_copyright);
5769
break;
5870
case CATEGORY:
5971
question = getString(R.string.review_category);
72+
catsView.setVisibility(View.VISIBLE);
6073
break;
6174
case SPAM:
6275
question = getString(R.string.review_spam);
@@ -66,6 +79,13 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container,
6679
}
6780
((TextView) textView).setText(question);
6881
simpleDraweeView = layoutView.findViewById(R.id.imageView);
82+
83+
if (fileName != null) {
84+
simpleDraweeView.setImageURI(Utils.makeThumbBaseUrl(fileName));
85+
}
86+
if (catString != null) {
87+
((TextView) catsView).setText(catString);
88+
}
6989
return layoutView;
7090
}
7191

app/src/main/java/fr/free/nrw/commons/review/ReviewPagerAdapter.java

+5
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,11 @@ public void updateFilename() {
3535
}
3636
}
3737

38+
public void updateCategories() {
39+
ReviewImageFragment categoryFragment = reviewImageFragments[ReviewImageFragment.CATEGORY];
40+
categoryFragment.updateCategories(ReviewController.categories);
41+
}
42+
3843
@Override
3944
public Fragment getItem(int position) {
4045
Bundle bundle = new Bundle();

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

+6-4
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@
44
import java.util.regex.Matcher;
55
import java.util.regex.Pattern;
66

7-
/**
8-
* Created by root on 19.05.2018.
9-
*/
10-
117
public class MediaDataExtractorUtil {
128

9+
/**
10+
* We could fetch all category links from API, but we actually only want the ones
11+
* directly in the page source so they're editable. In the future this may change.
12+
*
13+
* @param source wikitext source code
14+
*/
1315
public static ArrayList<String> extractCategories(String source) {
1416
ArrayList<String> categories = new ArrayList<>();
1517
Pattern regex = Pattern.compile("\\[\\[\\s*Category\\s*:([^]]*)\\s*\\]\\]", Pattern.CASE_INSENSITIVE);

app/src/main/res/layout/fragment_review_image.xml

+15-4
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
android:layout_height="0dp"
2323
android:layout_weight="1.1"
2424
android:textAlignment="center"
25-
android:textSize="32dp"
25+
android:textSize="32sp"
2626
android:gravity="center_vertical"
2727
android:text="testing1"
2828
/>
@@ -32,12 +32,23 @@
3232
android:layout_height="0dp"
3333
android:layout_weight="0.7"
3434
android:textAlignment="center"
35-
android:textSize="24dp"
35+
android:textSize="24sp"
3636
android:layout_marginBottom="15dp"
3737
android:gravity="center_vertical"
3838
android:text="testing2"
3939
/>
4040

41+
<TextView
42+
android:id="@+id/reviewCategories"
43+
android:visibility="gone"
44+
android:layout_width="match_parent"
45+
android:layout_height="0dp"
46+
android:layout_weight="0.7"
47+
android:textSize="24sp"
48+
android:textAlignment="center"
49+
android:text="testing2"
50+
/>
51+
4152
<LinearLayout
4253
android:layout_width="match_parent"
4354
android:layout_height="0dp"
@@ -52,7 +63,7 @@
5263
android:layout_weight="1"
5364
android:background="@android:color/transparent"
5465
android:text="@string/yes"
55-
android:textSize="20dp"
66+
android:textSize="20sp"
5667
android:textColor="?attr/colorPrimaryDark"
5768
/>
5869

@@ -63,7 +74,7 @@
6374
android:background="@android:color/transparent"
6475
android:layout_weight="1"
6576
android:text="@string/no"
66-
android:textSize="20dp"
77+
android:textSize="20sp"
6778
android:textColor="?attr/colorPrimaryDark"
6879
/>
6980

0 commit comments

Comments
 (0)