mirror of
https://github.com/InsanusMokrassar/MicroUtils.git
synced 2024-12-18 14:47:15 +00:00
FlowOnHierarchyChangeListener
This commit is contained in:
parent
aa9dfb4ab8
commit
9d95687d3c
@ -2,6 +2,11 @@
|
||||
|
||||
## 0.12.16
|
||||
|
||||
* `Coroutines`:
|
||||
* `Android`:
|
||||
* Add class `FlowOnHierarchyChangeListener`
|
||||
* Add `ViewGroup#setOnHierarchyChangeListenerRecursively(OnHierarchyChangeListener)`
|
||||
|
||||
## 0.12.15
|
||||
|
||||
* `Common`:
|
||||
|
50
coroutines/src/main/kotlin/FlowOnHierarchyChangeListener.kt
Normal file
50
coroutines/src/main/kotlin/FlowOnHierarchyChangeListener.kt
Normal file
@ -0,0 +1,50 @@
|
||||
package dev.inmo.micro_utils.coroutines
|
||||
|
||||
import android.view.View
|
||||
import android.view.ViewGroup
|
||||
import kotlinx.coroutines.flow.MutableSharedFlow
|
||||
import kotlinx.coroutines.flow.asSharedFlow
|
||||
|
||||
/**
|
||||
* [kotlinx.coroutines.flow.Flow]-based [android.view.ViewGroup.OnHierarchyChangeListener]
|
||||
*
|
||||
* @param recursive If set, any call of [onChildViewAdded] will check if child [View] is [ViewGroup] and subscribe to this
|
||||
* [ViewGroup] too
|
||||
* @param [_onChildViewAdded] Internal [MutableSharedFlow] which will be used to pass data to [onChildViewAdded] flow
|
||||
* @param [_onChildViewRemoved] Internal [MutableSharedFlow] which will be used to pass data to [onChildViewRemoved] flow
|
||||
*/
|
||||
class FlowOnHierarchyChangeListener(
|
||||
private val recursive: Boolean = false,
|
||||
private val _onChildViewAdded: MutableSharedFlow<Pair<View, View>> = MutableSharedFlow(extraBufferCapacity = Int.MAX_VALUE),
|
||||
private val _onChildViewRemoved: MutableSharedFlow<Pair<View, View>> = MutableSharedFlow(extraBufferCapacity = Int.MAX_VALUE)
|
||||
) : ViewGroup.OnHierarchyChangeListener {
|
||||
val onChildViewAdded = _onChildViewAdded.asSharedFlow()
|
||||
val onChildViewRemoved = _onChildViewRemoved.asSharedFlow()
|
||||
|
||||
/**
|
||||
* Will emit data into [onChildViewAdded] flow. If [recursive] is true and [child] is [ViewGroup] will also
|
||||
* subscribe to [child] hierarchy changes.
|
||||
*
|
||||
* Due to the fact that this method is not suspendable, [FlowOnHierarchyChangeListener] will use
|
||||
* [MutableSharedFlow.tryEmit] to send data into [_onChildViewAdded]. That is why its default extraBufferCapacity is
|
||||
* [Int.MAX_VALUE]
|
||||
*/
|
||||
override fun onChildViewAdded(parent: View, child: View) {
|
||||
_onChildViewAdded.tryEmit(parent to child)
|
||||
|
||||
if (recursive && child is ViewGroup) {
|
||||
child.setOnHierarchyChangeListener(this)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Just emit data into [onChildViewRemoved]
|
||||
*
|
||||
* Due to the fact that this method is not suspendable, [FlowOnHierarchyChangeListener] will use
|
||||
* [MutableSharedFlow.tryEmit] to send data into [_onChildViewRemoved]. That is why its default extraBufferCapacity is
|
||||
* [Int.MAX_VALUE]
|
||||
*/
|
||||
override fun onChildViewRemoved(parent: View, child: View) {
|
||||
_onChildViewRemoved.tryEmit(parent to child)
|
||||
}
|
||||
}
|
17
coroutines/src/main/kotlin/RecursiveHierarchySubscriber.kt
Normal file
17
coroutines/src/main/kotlin/RecursiveHierarchySubscriber.kt
Normal file
@ -0,0 +1,17 @@
|
||||
package dev.inmo.micro_utils.coroutines
|
||||
|
||||
import android.view.ViewGroup
|
||||
import android.view.ViewGroup.OnHierarchyChangeListener
|
||||
|
||||
/**
|
||||
* Use [ViewGroup.setOnHierarchyChangeListener] recursively for all available [ViewGroup]s starting with [this].
|
||||
* This extension DO NOT guarantee that recursive subscription will happen after this method call
|
||||
*/
|
||||
fun ViewGroup.setOnHierarchyChangeListenerRecursively(
|
||||
listener: OnHierarchyChangeListener
|
||||
) {
|
||||
setOnHierarchyChangeListener(listener)
|
||||
(0 until childCount).forEach {
|
||||
(getChildAt(it) as? ViewGroup) ?.setOnHierarchyChangeListener(listener)
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user