Skip to content

safeOffer instead of offer #8

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 30, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 12 additions & 7 deletions core/src/main/java/com/hoc/flowmvi/core/FlowBinding+Exts+Utils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,11 @@ import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.ItemTouchHelper
import androidx.recyclerview.widget.RecyclerView
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout
import java.util.concurrent.atomic.AtomicBoolean
import java.util.concurrent.atomic.AtomicReference
import kotlin.coroutines.EmptyCoroutineContext
import kotlinx.coroutines.CancellationException
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.cancel
import kotlinx.coroutines.channels.SendChannel
import kotlinx.coroutines.channels.awaitClose
import kotlinx.coroutines.coroutineScope
import kotlinx.coroutines.delay
Expand All @@ -37,6 +35,13 @@ import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.flow.onStart
import kotlinx.coroutines.flow.take
import kotlinx.coroutines.launch
import java.util.concurrent.atomic.AtomicBoolean
import java.util.concurrent.atomic.AtomicReference
import kotlin.coroutines.EmptyCoroutineContext

internal fun <T> SendChannel<T>.safeOffer(element: T): Boolean {
return runCatching { offer(element) }.getOrDefault(false)
}

@ExperimentalCoroutinesApi
fun EditText.firstChange(): Flow<Unit> {
Expand All @@ -45,7 +50,7 @@ fun EditText.firstChange(): Flow<Unit> {
override fun afterTextChanged(s: Editable?) = Unit
override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) = Unit
override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
offer(Unit)
safeOffer(Unit)
}
}.also { addTextChangedListener(it) }

Expand All @@ -62,7 +67,7 @@ fun EditText.firstChange(): Flow<Unit> {
@CheckResult
fun SwipeRefreshLayout.refreshes(): Flow<Unit> {
return callbackFlow {
setOnRefreshListener { offer(Unit) }
setOnRefreshListener { safeOffer(Unit) }
awaitClose { setOnRefreshListener(null) }
}
}
Expand All @@ -71,7 +76,7 @@ fun SwipeRefreshLayout.refreshes(): Flow<Unit> {
@CheckResult
fun View.clicks(): Flow<View> {
return callbackFlow {
setOnClickListener { offer(it) }
setOnClickListener { safeOffer(it) }
awaitClose { setOnClickListener(null) }
}
}
Expand All @@ -84,7 +89,7 @@ fun EditText.textChanges(): Flow<CharSequence?> {
override fun afterTextChanged(s: Editable?) = Unit
override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) = Unit
override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
offer(s)
safeOffer(s)
}
}
addTextChangedListener(listener)
Expand Down