2016-05-13 15:45:36 +02:00
|
|
|
package eu.kanade.tachiyomi.widget
|
|
|
|
|
|
|
|
import android.content.Context
|
|
|
|
import android.util.AttributeSet
|
2020-03-09 23:45:38 +01:00
|
|
|
import android.widget.LinearLayout
|
2016-05-13 15:45:36 +02:00
|
|
|
import android.widget.RelativeLayout
|
2020-03-09 23:59:17 +01:00
|
|
|
import androidx.annotation.StringRes
|
2020-03-29 18:10:36 +02:00
|
|
|
import androidx.appcompat.widget.AppCompatButton
|
2020-07-25 17:55:47 +02:00
|
|
|
import androidx.core.view.isVisible
|
2016-05-13 15:45:36 +02:00
|
|
|
import eu.kanade.tachiyomi.R
|
2020-03-09 23:45:38 +01:00
|
|
|
import kotlinx.android.synthetic.main.common_view_empty.view.actions_container
|
2020-03-09 23:59:17 +01:00
|
|
|
import kotlinx.android.synthetic.main.common_view_empty.view.text_face
|
2020-01-29 04:47:57 +01:00
|
|
|
import kotlinx.android.synthetic.main.common_view_empty.view.text_label
|
2020-09-14 00:48:20 +02:00
|
|
|
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
|
|
|
|
|
|
|
init {
|
2017-05-25 12:16:58 +02:00
|
|
|
inflate(context, R.layout.common_view_empty, this)
|
2016-05-13 15:45:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Hide the information view
|
|
|
|
*/
|
|
|
|
fun hide() {
|
2020-07-25 17:55:47 +02:00
|
|
|
this.isVisible = false
|
2016-05-13 15:45:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Show the information view
|
|
|
|
* @param textResource text of information view
|
|
|
|
*/
|
2020-03-09 23:45:38 +01:00
|
|
|
fun show(@StringRes textResource: Int, actions: List<Action>? = null) {
|
|
|
|
show(context.getString(textResource), actions)
|
|
|
|
}
|
|
|
|
|
|
|
|
fun show(message: String, actions: List<Action>? = null) {
|
2020-03-09 23:59:17 +01:00
|
|
|
text_face.text = getRandomErrorFace()
|
2020-03-09 23:45:38 +01:00
|
|
|
text_label.text = message
|
|
|
|
|
|
|
|
actions_container.removeAllViews()
|
|
|
|
if (!actions.isNullOrEmpty()) {
|
|
|
|
actions.forEach {
|
2020-03-29 18:10:36 +02:00
|
|
|
val button = AppCompatButton(context).apply {
|
2020-03-09 23:45:38 +01:00
|
|
|
layoutParams = LinearLayout.LayoutParams(
|
|
|
|
LinearLayout.LayoutParams.WRAP_CONTENT,
|
2020-04-25 20:24:45 +02:00
|
|
|
LinearLayout.LayoutParams.WRAP_CONTENT
|
|
|
|
)
|
2020-03-09 23:45:38 +01:00
|
|
|
|
|
|
|
setText(it.resId)
|
|
|
|
setOnClickListener(it.listener)
|
|
|
|
}
|
|
|
|
|
|
|
|
actions_container.addView(button)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-25 17:55:47 +02:00
|
|
|
this.isVisible = true
|
2020-03-09 23:59:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
companion object {
|
|
|
|
private val ERROR_FACES = listOf(
|
2020-03-09 23:45:38 +01:00
|
|
|
"(・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
|
|
|
}
|
2020-03-09 23:45:38 +01:00
|
|
|
|
|
|
|
data class Action(
|
|
|
|
@StringRes val resId: Int,
|
2020-04-18 20:47:22 +02:00
|
|
|
val listener: OnClickListener
|
2020-03-09 23:45:38 +01:00
|
|
|
)
|
2016-05-13 15:45:36 +02:00
|
|
|
}
|