From 022644083310ee687cb29d43c87eeca8657bab27 Mon Sep 17 00:00:00 2001 From: Alex Baker Date: Wed, 8 Jul 2020 15:53:35 -0500 Subject: [PATCH] Create edit fragments off main thread --- .../BaseTaskEditViewModelTest.kt | 21 ++++++++++++--- .../tasks/ui/editviewmodel/PriorityTests.kt | 6 ++--- .../tasks/ui/editviewmodel/ReminderTests.kt | 12 ++++----- .../org/tasks/ui/editviewmodel/RepeatTests.kt | 4 +-- .../ui/editviewmodel/TaskEditViewModelTest.kt | 6 ++--- .../org/tasks/ui/editviewmodel/TitleTests.kt | 6 ++--- .../todoroo/astrid/activity/MainActivity.kt | 10 ++++++- .../astrid/activity/TaskEditFragment.kt | 27 ++++++++++++++++--- .../java/org/tasks/ui/TaskEditViewModel.kt | 26 +++++++----------- 9 files changed, 76 insertions(+), 42 deletions(-) diff --git a/app/src/androidTest/java/org/tasks/ui/editviewmodel/BaseTaskEditViewModelTest.kt b/app/src/androidTest/java/org/tasks/ui/editviewmodel/BaseTaskEditViewModelTest.kt index 71a0aa3cf..3ccd092cf 100644 --- a/app/src/androidTest/java/org/tasks/ui/editviewmodel/BaseTaskEditViewModelTest.kt +++ b/app/src/androidTest/java/org/tasks/ui/editviewmodel/BaseTaskEditViewModelTest.kt @@ -4,6 +4,7 @@ import android.content.Context import com.todoroo.astrid.alarms.AlarmService import com.todoroo.astrid.dao.Database import com.todoroo.astrid.dao.TaskDao +import com.todoroo.astrid.data.Task import com.todoroo.astrid.gcal.GCalHelper import com.todoroo.astrid.service.TaskCompleter import com.todoroo.astrid.service.TaskDeleter @@ -14,6 +15,9 @@ import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.runBlocking import org.junit.Before import org.tasks.calendars.CalendarEventProvider +import org.tasks.data.AlarmDao +import org.tasks.data.LocationDao +import org.tasks.data.TagDataDao import org.tasks.injection.InjectingTestCase import org.tasks.location.GeofenceApi import org.tasks.preferences.DefaultFilterProvider @@ -33,9 +37,12 @@ open class BaseTaskEditViewModelTest : InjectingTestCase() { @Inject lateinit var taskMover: TaskMover @Inject lateinit var geofenceApi: GeofenceApi @Inject lateinit var preferences: Preferences - @Inject lateinit var defaultFilterProvider: DefaultFilterProvider @Inject lateinit var taskCompleter: TaskCompleter @Inject lateinit var alarmService: AlarmService + @Inject lateinit var defaultFilterProvider: DefaultFilterProvider + @Inject lateinit var locationDao: LocationDao + @Inject lateinit var tagDataDao: TagDataDao + @Inject lateinit var alarmDao: AlarmDao protected lateinit var viewModel: TaskEditViewModel @@ -56,14 +63,22 @@ open class BaseTaskEditViewModelTest : InjectingTestCase() { db.tagDao, db.tagDataDao, preferences, - defaultFilterProvider, db.googleTaskDao, db.caldavDao, taskCompleter, - db.alarmDao, alarmService) } + protected fun setup(task: Task) = runBlocking { + viewModel.setup( + task, + defaultFilterProvider.getList(task), + locationDao.getLocation(task, preferences), + tagDataDao.getTags(task), + alarmDao.getAlarms(task).map { it.time }.toLongArray() + ) + } + protected fun save(): Boolean = runBlocking(Dispatchers.Main) { viewModel.save() } diff --git a/app/src/androidTest/java/org/tasks/ui/editviewmodel/PriorityTests.kt b/app/src/androidTest/java/org/tasks/ui/editviewmodel/PriorityTests.kt index cbe86da55..c3bf72e75 100644 --- a/app/src/androidTest/java/org/tasks/ui/editviewmodel/PriorityTests.kt +++ b/app/src/androidTest/java/org/tasks/ui/editviewmodel/PriorityTests.kt @@ -14,7 +14,7 @@ import org.tasks.makers.TaskMaker class PriorityTests : BaseTaskEditViewModelTest() { @Test fun changePriorityCausesChange() { - viewModel.setup(TaskMaker.newTask(MakeItEasy.with(TaskMaker.PRIORITY, Task.Priority.HIGH))) + setup(TaskMaker.newTask(MakeItEasy.with(TaskMaker.PRIORITY, Task.Priority.HIGH))) viewModel.priority = Task.Priority.MEDIUM @@ -24,7 +24,7 @@ class PriorityTests : BaseTaskEditViewModelTest() { @Test fun applyPriorityChange() { val task = TaskMaker.newTask(MakeItEasy.with(TaskMaker.PRIORITY, Task.Priority.HIGH)) - viewModel.setup(task) + setup(task) viewModel.priority = Task.Priority.MEDIUM save() @@ -34,7 +34,7 @@ class PriorityTests : BaseTaskEditViewModelTest() { @Test fun noChangeWhenRevertingPriority() { - viewModel.setup(TaskMaker.newTask(MakeItEasy.with(TaskMaker.PRIORITY, Task.Priority.HIGH))) + setup(TaskMaker.newTask(MakeItEasy.with(TaskMaker.PRIORITY, Task.Priority.HIGH))) viewModel.priority = Task.Priority.MEDIUM viewModel.priority = Task.Priority.HIGH diff --git a/app/src/androidTest/java/org/tasks/ui/editviewmodel/ReminderTests.kt b/app/src/androidTest/java/org/tasks/ui/editviewmodel/ReminderTests.kt index 1eb1a6b6b..7ec88d8bb 100644 --- a/app/src/androidTest/java/org/tasks/ui/editviewmodel/ReminderTests.kt +++ b/app/src/androidTest/java/org/tasks/ui/editviewmodel/ReminderTests.kt @@ -15,7 +15,7 @@ class ReminderTests : BaseTaskEditViewModelTest() { @Test fun whenDueReminder() = runBlocking { val task = newTask() - viewModel.setup(task) + setup(task) viewModel.whenDue = true @@ -27,7 +27,7 @@ class ReminderTests : BaseTaskEditViewModelTest() { @Test fun whenOverDueReminder() = runBlocking { val task = newTask() - viewModel.setup(task) + setup(task) viewModel.whenOverdue = true @@ -39,7 +39,7 @@ class ReminderTests : BaseTaskEditViewModelTest() { @Test fun ringFiveTimes() = runBlocking { val task = newTask() - viewModel.setup(task) + setup(task) viewModel.ringFiveTimes = true @@ -51,7 +51,7 @@ class ReminderTests : BaseTaskEditViewModelTest() { @Test fun ringNonstop() = runBlocking { val task = newTask() - viewModel.setup(task) + setup(task) viewModel.ringNonstop = true @@ -63,7 +63,7 @@ class ReminderTests : BaseTaskEditViewModelTest() { @Test fun ringFiveTimesCantRingNonstop() = runBlocking { val task = newTask() - viewModel.setup(task) + setup(task) viewModel.ringNonstop = true viewModel.ringFiveTimes = true @@ -77,7 +77,7 @@ class ReminderTests : BaseTaskEditViewModelTest() { @Test fun ringNonStopCantRingFiveTimes() = runBlocking { val task = newTask() - viewModel.setup(task) + setup(task) viewModel.ringFiveTimes = true viewModel.ringNonstop = true diff --git a/app/src/androidTest/java/org/tasks/ui/editviewmodel/RepeatTests.kt b/app/src/androidTest/java/org/tasks/ui/editviewmodel/RepeatTests.kt index 66d6a3f8e..d37ab1750 100644 --- a/app/src/androidTest/java/org/tasks/ui/editviewmodel/RepeatTests.kt +++ b/app/src/androidTest/java/org/tasks/ui/editviewmodel/RepeatTests.kt @@ -17,7 +17,7 @@ class RepeatTests : BaseTaskEditViewModelTest() { @Test fun changeRepeatAfterCompletion() = runBlocking { val task = newTask(with(TaskMaker.RRULE, RRule("RRULE:FREQ=DAILY;INTERVAL=1"))) - viewModel.setup(task) + setup(task) viewModel.repeatAfterCompletion = true @@ -32,7 +32,7 @@ class RepeatTests : BaseTaskEditViewModelTest() { fun removeRepeatAfterCompletion() = runBlocking { val task = newTask() task.recurrence = "RRULE:FREQ=DAILY;INTERVAL=1;FROM=COMPLETION" - viewModel.setup(task) + setup(task) viewModel.repeatAfterCompletion = false diff --git a/app/src/androidTest/java/org/tasks/ui/editviewmodel/TaskEditViewModelTest.kt b/app/src/androidTest/java/org/tasks/ui/editviewmodel/TaskEditViewModelTest.kt index 3f6a3a37c..0c1db951b 100644 --- a/app/src/androidTest/java/org/tasks/ui/editviewmodel/TaskEditViewModelTest.kt +++ b/app/src/androidTest/java/org/tasks/ui/editviewmodel/TaskEditViewModelTest.kt @@ -15,14 +15,14 @@ import org.tasks.makers.TaskMaker.newTask class TaskEditViewModelTest : BaseTaskEditViewModelTest() { @Test fun noChangesForNewTask() { - viewModel.setup(newTask()) + setup(newTask()) assertFalse(viewModel.hasChanges()) } @Test fun dontSaveTaskWithoutChanges() = runBlocking { - viewModel.setup(newTask()) + setup(newTask()) assertFalse(save()) @@ -31,7 +31,7 @@ class TaskEditViewModelTest : BaseTaskEditViewModelTest() { @Test fun dontSaveTaskTwice() = runBlocking { - viewModel.setup(newTask()) + setup(newTask()) viewModel.priority = Task.Priority.HIGH diff --git a/app/src/androidTest/java/org/tasks/ui/editviewmodel/TitleTests.kt b/app/src/androidTest/java/org/tasks/ui/editviewmodel/TitleTests.kt index 25846147b..36abbb4c5 100644 --- a/app/src/androidTest/java/org/tasks/ui/editviewmodel/TitleTests.kt +++ b/app/src/androidTest/java/org/tasks/ui/editviewmodel/TitleTests.kt @@ -17,7 +17,7 @@ import org.tasks.makers.TaskMaker.newTask class TitleTests : BaseTaskEditViewModelTest() { @Test fun changeTitleCausesChange() { - viewModel.setup(newTask()) + setup(newTask()) viewModel.title = "Test" @@ -27,7 +27,7 @@ class TitleTests : BaseTaskEditViewModelTest() { @Test fun saveWithEmptyTitle() = runBlocking { val task = newTask() - viewModel.setup(task) + setup(task) viewModel.priority = HIGH @@ -38,7 +38,7 @@ class TitleTests : BaseTaskEditViewModelTest() { @Test fun newTaskPrepopulatedWithTitleHasChanges() { - viewModel.setup(newTask(with(TaskMaker.TITLE, "some title"))) + setup(newTask(with(TaskMaker.TITLE, "some title"))) assertTrue(viewModel.hasChanges()) } 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 72e6a6e19..3a6c2c30e 100644 --- a/app/src/main/java/com/todoroo/astrid/activity/MainActivity.kt +++ b/app/src/main/java/com/todoroo/astrid/activity/MainActivity.kt @@ -34,6 +34,7 @@ import org.tasks.LocalBroadcastManager import org.tasks.R import org.tasks.activities.TagSettingsActivity import org.tasks.billing.Inventory +import org.tasks.data.AlarmDao import org.tasks.data.LocationDao import org.tasks.data.Place import org.tasks.data.TagDataDao @@ -73,6 +74,7 @@ class MainActivity : InjectingAppCompatActivity(), TaskListFragmentCallbackHandl @Inject lateinit var colorProvider: ColorProvider @Inject lateinit var locationDao: LocationDao @Inject lateinit var tagDataDao: TagDataDao + @Inject lateinit var alarmDao: AlarmDao private lateinit var navigationDrawer: NavigationDrawerFragment private var currentNightMode = 0 @@ -315,7 +317,13 @@ class MainActivity : InjectingAppCompatActivity(), TaskListFragmentCallbackHandl } } clearUi() - val fragment = newTaskEditFragment(task, filterColor) + val fragment = newTaskEditFragment( + task, + defaultFilterProvider.getList(task), + locationDao.getLocation(task, preferences), + tagDataDao.getTags(task), + alarmDao.getAlarms(task), + filterColor) supportFragmentManager.beginTransaction() .replace(R.id.detail, fragment, TaskEditFragment.TAG_TASKEDIT_FRAGMENT) .addToBackStack(TaskEditFragment.TAG_TASKEDIT_FRAGMENT) diff --git a/app/src/main/java/com/todoroo/astrid/activity/TaskEditFragment.kt b/app/src/main/java/com/todoroo/astrid/activity/TaskEditFragment.kt index e2a906d33..74fe66a5b 100755 --- a/app/src/main/java/com/todoroo/astrid/activity/TaskEditFragment.kt +++ b/app/src/main/java/com/todoroo/astrid/activity/TaskEditFragment.kt @@ -44,8 +44,7 @@ import org.tasks.Event import org.tasks.R import org.tasks.Strings.isNullOrEmpty import org.tasks.analytics.Firebase -import org.tasks.data.UserActivity -import org.tasks.data.UserActivityDao +import org.tasks.data.* import org.tasks.databinding.FragmentTaskEditBinding import org.tasks.date.DateTimeUtils.newDateTime import org.tasks.dialogs.DialogBuilder @@ -82,7 +81,13 @@ class TaskEditFragment : Fragment(), Toolbar.OnMenuItemClickListener { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) - editViewModel.setup(requireArguments().getParcelable(EXTRA_TASK)!!) + val args = requireArguments() + editViewModel.setup( + args.getParcelable(EXTRA_TASK)!!, + args.getParcelable(EXTRA_LIST)!!, + args.getParcelable(EXTRA_LOCATION), + args.getParcelableArrayList(EXTRA_TAGS)!!, + args.getLongArray(EXTRA_ALARMS)!!) val activity = requireActivity() as MainActivity editViewModel.cleared.observe(activity, Observer> { activity.removeTaskEditFragment() @@ -339,12 +344,26 @@ class TaskEditFragment : Fragment(), Toolbar.OnMenuItemClickListener { companion object { const val TAG_TASKEDIT_FRAGMENT = "taskedit_fragment" private const val EXTRA_TASK = "extra_task" + private const val EXTRA_LIST = "extra_list" + private const val EXTRA_LOCATION = "extra_location" + private const val EXTRA_TAGS = "extra_tags" + private const val EXTRA_ALARMS = "extra_alarms" private const val EXTRA_THEME = "extra_theme" - fun newTaskEditFragment(task: Task, themeColor: ThemeColor?): TaskEditFragment { + fun newTaskEditFragment( + task: Task, + list: Filter, + location: Location?, + tags: ArrayList, + alarms: ArrayList, + themeColor: ThemeColor?): TaskEditFragment { val taskEditFragment = TaskEditFragment() val arguments = Bundle() arguments.putParcelable(EXTRA_TASK, task) + arguments.putParcelable(EXTRA_LIST, list) + arguments.putParcelable(EXTRA_LOCATION, location) + arguments.putParcelableArrayList(EXTRA_TAGS, tags) + arguments.putLongArray(EXTRA_ALARMS, alarms.map { it.time }.toLongArray()) arguments.putParcelable(EXTRA_THEME, themeColor) taskEditFragment.arguments = arguments return taskEditFragment diff --git a/app/src/main/java/org/tasks/ui/TaskEditViewModel.kt b/app/src/main/java/org/tasks/ui/TaskEditViewModel.kt index 1875b25a8..d2c081991 100644 --- a/app/src/main/java/org/tasks/ui/TaskEditViewModel.kt +++ b/app/src/main/java/org/tasks/ui/TaskEditViewModel.kt @@ -29,14 +29,13 @@ import kotlinx.collections.immutable.ImmutableSet import kotlinx.collections.immutable.toImmutableList import kotlinx.collections.immutable.toImmutableSet import kotlinx.coroutines.NonCancellable -import kotlinx.coroutines.async +import kotlinx.coroutines.runBlocking import org.tasks.Event import org.tasks.R import org.tasks.Strings import org.tasks.calendars.CalendarEventProvider import org.tasks.data.* import org.tasks.location.GeofenceApi -import org.tasks.preferences.DefaultFilterProvider import org.tasks.preferences.PermissionChecker import org.tasks.preferences.Preferences import org.tasks.time.DateTime @@ -58,30 +57,23 @@ class TaskEditViewModel @ViewModelInject constructor( private val tagDao: TagDao, private val tagDataDao: TagDataDao, private val preferences: Preferences, - private val defaultFilterProvider: DefaultFilterProvider, private val googleTaskDao: GoogleTaskDao, private val caldavDao: CaldavDao, private val taskCompleter: TaskCompleter, - private val alarmDao: AlarmDao, private val alarmService: AlarmService) : ViewModel() { val cleared = MutableLiveData>() - fun setup(task: Task) { + fun setup( + task: Task, list: Filter, location: Location?, tags: List, alarms: LongArray) { this.task = task isNew = task.isNew - runBlocking { - val list = async { defaultFilterProvider.getList(task) } - val location = async { locationDao.getLocation(task, preferences) } - val tags = async { tagDataDao.getTags(task) } - val alarms = async { alarmDao.getAlarms(task) } - originalList = list.await() - originalLocation = location.await() - originalTags = tags.await().toImmutableList() - originalAlarms = alarms.await().map { it.time }.toImmutableSet() - if (isNew && permissionChecker.canAccessCalendars()) { - originalCalendar = preferences.defaultCalendar - } + originalList = list + originalLocation = location + originalTags = tags.toImmutableList() + originalAlarms = alarms.toList().toImmutableSet() + if (isNew && permissionChecker.canAccessCalendars()) { + originalCalendar = preferences.defaultCalendar } }