From bb43e2aa034e1ee353173553b1227f3672343deb Mon Sep 17 00:00:00 2001 From: arkon Date: Mon, 9 Mar 2020 18:45:38 -0400 Subject: [PATCH] Show empty catalogue error view with actions --- .../browse/BrowseCatalogueController.kt | 38 +++++++++++---- .../eu/kanade/tachiyomi/widget/EmptyView.kt | 46 +++++++++++++++---- .../main/res/layout/catalogue_controller.xml | 35 +++++++++----- app/src/main/res/layout/common_view_empty.xml | 23 ++++++---- 4 files changed, 104 insertions(+), 38 deletions(-) diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/catalogue/browse/BrowseCatalogueController.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/catalogue/browse/BrowseCatalogueController.kt index 1249831d9c..e0a166078b 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/catalogue/browse/BrowseCatalogueController.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/catalogue/browse/BrowseCatalogueController.kt @@ -41,8 +41,10 @@ import eu.kanade.tachiyomi.util.view.inflate import eu.kanade.tachiyomi.util.view.snack import eu.kanade.tachiyomi.util.view.visible import eu.kanade.tachiyomi.widget.AutofitRecyclerView +import eu.kanade.tachiyomi.widget.EmptyView import java.util.concurrent.TimeUnit import kotlinx.android.synthetic.main.catalogue_controller.catalogue_view +import kotlinx.android.synthetic.main.catalogue_controller.empty_view import kotlinx.android.synthetic.main.catalogue_controller.progress import kotlinx.android.synthetic.main.main_activity.drawer import rx.Observable @@ -352,16 +354,32 @@ open class BrowseCatalogueController(bundle: Bundle) : snack?.dismiss() if (catalogue_view != null) { - snack = catalogue_view.snack(getErrorMessage(error), Snackbar.LENGTH_INDEFINITE) { - setAction(R.string.action_retry) { - // If not the first page, show bottom progress bar. - if (adapter.mainItemCount > 0) { - val item = progressItem ?: return@setAction - adapter.addScrollableFooterWithDelay(item, 0, true) - } else { - showProgressBar() - } - presenter.requestNext() + val message = getErrorMessage(error) + val retryAction = View.OnClickListener { + empty_view.hide() + + // If not the first page, show bottom progress bar. + if (adapter.mainItemCount > 0 && progressItem != null) { + adapter.addScrollableFooterWithDelay(progressItem!!, 0, true) + } else { + showProgressBar() + } + presenter.requestNext() + } + val openInWebViewAction = View.OnClickListener { + openInWebView() + } + + if (adapter.isEmpty) { + empty_view.show(message, listOf( + EmptyView.Action(R.string.action_retry, retryAction), + EmptyView.Action(R.string.action_open_in_web_view, openInWebViewAction) + )) + } else { + empty_view.hide() + + snack = catalogue_view.snack(message, Snackbar.LENGTH_INDEFINITE) { + setAction(R.string.action_retry, retryAction) } } } diff --git a/app/src/main/java/eu/kanade/tachiyomi/widget/EmptyView.kt b/app/src/main/java/eu/kanade/tachiyomi/widget/EmptyView.kt index 84628dac94..ca63025d8a 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/widget/EmptyView.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/widget/EmptyView.kt @@ -2,12 +2,16 @@ package eu.kanade.tachiyomi.widget import android.content.Context import android.util.AttributeSet +import android.view.View +import android.widget.Button +import android.widget.LinearLayout import android.widget.RelativeLayout import androidx.annotation.StringRes import eu.kanade.tachiyomi.R import eu.kanade.tachiyomi.util.view.gone import eu.kanade.tachiyomi.util.view.visible import kotlin.random.Random +import kotlinx.android.synthetic.main.common_view_empty.view.actions_container import kotlinx.android.synthetic.main.common_view_empty.view.text_face import kotlinx.android.synthetic.main.common_view_empty.view.text_label @@ -29,24 +33,50 @@ class EmptyView @JvmOverloads constructor(context: Context, attrs: AttributeSet? * Show the information view * @param textResource text of information view */ - fun show(@StringRes textResource: Int) { + fun show(@StringRes textResource: Int, actions: List? = null) { + show(context.getString(textResource), actions) + } + + fun show(message: String, actions: List? = null) { text_face.text = getRandomErrorFace() - text_label.text = context.getString(textResource) + text_label.text = message + + actions_container.removeAllViews() + if (!actions.isNullOrEmpty()) { + actions.forEach { + val button = Button(context).apply { + layoutParams = LinearLayout.LayoutParams( + LinearLayout.LayoutParams.WRAP_CONTENT, + LinearLayout.LayoutParams.WRAP_CONTENT) + + setText(it.resId) + setOnClickListener(it.listener) + } + + actions_container.addView(button) + } + } + this.visible() } companion object { private val ERROR_FACES = listOf( - "(・o・;)", - "Σ(ಠ_ಠ)", - "ಥ_ಥ", - "(˘・_・˘)", - "(; ̄Д ̄)", - "(・Д・。" + "(・o・;)", + "Σ(ಠ_ಠ)", + "ಥ_ಥ", + "(˘・_・˘)", + "(; ̄Д ̄)", + "(・Д・。" ) fun getRandomErrorFace(): String { return ERROR_FACES[Random.nextInt(ERROR_FACES.size)] } } + + data class Action( + @StringRes val resId: Int, + val listener: View.OnClickListener + ) } diff --git a/app/src/main/res/layout/catalogue_controller.xml b/app/src/main/res/layout/catalogue_controller.xml index de6afc1eea..12b00bbd3b 100644 --- a/app/src/main/res/layout/catalogue_controller.xml +++ b/app/src/main/res/layout/catalogue_controller.xml @@ -4,22 +4,35 @@ android:layout_width="match_parent" android:layout_height="match_parent"> - + android:fitsSystemWindows="true"> - + + + + + + - + diff --git a/app/src/main/res/layout/common_view_empty.xml b/app/src/main/res/layout/common_view_empty.xml index 7fdc6b21f8..5e2ca33b0a 100644 --- a/app/src/main/res/layout/common_view_empty.xml +++ b/app/src/main/res/layout/common_view_empty.xml @@ -1,17 +1,17 @@ - + android:layout_width="match_parent" + android:layout_height="wrap_content" + android:gravity="center" + android:orientation="vertical" + android:padding="16dp"> @@ -21,10 +21,15 @@ style="@style/TextAppearance.Medium.Body2.Hint" android:layout_width="wrap_content" android:layout_height="wrap_content" - android:layout_below="@+id/text_face" - android:layout_centerHorizontal="true" android:layout_margin="16dp" android:gravity="center" tools:text="Label" /> - + + +