mirror of
				https://github.com/commons-app/apps-android-commons.git
				synced 2025-10-31 14:53:59 +01:00 
			
		
		
		
	Check status before nominating for deletion
This commit is contained in:
		
							parent
							
								
									c303525508
								
							
						
					
					
						commit
						9a27f9c332
					
				
					 5 changed files with 120 additions and 75 deletions
				
			
		|  | @ -1,10 +1,10 @@ | |||
| package fr.free.nrw.commons.delete; | ||||
| 
 | ||||
| import android.app.AlertDialog; | ||||
| import android.content.Context; | ||||
| import android.content.DialogInterface; | ||||
| import android.content.Intent; | ||||
| import android.os.AsyncTask; | ||||
| import android.widget.Button; | ||||
| import android.widget.Toast; | ||||
| 
 | ||||
| import java.text.SimpleDateFormat; | ||||
| import java.util.Calendar; | ||||
| import java.util.Locale; | ||||
|  | @ -12,14 +12,19 @@ import java.util.Locale; | |||
| import javax.inject.Inject; | ||||
| 
 | ||||
| import fr.free.nrw.commons.Media; | ||||
| import fr.free.nrw.commons.R; | ||||
| import fr.free.nrw.commons.auth.SessionManager; | ||||
| import fr.free.nrw.commons.di.ApplicationlessInjection; | ||||
| import fr.free.nrw.commons.mwapi.MediaWikiApi; | ||||
| import timber.log.Timber; | ||||
| 
 | ||||
| import static android.view.View.GONE; | ||||
| import static android.support.v4.content.ContextCompat.startActivity; | ||||
| 
 | ||||
