mirror of
https://github.com/commons-app/apps-android-commons.git
synced 2025-10-26 12:23:58 +01:00
Refactor asynctasks to Rxjava implementation (#2707)
This commit is contained in:
parent
671e305225
commit
ce3349385f
5 changed files with 173 additions and 282 deletions
|
|
@ -65,7 +65,6 @@ public abstract class ActivityBuilderModule {
|
|||
@ContributesAndroidInjector
|
||||
abstract BookmarksActivity bindBookmarksActivity();
|
||||
|
||||
|
||||
@ContributesAndroidInjector
|
||||
abstract ReviewActivity bindReviewActivity();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -12,11 +12,9 @@ import fr.free.nrw.commons.auth.LoginActivity;
|
|||
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<Application
|
|||
|
||||
void inject(DeleteTask deleteTask);
|
||||
|
||||
void inject(CheckCategoryTask checkCategoryTask);
|
||||
|
||||
void inject(SendThankTask sendThankTask);
|
||||
|
||||
void inject(SettingsFragment fragment);
|
||||
|
||||
void inject(ReviewController reviewController);
|
||||
|
||||
@Override
|
||||
void inject(ApplicationlessInjection instance);
|
||||
|
||||
|
|
|
|||
|
|
@ -1,129 +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 timber.log.Timber;
|
||||
|
||||
|
||||
// Example code:
|
||||
// CheckCategoryTask deleteTask = new CheckCategoryTask(getActivity(), media);
|
||||
|
||||
// TODO: refactor; see DeleteTask and SendThankTask
|
||||
public class CheckCategoryTask extends AsyncTask<Void, Integer, Boolean> {
|
||||
|
||||
@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());
|
||||
}
|
||||
}
|
||||
|
|
@ -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<String> 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 class ReviewController {
|
|||
}
|
||||
|
||||
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 class ReviewController {
|
|||
}
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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<Void, Integer, Boolean> {
|
||||
|
||||
@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());
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue