Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: season episode list sort button #491

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,13 @@ package dev.jdtech.jellyfin.fragments

import android.os.Bundle
import android.view.LayoutInflater
import android.view.Menu
import android.view.MenuInflater
import android.view.MenuItem
import android.view.View
import android.view.ViewGroup
import androidx.core.view.MenuHost
import androidx.core.view.MenuProvider
import androidx.core.view.isVisible
import androidx.fragment.app.Fragment
import androidx.fragment.app.viewModels
Expand All @@ -23,6 +28,7 @@ import dev.jdtech.jellyfin.viewmodels.PlayerViewModel
import dev.jdtech.jellyfin.viewmodels.SeasonViewModel
import kotlinx.coroutines.launch
import timber.log.Timber
import dev.jdtech.jellyfin.core.R as CoreR

@AndroidEntryPoint
class SeasonFragment : Fragment() {
Expand All @@ -34,6 +40,8 @@ class SeasonFragment : Fragment() {

private lateinit var errorDialog: ErrorDialogFragment

private var ascendingEpisodeOrder: Boolean = true

override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
Expand All @@ -46,6 +54,35 @@ class SeasonFragment : Fragment() {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)

val menuHost: MenuHost = requireActivity()
menuHost.addMenuProvider(
object : MenuProvider {
override fun onCreateMenu(menu: Menu, menuInflater: MenuInflater) {
menuInflater.inflate(CoreR.menu.season_menu, menu)
}

override fun onMenuItemSelected(menuItem: MenuItem): Boolean {
return when (menuItem.itemId) {
CoreR.id.action_sort_order -> {
ascendingEpisodeOrder = !ascendingEpisodeOrder
menuItem.setIcon(
when (ascendingEpisodeOrder) {
true -> CoreR.drawable.ic_arrow_down_0_1
false -> CoreR.drawable.ic_arrow_down_1_0
},
)

viewModel.loadEpisodes(args.seriesId, args.seasonId, args.offline, ascendingEpisodeOrder)
true
}
else -> false
}
}
},
viewLifecycleOwner,
Lifecycle.State.RESUMED,
)

viewLifecycleOwner.lifecycleScope.launch {
viewLifecycleOwner.repeatOnLifecycle(Lifecycle.State.STARTED) {
launch {
Expand All @@ -68,7 +105,7 @@ class SeasonFragment : Fragment() {
}

binding.errorLayout.errorRetryButton.setOnClickListener {
viewModel.loadEpisodes(args.seriesId, args.seasonId, args.offline)
viewModel.loadEpisodes(args.seriesId, args.seasonId, args.offline, ascendingEpisodeOrder)
}

playerViewModel.onPlaybackRequested(lifecycleScope) { playerItems ->
Expand All @@ -95,7 +132,7 @@ class SeasonFragment : Fragment() {
override fun onResume() {
super.onResume()

viewModel.loadEpisodes(args.seriesId, args.seasonId, args.offline)
viewModel.loadEpisodes(args.seriesId, args.seasonId, args.offline, ascendingEpisodeOrder)
}

private fun bindUiStateNormal(uiState: SeasonViewModel.UiState.Normal) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,12 @@ constructor(

lateinit var season: FindroidSeason

fun loadEpisodes(seriesId: UUID, seasonId: UUID, offline: Boolean) {
fun loadEpisodes(seriesId: UUID, seasonId: UUID, offline: Boolean, ascendingOrder: Boolean) {
viewModelScope.launch {
_uiState.emit(UiState.Loading)
try {
season = getSeason(seasonId)
val episodes = getEpisodes(seriesId, seasonId, offline)
val episodes = getEpisodes(seriesId, seasonId, offline, ascendingOrder)
_uiState.emit(UiState.Normal(episodes))
} catch (_: NullPointerException) {
// Navigate back because item does not exist (probably because it's been deleted)
Expand All @@ -55,11 +55,12 @@ constructor(
return jellyfinRepository.getSeason(seasonId)
}

private suspend fun getEpisodes(seriesId: UUID, seasonId: UUID, offline: Boolean): List<EpisodeItem> {
private suspend fun getEpisodes(seriesId: UUID, seasonId: UUID, offline: Boolean, ascendingOrder: Boolean): List<EpisodeItem> {
val header = EpisodeItem.Header(seriesId = season.seriesId, seasonId = season.id, seriesName = season.seriesName, seasonName = season.name)
val episodes =
jellyfinRepository.getEpisodes(seriesId, seasonId, fields = listOf(ItemFields.OVERVIEW), offline = offline)
.map { EpisodeItem.Episode(it) }

return listOf(header) + episodes.map { EpisodeItem.Episode(it) }
return listOf(header) + (if (ascendingOrder) episodes else episodes.reversed())
}
}
41 changes: 41 additions & 0 deletions core/src/main/res/drawable/ic_arrow_down_0_1.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24">
<path
android:pathData="m3,16 l4,4 4,-4"
android:strokeLineJoin="round"
android:strokeWidth="2"
android:fillColor="#00000000"
android:strokeColor="@android:color/white"
android:strokeLineCap="round"/>
<path
android:pathData="M7,20V4"
android:strokeLineJoin="round"
android:strokeWidth="2"
android:fillColor="#00000000"
android:strokeColor="@android:color/white"
android:strokeLineCap="round"/>
<path
android:pathData="M17,4L17,4A2,2 0,0 1,19 6L19,8A2,2 0,0 1,17 10L17,10A2,2 0,0 1,15 8L15,6A2,2 0,0 1,17 4z"
android:strokeLineJoin="round"
android:strokeWidth="2"
android:fillColor="#00000000"
android:strokeColor="@android:color/white"
android:strokeLineCap="round"/>
<path
android:pathData="M17,20v-6h-2"
android:strokeLineJoin="round"
android:strokeWidth="2"
android:fillColor="#00000000"
android:strokeColor="@android:color/white"
android:strokeLineCap="round"/>
<path
android:pathData="M15,20h4"
android:strokeLineJoin="round"
android:strokeWidth="2"
android:fillColor="#00000000"
android:strokeColor="@android:color/white"
android:strokeLineCap="round"/>
</vector>
41 changes: 41 additions & 0 deletions core/src/main/res/drawable/ic_arrow_down_1_0.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24">
<path
android:pathData="m3,16 l4,4 4,-4"
android:strokeLineJoin="round"
android:strokeWidth="2"
android:fillColor="#00000000"
android:strokeColor="@android:color/white"
android:strokeLineCap="round"/>
<path
android:pathData="M7,20V4"
android:strokeLineJoin="round"
android:strokeWidth="2"
android:fillColor="#00000000"
android:strokeColor="@android:color/white"
android:strokeLineCap="round"/>
<path
android:pathData="M17,10V4h-2"
android:strokeLineJoin="round"
android:strokeWidth="2"
android:fillColor="#00000000"
android:strokeColor="@android:color/white"
android:strokeLineCap="round"/>
<path
android:pathData="M15,10h4"
android:strokeLineJoin="round"
android:strokeWidth="2"
android:fillColor="#00000000"
android:strokeColor="@android:color/white"
android:strokeLineCap="round"/>
<path
android:pathData="M17,14L17,14A2,2 0,0 1,19 16L19,18A2,2 0,0 1,17 20L17,20A2,2 0,0 1,15 18L15,16A2,2 0,0 1,17 14z"
android:strokeLineJoin="round"
android:strokeWidth="2"
android:fillColor="#00000000"
android:strokeColor="@android:color/white"
android:strokeLineCap="round"/>
</vector>
10 changes: 10 additions & 0 deletions core/src/main/res/menu/season_menu.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">

<item
android:id="@+id/action_sort_order"
android:icon="@drawable/ic_arrow_down_0_1"
android:title="@string/title_sort_order"
app:showAsAction="always" />
</menu>
1 change: 1 addition & 0 deletions core/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
<string name="title_favorite">Favorites</string>
<string name="title_settings">Settings</string>
<string name="title_download">Downloads</string>
<string name="title_sort_order">Sort Order</string>
<string name="view_all">View all</string>
<string name="error_loading_data">Error loading data</string>
<string name="retry">Retry</string>
Expand Down