Skip to content

Commit e42c6fe

Browse files
author
Suchit Kar
committed
Check status before nominating for deletion
1 parent 9a27f9c commit e42c6fe

File tree

8 files changed

+101
-50
lines changed

8 files changed

+101
-50
lines changed

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ public Media[] newArray(int i) {
4343
protected String license;
4444
protected String creator;
4545
protected ArrayList<String> categories; // as loaded at runtime?
46+
protected boolean requestedDeletion;
4647
private Map<String, String> descriptions; // multilingual descriptions as loaded
4748
private HashMap<String, Object> tags = new HashMap<>();
4849
private @Nullable LatLng coordinates;
@@ -416,4 +417,12 @@ public void writeToParcel(Parcel parcel, int flags) {
416417
parcel.writeStringList(categories);
417418
parcel.writeMap(descriptions);
418419
}
420+
421+
public void setRequestedDeletion(){
422+
requestedDeletion = true;
423+
}
424+
425+
public boolean getRequestedDeletion(){
426+
return requestedDeletion;
427+
}
419428
}

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
public class MediaDataExtractor {
3636
private final MediaWikiApi mediaWikiApi;
3737
private boolean fetched;
38+
private boolean deletionStatus;
3839
private ArrayList<String> categories;
3940
private Map<String, String> descriptions;
4041
private String license;
@@ -59,6 +60,14 @@ public void fetch(String filename, LicenseList licenseList) throws IOException {
5960
throw new IllegalStateException("Tried to call MediaDataExtractor.fetch() again.");
6061
}
6162

63+
try{
64+
Timber.d("Nominated for deletion: " + mediaWikiApi.pageExists("Commons:Deletion_requests/"+filename));
65+
deletionStatus = mediaWikiApi.pageExists("Commons:Deletion_requests/"+filename);
66+
}
67+
catch (Exception e){
68+
Timber.d(e.getMessage());
69+
}
70+
6271
MediaResult result = mediaWikiApi.fetchMediaByFilename(filename);
6372

6473
// In-page category links are extracted from source, as XML doesn't cover [[links]]
@@ -296,6 +305,9 @@ public void fill(Media media) {
296305
if (license != null) {
297306
media.setLicense(license);
298307
}
308+
if (deletionStatus){
309+
media.setRequestedDeletion();
310+
}
299311

300312
// add author, date, etc fields
301313
}

app/src/main/java/fr/free/nrw/commons/delete/DeleteTask.java

Lines changed: 27 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
import android.content.DialogInterface;
66
import android.content.Intent;
77
import android.os.AsyncTask;
8+
import android.widget.Toast;
9+
810
import java.text.SimpleDateFormat;
911
import java.util.Calendar;
1012
import java.util.Locale;
@@ -19,12 +21,9 @@
1921
import timber.log.Timber;
2022

2123
import static android.support.v4.content.ContextCompat.startActivity;
24+
import static android.widget.Toast.LENGTH_SHORT;
2225

23-
public class DeleteTask extends AsyncTask<Void, Void, Integer> {
24-
25-
private static final int SUCCESS = 0;
26-
private static final int FAILED = -1;
27-
private static final int ALREADY_DELETED = -2;
26+
public class DeleteTask extends AsyncTask<Void, Void, Boolean> {
2827

2928
@Inject MediaWikiApi mwApi;
3029
@Inject SessionManager sessionManager;
@@ -48,33 +47,23 @@ protected void onPreExecute(){
4847
}
4948

5049
@Override
51-
protected Integer doInBackground(Void ...voids) {
50+
protected Boolean doInBackground(Void ...voids) {
5251
String editToken;
5352
String authCookie;
5453
String summary = "Nominating " + media.getFilename() +" for deletion.";
5554

5655
authCookie = sessionManager.getAuthCookie();
5756
mwApi.setAuthCookie(authCookie);
5857

59-
try{
60-
if (mwApi.pageExists("Commons:Deletion_requests/"+media.getFilename())){
61-
return ALREADY_DELETED;
62-
}
63-
}
64-
catch (Exception e) {
65-
Timber.d(e.getMessage());
66-
return FAILED;
67-
}
68-
6958
try {
7059
editToken = mwApi.getEditToken();
7160
}
7261
catch (Exception e){
7362
Timber.d(e.getMessage());
74-
return FAILED;
63+
return false;
7564
}
7665
if (editToken.equals("+\\")) {
77-
return FAILED;
66+
return false;
7867
}
7968

8069
Calendar calendar = Calendar.getInstance();
@@ -90,7 +79,7 @@ protected Integer doInBackground(Void ...voids) {
9079
}
9180
catch (Exception e) {
9281
Timber.d(e.getMessage());
93-
return FAILED;
82+
return false;
9483
}
9584

9685
String subpageString = "=== [[:" + media.getFilename() + "]] ===\n" +
@@ -102,7 +91,7 @@ protected Integer doInBackground(Void ...voids) {
10291
}
10392
catch (Exception e) {
10493
Timber.d(e.getMessage());
105-
return FAILED;
94+
return false;
10695
}
10796

10897
String logPageString = "\n{{Commons:Deletion requests/" + media.getFilename() +
@@ -115,7 +104,7 @@ protected Integer doInBackground(Void ...voids) {
115104
}
116105
catch (Exception e) {
117106
Timber.d(e.getMessage());
118-
return FAILED;
107+
return false;
119108
}
120109

121110
String userPageString = "\n{{subst:idw|" + media.getFilename() +
@@ -126,30 +115,23 @@ protected Integer doInBackground(Void ...voids) {
126115
}
127116
catch (Exception e) {
128117
Timber.d(e.getMessage());
129-
return FAILED;
118+
return false;
130119
}
131-
return SUCCESS;
120+
return true;
132121
}
133122

134123
@Override
135-
protected void onPostExecute(Integer result) {
124+
protected void onPostExecute(Boolean result) {
136125
String message = "";
137126
String title = "";
138-
switch (result){
139-
case SUCCESS:
140-
title = "Success";
141-
message = "Successfully nominated " + media.getDisplayTitle() + " deletion.\n" +
142-
"Check the webpage for more details";
143-
break;
144-
case FAILED:
145-
title = "Failed";
146-
message = "Could not request deletion. Something went wrong.";
147-
break;
148-
case ALREADY_DELETED:
149-
title = "Already Nominated";
150-
message = media.getDisplayTitle() + " has already been nominated for deletion.\n" +
151-
"Check the webpage for more details";
152-
break;
127+
if (result){
128+
title = "Success";
129+
message = "Successfully nominated " + media.getDisplayTitle() + " deletion.\n" +
130+
"Check the webpage for more details";
131+
}
132+
else {
133+
title = "Failed";
134+
message = "Could not request deletion. Something went wrong.";
153135
}
154136
AlertDialog alert;
155137
AlertDialog.Builder builder = new AlertDialog.Builder(context);
@@ -165,7 +147,12 @@ public void onClick(DialogInterface dialog, int id) {}
165147
new DialogInterface.OnClickListener() {
166148
public void onClick(DialogInterface dialog, int id) {
167149
Intent browserIntent = new Intent(Intent.ACTION_VIEW, media.getFilePageTitle().getMobileUri());
168-
startActivity(context,browserIntent,null);
150+
if (browserIntent.resolveActivity(context.getPackageManager()) != null) {
151+
startActivity(context,browserIntent,null);
152+
} else {
153+
Toast toast = Toast.makeText(context, R.string.no_web_browser, LENGTH_SHORT);
154+
toast.show();
155+
}
169156
}
170157
});
171158
alert = builder.create();

app/src/main/java/fr/free/nrw/commons/media/MediaDetailFragment.java

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
import android.view.View;
1616
import android.view.ViewGroup;
1717
import android.view.ViewTreeObserver;
18-
import android.view.WindowManager;
1918
import android.widget.Button;
2019
import android.widget.EditText;
2120
import android.widget.LinearLayout;
@@ -42,6 +41,7 @@
4241
import fr.free.nrw.commons.delete.DeleteTask;
4342
import fr.free.nrw.commons.di.CommonsDaggerSupportFragment;
4443
import fr.free.nrw.commons.location.LatLng;
44+
import fr.free.nrw.commons.mwapi.MediaWikiApi;
4545
import fr.free.nrw.commons.ui.widget.CompatTextView;
4646
import timber.log.Timber;
4747

@@ -69,6 +69,9 @@ public static MediaDetailFragment forMedia(int index, boolean editable) {
6969

7070
@Inject
7171
Provider<MediaDataExtractor> mediaDataExtractorProvider;
72+
@Inject
73+
MediaWikiApi mwApi;
74+
7275

7376
private MediaWikiImageView image;
7477
private MediaDetailSpacer spacer;
@@ -79,6 +82,7 @@ public static MediaDetailFragment forMedia(int index, boolean editable) {
7982
private TextView license;
8083
private TextView coordinates;
8184
private TextView uploadedDate;
85+
private TextView nominatedforDeletion;
8286
private LinearLayout categoryContainer;
8387
private Button delete;
8488
private ScrollView scrollView;
@@ -133,6 +137,7 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle sa
133137
license = (TextView) view.findViewById(R.id.mediaDetailLicense);
134138
coordinates = (TextView) view.findViewById(R.id.mediaDetailCoordinates);
135139
uploadedDate = (TextView) view.findViewById(R.id.mediaDetailuploadeddate);
140+
nominatedforDeletion = (TextView) view.findViewById(R.id.nominatedDeletionBanner);
136141
delete = (Button) view.findViewById(R.id.nominateDeletion);
137142
categoryContainer = (LinearLayout) view.findViewById(R.id.mediaDetailCategoryContainer);
138143

@@ -231,7 +236,6 @@ protected void onPostExecute(Boolean success) {
231236

232237
if (success) {
233238
extractor.fill(media);
234-
235239
setTextFields(media);
236240
setOnClickListeners(media);
237241
} else {
@@ -284,21 +288,22 @@ private void setTextFields(Media media) {
284288
}
285289
rebuildCatList();
286290

287-
delete.setVisibility(View.VISIBLE);
291+
checkDeletion(media);
288292
}
289293

290294
private void setOnClickListeners(final Media media) {
291295
if (licenseLink(media) != null) {
292296
license.setOnClickListener(v -> openWebBrowser(licenseLink(media)));
293-
} else {
297+
} else {
294298
Toast toast = Toast.makeText(getContext(), getString(R.string.null_url), Toast.LENGTH_SHORT);
295299
toast.show();
296-
}
300+
}
297301
if (media.getCoordinates() != null) {
298302
coordinates.setOnClickListener(v -> openMap(media.getCoordinates()));
299303
}
300-
if (delete.getVisibility()==View.VISIBLE){
304+
if (delete.getVisibility() == View.VISIBLE) {
301305
delete.setOnClickListener(v -> {
306+
delete.setEnabled(false);
302307
AlertDialog.Builder alert = new AlertDialog.Builder(getActivity());
303308
alert.setMessage("Why should this file be deleted?");
304309
final EditText input = new EditText(getActivity());
@@ -343,6 +348,11 @@ public void onTextChanged(CharSequence s, int start, int before, int count) {
343348
d.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(false);
344349
});
345350
}
351+
if (nominatedforDeletion.getVisibility() == View.VISIBLE){
352+
nominatedforDeletion.setOnClickListener(v -> {
353+
openWebBrowser(media.getFilePageTitle().getMobileUri().toString());
354+
});
355+
}
346356
}
347357

348358
private void rebuildCatList() {
@@ -366,7 +376,7 @@ private View buildCatLabel(final String catName, ViewGroup categoryContainer) {
366376
viewIntent.setAction(Intent.ACTION_VIEW);
367377
viewIntent.setData(new PageTitle(selectedCategoryTitle).getCanonicalUri());
368378
//check if web browser available
369-
if(viewIntent.resolveActivity(getActivity().getPackageManager()) != null){
379+
if (viewIntent.resolveActivity(getActivity().getPackageManager()) != null) {
370380
startActivity(viewIntent);
371381
} else {
372382
Toast toast = Toast.makeText(getContext(), getString(R.string.no_web_browser), LENGTH_SHORT);
@@ -434,6 +444,16 @@ private String prettyCoordinates(Media media) {
434444
return media.getCoordinates().getPrettyCoordinateString();
435445
}
436446

447+
private void checkDeletion(Media media){
448+
if (media.getRequestedDeletion()){
449+
delete.setVisibility(View.GONE);
450+
nominatedforDeletion.setVisibility(View.VISIBLE);
451+
}
452+
else{
453+
delete.setVisibility(View.VISIBLE);
454+
nominatedforDeletion.setVisibility(View.GONE);
455+
}
456+
}
437457

438458
private @Nullable
439459
String licenseLink(Media media) {
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<shape xmlns:android="http://schemas.android.com/apk/res/android"
3+
android:shape="rectangle">
4+
<solid android:color="@color/deleteButtonColor"/>
5+
<corners android:radius="3dp" />
6+
<stroke android:width="5px" android:color="@color/deleteBackgroundColor" />
7+
</shape>

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

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -251,10 +251,24 @@
251251
android:layout_width="match_parent"
252252
android:layout_height="@dimen/small_gap" />
253253

254+
<TextView
255+
android:id="@+id/nominatedDeletionBanner"
256+
android:background="@color/deleteBackgroundColor"
257+
android:layout_width="match_parent"
258+
android:layout_height="wrap_content"
259+
android:paddingBottom="@dimen/tiny_gap"
260+
android:gravity="center"
261+
android:text="@string/nominated_for_deletion"
262+
android:textColor="@color/primaryTextColor"
263+
android:padding="@dimen/standard_gap"
264+
android:textSize="@dimen/normal_text"
265+
android:textStyle="bold"
266+
android:visibility="gone"/>
267+
254268
<Button
255269
android:id="@+id/nominateDeletion"
256-
style="?android:attr/buttonBarButtonStyle"
257-
android:textColor="@color/deleteTextColor"
270+
android:textColor="@color/primaryTextColor"
271+
android:background="@drawable/bg_delete_button"
258272
android:layout_margin="@dimen/standard_gap"
259273
android:layout_width="match_parent"
260274
android:layout_height="wrap_content"

app/src/main/res/values/colors.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
<color name="primaryTextColor">#ffffff</color>
1616
<color name="secondaryTextColor">#000000</color>
1717

18-
<color name="deleteTextColor">#d50000</color>
18+
<color name="deleteBackgroundColor">#90960a0a</color>
19+
<color name="deleteButtonColor">#44000000</color>
1920

2021
<!-- Some colours are same for dark/light themes. They are written two times in case
2122
we want to change light ones later.

app/src/main/res/values/strings.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,7 @@
231231
<string name="no_web_browser">No web browser found to open URL</string>
232232
<string name="null_url">Error! URL not found</string>
233233
<string name="nominate_deletion">Nominate for Deletion</string>
234+
<string name="nominated_for_deletion">This image has been nominated for deletion.\n\n<u>See webpage for details</u></string>
234235
<string name="view_browser">View in Browser</string>
235236

236237
<string name="nearby_location_has_not_changed">Location has not changed.</string>

0 commit comments

Comments
 (0)