diff --git a/app/src/main/java/com/todoroo/astrid/activity/MainActivity.kt b/app/src/main/java/com/todoroo/astrid/activity/MainActivity.kt index 3468b3e2c..b003b43d5 100644 --- a/app/src/main/java/com/todoroo/astrid/activity/MainActivity.kt +++ b/app/src/main/java/com/todoroo/astrid/activity/MainActivity.kt @@ -48,7 +48,6 @@ import org.tasks.intents.TaskIntents import org.tasks.location.LocationPickerActivity import org.tasks.preferences.DefaultFilterProvider import org.tasks.preferences.Preferences -import org.tasks.receivers.RepeatConfirmationReceiver import org.tasks.tasklist.ActionUtils import org.tasks.themes.ColorProvider import org.tasks.themes.Theme @@ -63,7 +62,6 @@ import javax.inject.Inject @AndroidEntryPoint class MainActivity : InjectingAppCompatActivity(), TaskListFragmentCallbackHandler, OnListChanged, TimerControlSetCallback, DueDateChangeListener, CommentBarFragmentCallback, SortDialogCallback { @Inject lateinit var preferences: Preferences - @Inject lateinit var repeatConfirmationReceiver: RepeatConfirmationReceiver @Inject lateinit var defaultFilterProvider: DefaultFilterProvider @Inject lateinit var theme: Theme @Inject lateinit var taskDao: TaskDao @@ -333,7 +331,6 @@ class MainActivity : InjectingAppCompatActivity(), TaskListFragmentCallbackHandl recreate() return } - localBroadcastManager.registerRepeatReceiver(repeatConfirmationReceiver) if (preferences.getBoolean(R.string.p_just_updated, false)) { if (preferences.getBoolean(R.string.p_show_whats_new, true)) { val fragmentManager = supportFragmentManager @@ -348,11 +345,6 @@ class MainActivity : InjectingAppCompatActivity(), TaskListFragmentCallbackHandl private val nightMode: Int get() = resources.configuration.uiMode and Configuration.UI_MODE_NIGHT_MASK - override fun onPause() { - super.onPause() - localBroadcastManager.unregisterReceiver(repeatConfirmationReceiver) - } - override fun onTaskListItemClicked(task: Task?) { AndroidUtilities.assertMainThread() if (task == null) { diff --git a/app/src/main/java/com/todoroo/astrid/activity/TaskListFragment.kt b/app/src/main/java/com/todoroo/astrid/activity/TaskListFragment.kt index 6bdda5bb2..5b6ec4055 100644 --- a/app/src/main/java/com/todoroo/astrid/activity/TaskListFragment.kt +++ b/app/src/main/java/com/todoroo/astrid/activity/TaskListFragment.kt @@ -32,7 +32,9 @@ import butterknife.BindView import butterknife.ButterKnife import butterknife.OnClick import com.google.android.material.snackbar.Snackbar +import com.google.ical.values.RRule import com.todoroo.andlib.utility.AndroidUtilities +import com.todoroo.andlib.utility.DateUtilities import com.todoroo.astrid.adapter.TaskAdapter import com.todoroo.astrid.adapter.TaskAdapterProvider import com.todoroo.astrid.api.* @@ -49,12 +51,14 @@ import io.reactivex.disposables.CompositeDisposable import io.reactivex.disposables.Disposable import io.reactivex.schedulers.Schedulers import io.reactivex.subjects.PublishSubject +import kotlinx.coroutines.NonCancellable import kotlinx.coroutines.launch import org.tasks.LocalBroadcastManager import org.tasks.R import org.tasks.ShortcutManager import org.tasks.activities.* import org.tasks.activities.ListPicker.Companion.newListPicker +import org.tasks.analytics.Firebase import org.tasks.caldav.BaseCaldavCalendarSettingsActivity import org.tasks.data.CaldavDao import org.tasks.data.TagDataDao @@ -75,6 +79,9 @@ import org.tasks.themes.ColorProvider import org.tasks.themes.ThemeColor import org.tasks.ui.TaskListViewModel import org.tasks.ui.Toaster +import timber.log.Timber +import java.text.ParseException +import java.time.format.FormatStyle import java.util.* import java.util.concurrent.TimeUnit import javax.inject.Inject @@ -85,6 +92,7 @@ class TaskListFragment : Fragment(), OnRefreshListener, Toolbar.OnMenuItemClickL MenuItem.OnActionExpandListener, SearchView.OnQueryTextListener, ActionMode.Callback, TaskViewHolder.ViewHolderCallbacks { private val refreshReceiver = RefreshReceiver() + private val repeatConfirmationReceiver = RepeatConfirmationReceiver() private var disposables: CompositeDisposable? = null @Inject lateinit var syncAdapters: SyncAdapters @@ -108,6 +116,8 @@ class TaskListFragment : Fragment(), OnRefreshListener, Toolbar.OnMenuItemClickL @Inject lateinit var notificationManager: NotificationManager @Inject lateinit var shortcutManager: ShortcutManager @Inject lateinit var taskCompleter: TaskCompleter + @Inject lateinit var locale: Locale + @Inject lateinit var firebase: Firebase @BindView(R.id.swipe_layout) lateinit var swipeRefreshLayout: SwipeRefreshLayout @@ -135,7 +145,7 @@ class TaskListFragment : Fragment(), OnRefreshListener, Toolbar.OnMenuItemClickL private var mode: ActionMode? = null private lateinit var themeColor: ThemeColor private lateinit var callbacks: TaskListFragmentCallbackHandler - + override fun onRefresh() { disposables!!.add( syncAdapters @@ -441,6 +451,7 @@ class TaskListFragment : Fragment(), OnRefreshListener, Toolbar.OnMenuItemClickL super.onResume() disposables = CompositeDisposable() localBroadcastManager.registerRefreshReceiver(refreshReceiver) + localBroadcastManager.registerRepeatReceiver(repeatConfirmationReceiver) refresh() } @@ -459,6 +470,7 @@ class TaskListFragment : Fragment(), OnRefreshListener, Toolbar.OnMenuItemClickL override fun onPause() { super.onPause() disposables?.dispose() + localBroadcastManager.unregisterReceiver(repeatConfirmationReceiver) localBroadcastManager.unregisterReceiver(refreshReceiver) } @@ -716,12 +728,6 @@ class TaskListFragment : Fragment(), OnRefreshListener, Toolbar.OnMenuItemClickL fun onNavigationIconClicked() } - private inner class RefreshReceiver : BroadcastReceiver() { - override fun onReceive(context: Context, intent: Intent) { - refresh() - } - } - val isActionModeActive: Boolean get() = mode != null @@ -805,6 +811,50 @@ class TaskListFragment : Fragment(), OnRefreshListener, Toolbar.OnMenuItemClickL } } + private inner class RefreshReceiver : BroadcastReceiver() { + override fun onReceive(context: Context, intent: Intent) { + refresh() + } + } + + private inner class RepeatConfirmationReceiver : BroadcastReceiver() { + override fun onReceive(context: Context, intent: Intent) { + val taskId = intent.getLongExtra(AstridApiConstants.EXTRAS_TASK_ID, 0) + if (taskId > 0) { + val oldDueDate = intent.getLongExtra(AstridApiConstants.EXTRAS_OLD_DUE_DATE, 0) + val newDueDate = intent.getLongExtra(AstridApiConstants.EXTRAS_NEW_DUE_DATE, 0) + lifecycleScope.launch { + val task = taskDao.fetch(taskId) + try { + val dueDateString = DateUtilities.getRelativeDateTime( + context, newDueDate, locale, FormatStyle.LONG, true) + makeSnackbar(R.string.repeat_snackbar, task!!.title, dueDateString) + .setAction(R.string.DLG_undo) { + task.setDueDateAdjustingHideUntil(oldDueDate) + task.completionDate = 0L + try { + val rrule = RRule(task.getRecurrenceWithoutFrom()) + val count = rrule.count + if (count > 0) { + rrule.count = count + 1 + } + task.setRecurrence(rrule, task.repeatAfterCompletion()) + } catch (e: ParseException) { + Timber.e(e) + } + lifecycleScope.launch(NonCancellable) { + taskDao.save(task) + } + } + .show() + } catch (e: Exception) { + firebase.reportException(e) + } + } + } + } + } + companion object { const val TAGS_METADATA_JOIN = "for_tags" // $NON-NLS-1$ const val GTASK_METADATA_JOIN = "googletask" // $NON-NLS-1$ diff --git a/app/src/main/java/org/tasks/receivers/RepeatConfirmationReceiver.java b/app/src/main/java/org/tasks/receivers/RepeatConfirmationReceiver.java deleted file mode 100644 index bc667a0fd..000000000 --- a/app/src/main/java/org/tasks/receivers/RepeatConfirmationReceiver.java +++ /dev/null @@ -1,92 +0,0 @@ -package org.tasks.receivers; - -import android.app.Activity; -import android.content.BroadcastReceiver; -import android.content.Context; -import android.content.Intent; -import com.google.ical.values.RRule; -import com.todoroo.andlib.utility.DateUtilities; -import com.todoroo.astrid.activity.MainActivity; -import com.todoroo.astrid.activity.TaskListFragment; -import com.todoroo.astrid.api.AstridApiConstants; -import com.todoroo.astrid.dao.TaskDaoBlocking; -import com.todoroo.astrid.data.Task; -import java.text.ParseException; -import javax.inject.Inject; -import org.tasks.R; -import org.tasks.analytics.Firebase; -import org.tasks.locale.Locale; -import java.time.format.FormatStyle; -import timber.log.Timber; - -public class RepeatConfirmationReceiver extends BroadcastReceiver { - - private final Activity activity; - private final Firebase firebase; - private final TaskDaoBlocking taskDao; - private final Locale locale; - - @Inject - public RepeatConfirmationReceiver( - Activity activity, Firebase firebase, TaskDaoBlocking taskDao, Locale locale) { - this.activity = activity; - this.firebase = firebase; - this.taskDao = taskDao; - this.locale = locale; - } - - @Override - public void onReceive(final Context context, final Intent intent) { - TaskListFragment taskListFragment = null; - if (activity instanceof MainActivity) { - taskListFragment = ((MainActivity) activity).getTaskListFragment(); - } - if (taskListFragment == null) { - Timber.d("No task list fragment"); - return; - } - long taskId = intent.getLongExtra(AstridApiConstants.EXTRAS_TASK_ID, 0); - - if (taskId > 0) { - long oldDueDate = intent.getLongExtra(AstridApiConstants.EXTRAS_OLD_DUE_DATE, 0); - long newDueDate = intent.getLongExtra(AstridApiConstants.EXTRAS_NEW_DUE_DATE, 0); - Task task = taskDao.fetchBlocking(taskId); - - try { - showSnackbar(taskListFragment, task, oldDueDate, newDueDate); - } catch (Exception e) { - firebase.reportException(e); - } - } - } - - private void showSnackbar( - TaskListFragment taskListFragment, - final Task task, - final long oldDueDate, - final long newDueDate) { - String dueDateString = - DateUtilities.getRelativeDateTime( - activity, newDueDate, locale.getLocale(), FormatStyle.LONG, true); - taskListFragment - .makeSnackbar(R.string.repeat_snackbar, task.getTitle(), dueDateString) - .setAction( - R.string.DLG_undo, - v -> { - task.setDueDateAdjustingHideUntil(oldDueDate); - task.setCompletionDate(0L); - try { - RRule rrule = new RRule(task.getRecurrenceWithoutFrom()); - int count = rrule.getCount(); - if (count > 0) { - rrule.setCount(count + 1); - } - task.setRecurrence(rrule, task.repeatAfterCompletion()); - } catch (ParseException e) { - Timber.e(e); - } - taskDao.save(task); - }) - .show(); - } -}