What do you guys think about the added visibility ...
# android
s
What do you guys think about the added visibility setters and getters on
View
in android-ktx? I find the getters really handy and readable, but the setters feel “imbalanced” and not thought through:
Copy code
inline var View.isVisible: Boolean
    get() = visibility == View.VISIBLE
    set(value) {
        visibility = if (value) View.VISIBLE else View.GONE
    }

inline var View.isInvisible: Boolean
    get() = visibility == View.INVISIBLE
    set(value) {
        visibility = if (value) View.INVISIBLE else View.VISIBLE
    }

inline var View.isGone: Boolean
    get() = visibility == View.GONE
    set(value) {
        visibility = if (value) View.GONE else View.VISIBLE
    }
In my opinion, saying
button.isVisible = false
in Android doesn't really make sense because of the difference between
View.INVISIBLE
and
View.GONE
. What do you guys think?
👍 1
s
I understand your confusion and agree with that -
isVissible = false
doesn’t mean
gone
logically. But in reality I think that switching between
VISIBLE
and
GONE
is way more often used. So I guess it’s a shortcut for this case.
2
s
Sure, but there doesn't really seem to be any parity between the setters. There's probably a reason why there wasn't any
View.isVisible(Boolean)
from the get-go, and I'm thinking this is exactly that reason. It doesn't really make sense when you have three different states of visibility.
👍 2
g
I ended up with having:
Copy code
fun View.show() {
    visibility = View.VISIBLE
}

fun View.hide() {
    visibility = View.GONE
}
n
I can’t remember the last time I checked the state of a view’s visibility… state is generally stored in your Presenter or MV* of choice - data driven, not UI driven. I agree with @ghedeon but would change it slightly to:
Copy code
fun View.show() {
    visibility = View.VISIBLE
}

fun View.gone() {
    visibility = View.GONE
}

fun View.invisible() {
    visibility = View.INVISIBLE
}
s
I propose
View.begone()
:))
d
I have barely used
View.INVISIBLE
so I find the current definition quite convenient. Keeping in mind that Kotlin does not have ternary operator, being able to do
view.isVisible = value
is quite nice