From b99fb04cbd8402df90c302a096502f6c27c8227e Mon Sep 17 00:00:00 2001 From: Isira Seneviratne Date: Mon, 13 Feb 2023 09:13:15 +0530 Subject: [PATCH] Simplify getAvailableResolutions(). --- .../github/libretube/obj/VideoResolution.kt | 3 +- .../libretube/ui/fragments/PlayerFragment.kt | 33 ++++++------------- 2 files changed, 11 insertions(+), 25 deletions(-) diff --git a/app/src/main/java/com/github/libretube/obj/VideoResolution.kt b/app/src/main/java/com/github/libretube/obj/VideoResolution.kt index b234cfda2..465e01a16 100644 --- a/app/src/main/java/com/github/libretube/obj/VideoResolution.kt +++ b/app/src/main/java/com/github/libretube/obj/VideoResolution.kt @@ -2,6 +2,5 @@ package com.github.libretube.obj data class VideoResolution( val name: String, - val resolution: Int? = null, - val adaptiveSourceUrl: String? = null + val resolution: Int ) diff --git a/app/src/main/java/com/github/libretube/ui/fragments/PlayerFragment.kt b/app/src/main/java/com/github/libretube/ui/fragments/PlayerFragment.kt index 6849876a8..2438b8718 100644 --- a/app/src/main/java/com/github/libretube/ui/fragments/PlayerFragment.kt +++ b/app/src/main/java/com/github/libretube/ui/fragments/PlayerFragment.kt @@ -1202,29 +1202,18 @@ class PlayerFragment : Fragment(R.layout.fragment_player), OnlinePlayerOptions { * Get all available player resolutions */ private fun getAvailableResolutions(): List { - val resolutions = exoPlayer.currentTracks.groups.asSequence().map { group -> - (0 until group.length).map { - group.getTrackFormat(it).height + val resolutions = exoPlayer.currentTracks.groups.asSequence() + .flatMap { group -> + (0 until group.length).map { + group.getTrackFormat(it).height + } } - }.flatten() .filter { it > 0 } - .sortedDescending() - .distinct() + .map { VideoResolution("${it}p", it) } + .toSortedSet(compareByDescending { it.resolution }) - return resolutions.map { - VideoResolution( - name = "${it}p", - resolution = it - ) - }.toMutableList().also { - it.add( - 0, - VideoResolution( - getString(R.string.auto_quality), - resolution = Int.MAX_VALUE - ) - ) - } + resolutions.add(VideoResolution(getString(R.string.auto_quality), Int.MAX_VALUE)) + return resolutions.toList() } private fun setResolutionAndSubtitles() { @@ -1366,9 +1355,7 @@ class PlayerFragment : Fragment(R.layout.fragment_player), OnlinePlayerOptions { if (currentQuality == it.resolution) "${it.name} ✓" else it.name } ) { which -> - resolutions[which].resolution?.let { - setPlayerResolution(it) - } + setPlayerResolution(resolutions[which].resolution) } .show(childFragmentManager) }