@@ -15,13 +15,11 @@ import androidx.core.content.ContextCompat
15
15
import androidx.recyclerview.widget.ItemTouchHelper
16
16
import androidx.recyclerview.widget.RecyclerView
17
17
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout
18
- import java.util.concurrent.atomic.AtomicBoolean
19
- import java.util.concurrent.atomic.AtomicReference
20
- import kotlin.coroutines.EmptyCoroutineContext
21
18
import kotlinx.coroutines.CancellationException
22
19
import kotlinx.coroutines.Dispatchers
23
20
import kotlinx.coroutines.ExperimentalCoroutinesApi
24
21
import kotlinx.coroutines.cancel
22
+ import kotlinx.coroutines.channels.SendChannel
25
23
import kotlinx.coroutines.channels.awaitClose
26
24
import kotlinx.coroutines.coroutineScope
27
25
import kotlinx.coroutines.delay
@@ -37,6 +35,13 @@ import kotlinx.coroutines.flow.onEach
37
35
import kotlinx.coroutines.flow.onStart
38
36
import kotlinx.coroutines.flow.take
39
37
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
+ }
40
45
41
46
@ExperimentalCoroutinesApi
42
47
fun EditText.firstChange (): Flow <Unit > {
@@ -45,7 +50,7 @@ fun EditText.firstChange(): Flow<Unit> {
45
50
override fun afterTextChanged (s : Editable ? ) = Unit
46
51
override fun beforeTextChanged (s : CharSequence? , start : Int , count : Int , after : Int ) = Unit
47
52
override fun onTextChanged (s : CharSequence? , start : Int , before : Int , count : Int ) {
48
- offer (Unit )
53
+ safeOffer (Unit )
49
54
}
50
55
}.also { addTextChangedListener(it) }
51
56
@@ -62,7 +67,7 @@ fun EditText.firstChange(): Flow<Unit> {
62
67
@CheckResult
63
68
fun SwipeRefreshLayout.refreshes (): Flow <Unit > {
64
69
return callbackFlow {
65
- setOnRefreshListener { offer (Unit ) }
70
+ setOnRefreshListener { safeOffer (Unit ) }
66
71
awaitClose { setOnRefreshListener(null ) }
67
72
}
68
73
}
@@ -71,7 +76,7 @@ fun SwipeRefreshLayout.refreshes(): Flow<Unit> {
71
76
@CheckResult
72
77
fun View.clicks (): Flow <View > {
73
78
return callbackFlow {
74
- setOnClickListener { offer (it) }
79
+ setOnClickListener { safeOffer (it) }
75
80
awaitClose { setOnClickListener(null ) }
76
81
}
77
82
}
@@ -84,7 +89,7 @@ fun EditText.textChanges(): Flow<CharSequence?> {
84
89
override fun afterTextChanged (s : Editable ? ) = Unit
85
90
override fun beforeTextChanged (s : CharSequence? , start : Int , count : Int , after : Int ) = Unit
86
91
override fun onTextChanged (s : CharSequence? , start : Int , before : Int , count : Int ) {
87
- offer (s)
92
+ safeOffer (s)
88
93
}
89
94
}
90
95
addTextChangedListener(listener)
0 commit comments