Hi guys. I have two functions here, not sure if it...
# codereview
a
Hi guys. I have two functions here, not sure if it can be more simplify.
Copy code
findViewById<Button>(R.id.test).setOnClickListener(View.OnClickListener {
    if (progressBar.visibility == View.VISIBLE) {
        showProgressBar(false)
    } else {
        showProgressBar(true)
    }
})
Copy code
fun showProgressBar(visibility: Boolean) {
    progressBar.visibility = if (visibility) {
        View.INVISIBLE
    } else {
        View.VISIBLE
    }
}
k
For first one you can simply do it like this:
Copy code
val shouldShow = progressBar.visibility != View.VISIBLE
showProgressBar(shouldShow)
Copy code
fun showProgressBar(shouldShow: Boolean) {
    progressBar.visibility = if (shouldShow) View.VISIBLE else View.INVISIBLE
}
I would do it like this & without braces for if else, also in single line. it will look more cleaner
a
I see. Thanks @Khan
r
setOnClickListener { if ... } you dont need the explicit View.OnClickListener
y
You can use
isVisible
Extension and it can simply be
Copy code
progressBar.isVisible = !progressBar.isVisible