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