list: switch to header divider

Switch to item decorations to manage header dividers.

This is much more reliable than encoding it in-data. Only cost comes in
that it forces me to backport yet another component since I still can't
update MDC after over half a year.
This commit is contained in:
Alexander Capehart
2023-01-17 14:37:33 -07:00
parent fb93b82b2b
commit 9e1f6af21e
20 changed files with 597 additions and 91 deletions
@@ -30,7 +30,7 @@ import androidx.navigation.NavController
import androidx.navigation.NavDestination
import androidx.navigation.findNavController
import androidx.navigation.fragment.findNavController
import com.google.android.material.bottomsheet.NeoBottomSheetBehavior
import com.google.android.material.bottomsheet.BackportBottomSheetBehavior
import com.google.android.material.shape.MaterialShapeDrawable
import com.google.android.material.transition.MaterialFadeThrough
import kotlin.math.max
@@ -101,10 +101,10 @@ class MainFragment :
val playbackSheetBehavior =
binding.playbackSheet.coordinatorLayoutBehavior as PlaybackBottomSheetBehavior
unlikelyToBeNull(binding.handleWrapper).setOnClickListener {
if (playbackSheetBehavior.state == NeoBottomSheetBehavior.STATE_EXPANDED &&
queueSheetBehavior.state == NeoBottomSheetBehavior.STATE_COLLAPSED) {
if (playbackSheetBehavior.state == BackportBottomSheetBehavior.STATE_EXPANDED &&
queueSheetBehavior.state == BackportBottomSheetBehavior.STATE_COLLAPSED) {
// Playback sheet is expanded and queue sheet is collapsed, we can expand it.
queueSheetBehavior.state = NeoBottomSheetBehavior.STATE_EXPANDED
queueSheetBehavior.state = BackportBottomSheetBehavior.STATE_EXPANDED
}
}
} else {
@@ -183,7 +183,7 @@ class MainFragment :
// Playback sheet intercepts queue sheet touch events, prevent that from
// occurring by disabling dragging whenever the queue sheet is expanded.
playbackSheetBehavior.isDraggable =
queueSheetBehavior.state == NeoBottomSheetBehavior.STATE_COLLAPSED
queueSheetBehavior.state == BackportBottomSheetBehavior.STATE_COLLAPSED
}
} else {
// No queue sheet, fade normally based on the playback sheet
@@ -317,9 +317,9 @@ class MainFragment :
val binding = requireBinding()
val playbackSheetBehavior =
binding.playbackSheet.coordinatorLayoutBehavior as PlaybackBottomSheetBehavior
if (playbackSheetBehavior.state == NeoBottomSheetBehavior.STATE_COLLAPSED) {
if (playbackSheetBehavior.state == BackportBottomSheetBehavior.STATE_COLLAPSED) {
// Playback sheet is not expanded and not hidden, we can expand it.
playbackSheetBehavior.state = NeoBottomSheetBehavior.STATE_EXPANDED
playbackSheetBehavior.state = BackportBottomSheetBehavior.STATE_EXPANDED
}
}
@@ -327,12 +327,12 @@ class MainFragment :
val binding = requireBinding()
val playbackSheetBehavior =
binding.playbackSheet.coordinatorLayoutBehavior as PlaybackBottomSheetBehavior
if (playbackSheetBehavior.state == NeoBottomSheetBehavior.STATE_EXPANDED) {
if (playbackSheetBehavior.state == BackportBottomSheetBehavior.STATE_EXPANDED) {
// Make sure the queue is also collapsed here.
val queueSheetBehavior =
binding.queueSheet.coordinatorLayoutBehavior as QueueBottomSheetBehavior?
playbackSheetBehavior.state = NeoBottomSheetBehavior.STATE_COLLAPSED
queueSheetBehavior?.state = NeoBottomSheetBehavior.STATE_COLLAPSED
playbackSheetBehavior.state = BackportBottomSheetBehavior.STATE_COLLAPSED
queueSheetBehavior?.state = BackportBottomSheetBehavior.STATE_COLLAPSED
}
}
@@ -340,7 +340,7 @@ class MainFragment :
val binding = requireBinding()
val playbackSheetBehavior =
binding.playbackSheet.coordinatorLayoutBehavior as PlaybackBottomSheetBehavior
if (playbackSheetBehavior.state == NeoBottomSheetBehavior.STATE_HIDDEN) {
if (playbackSheetBehavior.state == BackportBottomSheetBehavior.STATE_HIDDEN) {
val queueSheetBehavior =
binding.queueSheet.coordinatorLayoutBehavior as QueueBottomSheetBehavior?
// Queue sheet behavior is either collapsed or expanded, no hiding needed
@@ -348,7 +348,7 @@ class MainFragment :
playbackSheetBehavior.apply {
// Make sure the view is draggable, at least until the draw checks kick in.
isDraggable = true
state = NeoBottomSheetBehavior.STATE_COLLAPSED
state = BackportBottomSheetBehavior.STATE_COLLAPSED
}
}
}
@@ -357,19 +357,19 @@ class MainFragment :
val binding = requireBinding()
val playbackSheetBehavior =
binding.playbackSheet.coordinatorLayoutBehavior as PlaybackBottomSheetBehavior
if (playbackSheetBehavior.state != NeoBottomSheetBehavior.STATE_HIDDEN) {
if (playbackSheetBehavior.state != BackportBottomSheetBehavior.STATE_HIDDEN) {
val queueSheetBehavior =
binding.queueSheet.coordinatorLayoutBehavior as QueueBottomSheetBehavior?
// Make both bottom sheets non-draggable so the user can't halt the hiding event.
queueSheetBehavior?.apply {
isDraggable = false
state = NeoBottomSheetBehavior.STATE_COLLAPSED
state = BackportBottomSheetBehavior.STATE_COLLAPSED
}
playbackSheetBehavior.apply {
isDraggable = false
state = NeoBottomSheetBehavior.STATE_HIDDEN
state = BackportBottomSheetBehavior.STATE_HIDDEN
}
}
}
@@ -388,16 +388,16 @@ class MainFragment :
// If expanded, collapse the queue sheet first.
if (queueSheetBehavior != null &&
queueSheetBehavior.state != NeoBottomSheetBehavior.STATE_COLLAPSED &&
playbackSheetBehavior.state == NeoBottomSheetBehavior.STATE_EXPANDED) {
queueSheetBehavior.state = NeoBottomSheetBehavior.STATE_COLLAPSED
queueSheetBehavior.state != BackportBottomSheetBehavior.STATE_COLLAPSED &&
playbackSheetBehavior.state == BackportBottomSheetBehavior.STATE_EXPANDED) {
queueSheetBehavior.state = BackportBottomSheetBehavior.STATE_COLLAPSED
return
}
// If expanded, collapse the playback sheet next.
if (playbackSheetBehavior.state != NeoBottomSheetBehavior.STATE_COLLAPSED &&
playbackSheetBehavior.state != NeoBottomSheetBehavior.STATE_HIDDEN) {
playbackSheetBehavior.state = NeoBottomSheetBehavior.STATE_COLLAPSED
if (playbackSheetBehavior.state != BackportBottomSheetBehavior.STATE_COLLAPSED &&
playbackSheetBehavior.state != BackportBottomSheetBehavior.STATE_HIDDEN) {
playbackSheetBehavior.state = BackportBottomSheetBehavior.STATE_COLLAPSED
return
}
@@ -428,8 +428,8 @@ class MainFragment :
val exploreNavController = binding.exploreNavHost.findNavController()
isEnabled =
queueSheetBehavior?.state == NeoBottomSheetBehavior.STATE_EXPANDED ||
playbackSheetBehavior.state == NeoBottomSheetBehavior.STATE_EXPANDED ||
queueSheetBehavior?.state == BackportBottomSheetBehavior.STATE_EXPANDED ||
playbackSheetBehavior.state == BackportBottomSheetBehavior.STATE_EXPANDED ||
selectionModel.selected.value.isNotEmpty() ||
exploreNavController.currentDestination?.id !=
exploreNavController.graph.startDestinationId
@@ -27,4 +27,4 @@ interface Item
* @param titleRes The string resource used for the header's title.
* @param withDivider Whether to show a divider on the item.
*/
data class Header(@StringRes val titleRes: Int, val withDivider: Boolean = true) : Item
data class Header(@StringRes val titleRes: Int) : Item
@@ -45,6 +45,7 @@ constructor(context: Context, attrs: AttributeSet? = null, @AttrRes defStyleAttr
// Auxio's non-dialog RecyclerViews never change their size based on adapter contents,
// so we can enable fixed-size optimizations.
setHasFixedSize(true)
addItemDecoration(HeaderItemDecoration(context))
}
final override fun setHasFixedSize(hasFixedSize: Boolean) {
@@ -52,6 +53,10 @@ constructor(context: Context, attrs: AttributeSet? = null, @AttrRes defStyleAttr
super.setHasFixedSize(hasFixedSize)
}
final override fun addItemDecoration(decor: ItemDecoration) {
super.addItemDecoration(decor)
}
override fun onApplyWindowInsets(insets: WindowInsets): WindowInsets {
// Update the RecyclerView's padding such that the bottom insets are applied
// while still preserving bottom padding.
@@ -78,6 +83,8 @@ constructor(context: Context, attrs: AttributeSet? = null, @AttrRes defStyleAttr
}
}
/** A [RecyclerView.Adapter]-specific hook to control divider decoration visibility. */
/** An [RecyclerView.Adapter]-specific hook to [GridLayoutManager.SpanSizeLookup]. */
interface SpanSizeLookup {
/**
@@ -0,0 +1,44 @@
/*
* Copyright (c) 2023 Auxio Project
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package org.oxycblt.auxio.list.recycler
import android.content.Context
import android.util.AttributeSet
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.google.android.material.divider.BackportMaterialDividerItemDecoration
import org.oxycblt.auxio.R
import org.oxycblt.auxio.list.Header
class HeaderItemDecoration
@JvmOverloads
constructor(
context: Context,
attributeSet: AttributeSet? = null,
defStyleAttr: Int = R.attr.materialDividerStyle,
orientation: Int = LinearLayoutManager.VERTICAL
) : BackportMaterialDividerItemDecoration(context, attributeSet, defStyleAttr, orientation) {
override fun shouldDrawDivider(position: Int, adapter: RecyclerView.Adapter<*>?) =
try {
(adapter as DiffAdapter<*, *, *>).getItem(position + 1) is Header
} catch (e: ClassCastException) {
false
} catch (e: IndexOutOfBoundsException) {
false
}
}
@@ -18,7 +18,6 @@
package org.oxycblt.auxio.list.recycler
import android.view.View
import androidx.core.view.isVisible
import androidx.recyclerview.widget.RecyclerView
import org.oxycblt.auxio.IntegerTable
import org.oxycblt.auxio.R
@@ -31,6 +30,7 @@ import org.oxycblt.auxio.music.*
import org.oxycblt.auxio.util.context
import org.oxycblt.auxio.util.getPlural
import org.oxycblt.auxio.util.inflater
import org.oxycblt.auxio.util.logD
/**
* A [RecyclerView.ViewHolder] that displays a [Song]. Use [from] to create an instance.
@@ -249,8 +249,8 @@ class HeaderViewHolder private constructor(private val binding: ItemHeaderBindin
* @param header The new [Header] to bind.
*/
fun bind(header: Header) {
logD(binding.context.getString(header.titleRes))
binding.title.text = binding.context.getString(header.titleRes)
binding.headerDivider.isVisible = header.withDivider
}
companion object {
@@ -22,6 +22,7 @@ import androidx.recyclerview.widget.RecyclerView
import org.oxycblt.auxio.list.*
import org.oxycblt.auxio.list.recycler.*
import org.oxycblt.auxio.music.*
import org.oxycblt.auxio.util.logD
/**
* An adapter that displays search results.
@@ -54,6 +55,7 @@ class SearchAdapter(private val listener: SelectableListListener<Music>) :
}
override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
logD(position)
when (val item = getItem(position)) {
is Song -> (holder as SongViewHolder).bind(item, listener)
is Album -> (holder as AlbumViewHolder).bind(item, listener)
@@ -65,7 +67,16 @@ class SearchAdapter(private val listener: SelectableListListener<Music>) :
override fun isItemFullWidth(position: Int) = getItem(position) is Header
/**
* Make sure that the top header has a correctly configured divider visibility. This would
* normally be automatically done by the differ, but that results in a strange animation.
*/
fun pokeDividers() {
notifyItemChanged(0, PAYLOAD_UPDATE_DIVIDER)
}
private companion object {
val PAYLOAD_UPDATE_DIVIDER = 102249124
/** A comparator that can be used with DiffUtil. */
val DIFF_CALLBACK =
object : SimpleItemCallback<Item>() {
@@ -159,6 +159,7 @@ class SearchFragment : ListFragment<Music, FragmentSearchBinding>() {
// the query actually changes instead of once every re-creation event, but sadly
// that doesn't seem possible.
binding.searchRecycler.scrollToPosition(0)
searchAdapter.pokeDividers()
}
}
@@ -110,21 +110,21 @@ class SearchViewModel(application: Application) :
if (filterMode == null || filterMode == MusicMode.ARTISTS) {
library.artists.searchListImpl(query)?.let {
results.add(Header(R.string.lbl_artists, withDivider = results.isNotEmpty()))
results.add(Header(R.string.lbl_artists))
results.addAll(sort.artists(it))
}
}
if (filterMode == null || filterMode == MusicMode.ALBUMS) {
library.albums.searchListImpl(query)?.let {
results.add(Header(R.string.lbl_albums, withDivider = results.isNotEmpty()))
results.add(Header(R.string.lbl_albums))
results.addAll(sort.albums(it))
}
}
if (filterMode == null || filterMode == MusicMode.GENRES) {
library.genres.searchListImpl(query)?.let {
results.add(Header(R.string.lbl_genres, withDivider = results.isNotEmpty()))
results.add(Header(R.string.lbl_genres))
results.addAll(sort.genres(it))
}
}
@@ -133,7 +133,7 @@ class SearchViewModel(application: Application) :
library.songs
.searchListImpl(query) { q, song -> song.path.name.contains(q) }
?.let {
results.add(Header(R.string.lbl_songs, withDivider = results.isNotEmpty()))
results.add(Header(R.string.lbl_songs))
results.addAll(sort.songs(it))
}
}
@@ -87,6 +87,7 @@ abstract class BasePreferenceFragment(@XmlRes private val screen: Int) :
) =
super.onCreateRecyclerView(inflater, parent, savedInstanceState).apply {
clipToPadding = false
addItemDecoration(PreferenceHeaderItemDecoration(context))
setOnApplyWindowInsetsListener { _, insets ->
updatePadding(bottom = insets.systemBarInsetsCompat.bottom)
insets
@@ -0,0 +1,47 @@
/*
* Copyright (c) 2023 Auxio Project
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package org.oxycblt.auxio.settings.ui
import android.content.Context
import android.util.AttributeSet
import androidx.preference.PreferenceCategory
import androidx.preference.PreferenceGroupAdapter
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.google.android.material.divider.BackportMaterialDividerItemDecoration
import org.oxycblt.auxio.R
import org.oxycblt.auxio.util.logD
class PreferenceHeaderItemDecoration
@JvmOverloads
constructor(
context: Context,
attributeSet: AttributeSet? = null,
defStyleAttr: Int = R.attr.materialDividerStyle,
orientation: Int = LinearLayoutManager.VERTICAL
) : BackportMaterialDividerItemDecoration(context, attributeSet, defStyleAttr, orientation) {
override fun shouldDrawDivider(position: Int, adapter: RecyclerView.Adapter<*>?) =
try {
logD(position)
(adapter as PreferenceGroupAdapter).getItem(position) is PreferenceCategory
} catch (e: ClassCastException) {
false
} catch (e: IndexOutOfBoundsException) {
false
}
}
@@ -24,7 +24,7 @@ import android.view.View
import android.view.ViewGroup
import android.view.WindowInsets
import androidx.coordinatorlayout.widget.CoordinatorLayout
import com.google.android.material.bottomsheet.NeoBottomSheetBehavior
import com.google.android.material.bottomsheet.BackportBottomSheetBehavior
import org.oxycblt.auxio.R
import org.oxycblt.auxio.util.getDimen
import org.oxycblt.auxio.util.systemGestureInsetsCompat
@@ -37,7 +37,7 @@ import org.oxycblt.auxio.util.systemGestureInsetsCompat
* @author Alexander Capehart (OxygenCobalt)
*/
abstract class BaseBottomSheetBehavior<V : View>(context: Context, attributeSet: AttributeSet?) :
NeoBottomSheetBehavior<V>(context, attributeSet) {
BackportBottomSheetBehavior<V>(context, attributeSet) {
private var initalized = false
init {
@@ -22,7 +22,7 @@ import android.util.AttributeSet
import android.view.View
import android.view.WindowInsets
import androidx.coordinatorlayout.widget.CoordinatorLayout
import com.google.android.material.bottomsheet.NeoBottomSheetBehavior
import com.google.android.material.bottomsheet.BackportBottomSheetBehavior
import kotlin.math.abs
import org.oxycblt.auxio.util.coordinatorLayoutBehavior
import org.oxycblt.auxio.util.replaceSystemBarInsetsCompat
@@ -42,7 +42,7 @@ class BottomSheetContentBehavior<V : View>(context: Context, attributeSet: Attri
private var setup = false
override fun layoutDependsOn(parent: CoordinatorLayout, child: V, dependency: View): Boolean {
if (dependency.coordinatorLayoutBehavior is NeoBottomSheetBehavior) {
if (dependency.coordinatorLayoutBehavior is BackportBottomSheetBehavior) {
dep = dependency
return true
}
@@ -55,7 +55,7 @@ class BottomSheetContentBehavior<V : View>(context: Context, attributeSet: Attri
child: V,
dependency: View
): Boolean {
val behavior = dependency.coordinatorLayoutBehavior as NeoBottomSheetBehavior
val behavior = dependency.coordinatorLayoutBehavior as BackportBottomSheetBehavior
val consumed = behavior.calculateConsumedByBar()
if (consumed == Int.MIN_VALUE) {
return false
@@ -87,7 +87,7 @@ class BottomSheetContentBehavior<V : View>(context: Context, attributeSet: Attri
heightUsed: Int
): Boolean {
val dep = dep ?: return false
val behavior = dep.coordinatorLayoutBehavior as NeoBottomSheetBehavior
val behavior = dep.coordinatorLayoutBehavior as BackportBottomSheetBehavior
val consumed = behavior.calculateConsumedByBar()
if (consumed == Int.MIN_VALUE) {
return false
@@ -106,7 +106,7 @@ class BottomSheetContentBehavior<V : View>(context: Context, attributeSet: Attri
child.setOnApplyWindowInsetsListener { _, insets ->
lastInsets = insets
val dep = dep ?: return@setOnApplyWindowInsetsListener insets
val behavior = dep.coordinatorLayoutBehavior as NeoBottomSheetBehavior
val behavior = dep.coordinatorLayoutBehavior as BackportBottomSheetBehavior
val consumed = behavior.calculateConsumedByBar()
if (consumed == Int.MIN_VALUE) {
return@setOnApplyWindowInsetsListener insets
@@ -138,7 +138,7 @@ class BottomSheetContentBehavior<V : View>(context: Context, attributeSet: Attri
child.layout(0, 0, child.measuredWidth, child.measuredHeight)
}
private fun NeoBottomSheetBehavior<*>.calculateConsumedByBar(): Int {
private fun BackportBottomSheetBehavior<*>.calculateConsumedByBar(): Int {
val offset = calculateSlideOffset()
if (offset == Float.MIN_VALUE || peekHeight < 0) {
return Int.MIN_VALUE