Convert DragAndDropDiffer.submitList to coroutines

pull/1051/head
Alex Baker 4 years ago
parent 4dfb414374
commit be63596e94

@ -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<TaskContainer> ->
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<TaskContainer>) {
private suspend fun submitList(tasks: List<TaskContainer>) {
if (tasks is PagedList<*>) {
if (recyclerAdapter !is PagedListRecyclerAdapter) {
setAdapter(

@ -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<T, R> : ListUpdateCallback {
@ -17,11 +18,11 @@ interface DragAndDropDiffer<T, R> : ListUpdateCallback {
var items: R
var dragging: Boolean
fun submitList(list: List<T>) {
disposables.add(
Single.fromCallable { transform(list) }
.subscribeOn(Schedulers.computation())
.subscribe(publishSubject::onNext))
suspend fun submitList(list: List<T>) {
val transform = withContext(Dispatchers.Default) {
transform(list)
}
publishSubject.onNext(transform)
}
fun calculateDiff(last: Pair<R, DiffUtil.DiffResult?>, next: R): Pair<R, DiffUtil.DiffResult?> {

@ -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?) {

@ -61,7 +61,9 @@ class DragAndDropRecyclerAdapter(
override fun getItemViewType(position: Int) = if (items.isHeader(position)) 1 else 0
override fun submitList(list: List<TaskContainer>) = super.submitList(list)
override suspend fun submitList(list: List<TaskContainer>) {
super.submitList(list)
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder = if (viewType == 1) {
viewHolderFactory.newHeaderViewHolder(parent, this::toggleGroup)

@ -22,7 +22,7 @@ class PagedListRecyclerAdapter(
override fun getItem(position: Int) = differ.getItem(position)
override fun submitList(list: List<TaskContainer>) = differ.submitList(list as PagedList<TaskContainer>)
override suspend fun submitList(list: List<TaskContainer>) = differ.submitList(list as PagedList<TaskContainer>)
override fun onMoved(fromPosition: Int, toPosition: Int) {
val recyclerViewState = recyclerView.layoutManager!!.onSaveInstanceState()

@ -44,7 +44,7 @@ abstract class TaskListRecyclerAdapter internal constructor(
abstract fun dragAndDropEnabled(): Boolean
abstract fun submitList(list: List<TaskContainer>)
abstract suspend fun submitList(list: List<TaskContainer>)
override fun onInserted(position: Int, count: Int) {
notifyItemRangeInserted(position, count)

@ -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() {

@ -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<TaskContainer?>? -> recyclerAdapter.submitList(list) })
listViewModel.observe(this) {
list: List<TaskContainer?>? -> recyclerAdapter.submitList(list)
}
recyclerView.adapter = recyclerAdapter
}
}

@ -38,7 +38,7 @@ class TaskListViewModel @ViewModelInject constructor(
|| filter.supportsAstridSorting() && preferences.isAstridSort)
}
fun observe(owner: LifecycleOwner, observer: Observer<List<TaskContainer>>) =
fun observe(owner: LifecycleOwner, observer: (List<TaskContainer>) -> Unit) =
tasks.observe(owner, observer)
fun searchByFilter(filter: Filter?) {

Loading…
Cancel
Save