Skip to content

Commit 6aa981f

Browse files
authored
safeOffer instead of offer (#8)
1 parent cca0bf2 commit 6aa981f

File tree

1 file changed

+12
-7
lines changed

1 file changed

+12
-7
lines changed

core/src/main/java/com/hoc/flowmvi/core/FlowBinding+Exts+Utils.kt

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,11 @@ import androidx.core.content.ContextCompat
1515
import androidx.recyclerview.widget.ItemTouchHelper
1616
import androidx.recyclerview.widget.RecyclerView
1717
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout
18-
import java.util.concurrent.atomic.AtomicBoolean
19-
import java.util.concurrent.atomic.AtomicReference
20-
import kotlin.coroutines.EmptyCoroutineContext
2118
import kotlinx.coroutines.CancellationException
2219
import kotlinx.coroutines.Dispatchers
2320
import kotlinx.coroutines.ExperimentalCoroutinesApi
2421
import kotlinx.coroutines.cancel
22+
import kotlinx.coroutines.channels.SendChannel
2523
import kotlinx.coroutines.channels.awaitClose
2624
import kotlinx.coroutines.coroutineScope
2725
import kotlinx.coroutines.delay
@@ -37,6 +35,13 @@ import kotlinx.coroutines.flow.onEach
3735
import kotlinx.coroutines.flow.onStart
3836
import kotlinx.coroutines.flow.take
3937
import kotlinx.coroutines.launch
38+
import java.util.concurrent.atomic.AtomicBoolean
39+
import java.util.concurrent.atomic.AtomicReference
40+
import kotlin.coroutines.EmptyCoroutineContext
41+
42+
internal fun <T> SendChannel<T>.safeOffer(element: T): Boolean {
43+
return runCatching { offer(element) }.getOrDefault(false)
44+
}
4045

4146
@ExperimentalCoroutinesApi
4247
fun EditText.firstChange(): Flow<Unit> {
@@ -45,7 +50,7 @@ fun EditText.firstChange(): Flow<Unit> {
4550
override fun afterTextChanged(s: Editable?) = Unit
4651
override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) = Unit
4752
override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
48-
offer(Unit)
53+
safeOffer(Unit)
4954
}
5055
}.also { addTextChangedListener(it) }
5156

@@ -62,7 +67,7 @@ fun EditText.firstChange(): Flow<Unit> {
6267
@CheckResult
6368
fun SwipeRefreshLayout.refreshes(): Flow<Unit> {
6469
return callbackFlow {
65-
setOnRefreshListener { offer(Unit) }
70+
setOnRefreshListener { safeOffer(Unit) }
6671
awaitClose { setOnRefreshListener(null) }
6772
}
6873
}
@@ -71,7 +76,7 @@ fun SwipeRefreshLayout.refreshes(): Flow<Unit> {
7176
@CheckResult
7277
fun View.clicks(): Flow<View> {
7378
return callbackFlow {
74-
setOnClickListener { offer(it) }
79+
setOnClickListener { safeOffer(it) }
7580
awaitClose { setOnClickListener(null) }
7681
}
7782
}
@@ -84,7 +89,7 @@ fun EditText.textChanges(): Flow<CharSequence?> {
8489
override fun afterTextChanged(s: Editable?) = Unit
8590
override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) = Unit
8691
override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
87-
offer(s)
92+
safeOffer(s)
8893
}
8994
}
9095
addTextChangedListener(listener)

0 commit comments

Comments
 (0)