Add loading indicator + run network requests on separate thread
This commit is contained in:
parent
6a2469ca97
commit
b390e80987
3 changed files with 47 additions and 11 deletions
|
@ -28,6 +28,12 @@ class HomeFragment : Fragment() {
|
||||||
binding.viewModel = viewModel
|
binding.viewModel = viewModel
|
||||||
binding.viewsRecyclerView.adapter = ViewListAdapter()
|
binding.viewsRecyclerView.adapter = ViewListAdapter()
|
||||||
|
|
||||||
|
viewModel.finishedLoading.observe(viewLifecycleOwner, {
|
||||||
|
if (it) {
|
||||||
|
binding.loadingIncicator.visibility = View.GONE
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
return binding.root
|
return binding.root
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -1,13 +1,15 @@
|
||||||
package dev.jdtech.jellyfin.viewmodels
|
package dev.jdtech.jellyfin.viewmodels
|
||||||
|
|
||||||
import android.app.Application
|
import android.app.Application
|
||||||
import android.util.Log
|
|
||||||
import androidx.lifecycle.*
|
import androidx.lifecycle.*
|
||||||
import dev.jdtech.jellyfin.api.JellyfinApi
|
import dev.jdtech.jellyfin.api.JellyfinApi
|
||||||
import dev.jdtech.jellyfin.models.View
|
import dev.jdtech.jellyfin.models.View
|
||||||
import dev.jdtech.jellyfin.models.ViewItem
|
import dev.jdtech.jellyfin.models.ViewItem
|
||||||
|
import kotlinx.coroutines.Dispatchers
|
||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
|
import kotlinx.coroutines.withContext
|
||||||
import org.jellyfin.sdk.model.api.BaseItemDto
|
import org.jellyfin.sdk.model.api.BaseItemDto
|
||||||
|
import org.jellyfin.sdk.model.api.BaseItemDtoQueryResult
|
||||||
import java.util.*
|
import java.util.*
|
||||||
|
|
||||||
class HomeViewModel(
|
class HomeViewModel(
|
||||||
|
@ -21,18 +23,19 @@ class HomeViewModel(
|
||||||
private val _items = MutableLiveData<List<BaseItemDto>>()
|
private val _items = MutableLiveData<List<BaseItemDto>>()
|
||||||
val items: LiveData<List<BaseItemDto>> = _items
|
val items: LiveData<List<BaseItemDto>> = _items
|
||||||
|
|
||||||
|
private val _finishedLoading = MutableLiveData<Boolean>()
|
||||||
|
val finishedLoading: LiveData<Boolean> = _finishedLoading
|
||||||
|
|
||||||
init {
|
init {
|
||||||
viewModelScope.launch {
|
viewModelScope.launch {
|
||||||
val views: MutableList<View> = mutableListOf()
|
val views: MutableList<View> = mutableListOf()
|
||||||
|
val viewsResult = getViews(jellyfinApi.userId!!)
|
||||||
val result by jellyfinApi.viewsApi.getUserViews(jellyfinApi.userId!!)
|
for (view in viewsResult.items!!) {
|
||||||
|
|
||||||
for (view in result.items!!) {
|
|
||||||
val items: MutableList<ViewItem> = mutableListOf()
|
val items: MutableList<ViewItem> = mutableListOf()
|
||||||
val resultItems by jellyfinApi.userLibraryApi.getLatestMedia(jellyfinApi.userId!!, parentId = view.id)
|
val latestItems = getLatestMedia(jellyfinApi.userId!!, view.id)
|
||||||
if (resultItems.isEmpty()) continue
|
if (latestItems.isEmpty()) continue
|
||||||
val v = view.toView()
|
val v = view.toView()
|
||||||
for (item in resultItems) {
|
for (item in latestItems) {
|
||||||
val i = jellyfinApi.api.baseUrl?.let { item.toViewItem(it) }
|
val i = jellyfinApi.api.baseUrl?.let { item.toViewItem(it) }
|
||||||
if (i != null) {
|
if (i != null) {
|
||||||
items.add(i)
|
items.add(i)
|
||||||
|
@ -43,10 +46,26 @@ class HomeViewModel(
|
||||||
}
|
}
|
||||||
|
|
||||||
_views.value = views
|
_views.value = views
|
||||||
|
_finishedLoading.value = true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private suspend fun getViews(userId: UUID): BaseItemDtoQueryResult {
|
||||||
|
val views: BaseItemDtoQueryResult
|
||||||
|
withContext(Dispatchers.IO) {
|
||||||
|
views = jellyfinApi.viewsApi.getUserViews(userId).content
|
||||||
|
}
|
||||||
|
return views
|
||||||
|
}
|
||||||
|
|
||||||
|
private suspend fun getLatestMedia(userId: UUID, parentId: UUID): List<BaseItemDto> {
|
||||||
|
val items: List<BaseItemDto>
|
||||||
|
withContext(Dispatchers.IO) {
|
||||||
|
items = jellyfinApi.userLibraryApi.getLatestMedia(userId, parentId = parentId).content
|
||||||
|
}
|
||||||
|
return items
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun BaseItemDto.toViewItem(baseUrl: String): ViewItem {
|
private fun BaseItemDto.toViewItem(baseUrl: String): ViewItem {
|
||||||
|
|
|
@ -15,6 +15,17 @@
|
||||||
android:layout_height="match_parent"
|
android:layout_height="match_parent"
|
||||||
tools:context=".fragments.HomeFragment">
|
tools:context=".fragments.HomeFragment">
|
||||||
|
|
||||||
|
<com.google.android.material.progressindicator.CircularProgressIndicator
|
||||||
|
android:id="@+id/loading_incicator"
|
||||||
|
android:layout_width="wrap_content"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
app:layout_constraintStart_toStartOf="parent"
|
||||||
|
app:layout_constraintEnd_toEndOf="parent"
|
||||||
|
app:layout_constraintTop_toTopOf="parent"
|
||||||
|
app:layout_constraintBottom_toBottomOf="parent"
|
||||||
|
app:trackCornerRadius="10dp"
|
||||||
|
android:indeterminate="true"/>
|
||||||
|
|
||||||
<androidx.recyclerview.widget.RecyclerView
|
<androidx.recyclerview.widget.RecyclerView
|
||||||
android:id="@+id/views_recycler_view"
|
android:id="@+id/views_recycler_view"
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
|
|
Loading…
Reference in a new issue