Skip to content

Commit 152bf0a

Browse files
author
Adrián García
authored
Add more View Extensions (#10)
* Add more View Extensions * Add toggleAbility methods
1 parent d8c7e7a commit 152bf0a

File tree

3 files changed

+122
-5
lines changed

3 files changed

+122
-5
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
55
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

77
## [Unreleased]
8-
- No unreleased features, yet!
8+
### Added
9+
- Add new `View` extensions to change view and view lists visibility status and enabled/disabled status.
910

1011
## [1.1.0] - 2020-01-28
1112
### Added
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
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() }

mini-android/src/main/java/com/mini/android/ViewUtil.kt

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,5 @@ fun ViewGroup.inflateNoAttach(@LayoutRes layout: Int): View {
3636
return LayoutInflater.from(this.context).inflate(layout, this, false)
3737
}
3838

39-
fun View.makeVisible() = run { visibility = View.VISIBLE }
40-
fun View.makeInvisible() = run { visibility = View.INVISIBLE }
41-
fun View.makeGone() = run { visibility = View.GONE }
42-
4339
/** dp -> px */
4440
val Number.dp: Int get() = (this.toFloat() * Resources.getSystem().displayMetrics.density).roundToInt()

0 commit comments

Comments
 (0)