list: add sort menu scaffold

Add the base dialog that will replace the prior sort menus in-app.
This commit is contained in:
Alexander Capehart
2023-07-25 16:04:02 -06:00
parent 5ed69e8b72
commit ec7f2d979a
46 changed files with 429 additions and 50 deletions
@@ -76,6 +76,7 @@ class AlbumDetailFragment :
override val listModel: ListViewModel by activityViewModels()
override val musicModel: MusicViewModel by activityViewModels()
override val playbackModel: PlaybackViewModel by activityViewModels()
// Information about what album to display is initially within the navigation arguments
// as a UID, as that is the only safe way to parcel an album.
private val args: AlbumDetailFragmentArgs by navArgs()
@@ -165,6 +165,8 @@ data class Sort(val mode: Mode, val direction: Direction) {
val intCode: Int
/** The item ID of this sort mode in menu resources. */
val itemId: Int
/** The string resource of the human-readable name of this sort mode. */
val stringRes: Int
/**
* Get a [Comparator] that sorts [Song]s according to this [Mode].
@@ -223,6 +225,9 @@ data class Sort(val mode: Mode, val direction: Direction) {
override val itemId: Int
get() = R.id.option_sort_name
override val stringRes: Int
get() = R.string.lbl_name
override fun getSongComparator(direction: Direction) =
compareByDynamic(direction, BasicComparator.SONG)
@@ -251,6 +256,9 @@ data class Sort(val mode: Mode, val direction: Direction) {
override val itemId: Int
get() = R.id.option_sort_album
override val stringRes: Int
get() = R.string.lbl_album
override fun getSongComparator(direction: Direction): Comparator<Song> =
MultiComparator(
compareByDynamic(direction, BasicComparator.ALBUM) { it.album },
@@ -271,6 +279,9 @@ data class Sort(val mode: Mode, val direction: Direction) {
override val itemId: Int
get() = R.id.option_sort_artist
override val stringRes: Int
get() = R.string.lbl_artist
override fun getSongComparator(direction: Direction): Comparator<Song> =
MultiComparator(
compareByDynamic(direction, ListComparator.ARTISTS) { it.artists },
@@ -300,6 +311,9 @@ data class Sort(val mode: Mode, val direction: Direction) {
override val itemId: Int
get() = R.id.option_sort_year
override val stringRes: Int
get() = R.string.lbl_date
override fun getSongComparator(direction: Direction): Comparator<Song> =
MultiComparator(
compareByDynamic(direction, NullableComparator.DATE_RANGE) { it.album.dates },
@@ -322,6 +336,9 @@ data class Sort(val mode: Mode, val direction: Direction) {
override val itemId: Int
get() = R.id.option_sort_duration
override val stringRes: Int
get() = R.string.lbl_duration
override fun getSongComparator(direction: Direction): Comparator<Song> =
MultiComparator(
compareByDynamic(direction) { it.durationMs }, compareBy(BasicComparator.SONG))
@@ -357,6 +374,9 @@ data class Sort(val mode: Mode, val direction: Direction) {
override val itemId: Int
get() = R.id.option_sort_count
override val stringRes: Int
get() = R.string.lbl_song_count
override fun getAlbumComparator(direction: Direction): Comparator<Album> =
MultiComparator(
compareByDynamic(direction) { it.songs.size }, compareBy(BasicComparator.ALBUM))
@@ -388,6 +408,9 @@ data class Sort(val mode: Mode, val direction: Direction) {
override val itemId: Int
get() = R.id.option_sort_disc
override val stringRes: Int
get() = R.string.lbl_disc
override fun getSongComparator(direction: Direction): Comparator<Song> =
MultiComparator(
compareByDynamic(direction, NullableComparator.DISC) { it.disc },
@@ -407,6 +430,9 @@ data class Sort(val mode: Mode, val direction: Direction) {
override val itemId: Int
get() = R.id.option_sort_track
override val stringRes: Int
get() = R.string.lbl_track
override fun getSongComparator(direction: Direction): Comparator<Song> =
MultiComparator(
compareBy(NullableComparator.DISC) { it.disc },
@@ -427,6 +453,9 @@ data class Sort(val mode: Mode, val direction: Direction) {
override val itemId: Int
get() = R.id.option_sort_date_added
override val stringRes: Int
get() = R.string.lbl_date_added
override fun getSongComparator(direction: Direction): Comparator<Song> =
MultiComparator(
compareByDynamic(direction) { it.dateAdded }, compareBy(BasicComparator.SONG))
@@ -44,7 +44,7 @@ class MenuItemAdapter(private val listener: ClickableListListener<MenuItem>) :
}
/**
* A [DialogRecyclerView.ViewHolder] that displays a list of menu options based on [MenuItem].
* A [DialogRecyclerView.ViewHolder] that displays a [MenuItem].
*
* @author Alexander Capehart (OxygenCobalt)
*/
@@ -54,7 +54,7 @@ class MenuItemViewHolder private constructor(private val binding: ItemMenuOption
* Bind new data to this instance.
*
* @param item The new [MenuItem] to bind.
* @param listener An [ClickableListListener] to bind interactions to.
* @param listener A [ClickableListListener] to bind interactions to.
*/
fun bind(item: MenuItem, listener: ClickableListListener<MenuItem>) {
listener.bind(item, this)
@@ -22,7 +22,6 @@ import android.content.Context
import android.util.AttributeSet
import android.view.View
import android.view.ViewGroup
import android.view.WindowInsets
import androidx.annotation.AttrRes
import androidx.core.view.isInvisible
import androidx.core.view.updatePadding
@@ -32,7 +31,6 @@ import com.google.android.material.divider.MaterialDivider
import org.oxycblt.auxio.R
import org.oxycblt.auxio.list.recycler.DialogRecyclerView.ViewHolder
import org.oxycblt.auxio.util.getDimenPixels
import org.oxycblt.auxio.util.systemBarInsetsCompat
/**
* A [RecyclerView] intended for use in Dialogs, adding features such as:
@@ -76,13 +74,6 @@ constructor(context: Context, attrs: AttributeSet? = null, @AttrRes defStyleAttr
invalidateDividers()
}
override fun onApplyWindowInsets(insets: WindowInsets): WindowInsets {
// Update the RecyclerView's padding such that the bottom insets are applied
// while still preserving bottom padding.
updatePadding(bottom = insets.systemBarInsetsCompat.bottom)
return insets
}
override fun onScrolled(dx: Int, dy: Int) {
super.onScrolled(dx, dy)
// Scroll event occurred, need to update the dividers.
@@ -0,0 +1,89 @@
/*
* Copyright (c) 2023 Auxio Project
* SortDialog.kt is part of Auxio.
*
* 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.sort
import android.os.Bundle
import android.view.LayoutInflater
import androidx.core.view.updatePadding
import androidx.recyclerview.widget.RecyclerView
import org.oxycblt.auxio.R
import org.oxycblt.auxio.databinding.DialogSortBinding
import org.oxycblt.auxio.list.ClickableListListener
import org.oxycblt.auxio.list.Sort
import org.oxycblt.auxio.list.adapter.UpdateInstructions
import org.oxycblt.auxio.ui.ViewBindingBottomSheetDialogFragment
import org.oxycblt.auxio.util.systemBarInsetsCompat
abstract class SortDialog :
ViewBindingBottomSheetDialogFragment<DialogSortBinding>(), ClickableListListener<Sort.Mode> {
private val modeAdapter = SortModeAdapter(this)
abstract fun getInitialSort(): Sort
abstract fun applyChosenSort(sort: Sort)
abstract fun getModeChoices(): List<Sort.Mode>
override fun onCreateBinding(inflater: LayoutInflater) = DialogSortBinding.inflate(inflater)
override fun onBindingCreated(binding: DialogSortBinding, savedInstanceState: Bundle?) {
super.onBindingCreated(binding, savedInstanceState)
// --- UI SETUP ---
binding.root.setOnApplyWindowInsetsListener { v, insets ->
v.updatePadding(bottom = insets.systemBarInsetsCompat.bottom)
insets
}
binding.sortModeRecycler.adapter = modeAdapter
binding.sortCancel.setOnClickListener { dismiss() }
binding.sortSave.setOnClickListener {
val initial = getInitialSort()
// FIXME: This won't work for the playlist sort dialog.
val mode = modeAdapter.currentMode ?: initial.mode
val direction =
when (binding.sortDirectionGroup.checkedButtonId) {
R.id.sort_direction_asc -> Sort.Direction.ASCENDING
R.id.sort_direction_dsc -> Sort.Direction.DESCENDING
else -> initial.direction
}
applyChosenSort(Sort(mode, direction))
dismiss()
}
// --- STATE SETUP ---
val initial = getInitialSort()
modeAdapter.update(getModeChoices(), UpdateInstructions.Diff)
modeAdapter.setSelected(initial.mode)
val directionId =
when (initial.direction) {
Sort.Direction.ASCENDING -> R.id.sort_direction_asc
Sort.Direction.DESCENDING -> R.id.sort_direction_dsc
}
binding.sortDirectionGroup.check(directionId)
}
override fun onClick(item: Sort.Mode, viewHolder: RecyclerView.ViewHolder) {
modeAdapter.setSelected(item)
}
}
@@ -0,0 +1,121 @@
/*
* Copyright (c) 2023 Auxio Project
* SortModeAdapter.kt is part of Auxio.
*
* 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.sort
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.DiffUtil
import org.oxycblt.auxio.databinding.ItemSortModeBinding
import org.oxycblt.auxio.list.ClickableListListener
import org.oxycblt.auxio.list.Sort
import org.oxycblt.auxio.list.adapter.FlexibleListAdapter
import org.oxycblt.auxio.list.recycler.DialogRecyclerView
import org.oxycblt.auxio.util.context
import org.oxycblt.auxio.util.inflater
/**
* A [FlexibleListAdapter] that displays a list of [Sort.Mode]s.
*
* @param listener A [ClickableListListener] to bind interactions to.
* @author Alexander Capehart (OxygenCobalt)
*/
class SortModeAdapter(private val listener: ClickableListListener<Sort.Mode>) :
FlexibleListAdapter<Sort.Mode, SortModeViewHolder>(SortModeViewHolder.DIFF_CALLBACK) {
/** The currently selected [Sort.Mode] item in this adapter. */
var currentMode: Sort.Mode? = null
private set
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int) =
SortModeViewHolder.from(parent)
override fun onBindViewHolder(holder: SortModeViewHolder, position: Int) {
throw NotImplementedError()
}
override fun onBindViewHolder(holder: SortModeViewHolder, position: Int, payload: List<Any>) {
val mode = getItem(position)
if (payload.isEmpty()) {
holder.bind(mode, listener)
}
holder.setSelected(mode == currentMode)
}
/**
* Select a new [Sort.Mode] option, unselecting the prior one. Does nothing if [mode] equals
* [currentMode].
*
* @param mode The new [Sort.Mode] to select. Should be in the adapter data.
*/
fun setSelected(mode: Sort.Mode) {
if (mode == currentMode) return
val oldMode = currentList.indexOf(currentMode)
val newMode = currentList.indexOf(mode)
currentMode = mode
if (oldMode > -1) {
notifyItemChanged(oldMode, PAYLOAD_SELECTION_CHANGED)
}
notifyItemChanged(newMode, PAYLOAD_SELECTION_CHANGED)
}
private companion object {
val PAYLOAD_SELECTION_CHANGED = Any()
}
}
/**
* A [DialogRecyclerView.ViewHolder] that displays a [Sort.Mode].
*
* @author Alexander Capehart (OxygenCobalt)
*/
class SortModeViewHolder private constructor(private val binding: ItemSortModeBinding) :
DialogRecyclerView.ViewHolder(binding.root) {
/**
* Bind new data to this instance.
*
* @param mode The new [Sort.Mode] to bind.
* @param listener A [ClickableListListener] to bind interactions to.
*/
fun bind(mode: Sort.Mode, listener: ClickableListListener<Sort.Mode>) {
listener.bind(mode, this)
binding.sortRadio.text = binding.context.getString(mode.stringRes)
}
/**
* Set if this view should be shown as selected or not.
*
* @param selected True if selected, false if not.
*/
fun setSelected(selected: Boolean) {
binding.sortRadio.isChecked = selected
}
companion object {
fun from(parent: View) =
SortModeViewHolder(ItemSortModeBinding.inflate(parent.context.inflater))
val DIFF_CALLBACK =
object : DiffUtil.ItemCallback<Sort.Mode>() {
override fun areItemsTheSame(oldItem: Sort.Mode, newItem: Sort.Mode) =
oldItem == newItem
override fun areContentsTheSame(oldItem: Sort.Mode, newItem: Sort.Mode) =
oldItem == newItem
}
}
}