From c600bad7ad4817b883d4631bd147a6bdae367a1c Mon Sep 17 00:00:00 2001 From: vanshikaarora Date: Fri, 22 Mar 2019 19:24:58 +0530 Subject: [PATCH] Refactor asynctasks to Rxjava implementation --- .../nrw/commons/di/ActivityBuilderModule.java | 1 - .../di/CommonsApplicationComponent.java | 10 +- .../nrw/commons/review/CheckCategoryTask.java | 129 ------------- .../nrw/commons/review/ReviewController.java | 177 +++++++++++++++++- .../nrw/commons/review/SendThankTask.java | 138 -------------- 5 files changed, 173 insertions(+), 282 deletions(-) delete mode 100644 app/src/main/java/fr/free/nrw/commons/review/CheckCategoryTask.java delete mode 100644 app/src/main/java/fr/free/nrw/commons/review/SendThankTask.java diff --git a/app/src/main/java/fr/free/nrw/commons/di/ActivityBuilderModule.java b/app/src/main/java/fr/free/nrw/commons/di/ActivityBuilderModule.java index a94911b9f8..2950323e49 100644 --- a/app/src/main/java/fr/free/nrw/commons/di/ActivityBuilderModule.java +++ b/app/src/main/java/fr/free/nrw/commons/di/ActivityBuilderModule.java @@ -65,7 +65,6 @@ public abstract class ActivityBuilderModule { @ContributesAndroidInjector abstract BookmarksActivity bindBookmarksActivity(); - @ContributesAndroidInjector abstract ReviewActivity bindReviewActivity(); } diff --git a/app/src/main/java/fr/free/nrw/commons/di/CommonsApplicationComponent.java b/app/src/main/java/fr/free/nrw/commons/di/CommonsApplicationComponent.java index ec060611c7..dcb8bc4bac 100644 --- a/app/src/main/java/fr/free/nrw/commons/di/CommonsApplicationComponent.java +++ b/app/src/main/java/fr/free/nrw/commons/di/CommonsApplicationComponent.java @@ -12,11 +12,9 @@ import fr.free.nrw.commons.contributions.ContributionsSyncAdapter; import fr.free.nrw.commons.delete.DeleteTask; import fr.free.nrw.commons.modifications.ModificationsSyncAdapter; -import fr.free.nrw.commons.review.CheckCategoryTask; -import fr.free.nrw.commons.review.SendThankTask; +import fr.free.nrw.commons.review.ReviewController; import fr.free.nrw.commons.settings.SettingsFragment; import fr.free.nrw.commons.nearby.PlaceRenderer; -import fr.free.nrw.commons.settings.SettingsFragment; import fr.free.nrw.commons.upload.FileProcessor; import fr.free.nrw.commons.widget.PicOfDayAppWidget; @@ -45,12 +43,10 @@ public interface CommonsApplicationComponent extends AndroidInjector { - - @Inject - MediaWikiApi mwApi; - @Inject - SessionManager sessionManager; - - public static final int NOTIFICATION_CHECK_CATEGORY = 0x101; - - private NotificationManager notificationManager; - private NotificationCompat.Builder notificationBuilder; - private Context context; - private Media media; - - public CheckCategoryTask(Context context, Media media){ - this.context = context; - this.media = media; - } - - @Override - protected void onPreExecute(){ - ApplicationlessInjection - .getInstance(context.getApplicationContext()) - .getCommonsApplicationComponent() - .inject(this); - - notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); - notificationBuilder = new NotificationCompat.Builder(context); - Toast toast = new Toast(context); - toast.setGravity(Gravity.CENTER,0,0); - toast = Toast.makeText(context, context.getString(R.string.check_category_toast, media.getDisplayTitle()), Toast.LENGTH_SHORT); - toast.show(); - } - - @Override - protected Boolean doInBackground(Void ...voids) { - publishProgress(0); - - String editToken; - String authCookie; - String summary = context.getString(R.string.check_category_edit_summary); - - authCookie = sessionManager.getAuthCookie(); - mwApi.setAuthCookie(authCookie); - - try { - editToken = mwApi.getEditToken(); - if (editToken.equals("+\\")) { - return false; - } - publishProgress(1); - - mwApi.appendEdit(editToken, "\n{{subst:chc}}\n", media.getFilename(), summary); - publishProgress(2); - } - catch (Exception e) { - Timber.d(e.getMessage()); - return false; - } - return true; - } - - @Override - protected void onProgressUpdate (Integer... values){ - super.onProgressUpdate(values); - - int[] messages = new int[]{R.string.getting_edit_token, R.string.check_category_adding_template}; - String message = ""; - if (0 < values[0] && values[0] < messages.length) { - message = context.getString(messages[values[0]]); - } - - notificationBuilder.setContentTitle(context.getString(R.string.check_category_notification_title, media.getDisplayTitle())) - .setStyle(new NotificationCompat.BigTextStyle() - .bigText(message)) - .setSmallIcon(R.drawable.ic_launcher) - .setProgress(messages.length, values[0], false) - .setOngoing(true); - notificationManager.notify(NOTIFICATION_CHECK_CATEGORY, notificationBuilder.build()); - } - - @Override - protected void onPostExecute(Boolean result) { - String message = ""; - String title = ""; - - if (result){ - title = context.getString(R.string.check_category_success_title); - message = context.getString(R.string.check_category_success_message, media.getDisplayTitle()); - } - else { - title = context.getString(R.string.check_category_failure_title); - message = context.getString(R.string.check_category_failure_message, media.getDisplayTitle()); - } - - notificationBuilder.setDefaults(NotificationCompat.DEFAULT_ALL) - .setContentTitle(title) - .setStyle(new NotificationCompat.BigTextStyle() - .bigText(message)) - .setSmallIcon(R.drawable.ic_launcher) - .setProgress(0,0,false) - .setOngoing(false) - .setPriority(NotificationCompat.PRIORITY_HIGH); - notificationManager.notify(NOTIFICATION_CHECK_CATEGORY, notificationBuilder.build()); - } -} \ No newline at end of file diff --git a/app/src/main/java/fr/free/nrw/commons/review/ReviewController.java b/app/src/main/java/fr/free/nrw/commons/review/ReviewController.java index a95cc522d7..e9aec93f7d 100644 --- a/app/src/main/java/fr/free/nrw/commons/review/ReviewController.java +++ b/app/src/main/java/fr/free/nrw/commons/review/ReviewController.java @@ -1,34 +1,61 @@ package fr.free.nrw.commons.review; +import android.annotation.SuppressLint; +import android.app.NotificationManager; import android.content.Context; +import android.view.Gravity; +import android.widget.Toast; import java.util.ArrayList; +import javax.inject.Inject; +import javax.inject.Singleton; + import androidx.annotation.Nullable; +import androidx.core.app.NotificationCompat; import androidx.viewpager.widget.ViewPager; import fr.free.nrw.commons.Media; import fr.free.nrw.commons.R; +import fr.free.nrw.commons.auth.SessionManager; import fr.free.nrw.commons.delete.DeleteTask; +import fr.free.nrw.commons.di.ApplicationlessInjection; +import fr.free.nrw.commons.mwapi.MediaWikiApi; import fr.free.nrw.commons.mwapi.Revision; +import io.reactivex.Observable; +import io.reactivex.android.schedulers.AndroidSchedulers; +import io.reactivex.schedulers.Schedulers; +import timber.log.Timber; +@Singleton public class ReviewController { private String fileName; @Nullable public Revision firstRevision; // TODO: maybe we can expand this class to include fileName protected static ArrayList categories; + public static final int NOTIFICATION_SEND_THANK = 0x102; + public static final int NOTIFICATION_CHECK_CATEGORY = 0x101; + private NotificationManager notificationManager; + private NotificationCompat.Builder notificationBuilder; + private Media media; + + @Inject + MediaWikiApi mwApi; + @Inject + SessionManager sessionManager; private ReviewPagerAdapter reviewPagerAdapter; private ViewPager viewPager; private ReviewActivity reviewActivity; ReviewController(Context context) { - reviewActivity = (ReviewActivity)context; + reviewActivity = (ReviewActivity) context; reviewPagerAdapter = reviewActivity.reviewPagerAdapter; - viewPager = ((ReviewActivity)context).reviewPager; + viewPager = ((ReviewActivity) context).reviewPager; } public void onImageRefreshed(String fileName) { this.fileName = fileName; + media = new Media("File:" + fileName); ReviewController.categories = new ArrayList<>(); } @@ -37,7 +64,7 @@ public void onCategoriesRefreshed(ArrayList categories) { } public void swipeToNext() { - int nextPos = viewPager.getCurrentItem()+1; + int nextPos = viewPager.getCurrentItem() + 1; if (nextPos <= 3) { viewPager.setCurrentItem(nextPos); } else { @@ -46,26 +73,162 @@ public void swipeToNext() { } public void reportSpam() { - DeleteTask.askReasonAndExecute(new Media("File:"+fileName), + DeleteTask.askReasonAndExecute(new Media("File:" + fileName), reviewActivity, reviewActivity.getResources().getString(R.string.review_spam_report_question), reviewActivity.getResources().getString(R.string.review_spam_report_problem)); } public void reportPossibleCopyRightViolation() { - DeleteTask.askReasonAndExecute(new Media("File:"+fileName), + DeleteTask.askReasonAndExecute(new Media("File:" + fileName), reviewActivity, reviewActivity.getResources().getString(R.string.review_c_violation_report_question), reviewActivity.getResources().getString(R.string.review_c_violation_report_problem)); } + @SuppressLint("CheckResult") public void reportWrongCategory() { - new CheckCategoryTask(reviewActivity, new Media("File:"+fileName)).execute(); + ApplicationlessInjection + .getInstance(reviewActivity.getApplicationContext()) + .getCommonsApplicationComponent() + .inject(this); + + notificationManager = (NotificationManager) reviewActivity.getSystemService(Context.NOTIFICATION_SERVICE); + notificationBuilder = new NotificationCompat.Builder(reviewActivity); + Toast toast = new Toast(reviewActivity); + toast.setGravity(Gravity.CENTER, 0, 0); + toast = Toast.makeText(reviewActivity, reviewActivity.getString(R.string.check_category_toast, media.getDisplayTitle()), Toast.LENGTH_SHORT); + toast.show(); + + Observable.fromCallable(() -> { + publishProgress(0); + + String editToken; + String authCookie; + String summary = reviewActivity.getString(R.string.check_category_edit_summary); + + authCookie = sessionManager.getAuthCookie(); + mwApi.setAuthCookie(authCookie); + + try { + editToken = mwApi.getEditToken(); + if (editToken.equals("+\\")) { + return false; + } + publishProgress(1); + + mwApi.appendEdit(editToken, "\n{{subst:chc}}\n", media.getFilename(), summary); + publishProgress(2); + } catch (Exception e) { + Timber.d(e); + return false; + } + return true; + }) + .subscribeOn(Schedulers.io()) + .observeOn(AndroidSchedulers.mainThread()) + .subscribe((result) -> { + String message = ""; + String title = ""; + + if (result) { + title = reviewActivity.getString(R.string.check_category_success_title); + message = reviewActivity.getString(R.string.check_category_success_message, media.getDisplayTitle()); + } else { + title = reviewActivity.getString(R.string.check_category_failure_title); + message = reviewActivity.getString(R.string.check_category_failure_message, media.getDisplayTitle()); + } + + notificationBuilder.setDefaults(NotificationCompat.DEFAULT_ALL) + .setContentTitle(title) + .setStyle(new NotificationCompat.BigTextStyle() + .bigText(message)) + .setSmallIcon(R.drawable.ic_launcher) + .setProgress(0, 0, false) + .setOngoing(false) + .setPriority(NotificationCompat.PRIORITY_HIGH); + notificationManager.notify(NOTIFICATION_CHECK_CATEGORY, notificationBuilder.build()); + + }, Timber::e); swipeToNext(); } + private void publishProgress(int i) { + int[] messages = new int[]{R.string.getting_edit_token, R.string.check_category_adding_template}; + String message = ""; + if (0 < i && i < messages.length) { + message = reviewActivity.getString(messages[i]); + } + + notificationBuilder.setContentTitle(reviewActivity.getString(R.string.check_category_notification_title, media.getDisplayTitle())) + .setStyle(new NotificationCompat.BigTextStyle() + .bigText(message)) + .setSmallIcon(R.drawable.ic_launcher) + .setProgress(messages.length, i, false) + .setOngoing(true); + notificationManager.notify(NOTIFICATION_CHECK_CATEGORY, notificationBuilder.build()); + } + + @SuppressLint("CheckResult") public void sendThanks() { - new SendThankTask(reviewActivity, new Media("File:"+fileName), firstRevision).execute(); + ApplicationlessInjection + .getInstance(reviewActivity.getApplicationContext()) + .getCommonsApplicationComponent() + .inject(this); + notificationManager = (NotificationManager) reviewActivity.getSystemService(Context.NOTIFICATION_SERVICE); + notificationBuilder = new NotificationCompat.Builder(reviewActivity); + Toast toast = new Toast(reviewActivity); + toast.setGravity(Gravity.CENTER, 0, 0); + toast = Toast.makeText(reviewActivity, reviewActivity.getString(R.string.send_thank_toast, media.getDisplayTitle()), Toast.LENGTH_SHORT); + toast.show(); + + Observable.fromCallable(() -> { + publishProgress(0); + + String editToken; + String authCookie; + authCookie = sessionManager.getAuthCookie(); + mwApi.setAuthCookie(authCookie); + + try { + editToken = mwApi.getEditToken(); + if (editToken.equals("+\\")) { + return false; + } + publishProgress(1); + assert firstRevision != null; + mwApi.thank(editToken, firstRevision.revisionId); + publishProgress(2); + } catch (Exception e) { + Timber.d(e); + return false; + } + return true; + }) + .subscribeOn(Schedulers.io()) + .observeOn(AndroidSchedulers.mainThread()) + .subscribe((result) -> { + String message = ""; + String title = ""; + if (result) { + title = reviewActivity.getString(R.string.send_thank_success_title); + message = reviewActivity.getString(R.string.send_thank_success_message, media.getDisplayTitle()); + } else { + title = reviewActivity.getString(R.string.send_thank_failure_title); + message = reviewActivity.getString(R.string.send_thank_failure_message, media.getDisplayTitle()); + } + + notificationBuilder.setDefaults(NotificationCompat.DEFAULT_ALL) + .setContentTitle(title) + .setStyle(new NotificationCompat.BigTextStyle() + .bigText(message)) + .setSmallIcon(R.drawable.ic_launcher) + .setProgress(0, 0, false) + .setOngoing(false) + .setPriority(NotificationCompat.PRIORITY_HIGH); + notificationManager.notify(NOTIFICATION_SEND_THANK, notificationBuilder.build()); + + }, Timber::e); swipeToNext(); } } diff --git a/app/src/main/java/fr/free/nrw/commons/review/SendThankTask.java b/app/src/main/java/fr/free/nrw/commons/review/SendThankTask.java deleted file mode 100644 index 51e21b2a9a..0000000000 --- a/app/src/main/java/fr/free/nrw/commons/review/SendThankTask.java +++ /dev/null @@ -1,138 +0,0 @@ -package fr.free.nrw.commons.review; - -import android.app.NotificationManager; -import android.content.Context; -import android.os.AsyncTask; -import android.view.Gravity; -import android.widget.Toast; - -import javax.inject.Inject; - -import androidx.core.app.NotificationCompat; -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 fr.free.nrw.commons.mwapi.Revision; -import timber.log.Timber; - -// example code: -// -// media = new Media("File:Iru.png"); -// Observable.fromCallable(() -> mwApi.firstRevisionOfFile(media.getFilename())) -// .subscribeOn(Schedulers.io()) -// .observeOn(AndroidSchedulers.mainThread()) -// .subscribe(revision -> { -// SendThankTask task = new SendThankTask(getActivity(), media, revision); -// task.execute(); -// }); - -public class SendThankTask extends AsyncTask { - - @Inject - MediaWikiApi mwApi; - @Inject - SessionManager sessionManager; - - public static final int NOTIFICATION_SEND_THANK = 0x102; - - private NotificationManager notificationManager; - private NotificationCompat.Builder notificationBuilder; - private Context context; - private Media media; - private Revision revision; - - public SendThankTask(Context context, Media media, Revision revision){ - this.context = context; - this.media = media; - this.revision = revision; - } - - @Override - protected void onPreExecute(){ - ApplicationlessInjection - .getInstance(context.getApplicationContext()) - .getCommonsApplicationComponent() - .inject(this); - - notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); - notificationBuilder = new NotificationCompat.Builder(context); - Toast toast = new Toast(context); - toast.setGravity(Gravity.CENTER,0,0); - toast = Toast.makeText(context, context.getString(R.string.send_thank_toast, media.getDisplayTitle()), Toast.LENGTH_SHORT); - toast.show(); - } - - @Override - protected Boolean doInBackground(Void ...voids) { - publishProgress(0); - - String editToken; - String authCookie; - - authCookie = sessionManager.getAuthCookie(); - mwApi.setAuthCookie(authCookie); - - try { - editToken = mwApi.getEditToken(); - if (editToken.equals("+\\")) { - return false; - } - publishProgress(1); - - mwApi.thank(editToken, revision.revisionId); - - publishProgress(2); - } - catch (Exception e) { - Timber.d(e.getMessage()); - return false; - } - return true; - } - - @Override - protected void onProgressUpdate (Integer... values){ - super.onProgressUpdate(values); - - int[] messages = new int[]{R.string.getting_edit_token, R.string.send_thank_send}; - String message = ""; - if (0 < values[0] && values[0] < messages.length) { - message = context.getString(messages[values[0]]); - } - - notificationBuilder.setContentTitle(context.getString(R.string.send_thank_notification_title)) - .setStyle(new NotificationCompat.BigTextStyle() - .bigText(message)) - .setSmallIcon(R.drawable.ic_launcher) - .setProgress(messages.length, values[0], false) - .setOngoing(true); - notificationManager.notify(NOTIFICATION_SEND_THANK, notificationBuilder.build()); - } - - @Override - protected void onPostExecute(Boolean result) { - String message = ""; - String title = ""; - - if (result){ - title = context.getString(R.string.send_thank_success_title); - message = context.getString(R.string.send_thank_success_message, media.getDisplayTitle()); - } - else { - title = context.getString(R.string.send_thank_failure_title); - message = context.getString(R.string.send_thank_failure_message, media.getDisplayTitle()); - } - - notificationBuilder.setDefaults(NotificationCompat.DEFAULT_ALL) - .setContentTitle(title) - .setStyle(new NotificationCompat.BigTextStyle() - .bigText(message)) - .setSmallIcon(R.drawable.ic_launcher) - .setProgress(0,0,false) - .setOngoing(false) - .setPriority(NotificationCompat.PRIORITY_HIGH); - notificationManager.notify(NOTIFICATION_SEND_THANK, notificationBuilder.build()); - } -} \ No newline at end of file