Remove ProgressDialogAsyncTask

pull/795/head
Alex Baker 6 years ago
parent bf3c7c2c78
commit 4ed4d62ec9

@ -48,8 +48,11 @@ import com.todoroo.astrid.service.TaskCreator;
import com.todoroo.astrid.service.TaskDeleter; import com.todoroo.astrid.service.TaskDeleter;
import com.todoroo.astrid.service.TaskMover; import com.todoroo.astrid.service.TaskMover;
import com.todoroo.astrid.timers.TimerPlugin; import com.todoroo.astrid.timers.TimerPlugin;
import io.reactivex.Single;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.CompositeDisposable; import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.disposables.Disposable; import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
import io.reactivex.subjects.PublishSubject; import io.reactivex.subjects.PublishSubject;
import java.util.List; import java.util.List;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
@ -73,8 +76,8 @@ import org.tasks.tasklist.TaskListRecyclerAdapter;
import org.tasks.tasklist.ViewHolderFactory; import org.tasks.tasklist.ViewHolderFactory;
import org.tasks.ui.CheckBoxes; import org.tasks.ui.CheckBoxes;
import org.tasks.ui.MenuColorizer; import org.tasks.ui.MenuColorizer;
import org.tasks.ui.ProgressDialogAsyncTask;
import org.tasks.ui.TaskListViewModel; import org.tasks.ui.TaskListViewModel;
import org.tasks.ui.Toaster;
/** /**
* Primary activity for the Bente application. Shows a list of upcoming tasks and a user's coaches. * Primary activity for the Bente application. Shows a list of upcoming tasks and a user's coaches.
@ -111,6 +114,7 @@ public class TaskListFragment extends InjectingFragment
@Inject Device device; @Inject Device device;
@Inject TaskMover taskMover; @Inject TaskMover taskMover;
@Inject ActionModeProvider actionModeProvider; @Inject ActionModeProvider actionModeProvider;
@Inject Toaster toaster;
@BindView(R.id.swipe_layout) @BindView(R.id.swipe_layout)
SwipeRefreshLayout swipeRefreshLayout; SwipeRefreshLayout swipeRefreshLayout;
@ -383,17 +387,12 @@ public class TaskListFragment extends InjectingFragment
protected void clearCompleted() { protected void clearCompleted() {
tracker.reportEvent(Tracking.Events.CLEAR_COMPLETED); tracker.reportEvent(Tracking.Events.CLEAR_COMPLETED);
new ProgressDialogAsyncTask(getActivity(), dialogBuilder) { disposables.add(
@Override Single.fromCallable(() -> taskDeleter.clearCompleted(filter))
protected Integer doInBackground(Void... params) { .subscribeOn(Schedulers.io())
return taskDeleter.clearCompleted(filter); .observeOn(AndroidSchedulers.mainThread())
} .subscribe(
count -> toaster.longToast(R.string.delete_multiple_tasks_confirmation, count)));
@Override
protected int getResultResource() {
return R.string.delete_multiple_tasks_confirmation;
}
}.execute();
} }
@OnClick(R.id.fab) @OnClick(R.id.fab)
@ -401,7 +400,7 @@ public class TaskListFragment extends InjectingFragment
onTaskListItemClicked(addTask("")); onTaskListItemClicked(addTask(""));
} }
Task addTask(String title) { private Task addTask(String title) {
return taskCreator.createWithValues(filter, title); return taskCreator.createWithValues(filter, title);
} }
@ -543,7 +542,7 @@ public class TaskListFragment extends InjectingFragment
* ====================================================================== * ======================================================================
*/ */
public void onTaskSaved() { void onTaskSaved() {
recyclerAdapter.onTaskSaved(); recyclerAdapter.onTaskSaved();
} }

