Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
120 changes: 120 additions & 0 deletions mini-android/src/main/java/com/mini/android/ViewExtensions.kt
Original file line number Diff line number Diff line change
@@ -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<View>.makeVisible() = forEach { it.makeVisible() }

/**
* Makes the [View] list invisible.
*/
fun List<View>.makeInvisible() = forEach { it.makeInvisible() }

/**
* Makes the [View] list gone.
*/
fun List<View>.makeGone() = forEach { it.makeGone() }

/**
* Returns true if any [View] in the list is visible.
*/
fun List<View>.anyVisible() = any { it.isVisible() }

/**
* Returns true if any [View] in the list is invisible.
*/
fun List<View>.anyInvisible() = any { it.isInvisible() }

/**
* Returns true if any [View] in the list is gone.
*/
fun List<View>.anyGone() = any { it.isGone() }

/**
* Returns true if any [View] in the list is gone or invisible.
*/
fun List<View>.anyNotVisible() = any { it.isNotVisible() }

/**
* Returns true if all [View]s in the list are visible.
*/
fun List<View>.allVisible() = all { it.isVisible() }

/**
* Returns true if all [View]s in the list are invisible.
*/
fun List<View>.allInvisible() = all { it.isInvisible() }

/**
* Returns true if all [View]s in the list are gone.
*/
fun List<View>.allGone() = all { it.isGone() }

/**
* Returns true if all [View]s in the list are either gone or invisible.
*/
fun List<View>.allNotVisible() = all { it.isNotVisible() }

/**
* Toggles visibility of a [View] list between [View.VISIBLE] and the input [notVisibleState].
*/
fun List<View>.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<View>.toggleAbility() = forEach { it.toggleAbility() }
4 changes: 0 additions & 4 deletions mini-android/src/main/java/com/mini/android/ViewUtil.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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()