Add Continue Watching section to HomeFragment

This commit is contained in:
Jarne Demeulemeester 2021-06-30 15:36:47 +02:00
parent 7605eb4158
commit ee61738d69
No known key found for this signature in database
GPG key ID: B61B7B150DB6A6D2
5 changed files with 34 additions and 16 deletions

View file

@ -7,9 +7,8 @@ import androidx.recyclerview.widget.ListAdapter
import androidx.recyclerview.widget.RecyclerView
import dev.jdtech.jellyfin.databinding.NextUpSectionBinding
import dev.jdtech.jellyfin.databinding.ViewItemBinding
import dev.jdtech.jellyfin.models.NextUp
import dev.jdtech.jellyfin.models.HomeSection
import dev.jdtech.jellyfin.models.View
import org.jellyfin.sdk.model.api.BaseItemDto
import java.lang.ClassCastException
import java.util.*
@ -42,8 +41,8 @@ class ViewListAdapter(
class NextUpViewHolder(private var binding: NextUpSectionBinding) :
RecyclerView.ViewHolder(binding.root) {
fun bind(section: HomeItem.NextUpSection) {
binding.section = section.nextUp
fun bind(section: HomeItem.Section) {
binding.section = section.homeSection
binding.itemsRecyclerView.adapter = HomeEpisodeListAdapter()
binding.executePendingBindings()
}
@ -82,7 +81,7 @@ class ViewListAdapter(
override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
when (holder.itemViewType) {
ITEM_VIEW_TYPE_NEXT_UP -> {
val view = getItem(position) as HomeItem.NextUpSection
val view = getItem(position) as HomeItem.Section
(holder as NextUpViewHolder).bind(view)
}
ITEM_VIEW_TYPE_VIEW -> {
@ -94,7 +93,7 @@ class ViewListAdapter(
override fun getItemViewType(position: Int): Int {
return when (getItem(position)) {
is HomeItem.NextUpSection -> ITEM_VIEW_TYPE_NEXT_UP
is HomeItem.Section -> ITEM_VIEW_TYPE_NEXT_UP
is HomeItem.ViewItem -> ITEM_VIEW_TYPE_VIEW
}
}
@ -105,8 +104,8 @@ class ViewListAdapter(
}
sealed class HomeItem {
data class NextUpSection(val nextUp: NextUp) : HomeItem() {
override val id = nextUp.id
data class Section(val homeSection: HomeSection) : HomeItem() {
override val id = homeSection.id
}
data class ViewItem(val view: View) : HomeItem() {

View file

@ -3,7 +3,7 @@ package dev.jdtech.jellyfin.models
import org.jellyfin.sdk.model.api.BaseItemDto
import java.util.*
data class NextUp(
data class HomeSection(
val id: UUID,
val name: String?,
var items: List<BaseItemDto>? = null

View file

@ -5,7 +5,7 @@ import androidx.lifecycle.*
import dev.jdtech.jellyfin.R
import dev.jdtech.jellyfin.adapters.HomeItem
import dev.jdtech.jellyfin.api.JellyfinApi
import dev.jdtech.jellyfin.models.NextUp
import dev.jdtech.jellyfin.models.HomeSection
import dev.jdtech.jellyfin.models.View
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
@ -19,6 +19,7 @@ class HomeViewModel(
) : AndroidViewModel(application) {
private val jellyfinApi = JellyfinApi.getInstance(application, "")
private val continueWatchingString = application.resources.getString(R.string.continue_watching)
private val nextUpString = application.resources.getString(R.string.next_up)
private val _views = MutableLiveData<List<HomeItem>>()
@ -52,14 +53,24 @@ class HomeViewModel(
views.add(v)
}
val nextUpItems = getNextUp()
val nextUp = NextUp(UUID.randomUUID(), nextUpString, nextUpItems)
val items = mutableListOf<HomeItem>()
_views.value = when (nextUpItems) {
null -> views.map { HomeItem.ViewItem(it) }
else -> listOf(HomeItem.NextUpSection(nextUp)) + views.map { HomeItem.ViewItem(it) }
val resumeItems = getResumeItems()
val resumeSection = HomeSection(UUID.randomUUID(), continueWatchingString, resumeItems)
if (!resumeItems.isNullOrEmpty()) {
items.add(HomeItem.Section(resumeSection))
}
val nextUpItems = getNextUp()
val nextUpSection = HomeSection(UUID.randomUUID(), nextUpString, nextUpItems)
if (!nextUpItems.isNullOrEmpty()) {
items.add(HomeItem.Section(nextUpSection))
}
_views.value = items + views.map { HomeItem.ViewItem(it) }
_finishedLoading.value = true
} catch (e: Exception) {
_finishedLoading.value = true
@ -92,6 +103,13 @@ class HomeViewModel(
return items
}
private suspend fun getResumeItems(): List<BaseItemDto>? {
val items: List<BaseItemDto>?
withContext(Dispatchers.IO) {
items = jellyfinApi.itemsApi.getResumeItems(jellyfinApi.userId!!).content.items
}
return items
}
}
private fun BaseItemDto.toView(): View {

View file

@ -7,7 +7,7 @@
<variable
name="section"
type="dev.jdtech.jellyfin.models.NextUp" />
type="dev.jdtech.jellyfin.models.HomeSection" />
</data>
<LinearLayout

View file

@ -35,5 +35,6 @@
<string name="episode_name">%1$d. %2$s</string>
<string name="episode_name_extended">S%1$d:E%2$d - %3$s</string>
<string name="next_up">Next Up</string>
<string name="continue_watching">Continue Watching</string>
<string name="latest_library">Latest %1$s</string>
</resources>