@ -7,9 +7,15 @@
package com.todoroo.astrid.core; package com.todoroo.astrid.core;
import android.os.Bundle; import android.os.Bundle;
import androidx.annotation.StringRes;
import com.todoroo.astrid.dao.Database; import com.todoroo.astrid.dao.Database;
import com.todoroo.astrid.dao.TaskDao; import com.todoroo.astrid.dao.TaskDao;
import com.todoroo.astrid.service.TaskDeleter; import com.todoroo.astrid.service.TaskDeleter;
import io.reactivex.Single;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.schedulers.Schedulers;
import java.util.concurrent.Callable;
import javax.inject.Inject; import javax.inject.Inject;
import org.tasks.R; import org.tasks.R;
import org.tasks.calendars.CalendarEventProvider; import org.tasks.calendars.CalendarEventProvider;
@ -17,7 +23,7 @@ import org.tasks.dialogs.DialogBuilder;
import org.tasks.injection.ActivityComponent; import org.tasks.injection.ActivityComponent;
import org.tasks.injection.InjectingPreferenceActivity; import org.tasks.injection.InjectingPreferenceActivity;
import org.tasks.preferences.Preferences; import org.tasks.preferences.Preferences;
import org.tasks.ui.ProgressDialogAsyncTask; import org.tasks.ui.Toaster;
public class OldTaskPreferences extends InjectingPreferenceActivity { public class OldTaskPreferences extends InjectingPreferenceActivity {
@ -27,6 +33,9 @@ public class OldTaskPreferences extends InjectingPreferenceActivity {
@Inject TaskDao taskDao; @Inject TaskDao taskDao;
@Inject CalendarEventProvider calendarEventProvider; @Inject CalendarEventProvider calendarEventProvider;
@Inject TaskDeleter taskDeleter; @Inject TaskDeleter taskDeleter;
@Inject Toaster toaster;
private CompositeDisposable disposables;
@Override @Override
public void onCreate(Bundle savedInstanceState) { public void onCreate(Bundle savedInstanceState) {
@ -76,17 +85,8 @@ public class OldTaskPreferences extends InjectingPreferenceActivity {
.setPositiveButton( .setPositiveButton(
android.R.string.ok, android.R.string.ok,
(dialog, which) -> (dialog, which) ->
new ProgressDialogAsyncTask(OldTaskPreferences.this, dialogBuilder) { performAction(
@Override R.string.EPr_manage_purge_deleted_status, () -> taskDeleter.purgeDeleted()))
protected Integer doInBackground(Void... params) {
return taskDeleter.purgeDeleted();
}
@Override
protected int getResultResource() {
return R.string.EPr_manage_purge_deleted_status;
}
}.execute())
.setNegativeButton(android.R.string.cancel, null) .setNegativeButton(android.R.string.cancel, null)
.show(); .show();
} }
@ -97,19 +97,12 @@ public class OldTaskPreferences extends InjectingPreferenceActivity {
.setPositiveButton( .setPositiveButton(
android.R.string.ok, android.R.string.ok,
(dialog, which) -> (dialog, which) ->
new ProgressDialogAsyncTask(OldTaskPreferences.this, dialogBuilder) { performAction(
R.string.EPr_manage_delete_completed_gcal_status,
@Override () -> {
protected Integer doInBackground(Void... params) { calendarEventProvider.deleteEvents(taskDao.getCompletedCalendarEvents());
calendarEventProvider.deleteEvents(taskDao.getCompletedCalendarEvents()); return taskDao.clearCompletedCalendarEvents();
return taskDao.clearCompletedCalendarEvents(); }))
}
@Override
protected int getResultResource() {
return R.string.EPr_manage_delete_completed_gcal_status;
}
}.execute())
.setNegativeButton(android.R.string.cancel, null) .setNegativeButton(android.R.string.cancel, null)
.show(); .show();
} }
@ -120,22 +113,24 @@ public class OldTaskPreferences extends InjectingPreferenceActivity {
.setPositiveButton( .setPositiveButton(
android.R.string.ok, android.R.string.ok,
(dialog, which) -> (dialog, which) ->
new ProgressDialogAsyncTask(OldTaskPreferences.this, dialogBuilder) { performAction(
@Override R.string.EPr_manage_delete_all_gcal_status,
protected Integer doInBackground(Void... params) { () -> {
calendarEventProvider.deleteEvents(taskDao.getAllCalendarEvents()); calendarEventProvider.deleteEvents(taskDao.getAllCalendarEvents());
return taskDao.clearAllCalendarEvents(); return taskDao.clearAllCalendarEvents();
} }))
@Override
protected int getResultResource() {
return R.string.EPr_manage_delete_all_gcal_status;
}
}.execute())
.setNegativeButton(android.R.string.cancel, null) .setNegativeButton(android.R.string.cancel, null)
.show(); .show();
} }
private void performAction(@StringRes int message, Callable<Integer> callable) {
disposables.add(
Single.fromCallable(callable)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(c -> toaster.longToastUnformatted(message, c)));
}
private void resetPreferences() { private void resetPreferences() {
dialogBuilder dialogBuilder
.newMessageDialog(R.string.EPr_reset_preferences_warning) .newMessageDialog(R.string.EPr_reset_preferences_warning)
@ -162,6 +157,20 @@ public class OldTaskPreferences extends InjectingPreferenceActivity {
.show(); .show();
} }
@Override
protected void onResume() {
super.onResume();
disposables = new CompositeDisposable();
}
@Override
protected void onPause() {
super.onPause();
disposables.dispose();
}
@Override @Override
public void inject(ActivityComponent component) { public void inject(ActivityComponent component) {
component.inject(this); component.inject(this);

@ -1,43 +0,0 @@
package org.tasks.ui;
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.widget.Toast;
import org.tasks.R;
import org.tasks.dialogs.DialogBuilder;
import timber.log.Timber;
public abstract class ProgressDialogAsyncTask extends AsyncTask<Void, Void, Integer> {
private final Activity activity;
private final DialogBuilder dialogBuilder;
private ProgressDialog progressDialog;
public ProgressDialogAsyncTask(Activity activity, DialogBuilder dialogBuilder) {
this.activity = activity;
this.dialogBuilder = dialogBuilder;
}
@Override
protected void onPreExecute() {
progressDialog = dialogBuilder.newProgressDialog(R.string.DLG_wait);
progressDialog.show();
}
@Override
protected void onPostExecute(Integer integer) {
if (progressDialog.isShowing()) {
try {
progressDialog.dismiss();
} catch (Exception e) {
Timber.e(e);
}
}
Toast.makeText(activity, activity.getString(getResultResource(), integer), Toast.LENGTH_LONG)
.show();
}
protected abstract int getResultResource();
}

@ -0,0 +1,33 @@
package org.tasks.ui;
import static android.widget.Toast.LENGTH_LONG;
import android.content.Context;
import android.widget.Toast;
import androidx.annotation.StringRes;
import javax.inject.Inject;
import org.tasks.injection.ForActivity;
import org.tasks.locale.Locale;
public class Toaster {
private final Context context;
private final Locale locale;
@Inject
public Toaster(@ForActivity Context context, Locale locale) {
this.context = context;
this.locale = locale;
}
public void longToast(@StringRes int resId, int number) {
Toast.makeText(context, context.getString(resId, locale.formatNumber(number)), LENGTH_LONG)
.show();
}
@SuppressWarnings("DeprecatedIsStillUsed")
@Deprecated
public void longToastUnformatted(@StringRes int resId, int number) {
Toast.makeText(context, context.getString(resId, number), LENGTH_LONG).show();
}
}
Loading…
Cancel
Save