add error handling

This commit is contained in:
Konrad Pozniak 2020-07-01 22:03:07 +02:00
parent 8d3fd8eeff
commit 3d6f6206e6
7 changed files with 200 additions and 31 deletions

View File

@ -9,6 +9,8 @@ import androidx.recyclerview.widget.ConcatAdapter
import at.connyduck.pixelcat.R
import at.connyduck.pixelcat.components.general.BaseActivity
import at.connyduck.pixelcat.components.timeline.TimeLineActionListener
import at.connyduck.pixelcat.components.util.Error
import at.connyduck.pixelcat.components.util.Loading
import at.connyduck.pixelcat.components.util.Success
import at.connyduck.pixelcat.components.util.extension.getDisplayWidthInPx
import at.connyduck.pixelcat.components.util.extension.hide
@ -54,6 +56,10 @@ class DetailActivity: BaseActivity(), TimeLineActionListener {
viewModel.reload(false)
}
binding.detailStatus.setOnRetryListener {
viewModel.reload(true)
}
viewModel.setStatusId(intent.getStringExtra(EXTRA_STATUS_ID)!!)
val displayWidth = getDisplayWidthInPx()
@ -64,11 +70,27 @@ class DetailActivity: BaseActivity(), TimeLineActionListener {
binding.detailRecyclerView.adapter = ConcatAdapter(statusAdapter, repliesAdapter)
viewModel.currentStatus.observe(this, Observer {
if(it is Success) {
binding.detailProgress.hide()
binding.detailSwipeRefresh.isRefreshing = false
binding.detailRecyclerView.show()
statusAdapter.submitList(listOf(it.data))
when(it) {
is Success -> {
binding.detailSwipeRefresh.show()
binding.detailStatus.hide()
binding.detailProgress.hide()
binding.detailSwipeRefresh.isRefreshing = false
binding.detailRecyclerView.show()
statusAdapter.submitList(listOf(it.data))
}
is Loading -> {
binding.detailSwipeRefresh.hide()
binding.detailStatus.hide()
binding.detailProgress.show()
}
is Error -> {
binding.detailSwipeRefresh.hide()
binding.detailStatus.show()
binding.detailProgress.hide()
binding.detailStatus.setOnRetryListener { }
binding.detailStatus.showGeneralError()
}
}
})

View File

@ -0,0 +1,60 @@
/*
* Copyright (C) 2020 Conny Duck
*
* This file is part of Pixelcat.
*
* Pixelcat is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Pixelcat is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package at.connyduck.pixelcat.components.view
import android.content.Context
import android.util.AttributeSet
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.widget.LinearLayout
import at.connyduck.pixelcat.R
import at.connyduck.pixelcat.components.util.extension.getColorForAttr
import at.connyduck.pixelcat.databinding.ViewStatusBinding
class StatusView @JvmOverloads constructor(
context: Context,
attrs: AttributeSet? = null,
defStyleAttr: Int = 0
) : LinearLayout(context, attrs, defStyleAttr) {
val binding: ViewStatusBinding
init {
binding = ViewStatusBinding.inflate(LayoutInflater.from(context), this)
gravity = Gravity.CENTER
setBackgroundColor(context.getColorForAttr(R.attr.colorSurface))
orientation = VERTICAL
}
fun setOnRetryListener(listener: (View) -> Unit) {
binding.statusButton.setOnClickListener(listener)
}
fun showGeneralError() {
binding.statusMessage.setText(R.string.status_general_error)
binding.statusMessage.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.ic_alert_triangle, 0, 0)
}
fun showNetworkError() {
binding.statusMessage.setText(R.string.status_network_error)
binding.statusMessage.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.ic_wifi_off, 0, 0)
}
}

View File

@ -0,0 +1,14 @@
<vector android:height="96dp" android:viewportHeight="24"
android:viewportWidth="24" android:width="96dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="#00000000"
android:pathData="M10.29,3.86L1.82,18a2,2 0,0 0,1.71 3h16.94a2,2 0,0 0,1.71 -3L13.71,3.86a2,2 0,0 0,-3.42 0z"
android:strokeColor="?android:attr/textColorTertiary" android:strokeLineCap="round"
android:strokeLineJoin="round" android:strokeWidth="2"/>
<path android:fillColor="#00000000" android:pathData="M12,9L12,13"
android:strokeColor="?android:attr/textColorTertiary" android:strokeLineCap="round"
android:strokeLineJoin="round" android:strokeWidth="2"/>
<path android:fillColor="#00000000"
android:pathData="M12,17L12.01,17"
android:strokeColor="?android:attr/textColorTertiary" android:strokeLineCap="round"
android:strokeLineJoin="round" android:strokeWidth="2"/>
</vector>

View File

@ -0,0 +1,30 @@
<vector android:height="96dp" android:viewportHeight="24"
android:viewportWidth="24" android:width="96dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="#00000000" android:pathData="M1,1L23,23"
android:strokeColor="?android:attr/textColorTertiary" android:strokeLineCap="round"
android:strokeLineJoin="round" android:strokeWidth="2"/>
<path android:fillColor="#00000000"
android:pathData="M16.72,11.06A10.94,10.94 0,0 1,19 12.55"
android:strokeColor="?android:attr/textColorTertiary" android:strokeLineCap="round"
android:strokeLineJoin="round" android:strokeWidth="2"/>
<path android:fillColor="#00000000"
android:pathData="M5,12.55a10.94,10.94 0,0 1,5.17 -2.39"
android:strokeColor="?android:attr/textColorTertiary" android:strokeLineCap="round"
android:strokeLineJoin="round" android:strokeWidth="2"/>
<path android:fillColor="#00000000"
android:pathData="M10.71,5.05A16,16 0,0 1,22.58 9"
android:strokeColor="?android:attr/textColorTertiary" android:strokeLineCap="round"
android:strokeLineJoin="round" android:strokeWidth="2"/>
<path android:fillColor="#00000000"
android:pathData="M1.42,9a15.91,15.91 0,0 1,4.7 -2.88"
android:strokeColor="?android:attr/textColorTertiary" android:strokeLineCap="round"
android:strokeLineJoin="round" android:strokeWidth="2"/>
<path android:fillColor="#00000000"
android:pathData="M8.53,16.11a6,6 0,0 1,6.95 0"
android:strokeColor="?android:attr/textColorTertiary" android:strokeLineCap="round"
android:strokeLineJoin="round" android:strokeWidth="2"/>
<path android:fillColor="#00000000"
android:pathData="M12,20L12.01,20"
android:strokeColor="?android:attr/textColorTertiary" android:strokeLineCap="round"
android:strokeLineJoin="round" android:strokeWidth="2"/>
</vector>

View File

@ -1,46 +1,57 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/pixelcat_gradient">
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/pixelcat_gradient">
<com.google.android.material.appbar.AppBarLayout
android:id="@+id/detailAppbar"
android:layout_width="match_parent"
android:layout_height="wrap_content">
android:id="@+id/detailAppbar"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<androidx.appcompat.widget.Toolbar
android:id="@+id/detailToolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_scrollFlags="scroll|enterAlways"
app:navigationIcon="@drawable/ic_cat_small"
app:title="@string/app_name"
app:titleTextAppearance="@style/TextAppearanceToolbar"
app:titleTextColor="?attr/colorPrimary" />
android:id="@+id/detailToolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_scrollFlags="scroll|enterAlways"
app:navigationIcon="@drawable/ic_cat_small"
app:title="@string/app_name"
app:titleTextAppearance="@style/TextAppearanceToolbar"
app:titleTextColor="?attr/colorPrimary" />
</com.google.android.material.appbar.AppBarLayout>
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
android:id="@+id/detailSwipeRefresh"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="?android:attr/windowBackground"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
android:id="@+id/detailSwipeRefresh"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="?android:attr/windowBackground"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/detailRecyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager" />
android:id="@+id/detailRecyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager" />
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
<ProgressBar
android:id="@+id/detailProgress"
<at.connyduck.pixelcat.components.view.StatusView
android:id="@+id/detailStatus"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<FrameLayout
android:id="@+id/detailProgress"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center" />
</FrameLayout>
</androidx.coordinatorlayout.widget.CoordinatorLayout>

View File

@ -0,0 +1,28 @@
<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:background="?attr/colorSurface"
tools:gravity="center"
tools:orientation="vertical"
tools:parentTag="android.widget.LinearLayout">
<TextView
android:id="@+id/statusMessage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:drawablePadding="16dp"
android:lineSpacingMultiplier="1.1"
android:textAlignment="center"
tools:drawableTop="@drawable/ic_wifi_off"
tools:text="An unexpected error occured" />
<Button
android:id="@+id/statusButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:layout_marginBottom="16dp"
android:text="@string/action_retry" />
</merge>

View File

@ -76,4 +76,8 @@
<string name="sensitive_media">Sensitive media</string>
<string name="about_license_description">Pixelcat is free and open-source software. It is licensed under the GNU General Public License Version 3.</string>
<string name="status_general_error">An unexpected error occurred</string>
<string name="status_network_error">Failed to connect. Please check your internet connection-</string>
</resources>