Skip to content

Commit

Permalink
Fix items not getting checked in bitrate selection popup
Browse files Browse the repository at this point in the history
  • Loading branch information
Maxr1998 authored and nielsvanvelzen committed Dec 31, 2022
1 parent 1720fa1 commit 565d361
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class JellyfinMediaSource(
val item: BaseItemDto?,
val sourceInfo: MediaSourceInfo,
val playSessionId: String,
val maxStreamingBitrate: Int?,
private var startTimeTicks: Long? = null,
audioStreamIndex: Int? = null,
subtitleStreamIndex: Int? = null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ class MediaSourceResolver(private val apiClient: ApiClient) {
item = item,
sourceInfo = mediaSourceInfo,
playSessionId = playSessionId,
maxStreamingBitrate = maxStreamingBitrate,
startTimeTicks = startTimeTicks,
audioStreamIndex = audioStreamIndex,
subtitleStreamIndex = subtitleStreamIndex,
Expand Down
25 changes: 15 additions & 10 deletions app/src/main/java/org/jellyfin/mobile/player/ui/PlayerMenus.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ import android.widget.PopupMenu
import android.widget.TextView
import androidx.annotation.StringRes
import androidx.core.view.forEach
import androidx.core.view.get
import androidx.core.view.isVisible
import androidx.core.view.size
import org.jellyfin.mobile.R
import org.jellyfin.mobile.databinding.ExoPlayerControlViewBinding
import org.jellyfin.mobile.databinding.FragmentPlayerBinding
Expand Down Expand Up @@ -125,7 +127,7 @@ class PlayerMenus(
val height = mediaSource.selectedVideoStream?.height
val width = mediaSource.selectedVideoStream?.width
if (height != null && width != null) {
buildQualityMenu(qualityMenu.menu, width, height)
buildQualityMenu(qualityMenu.menu, mediaSource.maxStreamingBitrate, width, height)
} else {
qualityButton.isVisible = false
}
Expand Down Expand Up @@ -222,13 +224,10 @@ class PlayerMenus(
}

private fun createQualityMenu() = PopupMenu(context, qualityButton).apply {
setOnMenuItemClickListener { clickedItem: MenuItem ->
val newBitrate = clickedItem.itemId.takeUnless { bitrate -> bitrate == 0 }
setOnMenuItemClickListener { item: MenuItem ->
val newBitrate = item.itemId.takeUnless { bitrate -> bitrate == 0 }
fragment.onBitrateChanged(newBitrate) {
menu.forEach { item ->
item.isChecked = false
}
clickedItem.isChecked = true
// Ignore callback - menu will be recreated if bitrate changes
}
true
}
Expand Down Expand Up @@ -274,7 +273,10 @@ class PlayerMenus(
showNone: Boolean = false,
) {
menu.clear()
val itemNone = if (showNone) menu.add(groupId, -1, Menu.NONE, fragment.getString(R.string.menu_item_none)) else null
val itemNone = when {
showNone -> menu.add(groupId, -1, Menu.NONE, fragment.getString(R.string.menu_item_none))
else -> null
}
val menuItems = mediaStreams.map { mediaStream ->
menu.add(groupId, mediaStream.index, Menu.NONE, mediaStream.displayTitle)
}
Expand All @@ -297,17 +299,20 @@ class PlayerMenus(
subtitlesButton.setImageState(stateSet, true)
}

private fun buildQualityMenu(menu: Menu, videoWidth: Int, videoHeight: Int) {
private fun buildQualityMenu(menu: Menu, maxStreamingBitrate: Int?, videoWidth: Int, videoHeight: Int) {
menu.clear()
val options = qualityOptionsProvider.getApplicableQualityOptions(videoWidth, videoHeight)
options.map { option ->
options.forEach { option ->
val title = when (val bitrate = option.bitrate) {
0 -> context.getString(R.string.menu_item_auto)
else -> "${option.maxHeight}p - ${formatBitrate(bitrate.toDouble())}"
}
menu.add(QUALITY_MENU_GROUP, option.bitrate, Menu.NONE, title)
}
menu.setGroupCheckable(QUALITY_MENU_GROUP, true, true)

val selection = maxStreamingBitrate?.let(menu::findItem) ?: menu[menu.size - 1] // Last element is "auto"
selection.isChecked = true
}

fun dismissPlaybackInfo() {
Expand Down

0 comments on commit 565d361

Please sign in to comment.