Add error handling to Home fragment
This commit is contained in:
parent
a818aa7f18
commit
b4d84e77be
4 changed files with 78 additions and 15 deletions
|
@ -7,6 +7,7 @@ import android.view.ViewGroup
|
|||
import androidx.fragment.app.Fragment
|
||||
import androidx.lifecycle.ViewModelProvider
|
||||
import androidx.navigation.fragment.findNavController
|
||||
import dev.jdtech.jellyfin.R
|
||||
import dev.jdtech.jellyfin.adapters.ViewListAdapter
|
||||
import dev.jdtech.jellyfin.databinding.FragmentHomeBinding
|
||||
import dev.jdtech.jellyfin.viewmodels.HomeViewModel
|
||||
|
@ -31,9 +32,25 @@ class HomeFragment : Fragment() {
|
|||
)
|
||||
})
|
||||
|
||||
binding.errorLayout.findViewById<View>(R.id.retry_button).setOnClickListener {
|
||||
viewModel.loadData()
|
||||
}
|
||||
|
||||
viewModel.finishedLoading.observe(viewLifecycleOwner, {
|
||||
if (it) {
|
||||
binding.loadingIncicator.visibility = View.GONE
|
||||
binding.loadingIndicator.visibility = View.GONE
|
||||
} else {
|
||||
binding.loadingIndicator.visibility = View.VISIBLE
|
||||
}
|
||||
})
|
||||
|
||||
viewModel.error.observe(viewLifecycleOwner, {
|
||||
if (it) {
|
||||
binding.errorLayout.visibility = View.VISIBLE
|
||||
binding.viewsRecyclerView.visibility = View.GONE
|
||||
} else {
|
||||
binding.errorLayout.visibility = View.GONE
|
||||
binding.viewsRecyclerView.visibility = View.VISIBLE
|
||||
}
|
||||
})
|
||||
|
||||
|
|
|
@ -25,20 +25,34 @@ class HomeViewModel(
|
|||
private val _finishedLoading = MutableLiveData<Boolean>()
|
||||
val finishedLoading: LiveData<Boolean> = _finishedLoading
|
||||
|
||||
init {
|
||||
viewModelScope.launch {
|
||||
val views: MutableList<View> = mutableListOf()
|
||||
val viewsResult = getViews(jellyfinApi.userId!!)
|
||||
for (view in viewsResult.items!!) {
|
||||
val latestItems = getLatestMedia(jellyfinApi.userId!!, view.id)
|
||||
if (latestItems.isEmpty()) continue
|
||||
val v = view.toView()
|
||||
v.items = latestItems
|
||||
views.add(v)
|
||||
}
|
||||
private val _error = MutableLiveData<Boolean>()
|
||||
val error: LiveData<Boolean> = _error
|
||||
|
||||
_views.value = views
|
||||
_finishedLoading.value = true
|
||||
init {
|
||||
loadData()
|
||||
}
|
||||
|
||||
fun loadData() {
|
||||
_error.value = false
|
||||
_finishedLoading.value = false
|
||||
viewModelScope.launch {
|
||||
try {
|
||||
val views: MutableList<View> = mutableListOf()
|
||||
val viewsResult = getViews(jellyfinApi.userId!!)
|
||||
for (view in viewsResult.items!!) {
|
||||
val latestItems = getLatestMedia(jellyfinApi.userId!!, view.id)
|
||||
if (latestItems.isEmpty()) continue
|
||||
val v = view.toView()
|
||||
v.items = latestItems
|
||||
views.add(v)
|
||||
}
|
||||
|
||||
_views.value = views
|
||||
_finishedLoading.value = true
|
||||
} catch (e: Exception) {
|
||||
_finishedLoading.value = true
|
||||
_error.value = true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
22
app/src/main/res/layout/error.xml
Normal file
22
app/src/main/res/layout/error.xml
Normal file
|
@ -0,0 +1,22 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:orientation="vertical">
|
||||
|
||||
<TextView
|
||||
android:id="@+id/error_text"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="Error loading data"
|
||||
android:textAppearance="@style/TextAppearance.MaterialComponents.Body1" />
|
||||
|
||||
<Button
|
||||
android:id="@+id/retry_button"
|
||||
style="@style/Widget.MaterialComponents.Button.OutlinedButton"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_gravity="center"
|
||||
android:text="Retry" />
|
||||
|
||||
</LinearLayout>
|
|
@ -16,7 +16,7 @@
|
|||
tools:context=".fragments.HomeFragment">
|
||||
|
||||
<com.google.android.material.progressindicator.CircularProgressIndicator
|
||||
android:id="@+id/loading_incicator"
|
||||
android:id="@+id/loading_indicator"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:indeterminate="true"
|
||||
|
@ -26,6 +26,16 @@
|
|||
app:layout_constraintTop_toTopOf="parent"
|
||||
app:trackCornerRadius="10dp" />
|
||||
|
||||
<include
|
||||
android:id="@+id/error_layout"
|
||||
layout="@layout/error"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
app:layout_constraintBottom_toBottomOf="parent"
|
||||
app:layout_constraintEnd_toEndOf="parent"
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
app:layout_constraintTop_toTopOf="parent" />
|
||||
|
||||
<androidx.recyclerview.widget.RecyclerView
|
||||
android:id="@+id/views_recycler_view"
|
||||
android:layout_width="0dp"
|
||||
|
|
Loading…
Reference in a new issue