From df637a27aa54dd7c503bdcce67b576b938a1cb70 Mon Sep 17 00:00:00 2001 From: OxygenCobalt Date: Fri, 4 Dec 2020 16:28:42 -0700 Subject: [PATCH] Prevent fast scroller from being truncated Prevent the fast scroll from being truncated if there isnt enough screenspace to contain all the entries, such as in a phone's landscape mode. --- .../org/oxycblt/auxio/songs/SongsFragment.kt | 42 ++++++++++++++----- 1 file changed, 32 insertions(+), 10 deletions(-) diff --git a/app/src/main/java/org/oxycblt/auxio/songs/SongsFragment.kt b/app/src/main/java/org/oxycblt/auxio/songs/SongsFragment.kt index 672c623e4..e832bd479 100644 --- a/app/src/main/java/org/oxycblt/auxio/songs/SongsFragment.kt +++ b/app/src/main/java/org/oxycblt/auxio/songs/SongsFragment.kt @@ -2,6 +2,7 @@ package org.oxycblt.auxio.songs import android.os.Bundle import android.util.Log +import android.util.TypedValue import android.view.LayoutInflater import android.view.View import android.view.ViewGroup @@ -17,6 +18,7 @@ import org.oxycblt.auxio.music.MusicStore import org.oxycblt.auxio.playback.PlaybackViewModel import org.oxycblt.auxio.playback.state.PlaybackMode import org.oxycblt.auxio.ui.setupSongActions +import kotlin.math.ceil /** * A [Fragment] that shows a list of all songs on the device. Contains options to search/shuffle @@ -25,6 +27,12 @@ import org.oxycblt.auxio.ui.setupSongActions */ class SongsFragment : Fragment() { private val playbackModel: PlaybackViewModel by activityViewModels() + private val indicatorTextSize: Float by lazy { + TypedValue.applyDimension( + TypedValue.COMPLEX_UNIT_SP, 14F, + requireContext().resources.displayMetrics + ) + } override fun onCreateView( inflater: LayoutInflater, @@ -73,7 +81,7 @@ class SongsFragment : Fragment() { val musicStore = MusicStore.getInstance() binding.songFastScroll.apply { - var hasAddedNumber = false + var concatInterval = -1 setupWithRecyclerView( binding.songRecycler, @@ -96,7 +104,8 @@ class SongsFragment : Fragment() { item.name[0].toUpperCase() } - // Use "#" if the character is a digit. + // Use "#" if the character is a digit, also has the nice side-effect of + // truncating extra numbers. if (char.isDigit()) { FastScrollItemIndicator.Text("#") } else { @@ -105,20 +114,33 @@ class SongsFragment : Fragment() { } ) - showIndicator = { indicator, _, _ -> + showIndicator = { _, i, total -> var isGood = true - // Remove all but the first "#" character - if (indicator is FastScrollItemIndicator.Text) { - if (indicator.text == "#") { - if (hasAddedNumber) { - isGood = false - } + if (concatInterval == -1) { + // If the screen size is too small to contain all the entries, + val maxEntries = (height / (indicatorTextSize + textPadding)) - hasAddedNumber = true + if (total > maxEntries.toInt()) { + concatInterval = ceil(total / maxEntries).toInt() + + Log.d( + this::class.simpleName, + "More entries than screen space, truncating by $concatInterval..." + ) + + check(concatInterval > 1) { + "ConcatInterval was one despite truncation being needed" + } + } else { + concatInterval = 1 } } + if ((i % concatInterval) != 0) { + isGood = false + } + isGood }