Glimpse: Only send ContentObserver callbacks when Job is active

Change-Id: I83dfe71285e2ee1b38087ae3537a8fadc6d32ca3
This commit is contained in:
Luca Stefani 2023-08-19 18:07:37 +02:00 committed by Sebastiano Barezzi
parent d61b281506
commit 3107835489
No known key found for this signature in database
GPG Key ID: 763BD3AE91A7A13F
1 changed files with 4 additions and 1 deletions

View File

@ -20,6 +20,7 @@ import kotlinx.coroutines.channels.awaitClose
import kotlinx.coroutines.flow.callbackFlow
import kotlinx.coroutines.flow.conflate
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.isActive
@RequiresApi(Build.VERSION_CODES.R)
fun ContentResolver.createDeleteRequest(vararg uris: Uri) = IntentSenderRequest.Builder(
@ -41,7 +42,9 @@ fun ContentResolver.createTrashRequest(value: Boolean, vararg uris: Uri) =
fun ContentResolver.uriFlow(uri: Uri) = callbackFlow {
val observer = object : ContentObserver(Handler(Looper.getMainLooper())) {
override fun onChange(selfChange: Boolean) {
trySend(Unit)
if (isActive) {
trySend(Unit)
}
}
}
registerContentObserver(uri, true, observer)