mirror of
https://github.com/InsanusMokrassar/MicroUtils.git
synced 2024-11-26 20:18:48 +00:00
commit
9c5b44efb3
10
CHANGELOG.md
10
CHANGELOG.md
@ -1,5 +1,15 @@
|
|||||||
# Changelog
|
# Changelog
|
||||||
|
|
||||||
|
## 0.12.15
|
||||||
|
|
||||||
|
* `Common`:
|
||||||
|
* `applyDiff` will return `Diff` object since this release
|
||||||
|
* `Android`:
|
||||||
|
* New functions/extensions `findViewsByTag` and `findViewsByTagInActivity`
|
||||||
|
* `Coroutines`:
|
||||||
|
* Add `Flow` extensions `flatMap`, `flatMapNotNull` and `flatten`
|
||||||
|
* Add `Flow` extensions `takeNotNull` and `filterNotNull`
|
||||||
|
|
||||||
## 0.12.14
|
## 0.12.14
|
||||||
|
|
||||||
* `Versions`:
|
* `Versions`:
|
||||||
|
@ -173,7 +173,7 @@ inline fun <T> Iterable<T>.calculateStrictDiff(
|
|||||||
fun <T> MutableList<T>.applyDiff(
|
fun <T> MutableList<T>.applyDiff(
|
||||||
source: Iterable<T>,
|
source: Iterable<T>,
|
||||||
strictComparison: Boolean = false
|
strictComparison: Boolean = false
|
||||||
) = calculateDiff(source, strictComparison).let {
|
): Diff<T> = calculateDiff(source, strictComparison).also {
|
||||||
for (i in it.removed.indices.sortedDescending()) {
|
for (i in it.removed.indices.sortedDescending()) {
|
||||||
removeAt(it.removed[i].index)
|
removeAt(it.removed[i].index)
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,61 @@
|
|||||||
|
package dev.inmo.micro_utils.common
|
||||||
|
|
||||||
|
import android.app.Activity
|
||||||
|
import android.view.View
|
||||||
|
import android.view.ViewGroup
|
||||||
|
import androidx.core.view.children
|
||||||
|
import androidx.fragment.app.Fragment
|
||||||
|
|
||||||
|
fun findViewsByTag(viewGroup: ViewGroup, tag: Any?): List<View> {
|
||||||
|
return viewGroup.children.flatMap {
|
||||||
|
findViewsByTag(it, tag)
|
||||||
|
}.toList()
|
||||||
|
}
|
||||||
|
|
||||||
|
fun findViewsByTag(viewGroup: ViewGroup, key: Int, tag: Any?): List<View> {
|
||||||
|
return viewGroup.children.flatMap {
|
||||||
|
findViewsByTag(it, key, tag)
|
||||||
|
}.toList()
|
||||||
|
}
|
||||||
|
|
||||||
|
fun findViewsByTag(view: View, tag: Any?): List<View> {
|
||||||
|
val result = mutableListOf<View>()
|
||||||
|
if (view.tag == tag) {
|
||||||
|
result.add(view)
|
||||||
|
}
|
||||||
|
if (view is ViewGroup) {
|
||||||
|
result.addAll(findViewsByTag(view, tag))
|
||||||
|
}
|
||||||
|
return result.toList()
|
||||||
|
}
|
||||||
|
|
||||||
|
fun findViewsByTag(view: View, key: Int, tag: Any?): List<View> {
|
||||||
|
val result = mutableListOf<View>()
|
||||||
|
if (view.getTag(key) == tag) {
|
||||||
|
result.add(view)
|
||||||
|
}
|
||||||
|
if (view is ViewGroup) {
|
||||||
|
result.addAll(findViewsByTag(view, key, tag))
|
||||||
|
}
|
||||||
|
return result.toList()
|
||||||
|
}
|
||||||
|
|
||||||
|
fun Activity.findViewsByTag(tag: Any?) = rootView ?.let {
|
||||||
|
findViewsByTag(it, tag)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun Activity.findViewsByTag(key: Int, tag: Any?) = rootView ?.let {
|
||||||
|
findViewsByTag(it, key, tag)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun Fragment.findViewsByTag(tag: Any?) = view ?.let {
|
||||||
|
findViewsByTag(it, tag)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun Fragment.findViewsByTag(key: Int, tag: Any?) = view ?.let {
|
||||||
|
findViewsByTag(it, key, tag)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun Fragment.findViewsByTagInActivity(tag: Any?) = activity ?.findViewsByTag(tag)
|
||||||
|
|
||||||
|
fun Fragment.findViewsByTagInActivity(key: Int, tag: Any?) = activity ?.findViewsByTag(key, tag)
|
@ -0,0 +1,7 @@
|
|||||||
|
package dev.inmo.micro_utils.common
|
||||||
|
|
||||||
|
import android.app.Activity
|
||||||
|
import android.view.View
|
||||||
|
|
||||||
|
val Activity.rootView: View?
|
||||||
|
get() = findViewById<View?>(android.R.id.content) ?.rootView ?: window.decorView.findViewById<View?>(android.R.id.content) ?.rootView
|
@ -0,0 +1,39 @@
|
|||||||
|
package dev.inmo.micro_utils.coroutines
|
||||||
|
|
||||||
|
import kotlinx.coroutines.flow.*
|
||||||
|
import kotlin.js.JsName
|
||||||
|
import kotlin.jvm.JvmName
|
||||||
|
|
||||||
|
inline fun <T, R> Flow<Flow<T>>.flatMap(
|
||||||
|
crossinline mapper: suspend (T) -> R
|
||||||
|
) = flow {
|
||||||
|
collect {
|
||||||
|
it.collect {
|
||||||
|
emit(mapper(it))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@JsName("flatMapIterable")
|
||||||
|
@JvmName("flatMapIterable")
|
||||||
|
inline fun <T, R> Flow<Iterable<T>>.flatMap(
|
||||||
|
crossinline mapper: suspend (T) -> R
|
||||||
|
) = map {
|
||||||
|
it.asFlow()
|
||||||
|
}.flatMap(mapper)
|
||||||
|
|
||||||
|
inline fun <T, R> Flow<Flow<T>>.flatMapNotNull(
|
||||||
|
crossinline mapper: suspend (T) -> R
|
||||||
|
) = flatMap(mapper).takeNotNull()
|
||||||
|
|
||||||
|
@JsName("flatMapNotNullIterable")
|
||||||
|
@JvmName("flatMapNotNullIterable")
|
||||||
|
inline fun <T, R> Flow<Iterable<T>>.flatMapNotNull(
|
||||||
|
crossinline mapper: suspend (T) -> R
|
||||||
|
) = flatMap(mapper).takeNotNull()
|
||||||
|
|
||||||
|
fun <T> Flow<Flow<T>>.flatten() = flatMap { it }
|
||||||
|
|
||||||
|
@JsName("flattenIterable")
|
||||||
|
@JvmName("flattenIterable")
|
||||||
|
fun <T> Flow<Iterable<T>>.flatten() = flatMap { it }
|
@ -0,0 +1,6 @@
|
|||||||
|
package dev.inmo.micro_utils.coroutines
|
||||||
|
|
||||||
|
import kotlinx.coroutines.flow.*
|
||||||
|
|
||||||
|
fun <T> Flow<T>.takeNotNull() = mapNotNull { it }
|
||||||
|
fun <T> Flow<T>.filterNotNull() = takeNotNull()
|
@ -14,5 +14,5 @@ crypto_js_version=4.1.1
|
|||||||
# Project data
|
# Project data
|
||||||
|
|
||||||
group=dev.inmo
|
group=dev.inmo
|
||||||
version=0.12.14
|
version=0.12.15
|
||||||
android_code_version=153
|
android_code_version=154
|
||||||
|
Loading…
Reference in New Issue
Block a user