| public class DeleteTask extends AsyncTask<Void, Void, Boolean> { | ||||
| public class DeleteTask extends AsyncTask<Void, Void, Integer> { | ||||
| 
 | ||||
|     private static final int SUCCESS = 0; | ||||
|     private static final int FAILED = -1; | ||||
|     private static final int ALREADY_DELETED = -2; | ||||
| 
 | ||||
|     @Inject MediaWikiApi mwApi; | ||||
|     @Inject SessionManager sessionManager; | ||||
|  | @ -43,7 +48,7 @@ public class DeleteTask extends AsyncTask<Void, Void, Boolean> { | |||
|     } | ||||
| 
 | ||||
|     @Override | ||||
|     protected Boolean doInBackground(Void ...voids) { | ||||
|     protected Integer doInBackground(Void ...voids) { | ||||
|         String editToken; | ||||
|         String authCookie; | ||||
|         String summary = "Nominating " + media.getFilename() +" for deletion."; | ||||
|  | @ -51,15 +56,25 @@ public class DeleteTask extends AsyncTask<Void, Void, Boolean> { | |||
|         authCookie = sessionManager.getAuthCookie(); | ||||
|         mwApi.setAuthCookie(authCookie); | ||||
| 
 | ||||
|         try{ | ||||
|             if (mwApi.pageExists("Commons:Deletion_requests/"+media.getFilename())){ | ||||
|                 return ALREADY_DELETED; | ||||
|             } | ||||
|         } | ||||
|         catch (Exception e) { | ||||
|             Timber.d(e.getMessage()); | ||||
|             return FAILED; | ||||
|         } | ||||
| 
 | ||||
|         try { | ||||
|             editToken = mwApi.getEditToken(); | ||||
|         } | ||||
|         catch (Exception e){ | ||||
|             Timber.d(e.getMessage()); | ||||
|             return false; | ||||
|             return FAILED; | ||||
|         } | ||||
|         if (editToken.equals("+\\")) { | ||||
|             return false; | ||||
|             return FAILED; | ||||
|         } | ||||
| 
 | ||||
|         Calendar calendar = Calendar.getInstance(); | ||||
|  | @ -75,7 +90,7 @@ public class DeleteTask extends AsyncTask<Void, Void, Boolean> { | |||
|         } | ||||
|         catch (Exception e) { | ||||
|             Timber.d(e.getMessage()); | ||||
|             return false; | ||||
|             return FAILED; | ||||
|         } | ||||
| 
 | ||||
|         String subpageString = "=== [[:" + media.getFilename() + "]] ===\n" + | ||||
|  | @ -83,11 +98,11 @@ public class DeleteTask extends AsyncTask<Void, Void, Boolean> { | |||
|                 " ~~~~"; | ||||
|         try{ | ||||
|             mwApi.edit(editToken,subpageString+"\n", | ||||
|                     "Commons:Deletion requests/"+media.getFilename(),summary); | ||||
|                     "Commons:Deletion_requests/"+media.getFilename(),summary); | ||||
|         } | ||||
|         catch (Exception e) { | ||||
|             Timber.d(e.getMessage()); | ||||
|             return false; | ||||
|             return FAILED; | ||||
|         } | ||||
| 
 | ||||
|         String logPageString = "\n{{Commons:Deletion requests/" + media.getFilename() + | ||||
|  | @ -96,11 +111,11 @@ public class DeleteTask extends AsyncTask<Void, Void, Boolean> { | |||
|         String date = sdf.format(calendar.getTime()); | ||||
|         try{ | ||||
|             mwApi.appendEdit(editToken,logPageString+"\n", | ||||
|                     "Commons:Deletion requests/"+date,summary); | ||||
|                     "Commons:Deletion_requests/"+date,summary); | ||||
|         } | ||||
|         catch (Exception e) { | ||||
|             Timber.d(e.getMessage()); | ||||
|             return false; | ||||
|             return FAILED; | ||||
|         } | ||||
| 
 | ||||
|         String userPageString = "\n{{subst:idw|" + media.getFilename() + | ||||
|  | @ -111,20 +126,49 @@ public class DeleteTask extends AsyncTask<Void, Void, Boolean> { | |||
|         } | ||||
|         catch (Exception e) { | ||||
|             Timber.d(e.getMessage()); | ||||
|             return false; | ||||
|             return FAILED; | ||||
|         } | ||||
|         return true; | ||||
|         return SUCCESS; | ||||
|     } | ||||
| 
 | ||||
|     @Override | ||||
|     protected void onPostExecute(Boolean result) { | ||||
|         String toastText = ""; | ||||
|         if (result) { | ||||
|             toastText = "Successfully requested deletion."; | ||||
|     protected void onPostExecute(Integer result) { | ||||
|         String message = ""; | ||||
|         String title = ""; | ||||
|         switch (result){ | ||||
|             case SUCCESS: | ||||
|                 title = "Success"; | ||||
|                 message = "Successfully nominated " + media.getDisplayTitle() + " deletion.\n" + | ||||
|                         "Check the webpage for more details"; | ||||
|                 break; | ||||
|             case FAILED: | ||||
|                 title = "Failed"; | ||||
|                 message = "Could not request deletion. Something went wrong."; | ||||
|                 break; | ||||
|             case ALREADY_DELETED: | ||||
|                 title = "Already Nominated"; | ||||
|                 message = media.getDisplayTitle() + " has already been nominated for deletion.\n" + | ||||
|                         "Check the webpage for more details"; | ||||
|                 break; | ||||
|         } | ||||
|         else{ | ||||
|             toastText = "Could not request deletion."; | ||||
|         } | ||||
|         Toast.makeText(context, toastText, Toast.LENGTH_SHORT).show(); | ||||
|         AlertDialog alert; | ||||
|         AlertDialog.Builder builder = new AlertDialog.Builder(context); | ||||
|         builder.setTitle(title); | ||||
|         builder.setMessage(message); | ||||
|         builder.setCancelable(true); | ||||
|         builder.setPositiveButton( | ||||
|                 R.string.ok, | ||||
|                 new DialogInterface.OnClickListener() { | ||||
|                     public void onClick(DialogInterface dialog, int id) {} | ||||
|                 }); | ||||
|         builder.setNeutralButton(R.string.view_browser, | ||||
|                 new DialogInterface.OnClickListener() { | ||||
|                     public void onClick(DialogInterface dialog, int id) { | ||||
|                         Intent browserIntent = new Intent(Intent.ACTION_VIEW, media.getFilePageTitle().getMobileUri()); | ||||
|                         startActivity(context,browserIntent,null); | ||||
|                     } | ||||
|                 }); | ||||
|         alert = builder.create(); | ||||
|         alert.show(); | ||||
|     } | ||||
| } | ||||
|  | @ -15,6 +15,7 @@ import android.view.LayoutInflater; | |||
| import android.view.View; | ||||
| import android.view.ViewGroup; | ||||
| import android.view.ViewTreeObserver; | ||||
| import android.view.WindowManager; | ||||
| import android.widget.Button; | ||||
| import android.widget.EditText; | ||||
| import android.widget.LinearLayout; | ||||
|  | @ -298,58 +299,48 @@ public class MediaDetailFragment extends CommonsDaggerSupportFragment { | |||
|         } | ||||
|         if (delete.getVisibility()==View.VISIBLE){ | ||||
|             delete.setOnClickListener(v -> { | ||||
|                 AlertDialog alert; | ||||
|                 AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); | ||||
|                 builder.setMessage("Sure you want to delete?"); | ||||
|                 builder.setCancelable(true); | ||||
|                 builder.setPositiveButton( | ||||
|                         R.string.yes, | ||||
|                         new DialogInterface.OnClickListener() { | ||||
|                             public void onClick(DialogInterface dialog, int id) { | ||||
|                                 AlertDialog.Builder alert = new AlertDialog.Builder(getActivity()); | ||||
|                                 alert.setTitle("Why should this file be deleted?"); | ||||
|                                 final EditText input = new EditText(getActivity()); | ||||
|                                 alert.setView(input); | ||||
|                                 alert.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() { | ||||
|                                     public void onClick(DialogInterface dialog, int whichButton) { | ||||
|                                         String reason = input.getText().toString(); | ||||
|                                         DeleteTask deleteTask = new DeleteTask(getActivity(), media, reason); | ||||
|                                         deleteTask.execute(); | ||||
|                                     } | ||||
|                                 }); | ||||
|                                 alert.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() { | ||||
|                                     public void onClick(DialogInterface dialog, int whichButton) {} | ||||
|                                 }); | ||||
|                                 AlertDialog d = alert.create(); | ||||
|                                 input.addTextChangedListener(new TextWatcher() { | ||||
|                                     private void handleText() { | ||||
|                                         final Button okButton = d.getButton(AlertDialog.BUTTON_POSITIVE); | ||||
|                                         if(input.getText().length() == 0) { | ||||
|                                             okButton.setEnabled(false); | ||||
|                                         } else { | ||||
|                                             okButton.setEnabled(true); | ||||
|                                         } | ||||
|                                     } | ||||
|                                     @Override | ||||
|                                     public void afterTextChanged(Editable arg0) { | ||||
|                                         handleText(); | ||||
|                                     } | ||||
|                                     @Override | ||||
|                                     public void beforeTextChanged(CharSequence s, int start, int count, int after) {} | ||||
|                                     @Override | ||||
|                                     public void onTextChanged(CharSequence s, int start, int before, int count) {} | ||||
|                                 }); | ||||
|                                 d.show(); | ||||
|                                 d.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(false); | ||||
|                             } | ||||
|                         }); | ||||
|                 builder.setNegativeButton( | ||||
|                         R.string.no, | ||||
|                         new DialogInterface.OnClickListener() { | ||||
|                             public void onClick(DialogInterface dialog, int id) {} | ||||
|                         }); | ||||
|                 alert = builder.create(); | ||||
|                 alert.show(); | ||||
|                 AlertDialog.Builder alert = new AlertDialog.Builder(getActivity()); | ||||
|                 alert.setMessage("Why should this file be deleted?"); | ||||
|                 final EditText input = new EditText(getActivity()); | ||||
|                 alert.setView(input); | ||||
|                 input.requestFocus(); | ||||
|                 alert.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() { | ||||
|                     public void onClick(DialogInterface dialog, int whichButton) { | ||||
|                         String reason = input.getText().toString(); | ||||
|                         DeleteTask deleteTask = new DeleteTask(getActivity(), media, reason); | ||||
|                         deleteTask.execute(); | ||||
|                     } | ||||
|                 }); | ||||
|                 alert.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() { | ||||
|                     public void onClick(DialogInterface dialog, int whichButton) { | ||||
|                     } | ||||
|                 }); | ||||
|                 AlertDialog d = alert.create(); | ||||
|                 input.addTextChangedListener(new TextWatcher() { | ||||
|                     private void handleText() { | ||||
|                         final Button okButton = d.getButton(AlertDialog.BUTTON_POSITIVE); | ||||
|                         if (input.getText().length() == 0) { | ||||
|                             okButton.setEnabled(false); | ||||
|                         } else { | ||||
|                             okButton.setEnabled(true); | ||||
|                         } | ||||
|                     } | ||||
| 
 | ||||
|                     @Override | ||||
|                     public void afterTextChanged(Editable arg0) { | ||||
|                         handleText(); | ||||
|                     } | ||||
| 
 | ||||
|                     @Override | ||||
|                     public void beforeTextChanged(CharSequence s, int start, int count, int after) { | ||||
|                     } | ||||
| 
 | ||||
|                     @Override | ||||
|                     public void onTextChanged(CharSequence s, int start, int before, int count) { | ||||
|                     } | ||||
|                 }); | ||||
|                 d.show(); | ||||
|                 d.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(false); | ||||
|             }); | ||||
|         } | ||||
|     } | ||||
|  |  | |||
|  | @ -205,6 +205,13 @@ public class ApacheHttpClientMediaWikiApi implements MediaWikiApi { | |||
|                 .getNodes("/api/query/pages/page/imageinfo").size() > 0; | ||||
|     } | ||||
| 
 | ||||
|     @Override | ||||
|     public boolean pageExists(String pageName) throws IOException { | ||||
|         return Double.parseDouble( api.action("query") | ||||
|                 .param("titles", pageName) | ||||
|                 .get() | ||||
|                 .getString("/api/query/pages/page/@_idx")) != -1; | ||||
|     } | ||||
| 
 | ||||
|     @Override | ||||
|     @Nullable | ||||
|  |  | |||
|  | @ -28,6 +28,8 @@ public interface MediaWikiApi { | |||
| 
 | ||||
|     boolean fileExistsWithName(String fileName) throws IOException; | ||||
| 
 | ||||
|     boolean pageExists(String pageName) throws IOException; | ||||
| 
 | ||||
|     String findThumbnailByFilename(String filename) throws IOException; | ||||
| 
 | ||||
|     boolean logEvents(LogBuilder[] logBuilders); | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Suchit Kar
						Suchit Kar