mihon/app/src/main/java/eu/kanade/tachiyomi/widget/EmptyView.kt

95 lines
2.8 KiB
Kotlin
Raw Normal View History

2016-05-13 15:45:36 +02:00
package eu.kanade.tachiyomi.widget
import android.content.Context
import android.content.res.ColorStateList
2016-05-13 15:45:36 +02:00
import android.util.AttributeSet
import android.view.LayoutInflater
import android.widget.LinearLayout
2016-05-13 15:45:36 +02:00
import android.widget.RelativeLayout
import androidx.annotation.DrawableRes
2020-03-09 23:59:17 +01:00
import androidx.annotation.StringRes
import androidx.appcompat.view.ContextThemeWrapper
import androidx.core.view.isVisible
import com.google.android.material.button.MaterialButton
import com.mikepenz.aboutlibraries.util.getThemeColor
import eu.kanade.tachiyomi.R
import eu.kanade.tachiyomi.databinding.CommonViewEmptyBinding
import kotlin.random.Random
2016-05-13 15:45:36 +02:00
class EmptyView @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null) :
2020-04-25 20:24:45 +02:00
RelativeLayout(context, attrs) {
2016-05-13 15:45:36 +02:00
private val binding: CommonViewEmptyBinding =
CommonViewEmptyBinding.inflate(LayoutInflater.from(context), this, true)
2016-05-13 15:45:36 +02:00
/**
* Hide the information view
*/
fun hide() {
this.isVisible = false
2016-05-13 15:45:36 +02:00
}
/**
* Show the information view
* @param textResource text of information view
*/
fun show(@StringRes textResource: Int, actions: List<Action>? = null) {
show(context.getString(textResource), actions)
}
fun show(message: String, actions: List<Action>? = null) {
binding.textFace.text = getRandomErrorFace()
binding.textLabel.text = message
binding.actionsContainer.removeAllViews()
val buttonContext = ContextThemeWrapper(context, R.style.Widget_Tachiyomi_Button_ActionButton)
val buttonColor = ColorStateList.valueOf(context.getThemeColor(R.attr.colorOnBackground))
actions?.forEach {
val button = MaterialButton(
buttonContext,
null,
R.attr.borderlessButtonStyle
).apply {
layoutParams = LinearLayout.LayoutParams(
0,
LinearLayout.LayoutParams.WRAP_CONTENT,
1f / actions.size
)
setTextColor(buttonColor)
iconTint = buttonColor
setIconResource(it.iconResId)
setText(it.stringResId)
setOnClickListener(it.listener)
}
binding.actionsContainer.addView(button)
}
this.isVisible = true
2020-03-09 23:59:17 +01:00
}
companion object {
private val ERROR_FACES = listOf(
"(・o・;)",
"Σ(ಠ_ಠ)",
"ಥ_ಥ",
"(˘・_・˘)",
"(; ̄Д ̄)",
"(・Д・。"
2020-03-09 23:59:17 +01:00
)
fun getRandomErrorFace(): String {
return ERROR_FACES[Random.nextInt(ERROR_FACES.size)]
}
2016-05-13 15:45:36 +02:00
}
data class Action(
@StringRes val stringResId: Int,
@DrawableRes val iconResId: Int,
val listener: OnClickListener
)
2016-05-13 15:45:36 +02:00
}