Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Solved an issue in which the endless recycler view stopped working on…
… layout refresh
  • Loading branch information
Javinator9889 committed Jun 30, 2020
1 parent 5f31f03 commit e2de789
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 33 deletions.
Expand Up @@ -69,6 +69,11 @@ class NewsFragment : BaseFragmentView(), LayoutVisibilityChange {
newsViewModel.newsData.observe(viewLifecycleOwner, Observer {
if (::footerAdapter.isInitialized)
footerAdapter.clear()
if (it.hasError) {
if (newsAdapter.adapterItemCount == 0) {

}
}
if (it.id !in activeItems) {
val newsObject = News(
title = it.title,
Expand Down Expand Up @@ -128,9 +133,15 @@ class NewsFragment : BaseFragmentView(), LayoutVisibilityChange {
refreshLayout.isRefreshing = true
newsAdapter.clear()
activeItems.clear()
footerAdapter.clear()
scrollListener.disable()
lifecycleScope.launch {
newsViewModel.populateData(language = UserProperties.language)
}.invokeOnCompletion { refreshLayout.isRefreshing = false }
}.invokeOnCompletion {
refreshLayout.isRefreshing = false
scrollListener.enable()
scrollListener.resetPageCount()
}
container.visibility = View.INVISIBLE
}
}
Expand Down
Expand Up @@ -32,6 +32,7 @@ import kotlinx.coroutines.withContext
import okhttp3.Headers
import timber.log.Timber
import java.io.Reader
import java.util.*


class NewsViewModel : ViewModel() {
Expand All @@ -42,30 +43,44 @@ class NewsViewModel : ViewModel() {
amount: Int = 10,
@Language language: String = Language.ENGLISH
) {
val httpRequest = HttpDownloader()
val klaxon = with(Klaxon()) {
propertyStrategy(newsStrategy)
fieldConverter(KlaxonDate::class, dateConverter)
fieldConverter(KlaxonElements::class, elementConverter)
}
var requestReader: Reader? = null
Auth.init()
val token = Auth.token()
Timber.d("Auth token: $token")
withContext(Dispatchers.IO) {
requestReader = httpRequest.json(
"${API_URL}/api/v1?from=$from&amount=$amount&lang=$language",
headers = Headers.of(mapOf("Authorization" to "Bearer $token"))
)
}
withContext(Dispatchers.Default) {
JsonReader(requestReader!!).use { reader ->
reader.beginArray {
while (reader.hasNext()) {
newsData.postValue(klaxon.parse<NewsData>(reader))
try {
val httpRequest = HttpDownloader()
val klaxon = with(Klaxon()) {
propertyStrategy(newsStrategy)
fieldConverter(KlaxonDate::class, dateConverter)
fieldConverter(KlaxonElements::class, elementConverter)
}
var requestReader: Reader? = null
Auth.init()
val token = Auth.token()
Timber.d("Auth token: $token")
withContext(Dispatchers.IO) {
requestReader = httpRequest.json(
"${API_URL}/api/v1?from=$from&amount=$amount&lang=$language",
headers = Headers.of(mapOf("Authorization" to "Bearer $token"))
)
}
withContext(Dispatchers.Default) {
JsonReader(requestReader!!).use { reader ->
reader.beginArray {
while (reader.hasNext()) {
newsData.postValue(klaxon.parse<NewsData>(reader))
}
}
}
}
} catch (e: Throwable) {
Timber.w(e, "Exception while populating data")
withContext(Dispatchers.Main) {
newsData.value = NewsData(
hasError = true,
id = "",
discoverDate = Date(),
title = "",
text = "",
url = ""
)
}
}
}
}
Expand Up @@ -35,7 +35,9 @@ data class NewsData(
val url: String,
@KlaxonElements
val elements: Elements? = null,
val website: Website? = null
val website: Website? = null,
@Json(ignored = true)
val hasError: Boolean = false
) : Parcelable {
constructor(parcel: Parcel) : this(
parcel.readString()!!,
Expand All @@ -44,7 +46,8 @@ data class NewsData(
parcel.readString()!!,
parcel.readString()!!,
parcel.readParcelable(Elements::class.java.classLoader),
parcel.readParcelable(Website::class.java.classLoader)
parcel.readParcelable(Website::class.java.classLoader),
parcel.readInt() == 1
)

override fun writeToParcel(parcel: Parcel, flags: Int) {
Expand All @@ -55,18 +58,15 @@ data class NewsData(
parcel.writeString(url)
parcel.writeParcelable(elements, flags)
parcel.writeParcelable(website, flags)
parcel.writeInt(if (hasError) 1 else 0)
}

override fun describeContents() = 0

companion object CREATOR : Parcelable.Creator<NewsData> {
override fun createFromParcel(parcel: Parcel): NewsData {
return NewsData(parcel)
}
override fun createFromParcel(parcel: Parcel) = NewsData(parcel)

override fun newArray(size: Int): Array<NewsData?> {
return arrayOfNulls(size)
}
override fun newArray(size: Int) = arrayOfNulls<NewsData>(size)
}
}

Expand Down
36 changes: 33 additions & 3 deletions app/src/main/res/layout/loading_recycler_view.xml
@@ -1,8 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
<androidx.constraintlayout.widget.ConstraintLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white">

<ProgressBar
Expand All @@ -15,6 +15,36 @@
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/errorScreen"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="invisible"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">

<com.javinator9889.handwashingreminder.graphics.LottieAdaptedPerformanceAnimationView
android:id="@+id/errorAnimation"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_marginBottom="19dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<com.google.android.material.textview.MaterialTextView
android:id="@+id/errorText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/errorAnimation" />
</androidx.constraintlayout.widget.ConstraintLayout>

<androidx.recyclerview.widget.RecyclerView
android:id="@+id/container"
android:layout_width="match_parent"
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/raw/error_state.json

Large diffs are not rendered by default.

0 comments on commit e2de789

Please sign in to comment.