|
| 1 | +package com.mini.android |
| 2 | + |
| 3 | +import android.view.View |
| 4 | +import androidx.annotation.IntRange |
| 5 | + |
| 6 | +/** |
| 7 | + * Makes the [View] visible. |
| 8 | + */ |
| 9 | +fun View.makeVisible() = run { visibility = View.VISIBLE } |
| 10 | + |
| 11 | +/** |
| 12 | + * Makes the [View] invisible. |
| 13 | + */ |
| 14 | +fun View.makeInvisible() = run { visibility = View.INVISIBLE } |
| 15 | + |
| 16 | +/** |
| 17 | + * Makes the [View] gone. |
| 18 | + */ |
| 19 | +fun View.makeGone() = run { visibility = View.GONE } |
| 20 | + |
| 21 | +/** |
| 22 | + * Returns true if the [View] is visible, false otherwise. |
| 23 | + */ |
| 24 | +fun View.isVisible() = visibility == View.VISIBLE |
| 25 | + |
| 26 | +/** |
| 27 | + * Returns true if the [View] is invisible, false otherwise. |
| 28 | + */ |
| 29 | +fun View.isInvisible() = visibility == View.INVISIBLE |
| 30 | + |
| 31 | +/** |
| 32 | + * Returns true if the [View] is gone, false otherwise. |
| 33 | + */ |
| 34 | +fun View.isGone() = visibility == View.GONE |
| 35 | + |
| 36 | +/** |
| 37 | + * Returns true if the [View] is either in invisible or gone status. |
| 38 | + */ |
| 39 | +fun View.isNotVisible() = isInvisible() || isGone() |
| 40 | + |
| 41 | +/** |
| 42 | + * Toggles visibility of a [View] between [View.VISIBLE] and the input [notVisibleState]. |
| 43 | + */ |
| 44 | +fun View.toggleVisibility(@IntRange(from = View.INVISIBLE.toLong(), to = View.GONE.toLong()) notVisibleState: Int = View.GONE) { |
| 45 | + visibility = if (isVisible()) notVisibleState else View.VISIBLE |
| 46 | +} |
| 47 | + |
| 48 | +/** |
| 49 | + * Toggles the [View] ability status: enabled if disabled or disabled if enabled |
| 50 | + */ |
| 51 | +fun View.toggleAbility() { |
| 52 | + isEnabled = !isEnabled |
| 53 | +} |
| 54 | + |
| 55 | +/** |
| 56 | + * Makes the [View] list visible. |
| 57 | + */ |
| 58 | +fun List<View>.makeVisible() = forEach { it.makeVisible() } |
| 59 | + |
| 60 | +/** |
| 61 | + * Makes the [View] list invisible. |
| 62 | + */ |
| 63 | +fun List<View>.makeInvisible() = forEach { it.makeInvisible() } |
| 64 | + |
| 65 | +/** |
| 66 | + * Makes the [View] list gone. |
| 67 | + */ |
| 68 | +fun List<View>.makeGone() = forEach { it.makeGone() } |
| 69 | + |
| 70 | +/** |
| 71 | + * Returns true if any [View] in the list is visible. |
| 72 | + */ |
| 73 | +fun List<View>.anyVisible() = any { it.isVisible() } |
| 74 | + |
| 75 | +/** |
| 76 | + * Returns true if any [View] in the list is invisible. |
| 77 | + */ |
| 78 | +fun List<View>.anyInvisible() = any { it.isInvisible() } |
| 79 | + |
| 80 | +/** |
| 81 | + * Returns true if any [View] in the list is gone. |
| 82 | + */ |
| 83 | +fun List<View>.anyGone() = any { it.isGone() } |
| 84 | + |
| 85 | +/** |
| 86 | + * Returns true if any [View] in the list is gone or invisible. |
| 87 | + */ |
| 88 | +fun List<View>.anyNotVisible() = any { it.isNotVisible() } |
| 89 | + |
| 90 | +/** |
| 91 | + * Returns true if all [View]s in the list are visible. |
| 92 | + */ |
| 93 | +fun List<View>.allVisible() = all { it.isVisible() } |
| 94 | + |
| 95 | +/** |
| 96 | + * Returns true if all [View]s in the list are invisible. |
| 97 | + */ |
| 98 | +fun List<View>.allInvisible() = all { it.isInvisible() } |
| 99 | + |
| 100 | +/** |
| 101 | + * Returns true if all [View]s in the list are gone. |
| 102 | + */ |
| 103 | +fun List<View>.allGone() = all { it.isGone() } |
| 104 | + |
| 105 | +/** |
| 106 | + * Returns true if all [View]s in the list are either gone or invisible. |
| 107 | + */ |
| 108 | +fun List<View>.allNotVisible() = all { it.isNotVisible() } |
| 109 | + |
| 110 | +/** |
| 111 | + * Toggles visibility of a [View] list between [View.VISIBLE] and the input [notVisibleState]. |
| 112 | + */ |
| 113 | +fun List<View>.toggleVisibility(@IntRange(from = View.INVISIBLE.toLong(), to = View.GONE.toLong()) notVisibleState: Int = View.GONE) { |
| 114 | + forEach { it.visibility = if (it.isVisible()) notVisibleState else View.VISIBLE } |
| 115 | +} |
| 116 | + |
| 117 | +/** |
| 118 | + * Toggles the [View] ability status: enabled if disabled or disabled if enabled |
| 119 | + */ |
| 120 | +fun List<View>.toggleAbility() = forEach { it.toggleAbility() } |
0 commit comments