From be63596e941aa97e754c5729ace553e0f6c581a7 Mon Sep 17 00:00:00 2001 From: Alex Baker Date: Sun, 12 Jul 2020 07:22:29 -0500 Subject: [PATCH] Convert DragAndDropDiffer.submitList to coroutines --- .../astrid/activity/TaskListFragment.kt | 27 +++++++++---------- .../org/tasks/activities/DragAndDropDiffer.kt | 13 ++++----- .../NavigationDrawerCustomization.kt | 2 +- .../tasklist/DragAndDropRecyclerAdapter.kt | 4 ++- .../tasklist/PagedListRecyclerAdapter.kt | 2 +- .../tasks/tasklist/TaskListRecyclerAdapter.kt | 2 +- .../org/tasks/ui/NavigationDrawerFragment.kt | 3 +-- .../java/org/tasks/ui/SubtaskControlSet.kt | 5 ++-- .../java/org/tasks/ui/TaskListViewModel.kt | 2 +- 9 files changed, 31 insertions(+), 29 deletions(-) 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 8b4f9ec47..6bdda5bb2 100644 --- a/app/src/main/java/com/todoroo/astrid/activity/TaskListFragment.kt +++ b/app/src/main/java/com/todoroo/astrid/activity/TaskListFragment.kt @@ -21,7 +21,6 @@ import androidx.appcompat.widget.Toolbar import androidx.coordinatorlayout.widget.CoordinatorLayout import androidx.fragment.app.Fragment import androidx.fragment.app.viewModels -import androidx.lifecycle.Observer import androidx.lifecycle.lifecycleScope import androidx.paging.PagedList import androidx.recyclerview.widget.DefaultItemAnimator @@ -202,18 +201,18 @@ class TaskListFragment : Fragment(), OnRefreshListener, Toolbar.OnMenuItemClickL listViewModel.setFilter((if (searchQuery == null) filter else createSearchFilter(searchQuery!!))) (recyclerView.itemAnimator as DefaultItemAnimator).supportsChangeAnimations = false recyclerView.layoutManager = LinearLayoutManager(context) - listViewModel.observe( - this, - Observer { list: List -> - submitList(list) - if (list.isEmpty()) { - swipeRefreshLayout.visibility = View.GONE - emptyRefreshLayout.visibility = View.VISIBLE - } else { - swipeRefreshLayout.visibility = View.VISIBLE - emptyRefreshLayout.visibility = View.GONE - } - }) + listViewModel.observe(this) { + lifecycleScope.launch { + submitList(it) + if (it.isEmpty()) { + swipeRefreshLayout.visibility = View.GONE + emptyRefreshLayout.visibility = View.VISIBLE + } else { + swipeRefreshLayout.visibility = View.VISIBLE + emptyRefreshLayout.visibility = View.GONE + } + } + } setupRefresh(swipeRefreshLayout) setupRefresh(emptyRefreshLayout) toolbar.title = filter.listingTitle @@ -224,7 +223,7 @@ class TaskListFragment : Fragment(), OnRefreshListener, Toolbar.OnMenuItemClickL return parent } - private fun submitList(tasks: List) { + private suspend fun submitList(tasks: List) { if (tasks is PagedList<*>) { if (recyclerAdapter !is PagedListRecyclerAdapter) { setAdapter( diff --git a/app/src/main/java/org/tasks/activities/DragAndDropDiffer.kt b/app/src/main/java/org/tasks/activities/DragAndDropDiffer.kt index e5450a99c..4462e503d 100644 --- a/app/src/main/java/org/tasks/activities/DragAndDropDiffer.kt +++ b/app/src/main/java/org/tasks/activities/DragAndDropDiffer.kt @@ -3,11 +3,12 @@ package org.tasks.activities import androidx.recyclerview.widget.DiffUtil import androidx.recyclerview.widget.ListUpdateCallback import com.todoroo.andlib.utility.AndroidUtilities -import io.reactivex.Single import io.reactivex.android.schedulers.AndroidSchedulers import io.reactivex.disposables.CompositeDisposable import io.reactivex.schedulers.Schedulers import io.reactivex.subjects.PublishSubject +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.withContext import java.util.* interface DragAndDropDiffer : ListUpdateCallback { @@ -17,11 +18,11 @@ interface DragAndDropDiffer : ListUpdateCallback { var items: R var dragging: Boolean - fun submitList(list: List) { - disposables.add( - Single.fromCallable { transform(list) } - .subscribeOn(Schedulers.computation()) - .subscribe(publishSubject::onNext)) + suspend fun submitList(list: List) { + val transform = withContext(Dispatchers.Default) { + transform(list) + } + publishSubject.onNext(transform) } fun calculateDiff(last: Pair, next: R): Pair { diff --git a/app/src/main/java/org/tasks/activities/NavigationDrawerCustomization.kt b/app/src/main/java/org/tasks/activities/NavigationDrawerCustomization.kt index 9c146a592..bd9b37473 100644 --- a/app/src/main/java/org/tasks/activities/NavigationDrawerCustomization.kt +++ b/app/src/main/java/org/tasks/activities/NavigationDrawerCustomization.kt @@ -90,7 +90,7 @@ class NavigationDrawerCustomization : ThemedInjectingAppCompatActivity(), Toolba filterProvider .drawerCustomizationItems() .onEach { f -> f.count = 0 } - .apply(adapter::submitList) + .let { adapter.submitList(it) } } private fun onClick(item: FilterListItem?) { diff --git a/app/src/main/java/org/tasks/tasklist/DragAndDropRecyclerAdapter.kt b/app/src/main/java/org/tasks/tasklist/DragAndDropRecyclerAdapter.kt index 595836031..987006c23 100644 --- a/app/src/main/java/org/tasks/tasklist/DragAndDropRecyclerAdapter.kt +++ b/app/src/main/java/org/tasks/tasklist/DragAndDropRecyclerAdapter.kt @@ -61,7 +61,9 @@ class DragAndDropRecyclerAdapter( override fun getItemViewType(position: Int) = if (items.isHeader(position)) 1 else 0 - override fun submitList(list: List) = super.submitList(list) + override suspend fun submitList(list: List) { + super.submitList(list) + } override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder = if (viewType == 1) { viewHolderFactory.newHeaderViewHolder(parent, this::toggleGroup) diff --git a/app/src/main/java/org/tasks/tasklist/PagedListRecyclerAdapter.kt b/app/src/main/java/org/tasks/tasklist/PagedListRecyclerAdapter.kt index 245b0980a..017f0d138 100644 --- a/app/src/main/java/org/tasks/tasklist/PagedListRecyclerAdapter.kt +++ b/app/src/main/java/org/tasks/tasklist/PagedListRecyclerAdapter.kt @@ -22,7 +22,7 @@ class PagedListRecyclerAdapter( override fun getItem(position: Int) = differ.getItem(position) - override fun submitList(list: List) = differ.submitList(list as PagedList) + override suspend fun submitList(list: List) = differ.submitList(list as PagedList) override fun onMoved(fromPosition: Int, toPosition: Int) { val recyclerViewState = recyclerView.layoutManager!!.onSaveInstanceState() diff --git a/app/src/main/java/org/tasks/tasklist/TaskListRecyclerAdapter.kt b/app/src/main/java/org/tasks/tasklist/TaskListRecyclerAdapter.kt index ead2d64eb..903017235 100644 --- a/app/src/main/java/org/tasks/tasklist/TaskListRecyclerAdapter.kt +++ b/app/src/main/java/org/tasks/tasklist/TaskListRecyclerAdapter.kt @@ -44,7 +44,7 @@ abstract class TaskListRecyclerAdapter internal constructor( abstract fun dragAndDropEnabled(): Boolean - abstract fun submitList(list: List) + abstract suspend fun submitList(list: List) override fun onInserted(position: Int, count: Int) { notifyItemRangeInserted(position, count) diff --git a/app/src/main/java/org/tasks/ui/NavigationDrawerFragment.kt b/app/src/main/java/org/tasks/ui/NavigationDrawerFragment.kt index 49e8ff4c2..30417cba6 100644 --- a/app/src/main/java/org/tasks/ui/NavigationDrawerFragment.kt +++ b/app/src/main/java/org/tasks/ui/NavigationDrawerFragment.kt @@ -13,7 +13,6 @@ import android.view.ViewGroup import androidx.drawerlayout.widget.DrawerLayout import androidx.drawerlayout.widget.DrawerLayout.SimpleDrawerListener import androidx.fragment.app.Fragment -import androidx.lifecycle.ViewModelProvider import androidx.lifecycle.lifecycleScope import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.RecyclerView @@ -141,7 +140,7 @@ class NavigationDrawerFragment : Fragment() { it.count = taskDao.count(it) } } - .apply(adapter::submitList) + .let { adapter.submitList(it) } } private inner class RefreshReceiver : BroadcastReceiver() { diff --git a/app/src/main/java/org/tasks/ui/SubtaskControlSet.kt b/app/src/main/java/org/tasks/ui/SubtaskControlSet.kt index ba88a6a56..40277c151 100644 --- a/app/src/main/java/org/tasks/ui/SubtaskControlSet.kt +++ b/app/src/main/java/org/tasks/ui/SubtaskControlSet.kt @@ -16,7 +16,6 @@ import android.widget.EditText import android.widget.LinearLayout import androidx.core.widget.addTextChangedListener import androidx.fragment.app.viewModels -import androidx.lifecycle.Observer import androidx.lifecycle.lifecycleScope import androidx.recyclerview.widget.DefaultItemAnimator import androidx.recyclerview.widget.LinearLayoutManager @@ -85,7 +84,9 @@ class SubtaskControlSet : TaskEditControlFragment(), SubtaskViewHolder.Callbacks (recyclerView.itemAnimator as DefaultItemAnimator).supportsChangeAnimations = false recyclerView.layoutManager = LinearLayoutManager(activity) recyclerView.isNestedScrollingEnabled = false - listViewModel.observe(this, Observer { list: List? -> recyclerAdapter.submitList(list) }) + listViewModel.observe(this) { + list: List? -> recyclerAdapter.submitList(list) + } recyclerView.adapter = recyclerAdapter } } diff --git a/app/src/main/java/org/tasks/ui/TaskListViewModel.kt b/app/src/main/java/org/tasks/ui/TaskListViewModel.kt index ae76b2397..c1d294e48 100644 --- a/app/src/main/java/org/tasks/ui/TaskListViewModel.kt +++ b/app/src/main/java/org/tasks/ui/TaskListViewModel.kt @@ -38,7 +38,7 @@ class TaskListViewModel @ViewModelInject constructor( || filter.supportsAstridSorting() && preferences.isAstridSort) } - fun observe(owner: LifecycleOwner, observer: Observer>) = + fun observe(owner: LifecycleOwner, observer: (List) -> Unit) = tasks.observe(owner, observer) fun searchByFilter(filter: Filter?) {