Skip to content

Commit

Permalink
Solved a problem with news loading when an exception occurs
Browse files Browse the repository at this point in the history
  • Loading branch information
Javinator9889 committed Jul 1, 2020
1 parent 0a9cb85 commit 1ecd615
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 13 deletions.
Expand Up @@ -24,8 +24,8 @@ import android.os.Bundle
import android.view.View
import androidx.annotation.LayoutRes
import androidx.fragment.app.viewModels
import androidx.lifecycle.Observer
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.observe
import androidx.lifecycle.whenStarted
import androidx.recyclerview.widget.DefaultItemAnimator
import androidx.recyclerview.widget.LinearLayoutManager
Expand Down Expand Up @@ -66,14 +66,15 @@ class NewsFragment : BaseFragmentView(), LayoutVisibilityChange {
whenStarted {
loading.visibility = View.VISIBLE
refreshLayout.isEnabled = false
newsViewModel.newsData.observe(viewLifecycleOwner, Observer {
newsViewModel.newsData.observe(viewLifecycleOwner) {
if (::footerAdapter.isInitialized)
footerAdapter.clear()
if (it.hasError) {
if (newsAdapter.adapterItemCount == 0) {

}
}
if (it.hasError && newsAdapter.adapterItemCount == 0) {
errorScreen.visibility = View.VISIBLE
container.visibility = View.INVISIBLE
refreshLayout.isEnabled = true
return@observe
} else errorScreen.visibility = View.INVISIBLE
if (it.id !in activeItems) {
val newsObject = News(
title = it.title,
Expand All @@ -91,7 +92,7 @@ class NewsFragment : BaseFragmentView(), LayoutVisibilityChange {
refreshLayout.isEnabled = true
activeItems.add(it.id)
}
})
}
}
}
}
Expand Down Expand Up @@ -143,6 +144,7 @@ class NewsFragment : BaseFragmentView(), LayoutVisibilityChange {
scrollListener.resetPageCount()
}
container.visibility = View.INVISIBLE
errorScreen.visibility = View.INVISIBLE
}
}

Expand Down
19 changes: 14 additions & 5 deletions app/src/main/res/layout/loading_recycler_view.xml
@@ -1,6 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white">
Expand All @@ -27,22 +28,30 @@

<com.javinator9889.handwashingreminder.graphics.LottieAdaptedPerformanceAnimationView
android:id="@+id/errorAnimation"
android:layout_width="200dp"
android:layout_width="0dp"
android:layout_height="200dp"
android:layout_marginBottom="19dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintBottom_toTopOf="@+id/errorText"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
app:layout_constraintTop_toTopOf="parent"
app:lottie_autoPlay="true"
app:lottie_loop="true"
app:lottie_rawRes="@raw/error_state" />

<com.google.android.material.textview.MaterialTextView
android:id="@+id/errorText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:fontFamily="@font/raleway_medium"
android:text="@string/news_error"
android:textAppearance="@style/TextAppearance.AppCompat.Display1"
android:textSize="18sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/errorAnimation" />
app:layout_constraintTop_toBottomOf="@+id/errorAnimation"
tools:text="Error while obtaining new data" />
</androidx.constraintlayout.widget.ConstraintLayout>

<androidx.recyclerview.widget.RecyclerView
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values-es/strings.xml
Expand Up @@ -290,4 +290,5 @@
<string name="animations_on">Actualmente, las animaciones están activadas.
Ten en cuenta que esto puede afectar tanto al rendimiento como a la batería</string>
<string name="animations_off">No se mostrará ninguna animación</string>
<string name="news_error">Error al cargar información sobre las noticias</string>
</resources>
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Expand Up @@ -318,4 +318,5 @@
<string name="animations_on">Currently, the app will play some animations.
Keep in mind that it can affect both performance and battery life</string>
<string name="animations_off">No animations will be played</string>
<string name="news_error">Error while loading news data</string>
</resources>

0 comments on commit 1ecd615

Please sign in to comment.