2019-01-28 19:02:31 +01:00
|
|
|
package com.keylesspalace.tusky.view
|
|
|
|
|
|
|
|
import android.content.Context
|
2023-06-15 19:59:30 +02:00
|
|
|
import android.text.method.LinkMovementMethod
|
2019-01-28 19:02:31 +01:00
|
|
|
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
|
2023-03-21 19:44:35 +01:00
|
|
|
import android.widget.TextView
|
2019-01-28 19:02:31 +01:00
|
|
|
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
|
2023-03-21 19:44:35 +01:00
|
|
|
import com.keylesspalace.tusky.util.addDrawables
|
2023-06-19 23:49:20 +02:00
|
|
|
import com.keylesspalace.tusky.util.getDrawableRes
|
|
|
|
import com.keylesspalace.tusky.util.getErrorString
|
2019-01-28 19:02:31 +01:00
|
|
|
import com.keylesspalace.tusky.util.visible
|
|
|
|
|
|
|
|
/**
|
2023-03-21 19:44:35 +01:00
|
|
|
* This view is used for screens with content which may be empty or might have failed to download.
|
2019-01-28 19:02:31 +01:00
|
|
|
*/
|
|
|
|
class BackgroundMessageView @JvmOverloads constructor(
|
2021-06-28 21:13:24 +02:00
|
|
|
context: Context,
|
|
|
|
attrs: AttributeSet? = null,
|
|
|
|
defStyleAttr: Int = 0
|
2019-01-28 19:02:31 +01:00
|
|
|
) : 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) {
|
2023-08-05 15:39:07 +02:00
|
|
|
setup(R.drawable.errorphant_offline, R.string.error_network) {}
|
2019-01-28 19:02:31 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-19 23:49:20 +02:00
|
|
|
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)
|
|
|
|
|
2019-01-28 19:02:31 +01:00
|
|
|
/**
|
|
|
|
* 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(
|
2021-06-28 21:13:24 +02:00
|
|
|
@DrawableRes imageRes: Int,
|
2023-06-19 23:49:20 +02:00
|
|
|
message: String,
|
2021-06-28 21:13:24 +02:00
|
|
|
clickListener: ((v: View) -> Unit)? = null
|
2021-03-07 19:04:22 +01:00
|
|
|
) {
|
2023-06-19 23:49:20 +02:00
|
|
|
binding.messageTextView.text = message
|
2023-06-15 19:59:30 +02:00
|
|
|
binding.messageTextView.movementMethod = LinkMovementMethod.getInstance()
|
2021-03-07 19:04:22 +01:00
|
|
|
binding.imageView.setImageResource(imageRes)
|
|
|
|
binding.button.setOnClickListener(clickListener)
|
|
|
|
binding.button.visible(clickListener != null)
|
2023-04-18 11:25:17 +02:00
|
|
|
binding.helpText.visible(false)
|
2019-01-28 19:02:31 +01:00
|
|
|
}
|
2023-03-21 19:44:35 +01:00
|
|
|
|
|
|
|
fun showHelp(@StringRes helpRes: Int) {
|
|
|
|
val size: Int = binding.helpText.textSize.toInt() + 2
|
|
|
|
val color = binding.helpText.currentTextColor
|
|
|
|
val text = context.getText(helpRes)
|
|
|
|
val textWithDrawables = addDrawables(text, color, size, context)
|
|
|
|
|
|
|
|
binding.helpText.setText(textWithDrawables, TextView.BufferType.SPANNABLE)
|
|
|
|
|
|
|
|
binding.helpText.visible(true)
|
|
|
|
}
|
2021-06-28 21:13:24 +02:00
|
|
|
}
|