From ac587a67e68e2dc675dd3c4e1411941886421cf1 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Fri, 23 Sep 2022 16:43:33 +0600 Subject: [PATCH] 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