Added actionable items to empty view

This commit is contained in:
arkon 2020-04-25 20:18:29 -04:00 committed by Jay
parent ad2e14bf1a
commit 0ea6764671
3 changed files with 61 additions and 14 deletions

View File

@ -2,11 +2,15 @@ package eu.kanade.tachiyomi.widget
import android.content.Context import android.content.Context
import android.util.AttributeSet import android.util.AttributeSet
import android.view.View
import android.widget.RelativeLayout import android.widget.RelativeLayout
import androidx.annotation.DrawableRes
import androidx.annotation.StringRes
import com.google.android.material.button.MaterialButton
import eu.kanade.tachiyomi.R import eu.kanade.tachiyomi.R
import eu.kanade.tachiyomi.util.system.getResourceColor import eu.kanade.tachiyomi.util.system.getResourceColor
import eu.kanade.tachiyomi.util.view.gone
import eu.kanade.tachiyomi.util.view.setVectorCompat import eu.kanade.tachiyomi.util.view.setVectorCompat
import eu.kanade.tachiyomi.util.view.visible
import kotlinx.android.synthetic.main.common_view_empty.view.* import kotlinx.android.synthetic.main.common_view_empty.view.*
class EmptyView @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null) : class EmptyView @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null) :
@ -20,17 +24,46 @@ class EmptyView @JvmOverloads constructor(context: Context, attrs: AttributeSet?
* Hide the information view * Hide the information view
*/ */
fun hide() { fun hide() {
this.visibility = View.GONE this.gone()
} }
/**
* Show the information view
* @param textResource text of information view
*/
fun show(@DrawableRes drawable: Int, @StringRes textResource: Int, actions: List<Action>? = null) {
show(drawable, context.getString(textResource), actions)
}
/** /**
* Show the information view * Show the information view
* @param drawable icon of information view * @param drawable icon of information view
* @param textResource text of information view * @param textResource text of information view
*/ */
fun show(drawable: Int, textResource: Int) { fun show(@DrawableRes drawable: Int, message: String, actions: List<Action>? = null) {
image_view.setVectorCompat(drawable, context.getResourceColor(android.R.attr.textColorHint)) image_view.setVectorCompat(drawable, context.getResourceColor(android.R.attr.textColorHint))
text_label.text = context.getString(textResource) text_label.text = message
this.visibility = View.VISIBLE
actions_container.removeAllViews()
if (!actions.isNullOrEmpty()) {
actions.forEach {
val button = (inflate(
context,
R.layout.material_text_button,
null
) as MaterialButton).apply {
setText(it.resId)
setOnClickListener(it.listener)
}
actions_container.addView(button)
} }
} }
this.visible()
}
data class Action(
@StringRes val resId: Int,
val listener: OnClickListener
)
}

View File

@ -1,24 +1,32 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content" xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_gravity="center" android:gravity="center"
xmlns:android="http://schemas.android.com/apk/res/android"> android:orientation="vertical"
android:padding="16dp">
<androidx.appcompat.widget.AppCompatImageView <androidx.appcompat.widget.AppCompatImageView
android:id="@+id/image_view" android:id="@+id/image_view"
android:layout_width="128dp" android:layout_width="128dp"
android:layout_height="128dp" android:layout_height="128dp"
android:layout_centerHorizontal="true"/> tools:src="@drawable/ic_file_download_white_24dp"/>
<TextView <TextView
android:id="@+id/text_label" android:id="@+id/text_label"
android:layout_margin="16dp"
style="@style/TextAppearance.Medium.Body2.Hint" style="@style/TextAppearance.Medium.Body2.Hint"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_below="@+id/image_view" android:layout_margin="16dp"
android:gravity="center" android:gravity="center"
android:layout_centerHorizontal="true"/> tools:text="Label" />
</RelativeLayout> <LinearLayout
android:id="@+id/actions_container"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical" />
</LinearLayout>

View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<com.google.android.material.button.MaterialButton style="@style/Theme.Widget.Button.TextButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/set_as_default_for_all"
xmlns:android="http://schemas.android.com/apk/res/android" />