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

View file

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

View file

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

View file

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