Simplify repeated code that shows errors (#3762)

Instead of repeating the same if/else check on the error type when setting up the background message, move this in to BackgroundMessageView.

Provide different `setup()` variants, including one that just takes a throwable and a handler, and figures out the correct drawables and error message.

Update and simplify call sites.
This commit is contained in:
Nik Clayton 2023-06-19 23:49:20 +02:00 committed by GitHub
commit 153a9ad9c2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
14 changed files with 46 additions and 124 deletions

View file

@ -13,6 +13,8 @@ import androidx.annotation.StringRes
import com.keylesspalace.tusky.R
import com.keylesspalace.tusky.databinding.ViewBackgroundMessageBinding
import com.keylesspalace.tusky.util.addDrawables
import com.keylesspalace.tusky.util.getDrawableRes
import com.keylesspalace.tusky.util.getErrorString
import com.keylesspalace.tusky.util.visible
/**
@ -35,16 +37,26 @@ class BackgroundMessageView @JvmOverloads constructor(
}
}
fun setup(throwable: Throwable, listener: ((v: View) -> Unit)? = null) {
setup(throwable.getDrawableRes(), throwable.getErrorString(context), listener)
}
fun setup(
@DrawableRes imageRes: Int,
@StringRes messageRes: Int,
clickListener: ((v: View) -> Unit)? = null
) = setup(imageRes, context.getString(messageRes), clickListener)
/**
* Setup image, message and button.
* If [clickListener] is `null` then the button will be hidden.
*/
fun setup(
@DrawableRes imageRes: Int,
@StringRes messageRes: Int,
message: String,
clickListener: ((v: View) -> Unit)? = null
) {
binding.messageTextView.setText(messageRes)
binding.messageTextView.text = message
binding.messageTextView.movementMethod = LinkMovementMethod.getInstance()
binding.imageView.setImageResource(imageRes)
binding.button.setOnClickListener(clickListener)