From 4dfb4143748b13f1aaf9c5caf4aeb98729d89cdc Mon Sep 17 00:00:00 2001 From: Alex Baker Date: Sat, 11 Jul 2020 09:43:43 -0500 Subject: [PATCH] Use coroutines in drawer customization --- .../NavigationDrawerCustomization.kt | 60 ++++++++++--------- 1 file changed, 33 insertions(+), 27 deletions(-) diff --git a/app/src/main/java/org/tasks/activities/NavigationDrawerCustomization.kt b/app/src/main/java/org/tasks/activities/NavigationDrawerCustomization.kt index 3f9c6599d..9c146a592 100644 --- a/app/src/main/java/org/tasks/activities/NavigationDrawerCustomization.kt +++ b/app/src/main/java/org/tasks/activities/NavigationDrawerCustomization.kt @@ -40,11 +40,11 @@ class NavigationDrawerCustomization : ThemedInjectingAppCompatActivity(), Toolba @Inject lateinit var adapter: NavigationDrawerAdapter @Inject lateinit var localBroadcastManager: LocalBroadcastManager @Inject lateinit var preferences: Preferences - @Inject lateinit var tagDataDao: TagDataDaoBlocking - @Inject lateinit var googleTaskListDao: GoogleTaskListDaoBlocking - @Inject lateinit var filterDao: FilterDaoBlocking - @Inject lateinit var caldavDao: CaldavDaoBlocking - @Inject lateinit var locationDao: LocationDaoBlocking + @Inject lateinit var tagDataDao: TagDataDao + @Inject lateinit var googleTaskListDao: GoogleTaskListDao + @Inject lateinit var filterDao: FilterDao + @Inject lateinit var caldavDao: CaldavDao + @Inject lateinit var locationDao: LocationDao private lateinit var binding: ActivityTagOrganizerBinding private lateinit var toolbar: Toolbar @@ -107,10 +107,12 @@ class NavigationDrawerCustomization : ThemedInjectingAppCompatActivity(), Toolba .putExtra(GoogleTaskListSettingsActivity.EXTRA_STORE_DATA, item.list) .apply(this::startActivity) is CaldavFilter -> - caldavDao.getAccountByUuid(item.account)?.let { - Intent(this, it.listSettingsClass()) - .putExtra(BaseCaldavCalendarSettingsActivity.EXTRA_CALDAV_CALENDAR, item.calendar) - .apply(this::startActivity) + lifecycleScope.launch { + caldavDao.getAccountByUuid(item.account)?.let { + Intent(this@NavigationDrawerCustomization, it.listSettingsClass()) + .putExtra(BaseCaldavCalendarSettingsActivity.EXTRA_CALDAV_CALENDAR, item.calendar) + .apply { startActivity(this) } + } } is CustomFilter -> Intent(this, FilterSettingsActivity::class.java) @@ -130,12 +132,14 @@ class NavigationDrawerCustomization : ThemedInjectingAppCompatActivity(), Toolba override fun onMenuItemClick(item: MenuItem): Boolean { return if (item.itemId == R.id.reset_sort) { - filterDao.resetOrders() - caldavDao.resetOrders() - googleTaskListDao.resetOrders() - tagDataDao.resetOrders() - locationDao.resetOrders() - updateFilters() + lifecycleScope.launch { + filterDao.resetOrders() + caldavDao.resetOrders() + googleTaskListDao.resetOrders() + tagDataDao.resetOrders() + locationDao.resetOrders() + updateFilters() + } true } else { false @@ -201,17 +205,19 @@ class NavigationDrawerCustomization : ThemedInjectingAppCompatActivity(), Toolba if (from != to) { viewHolder.filter.order = to - adapter.items - .apply { - removeAt(from) - add(to, viewHolder.filter) - } - .filter(getPredicate(viewHolder.filter)) - .forEachIndexed { order, filter -> - filter.order = order - setOrder(order, filter) - } - updateFilters() + lifecycleScope.launch { + adapter.items + .apply { + removeAt(from) + add(to, viewHolder.filter) + } + .filter(getPredicate(viewHolder.filter)) + .forEachIndexed { order, filter -> + filter.order = order + setOrder(order, filter) + } + updateFilters() + } } adapter.dragging = false @@ -227,7 +233,7 @@ class NavigationDrawerCustomization : ThemedInjectingAppCompatActivity(), Toolba } } - private fun setOrder(order: Int, filter: FilterListItem) { + private suspend fun setOrder(order: Int, filter: FilterListItem) { when (filter) { is GtasksFilter -> googleTaskListDao.setOrder(filter.list.id, order) is CaldavFilter -> caldavDao.setOrder(filter.calendar.id, order)