Skip to content

Commit

Permalink
Add "Quick Play" for items in LibraryFragment
Browse files Browse the repository at this point in the history
  • Loading branch information
Natanel-Shitrit committed Sep 29, 2023
1 parent b1d5663 commit 111230f
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import dev.jdtech.jellyfin.core.R as CoreR

class ViewItemPagingAdapter(
private val onClickListener: (item: FindroidItem) -> Unit,
private val onLongClickListener: (item: FindroidItem) -> Unit,
private val fixedWidth: Boolean = false,
) : PagingDataAdapter<FindroidItem, ViewItemPagingAdapter.ItemViewHolder>(DiffCallback) {

Expand Down Expand Up @@ -67,6 +68,10 @@ class ViewItemPagingAdapter(
holder.itemView.setOnClickListener {
onClickListener(item)
}
holder.itemView.setOnLongClickListener {
onLongClickListener(item)
true
}
holder.bind(item, fixedWidth)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,14 @@ import dev.jdtech.jellyfin.models.FindroidBoxSet
import dev.jdtech.jellyfin.models.FindroidItem
import dev.jdtech.jellyfin.models.FindroidMovie
import dev.jdtech.jellyfin.models.FindroidShow
import dev.jdtech.jellyfin.models.PlayerItem
import dev.jdtech.jellyfin.models.SortBy
import dev.jdtech.jellyfin.utils.checkIfLoginRequired
import dev.jdtech.jellyfin.viewmodels.LibraryViewModel
import dev.jdtech.jellyfin.viewmodels.PlayerViewModel
import kotlinx.coroutines.launch
import org.jellyfin.sdk.model.api.SortOrder
import timber.log.Timber
import java.lang.IllegalArgumentException
import javax.inject.Inject
import dev.jdtech.jellyfin.core.R as CoreR
Expand All @@ -42,6 +45,7 @@ class LibraryFragment : Fragment() {

private lateinit var binding: FragmentLibraryBinding
private val viewModel: LibraryViewModel by viewModels()
private val playerViewModel: PlayerViewModel by viewModels()
private val args: LibraryFragmentArgs by navArgs()

private lateinit var errorDialog: ErrorDialogFragment
Expand Down Expand Up @@ -115,9 +119,8 @@ class LibraryFragment : Fragment() {

binding.itemsRecyclerView.adapter =
ViewItemPagingAdapter(
{ item ->
navigateToItem(item)
},
onClickListener = { item -> navigateToItem(item) },
onLongClickListener = { item -> playerViewModel.loadPlayerItems(item) },
)

(binding.itemsRecyclerView.adapter as ViewItemPagingAdapter).addLoadStateListener {
Expand Down Expand Up @@ -167,6 +170,21 @@ class LibraryFragment : Fragment() {
)
}
}

playerViewModel.onPlaybackRequested(lifecycleScope) { playerItems ->
when (playerItems) {
is PlayerViewModel.PlayerItemError -> bindPlayerItemsError(playerItems)
is PlayerViewModel.PlayerItems -> bindPlayerItems(playerItems)
}
}
}

private fun bindPlayerItemsError(error: PlayerViewModel.PlayerItemError) {
Timber.e(error.error.message)
}

private fun bindPlayerItems(items: PlayerViewModel.PlayerItems) {
navigateToPlayerActivity(items.items.toTypedArray())
}

private fun bindUiStateNormal(uiState: LibraryViewModel.UiState.Normal) {
Expand Down Expand Up @@ -224,4 +242,14 @@ class LibraryFragment : Fragment() {
}
}
}

private fun navigateToPlayerActivity(
playerItems: Array<PlayerItem>,
) {
findNavController().navigate(
LibraryFragmentDirections.actionLibraryFragmentToPlayerActivity(
playerItems,
),
)
}
}

0 comments on commit 111230f

Please sign in to comment.