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.fragment.app.Fragment
|
||||||
import androidx.lifecycle.ViewModelProvider
|
import androidx.lifecycle.ViewModelProvider
|
||||||
import androidx.navigation.fragment.findNavController
|
import androidx.navigation.fragment.findNavController
|
||||||
|
import dev.jdtech.jellyfin.R
|
||||||
import dev.jdtech.jellyfin.adapters.ViewListAdapter
|
import dev.jdtech.jellyfin.adapters.ViewListAdapter
|
||||||
import dev.jdtech.jellyfin.databinding.FragmentHomeBinding
|
import dev.jdtech.jellyfin.databinding.FragmentHomeBinding
|
||||||
import dev.jdtech.jellyfin.viewmodels.HomeViewModel
|
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, {
|
viewModel.finishedLoading.observe(viewLifecycleOwner, {
|
||||||
if (it) {
|
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,8 +25,18 @@ class HomeViewModel(
|
||||||
private val _finishedLoading = MutableLiveData<Boolean>()
|
private val _finishedLoading = MutableLiveData<Boolean>()
|
||||||
val finishedLoading: LiveData<Boolean> = _finishedLoading
|
val finishedLoading: LiveData<Boolean> = _finishedLoading
|
||||||
|
|
||||||
|
private val _error = MutableLiveData<Boolean>()
|
||||||
|
val error: LiveData<Boolean> = _error
|
||||||
|
|
||||||
init {
|
init {
|
||||||
|
loadData()
|
||||||
|
}
|
||||||
|
|
||||||
|
fun loadData() {
|
||||||
|
_error.value = false
|
||||||
|
_finishedLoading.value = false
|
||||||
viewModelScope.launch {
|
viewModelScope.launch {
|
||||||
|
try {
|
||||||
val views: MutableList<View> = mutableListOf()
|
val views: MutableList<View> = mutableListOf()
|
||||||
val viewsResult = getViews(jellyfinApi.userId!!)
|
val viewsResult = getViews(jellyfinApi.userId!!)
|
||||||
for (view in viewsResult.items!!) {
|
for (view in viewsResult.items!!) {
|
||||||
|
@ -39,6 +49,10 @@ class HomeViewModel(
|
||||||
|
|
||||||
_views.value = views
|
_views.value = views
|
||||||
_finishedLoading.value = true
|
_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">
|
tools:context=".fragments.HomeFragment">
|
||||||
|
|
||||||
<com.google.android.material.progressindicator.CircularProgressIndicator
|
<com.google.android.material.progressindicator.CircularProgressIndicator
|
||||||
android:id="@+id/loading_incicator"
|
android:id="@+id/loading_indicator"
|
||||||
android:layout_width="wrap_content"
|
android:layout_width="wrap_content"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
android:indeterminate="true"
|
android:indeterminate="true"
|
||||||
|
@ -26,6 +26,16 @@
|
||||||
app:layout_constraintTop_toTopOf="parent"
|
app:layout_constraintTop_toTopOf="parent"
|
||||||
app:trackCornerRadius="10dp" />
|
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
|
<androidx.recyclerview.widget.RecyclerView
|
||||||
android:id="@+id/views_recycler_view"
|
android:id="@+id/views_recycler_view"
|
||||||
android:layout_width="0dp"
|
android:layout_width="0dp"
|
||||||
|
|
Loading…
Reference in a new issue