From dbfbeef90a6e60456fcd09bc6d68cafed2e2ac87 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Fri, 23 Sep 2022 12:12:15 +0600 Subject: [PATCH 1/6] start 0.12.15 --- CHANGELOG.md | 2 ++ gradle.properties | 4 ++-- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index caa403afbaf..3b90c01aaba 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,7 @@ # Changelog +## 0.12.15 + ## 0.12.14 * `Versions`: diff --git a/gradle.properties b/gradle.properties index 5c3e84ae4f3..81e464f4bb1 100644 --- a/gradle.properties +++ b/gradle.properties @@ -14,5 +14,5 @@ crypto_js_version=4.1.1 # Project data group=dev.inmo -version=0.12.14 -android_code_version=153 +version=0.12.16 +android_code_version=155 From b7934cf357b250b01c1fcf532c63295a64d97efc Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Fri, 23 Sep 2022 12:33:00 +0600 Subject: [PATCH 2/6] flows extensions --- CHANGELOG.md | 3 ++ .../micro_utils/coroutines/FlowFlatten.kt | 31 +++++++++++++++++++ 2 files changed, 34 insertions(+) create mode 100644 coroutines/src/commonMain/kotlin/dev/inmo/micro_utils/coroutines/FlowFlatten.kt diff --git a/CHANGELOG.md b/CHANGELOG.md index 3b90c01aaba..69d4ee6307f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,9 @@ ## 0.12.15 +* `Coroutines`: + * Add `Flow` extensions `flatMap` and `flatten` + ## 0.12.14 * `Versions`: diff --git a/coroutines/src/commonMain/kotlin/dev/inmo/micro_utils/coroutines/FlowFlatten.kt b/coroutines/src/commonMain/kotlin/dev/inmo/micro_utils/coroutines/FlowFlatten.kt new file mode 100644 index 00000000000..095ebd3573d --- /dev/null +++ b/coroutines/src/commonMain/kotlin/dev/inmo/micro_utils/coroutines/FlowFlatten.kt @@ -0,0 +1,31 @@ +package dev.inmo.micro_utils.coroutines + +import kotlinx.coroutines.flow.* + +inline fun Flow>.flatMap( + crossinline mapper: suspend (T) -> R +) = flow { + collect { + it.collect { + emit(mapper(it)) + } + } +} + +inline fun Flow>.flatMap( + crossinline mapper: suspend (T) -> R +) = map { + it.asFlow() +}.flatMap(mapper) + +inline fun Flow>.flatMapNotNull( + crossinline mapper: suspend (T) -> R +) = flatMap(mapper).filterNot { it == null } + +inline fun Flow>.flatMapNotNull( + crossinline mapper: suspend (T) -> R +) = flatMap(mapper).filterNot { it == null } + +fun Flow>.flatten() = flatMap { it } + +fun Flow>.flatten() = flatMap { it } From be52871de863c94f6f7dbd278a3c0d2f7b4ae9ae Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Fri, 23 Sep 2022 12:41:07 +0600 Subject: [PATCH 3/6] flows extensions --- CHANGELOG.md | 3 ++- .../inmo/micro_utils/coroutines/FlowFlatten.kt | 16 ++++++++++++---- .../inmo/micro_utils/coroutines/FlowNullables.kt | 6 ++++++ 3 files changed, 20 insertions(+), 5 deletions(-) create mode 100644 coroutines/src/commonMain/kotlin/dev/inmo/micro_utils/coroutines/FlowNullables.kt diff --git a/CHANGELOG.md b/CHANGELOG.md index 69d4ee6307f..aba0a1b17b4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,7 +3,8 @@ ## 0.12.15 * `Coroutines`: - * Add `Flow` extensions `flatMap` and `flatten` + * Add `Flow` extensions `flatMap`, `flatMapNotNull` and `flatten` + * Add `Flow` extensions `takeNotNull` and `filterNotNull` ## 0.12.14 diff --git a/coroutines/src/commonMain/kotlin/dev/inmo/micro_utils/coroutines/FlowFlatten.kt b/coroutines/src/commonMain/kotlin/dev/inmo/micro_utils/coroutines/FlowFlatten.kt index 095ebd3573d..6e5c6d4b729 100644 --- a/coroutines/src/commonMain/kotlin/dev/inmo/micro_utils/coroutines/FlowFlatten.kt +++ b/coroutines/src/commonMain/kotlin/dev/inmo/micro_utils/coroutines/FlowFlatten.kt @@ -1,6 +1,8 @@ package dev.inmo.micro_utils.coroutines import kotlinx.coroutines.flow.* +import kotlin.js.JsName +import kotlin.jvm.JvmName inline fun Flow>.flatMap( crossinline mapper: suspend (T) -> R @@ -12,6 +14,8 @@ inline fun Flow>.flatMap( } } +@JsName("flatMapIterable") +@JvmName("flatMapIterable") inline fun Flow>.flatMap( crossinline mapper: suspend (T) -> R ) = map { @@ -20,12 +24,16 @@ inline fun Flow>.flatMap( inline fun Flow>.flatMapNotNull( crossinline mapper: suspend (T) -> R -) = flatMap(mapper).filterNot { it == null } +) = flatMap(mapper).takeNotNull() +@JsName("flatMapNotNullIterable") +@JvmName("flatMapNotNullIterable") inline fun Flow>.flatMapNotNull( crossinline mapper: suspend (T) -> R -) = flatMap(mapper).filterNot { it == null } - -fun Flow>.flatten() = flatMap { it } +) = flatMap(mapper).takeNotNull() fun Flow>.flatten() = flatMap { it } + +@JsName("flattenIterable") +@JvmName("flattenIterable") +fun Flow>.flatten() = flatMap { it } diff --git a/coroutines/src/commonMain/kotlin/dev/inmo/micro_utils/coroutines/FlowNullables.kt b/coroutines/src/commonMain/kotlin/dev/inmo/micro_utils/coroutines/FlowNullables.kt new file mode 100644 index 00000000000..e6b665e47f3 --- /dev/null +++ b/coroutines/src/commonMain/kotlin/dev/inmo/micro_utils/coroutines/FlowNullables.kt @@ -0,0 +1,6 @@ +package dev.inmo.micro_utils.coroutines + +import kotlinx.coroutines.flow.* + +fun Flow.takeNotNull() = mapNotNull { it } +fun Flow.filterNotNull() = takeNotNull() From 60bdb59d713d1003fc04af1ac92bdd43ed60c421 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Fri, 23 Sep 2022 12:41:57 +0600 Subject: [PATCH 4/6] fix version change --- gradle.properties | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/gradle.properties b/gradle.properties index 81e464f4bb1..3732f846fb0 100644 --- a/gradle.properties +++ b/gradle.properties @@ -14,5 +14,5 @@ crypto_js_version=4.1.1 # Project data group=dev.inmo -version=0.12.16 -android_code_version=155 +version=0.12.15 +android_code_version=154 From 59428140a8653913e6cfd26977f90d8fad5ee3c4 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Fri, 23 Sep 2022 16:05:32 +0600 Subject: [PATCH 5/6] applyDiff: Diff --- CHANGELOG.md | 2 ++ .../commonMain/kotlin/dev/inmo/micro_utils/common/DiffUtils.kt | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index aba0a1b17b4..534b61c91e4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ ## 0.12.15 +* `Common`: + * `applyDiff` will return `Diff` object since this release * `Coroutines`: * Add `Flow` extensions `flatMap`, `flatMapNotNull` and `flatten` * Add `Flow` extensions `takeNotNull` and `filterNotNull` diff --git a/common/src/commonMain/kotlin/dev/inmo/micro_utils/common/DiffUtils.kt b/common/src/commonMain/kotlin/dev/inmo/micro_utils/common/DiffUtils.kt index f885b9f2ba1..596e413fa21 100644 --- a/common/src/commonMain/kotlin/dev/inmo/micro_utils/common/DiffUtils.kt +++ b/common/src/commonMain/kotlin/dev/inmo/micro_utils/common/DiffUtils.kt @@ -173,7 +173,7 @@ inline fun Iterable.calculateStrictDiff( fun MutableList.applyDiff( source: Iterable, strictComparison: Boolean = false -) = calculateDiff(source, strictComparison).let { +): Diff = calculateDiff(source, strictComparison).also { for (i in it.removed.indices.sortedDescending()) { removeAt(it.removed[i].index) } From ac587a67e68e2dc675dd3c4e1411941886421cf1 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Fri, 23 Sep 2022 16:43:33 +0600 Subject: [PATCH 6/6] findViewsByTag --- CHANGELOG.md | 2 + .../inmo/micro_utils/common/FindViewsByTag.kt | 61 +++++++++++++++++++ .../dev/inmo/micro_utils/common/RootView.kt | 7 +++ 3 files changed, 70 insertions(+) create mode 100644 common/src/main/kotlin/dev/inmo/micro_utils/common/FindViewsByTag.kt create mode 100644 common/src/main/kotlin/dev/inmo/micro_utils/common/RootView.kt diff --git a/CHANGELOG.md b/CHANGELOG.md index 534b61c91e4..89d11bb3aa0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,8 @@ * `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` diff --git a/common/src/main/kotlin/dev/inmo/micro_utils/common/FindViewsByTag.kt b/common/src/main/kotlin/dev/inmo/micro_utils/common/FindViewsByTag.kt new file mode 100644 index 00000000000..ddd21752a5a --- /dev/null +++ b/common/src/main/kotlin/dev/inmo/micro_utils/common/FindViewsByTag.kt @@ -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 { + return viewGroup.children.flatMap { + findViewsByTag(it, tag) + }.toList() +} + +fun findViewsByTag(viewGroup: ViewGroup, key: Int, tag: Any?): List { + return viewGroup.children.flatMap { + findViewsByTag(it, key, tag) + }.toList() +} + +fun findViewsByTag(view: View, tag: Any?): List { + val result = mutableListOf() + 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 { + val result = mutableListOf() + 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) diff --git a/common/src/main/kotlin/dev/inmo/micro_utils/common/RootView.kt b/common/src/main/kotlin/dev/inmo/micro_utils/common/RootView.kt new file mode 100644 index 00000000000..603f53f5f62 --- /dev/null +++ b/common/src/main/kotlin/dev/inmo/micro_utils/common/RootView.kt @@ -0,0 +1,7 @@ +package dev.inmo.micro_utils.common + +import android.app.Activity +import android.view.View + +val Activity.rootView: View? + get() = findViewById(android.R.id.content) ?.rootView ?: window.decorView.findViewById(android.R.id.content) ?.rootView