all: fix merge junk
This commit is contained in:
parent
816ab04252
commit
ed08559b94
4 changed files with 45 additions and 188 deletions
|
|
@ -1,88 +0,0 @@
|
|||
/*
|
||||
* Copyright (c) 2023 Auxio Project
|
||||
* SortAdapter.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 androidx.recyclerview.widget.RecyclerView
|
||||
import org.oxycblt.auxio.databinding.ItemSortModeBinding
|
||||
import org.oxycblt.auxio.list.Sort
|
||||
import org.oxycblt.auxio.list.adapter.FlexibleListAdapter
|
||||
import org.oxycblt.auxio.util.inflater
|
||||
|
||||
class SortAdapter(var selectedMode: Sort.Mode) :
|
||||
FlexibleListAdapter<Sort.Mode, SortModeViewHolder>(SortModeViewHolder.DIFF_CALLBACK) {
|
||||
var currentlySelected = selectedMode
|
||||
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)
|
||||
}
|
||||
holder.setSelected(mode == currentlySelected)
|
||||
}
|
||||
|
||||
fun setSelected(mode: Sort.Mode) {
|
||||
if (mode == currentlySelected) return
|
||||
val oldMode = currentList.indexOf(currentlySelected)
|
||||
val newMode = currentList.indexOf(mode)
|
||||
currentlySelected = selectedMode
|
||||
notifyItemChanged(oldMode, PAYLOAD_SELECTION_CHANGED)
|
||||
notifyItemChanged(newMode, PAYLOAD_SELECTION_CHANGED)
|
||||
}
|
||||
|
||||
private companion object {
|
||||
val PAYLOAD_SELECTION_CHANGED = Any()
|
||||
}
|
||||
}
|
||||
|
||||
class SortModeViewHolder private constructor(private val binding: ItemSortModeBinding) :
|
||||
RecyclerView.ViewHolder(binding.root) {
|
||||
fun bind(mode: Sort.Mode) {
|
||||
// TODO: Add names to sort.mode
|
||||
binding.sortRadio.text = mode.toString()
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,6 +1,6 @@
|
|||
/*
|
||||
* Copyright (c) 2023 Auxio Project
|
||||
* MenuDialogFragment.kt is part of Auxio.
|
||||
* 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
|
||||
|
|
@ -16,126 +16,74 @@
|
|||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package org.oxycblt.auxio.list.menu
|
||||
package org.oxycblt.auxio.list.sort
|
||||
|
||||
import android.annotation.SuppressLint
|
||||
import android.os.Bundle
|
||||
import android.view.LayoutInflater
|
||||
import android.view.MenuInflater
|
||||
import android.view.MenuItem
|
||||
import androidx.appcompat.view.menu.MenuBuilder
|
||||
import androidx.core.view.children
|
||||
import androidx.navigation.fragment.findNavController
|
||||
import androidx.core.view.updatePadding
|
||||
import androidx.recyclerview.widget.RecyclerView
|
||||
import org.oxycblt.auxio.databinding.DialogMenuBinding
|
||||
import org.oxycblt.auxio.R
|
||||
import org.oxycblt.auxio.databinding.DialogSortBinding
|
||||
import org.oxycblt.auxio.list.ClickableListListener
|
||||
import org.oxycblt.auxio.list.ListViewModel
|
||||
import org.oxycblt.auxio.list.Menu
|
||||
import org.oxycblt.auxio.list.Sort
|
||||
import org.oxycblt.auxio.list.adapter.UpdateInstructions
|
||||
import org.oxycblt.auxio.ui.ViewBindingBottomSheetDialogFragment
|
||||
import org.oxycblt.auxio.util.collectImmediately
|
||||
import org.oxycblt.auxio.util.logD
|
||||
import org.oxycblt.auxio.util.systemBarInsetsCompat
|
||||
|
||||
/**
|
||||
* A [ViewBindingBottomSheetDialogFragment] that displays basic music information and a series of
|
||||
* options.
|
||||
*
|
||||
* @author Alexander Capehart (OxygenCobalt)
|
||||
*
|
||||
* TODO: Extend the amount of music info shown in the dialog
|
||||
*/
|
||||
abstract class MenuDialogFragment<M : Menu> :
|
||||
ViewBindingBottomSheetDialogFragment<DialogMenuBinding>(), ClickableListListener<MenuItem> {
|
||||
protected abstract val menuModel: MenuViewModel
|
||||
protected abstract val listModel: ListViewModel
|
||||
private val menuAdapter = MenuItemAdapter(@Suppress("LeakingThis") this)
|
||||
abstract class SortDialog :
|
||||
ViewBindingBottomSheetDialogFragment<DialogSortBinding>(), ClickableListListener<Sort.Mode> {
|
||||
private val modeAdapter = SortModeAdapter(this)
|
||||
|
||||
abstract val parcel: Menu.Parcel
|
||||
abstract fun getInitialSort(): Sort
|
||||
|
||||
/**
|
||||
* Get the options to disable in the context of the currently shown [M].
|
||||
*
|
||||
* @param menu The currently-shown menu [M].
|
||||
*/
|
||||
abstract fun getDisabledItemIds(menu: M): Set<Int>
|
||||
abstract fun applyChosenSort(sort: Sort)
|
||||
|
||||
/**
|
||||
* Update the displayed information about the currently shown [M].
|
||||
*
|
||||
* @param binding The [DialogMenuBinding] to bind information to.
|
||||
* @param menu The currently-shown menu [M].
|
||||
*/
|
||||
abstract fun updateMenu(binding: DialogMenuBinding, menu: M)
|
||||
abstract fun getModeChoices(): List<Sort.Mode>
|
||||
|
||||
/**
|
||||
* Forward the clicked [MenuItem] to it's corresponding handler in another module.
|
||||
*
|
||||
* @param item The [MenuItem] that was clicked.
|
||||
* @param menu The currently-shown menu [M].
|
||||
*/
|
||||
abstract fun onClick(item: MenuItem, menu: M)
|
||||
override fun onCreateBinding(inflater: LayoutInflater) = DialogSortBinding.inflate(inflater)
|
||||
|
||||
override fun onCreateBinding(inflater: LayoutInflater) = DialogMenuBinding.inflate(inflater)
|
||||
|
||||
override fun onBindingCreated(binding: DialogMenuBinding, savedInstanceState: Bundle?) {
|
||||
override fun onBindingCreated(binding: DialogSortBinding, savedInstanceState: Bundle?) {
|
||||
super.onBindingCreated(binding, savedInstanceState)
|
||||
|
||||
// --- UI SETUP ---
|
||||
binding.menuName.isSelected = true
|
||||
binding.menuInfo.isSelected = true
|
||||
binding.menuOptionRecycler.apply {
|
||||
adapter = menuAdapter
|
||||
itemAnimator = null
|
||||
binding.root.setOnApplyWindowInsetsListener { v, insets ->
|
||||
v.updatePadding(bottom = insets.systemBarInsetsCompat.bottom)
|
||||
insets
|
||||
}
|
||||
|
||||
// --- VIEWMODEL SETUP ---
|
||||
listModel.menu.consume()
|
||||
menuModel.setMenu(parcel)
|
||||
collectImmediately(menuModel.currentMenu, this::updateMenu)
|
||||
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()
|
||||
}
|
||||
|
||||
override fun onDestroyBinding(binding: DialogMenuBinding) {
|
||||
super.onDestroyBinding(binding)
|
||||
binding.menuName.isSelected = false
|
||||
binding.menuInfo.isSelected = false
|
||||
binding.menuOptionRecycler.adapter = null
|
||||
// --- 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)
|
||||
}
|
||||
|
||||
private fun updateMenu(menu: Menu?) {
|
||||
if (menu == null) {
|
||||
logD("No menu to show, navigating away")
|
||||
findNavController().navigateUp()
|
||||
return
|
||||
}
|
||||
|
||||
@Suppress("UNCHECKED_CAST") val casted = menu as? M
|
||||
check(casted != null) { "Unexpected menu instance ${menu::class.simpleName}" }
|
||||
|
||||
// We need to inflate the menu on every menu update since it might have changed
|
||||
// what options are available (ex. if an artist with no songs has had new songs added).
|
||||
// Since we don't have (and don't want) a dummy view to inflate this menu, just
|
||||
// depend on the AndroidX Toolbar internal API and hope for the best.
|
||||
@SuppressLint("RestrictedApi") val builder = MenuBuilder(requireContext())
|
||||
MenuInflater(requireContext()).inflate(casted.res, builder)
|
||||
|
||||
// Disable any menu options as specified by the impl
|
||||
val disabledIds = getDisabledItemIds(casted)
|
||||
val visible =
|
||||
builder.children.mapTo(mutableListOf()) {
|
||||
it.isEnabled = !disabledIds.contains(it.itemId)
|
||||
it
|
||||
}
|
||||
menuAdapter.update(visible, UpdateInstructions.Diff)
|
||||
|
||||
// Delegate to impl how to show music
|
||||
updateMenu(requireBinding(), casted)
|
||||
}
|
||||
|
||||
final override fun onClick(item: MenuItem, viewHolder: RecyclerView.ViewHolder) {
|
||||
// All option selections close the dialog currently.
|
||||
// TODO: This should change if the app is 100% migrated to menu dialogs
|
||||
findNavController().navigateUp()
|
||||
// Delegate to impl on how to handle items
|
||||
@Suppress("UNCHECKED_CAST") onClick(item, menuModel.currentMenu.value as M)
|
||||
override fun onClick(item: Sort.Mode, viewHolder: RecyclerView.ViewHolder) {
|
||||
modeAdapter.setSelected(item)
|
||||
}
|
||||
}
|
||||
|
|
@ -210,9 +210,6 @@
|
|||
<action
|
||||
android:id="@+id/play_from_genre"
|
||||
app:destination="@id/play_from_genre_dialog" />
|
||||
<action
|
||||
android:id="@+id/sort"
|
||||
app:destination="@id/sort_dialog" />
|
||||
</fragment>
|
||||
|
||||
<dialog
|
||||
|
|
|
|||
Loading…
Reference in a new issue