reusing existing setTextOrHide by adding an additional parameter to hide other views

This commit is contained in:
Adam Brown 2021-09-24 15:44:10 +01:00
parent 95b4f99970
commit 7fd794bd7a
3 changed files with 6 additions and 16 deletions

View file

@ -39,13 +39,15 @@ import im.vector.app.features.themes.ThemeUtils
/** /**
* Set a text in the TextView, or set visibility to GONE if the text is null * Set a text in the TextView, or set visibility to GONE if the text is null
*/ */
fun TextView.setTextOrHide(newText: CharSequence?, hideWhenBlank: Boolean = true) { fun TextView.setTextOrHide(newText: CharSequence?, hideWhenBlank: Boolean = true, vararg relatedViews: View = emptyArray()) {
if (newText == null if (newText == null
|| (newText.isBlank() && hideWhenBlank)) { || (newText.isBlank() && hideWhenBlank)) {
isVisible = false isVisible = false
relatedViews.forEach { it.isVisible = false }
} else { } else {
this.text = newText this.text = newText
isVisible = true isVisible = true
relatedViews.forEach { it.isVisible = true }
} }
} }

View file

@ -22,7 +22,6 @@ import android.view.View
import android.view.ViewGroup import android.view.ViewGroup
import android.widget.EditText import android.widget.EditText
import android.widget.ImageView import android.widget.ImageView
import android.widget.TextView
import androidx.appcompat.widget.SearchView import androidx.appcompat.widget.SearchView
import androidx.core.view.isVisible import androidx.core.view.isVisible
import im.vector.app.R import im.vector.app.R
@ -59,14 +58,3 @@ fun ImageView.setDrawableOrHide(drawableRes: Drawable?) {
setImageDrawable(drawableRes) setImageDrawable(drawableRes)
isVisible = drawableRes != null isVisible = drawableRes != null
} }
fun TextView.setText(text: String?, nullVisibility: Int, vararg relatedViews: View) {
if (text == null) {
visibility = nullVisibility
relatedViews.forEach { it.visibility = nullVisibility }
} else {
visibility = View.VISIBLE
relatedViews.forEach { it.visibility = View.VISIBLE }
this.text = text
}
}

View file

@ -23,7 +23,7 @@ import com.airbnb.epoxy.EpoxyModelClass
import com.airbnb.epoxy.EpoxyModelWithHolder import com.airbnb.epoxy.EpoxyModelWithHolder
import im.vector.app.R import im.vector.app.R
import im.vector.app.core.epoxy.VectorEpoxyHolder import im.vector.app.core.epoxy.VectorEpoxyHolder
import im.vector.app.core.extensions.setText import im.vector.app.core.extensions.setTextOrHide
import org.matrix.android.sdk.api.session.pushers.Pusher import org.matrix.android.sdk.api.session.pushers.Pusher
@EpoxyModelClass(layout = R.layout.item_pushgateway) @EpoxyModelClass(layout = R.layout.item_pushgateway)
@ -46,8 +46,8 @@ abstract class PushGatewayItem : EpoxyModelWithHolder<PushGatewayItem.Holder>()
holder.appId.text = pusher.appId holder.appId.text = pusher.appId
holder.pushKey.text = pusher.pushKey holder.pushKey.text = pusher.pushKey
holder.appName.text = pusher.appDisplayName holder.appName.text = pusher.appDisplayName
holder.url.setText(pusher.data.url, nullVisibility = View.GONE, holder.urlTitle) holder.url.setTextOrHide(pusher.data.url, hideWhenBlank = true, holder.urlTitle)
holder.format.setText(pusher.data.format, nullVisibility = View.GONE, holder.formatTitle) holder.format.setTextOrHide(pusher.data.format, hideWhenBlank = true, holder.formatTitle)
holder.deviceName.text = pusher.deviceDisplayName holder.deviceName.text = pusher.deviceDisplayName
holder.removeButton.setOnClickListener { holder.removeButton.setOnClickListener {
interactions.onRemovePushTapped(pusher) interactions.onRemovePushTapped(pusher)