diff --git a/CHANGELOG.md b/CHANGELOG.md index 6b1ae3d..97516a7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,7 +5,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). ## [Unreleased] -- No unreleased features, yet! +### Added +- Add new `View` extensions to change view and view lists visibility status and enabled/disabled status. ## [1.1.0] - 2020-01-28 ### Added diff --git a/mini-android/src/main/java/com/mini/android/ViewExtensions.kt b/mini-android/src/main/java/com/mini/android/ViewExtensions.kt new file mode 100644 index 0000000..17a62af --- /dev/null +++ b/mini-android/src/main/java/com/mini/android/ViewExtensions.kt @@ -0,0 +1,120 @@ +package com.mini.android + +import android.view.View +import androidx.annotation.IntRange + +/** + * Makes the [View] visible. + */ +fun View.makeVisible() = run { visibility = View.VISIBLE } + +/** + * Makes the [View] invisible. + */ +fun View.makeInvisible() = run { visibility = View.INVISIBLE } + +/** + * Makes the [View] gone. + */ +fun View.makeGone() = run { visibility = View.GONE } + +/** + * Returns true if the [View] is visible, false otherwise. + */ +fun View.isVisible() = visibility == View.VISIBLE + +/** + * Returns true if the [View] is invisible, false otherwise. + */ +fun View.isInvisible() = visibility == View.INVISIBLE + +/** + * Returns true if the [View] is gone, false otherwise. + */ +fun View.isGone() = visibility == View.GONE + +/** + * Returns true if the [View] is either in invisible or gone status. + */ +fun View.isNotVisible() = isInvisible() || isGone() + +/** + * Toggles visibility of a [View] between [View.VISIBLE] and the input [notVisibleState]. + */ +fun View.toggleVisibility(@IntRange(from = View.INVISIBLE.toLong(), to = View.GONE.toLong()) notVisibleState: Int = View.GONE) { + visibility = if (isVisible()) notVisibleState else View.VISIBLE +} + +/** + * Toggles the [View] ability status: enabled if disabled or disabled if enabled + */ +fun View.toggleAbility() { + isEnabled = !isEnabled +} + +/** + * Makes the [View] list visible. + */ +fun List.makeVisible() = forEach { it.makeVisible() } + +/** + * Makes the [View] list invisible. + */ +fun List.makeInvisible() = forEach { it.makeInvisible() } + +/** + * Makes the [View] list gone. + */ +fun List.makeGone() = forEach { it.makeGone() } + +/** + * Returns true if any [View] in the list is visible. + */ +fun List.anyVisible() = any { it.isVisible() } + +/** + * Returns true if any [View] in the list is invisible. + */ +fun List.anyInvisible() = any { it.isInvisible() } + +/** + * Returns true if any [View] in the list is gone. + */ +fun List.anyGone() = any { it.isGone() } + +/** + * Returns true if any [View] in the list is gone or invisible. + */ +fun List.anyNotVisible() = any { it.isNotVisible() } + +/** + * Returns true if all [View]s in the list are visible. + */ +fun List.allVisible() = all { it.isVisible() } + +/** + * Returns true if all [View]s in the list are invisible. + */ +fun List.allInvisible() = all { it.isInvisible() } + +/** + * Returns true if all [View]s in the list are gone. + */ +fun List.allGone() = all { it.isGone() } + +/** + * Returns true if all [View]s in the list are either gone or invisible. + */ +fun List.allNotVisible() = all { it.isNotVisible() } + +/** + * Toggles visibility of a [View] list between [View.VISIBLE] and the input [notVisibleState]. + */ +fun List.toggleVisibility(@IntRange(from = View.INVISIBLE.toLong(), to = View.GONE.toLong()) notVisibleState: Int = View.GONE) { + forEach { it.visibility = if (it.isVisible()) notVisibleState else View.VISIBLE } +} + +/** + * Toggles the [View] ability status: enabled if disabled or disabled if enabled + */ +fun List.toggleAbility() = forEach { it.toggleAbility() } diff --git a/mini-android/src/main/java/com/mini/android/ViewUtil.kt b/mini-android/src/main/java/com/mini/android/ViewUtil.kt index 5eddbb1..ce10d6f 100644 --- a/mini-android/src/main/java/com/mini/android/ViewUtil.kt +++ b/mini-android/src/main/java/com/mini/android/ViewUtil.kt @@ -36,9 +36,5 @@ fun ViewGroup.inflateNoAttach(@LayoutRes layout: Int): View { return LayoutInflater.from(this.context).inflate(layout, this, false) } -fun View.makeVisible() = run { visibility = View.VISIBLE } -fun View.makeInvisible() = run { visibility = View.INVISIBLE } -fun View.makeGone() = run { visibility = View.GONE } - /** dp -> px */ val Number.dp: Int get() = (this.toFloat() * Resources.getSystem().displayMetrics.density).roundToInt()