Replace flow with channel

pull/1718/head
Alex Baker 3 years ago
parent 4ea948e3f3
commit a0ecff9a3d

@ -17,7 +17,7 @@ import com.todoroo.astrid.api.FilterListItem
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Job
import kotlinx.coroutines.asCoroutineDispatcher
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.channels.Channel
import org.tasks.LocalBroadcastManager
import org.tasks.activities.DragAndDropDiffer
import org.tasks.billing.Inventory
@ -45,7 +45,7 @@ class NavigationDrawerAdapter @Inject constructor(
private lateinit var onClick: (FilterListItem?) -> Unit
private var selected: Filter? = null
override val flow = MutableSharedFlow<MutableList<FilterListItem>>()
override val channel = Channel<List<FilterListItem>>(Channel.UNLIMITED)
override val updates: Queue<Pair<MutableList<FilterListItem>, DiffUtil.DiffResult?>> = LinkedList()
override val scope: CoroutineScope =
CoroutineScope(Executors.newSingleThreadExecutor().asCoroutineDispatcher() + Job())

@ -3,23 +3,30 @@ package org.tasks.activities
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.ListUpdateCallback
import com.todoroo.andlib.utility.AndroidUtilities
import kotlinx.coroutines.*
import kotlinx.coroutines.flow.*
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.Job
import kotlinx.coroutines.cancel
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.flow.consumeAsFlow
import kotlinx.coroutines.flow.drop
import kotlinx.coroutines.flow.flowOn
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.flow.scan
import java.util.*
import java.util.concurrent.Executors
interface DragAndDropDiffer<T, R> : ListUpdateCallback {
val flow: MutableSharedFlow<R>
val channel: Channel<List<T>>
val updates: Queue<Pair<R, DiffUtil.DiffResult?>>
var items: R
var dragging: Boolean
val scope: CoroutineScope
fun submitList(list: List<T>) {
scope.launch {
val transform = transform(list)
flow.emit(transform)
}
channel.trySend(list)
}
fun calculateDiff(last: Pair<R, DiffUtil.DiffResult?>, next: R): Pair<R, DiffUtil.DiffResult?> {
@ -48,18 +55,16 @@ interface DragAndDropDiffer<T, R> : ListUpdateCallback {
@ExperimentalCoroutinesApi
fun initializeDiffer(list: List<T>): R {
val initial = transform(list)
flow
channel
.consumeAsFlow()
.map { transform(it) }
.scan(Pair(initial, null), { last: Pair<R, DiffUtil.DiffResult?>, next: R ->
calculateDiff(last, next)
})
.drop(1)
.flowOn(Executors.newSingleThreadExecutor().asCoroutineDispatcher())
.onEach {
withContext(Dispatchers.Main) {
applyDiff(it)
}
}
.launchIn(scope)
.flowOn(Dispatchers.Default)
.onEach { applyDiff(it) }
.launchIn(CoroutineScope(Dispatchers.Main + Job()))
return initial
}

@ -13,7 +13,7 @@ import com.todoroo.astrid.utility.Flags
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Job
import kotlinx.coroutines.asCoroutineDispatcher
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.runBlocking
import org.tasks.activities.DragAndDropDiffer
import org.tasks.data.TaskContainer
@ -39,7 +39,7 @@ class DragAndDropRecyclerAdapter(
private val itemTouchHelper = ItemTouchHelper(ItemTouchHelperCallback()).apply {
attachToRecyclerView(recyclerView)
}
override val flow = MutableSharedFlow<SectionedDataSource>()
override val channel = Channel<List<TaskContainer>>(Channel.UNLIMITED)
override val updates: Queue<Pair<SectionedDataSource, DiffUtil.DiffResult?>> = LinkedList()
override var dragging = false
override val scope: CoroutineScope =

Loading…
Cancel
Save