https://kotlinlang.org logo
#codereview
Title
# codereview
a

Ayden

10/27/2019, 5:01 AM
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

Khan

10/27/2019, 8:53 AM
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

Ayden

10/27/2019, 9:11 AM
I see. Thanks @Khan
r

rkeazor

10/27/2019, 3:54 PM
setOnClickListener { if ... } you dont need the explicit View.OnClickListener
y

yousefa2

12/04/2019, 8:51 AM
You can use
isVisible
Extension and it can simply be
Copy code
progressBar.isVisible = !progressBar.isVisible
2 Views