2019-01-28 19:02:31 +01:00
|
|
|
package com.keylesspalace.tusky.view
|
|
|
|
|
|
|
|
import android.content.Context
|
|
|
|
import android.util.AttributeSet
|
|
|
|
import android.view.Gravity
|
2021-03-07 19:04:22 +01:00
|
|
|
import android.view.LayoutInflater
|
2019-01-28 19:02:31 +01:00
|
|
|
import android.view.View
|
|
|
|
import android.widget.LinearLayout
|
|
|
|
import androidx.annotation.DrawableRes
|
|
|
|
import androidx.annotation.StringRes
|
|
|
|
import com.keylesspalace.tusky.R
|
2021-03-07 19:04:22 +01:00
|
|
|
import com.keylesspalace.tusky.databinding.ViewBackgroundMessageBinding
|
2019-01-28 19:02:31 +01:00
|
|
|
import com.keylesspalace.tusky.util.visible
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This view is used for screens with downloadable content which may fail.
|
|
|
|
* Can show an image, text and button below them.
|
|
|
|
*/
|
|
|
|
class BackgroundMessageView @JvmOverloads constructor(
|
|
|
|
context: Context,
|
|
|
|
attrs: AttributeSet? = null,
|
|
|
|
defStyleAttr: Int = 0
|
|
|
|
) : LinearLayout(context, attrs, defStyleAttr) {
|
|
|
|
|
2021-03-07 19:04:22 +01:00
|
|
|
private val binding = ViewBackgroundMessageBinding.inflate(LayoutInflater.from(context), this)
|
|
|
|
|
2019-01-28 19:02:31 +01:00
|
|
|
init {
|
|
|
|
gravity = Gravity.CENTER_HORIZONTAL
|
|
|
|
orientation = VERTICAL
|
|
|
|
|
|
|
|
if (isInEditMode) {
|
|
|
|
setup(R.drawable.elephant_offline, R.string.error_network) {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Setup image, message and button.
|
|
|
|
* If [clickListener] is `null` then the button will be hidden.
|
|
|
|
*/
|
2021-03-07 19:04:22 +01:00
|
|
|
fun setup(
|
|
|
|
@DrawableRes imageRes: Int,
|
|
|
|
@StringRes messageRes: Int,
|
|
|
|
clickListener: ((v: View) -> Unit)? = null
|
|
|
|
) {
|
|
|
|
binding.messageTextView.setText(messageRes)
|
|
|
|
binding.imageView.setImageResource(imageRes)
|
|
|
|
binding.button.setOnClickListener(clickListener)
|
|
|
|
binding.button.visible(clickListener != null)
|
2019-01-28 19:02:31 +01:00
|
|
|
}
|
|
|
|
}
|