From ed08559b94b24c2a644652ddc850ff93dc6314d7 Mon Sep 17 00:00:00 2001 From: Alexander Capehart Date: Tue, 25 Jul 2023 17:40:53 -0600 Subject: [PATCH] all: fix merge junk --- .../auxio/detail/AlbumDetailFragment.kt | 2 +- .../oxycblt/auxio/list/sort/SortAdapter.kt | 88 ----------- .../org/oxycblt/auxio/list/sort/SortDialog.kt | 140 ++++++------------ app/src/main/res/navigation/inner.xml | 3 - 4 files changed, 45 insertions(+), 188 deletions(-) delete mode 100644 app/src/main/java/org/oxycblt/auxio/list/sort/SortAdapter.kt diff --git a/app/src/main/java/org/oxycblt/auxio/detail/AlbumDetailFragment.kt b/app/src/main/java/org/oxycblt/auxio/detail/AlbumDetailFragment.kt index b89cfc9e4..d1b7199f8 100644 --- a/app/src/main/java/org/oxycblt/auxio/detail/AlbumDetailFragment.kt +++ b/app/src/main/java/org/oxycblt/auxio/detail/AlbumDetailFragment.kt @@ -192,7 +192,7 @@ class AlbumDetailFragment : override fun onShuffle() { playbackModel.shuffle(unlikelyToBeNull(detailModel.currentAlbum.value)) } - + override fun onOpenSortMenu() { findNavController().navigateSafe(AlbumDetailFragmentDirections.sort()) } diff --git a/app/src/main/java/org/oxycblt/auxio/list/sort/SortAdapter.kt b/app/src/main/java/org/oxycblt/auxio/list/sort/SortAdapter.kt deleted file mode 100644 index 16ac05fd2..000000000 --- a/app/src/main/java/org/oxycblt/auxio/list/sort/SortAdapter.kt +++ /dev/null @@ -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 . - */ - -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(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) { - 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() { - override fun areItemsTheSame(oldItem: Sort.Mode, newItem: Sort.Mode) = - oldItem == newItem - - override fun areContentsTheSame(oldItem: Sort.Mode, newItem: Sort.Mode) = - oldItem == newItem - } - } -} diff --git a/app/src/main/java/org/oxycblt/auxio/list/sort/SortDialog.kt b/app/src/main/java/org/oxycblt/auxio/list/sort/SortDialog.kt index f6391ce87..39e14d199 100644 --- a/app/src/main/java/org/oxycblt/auxio/list/sort/SortDialog.kt +++ b/app/src/main/java/org/oxycblt/auxio/list/sort/SortDialog.kt @@ -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 . */ -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 : - ViewBindingBottomSheetDialogFragment(), ClickableListListener { - protected abstract val menuModel: MenuViewModel - protected abstract val listModel: ListViewModel - private val menuAdapter = MenuItemAdapter(@Suppress("LeakingThis") this) +abstract class SortDialog : + ViewBindingBottomSheetDialogFragment(), ClickableListListener { + 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 + 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 - /** - * 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 - override fun onDestroyBinding(binding: DialogMenuBinding) { - super.onDestroyBinding(binding) - binding.menuName.isSelected = false - binding.menuInfo.isSelected = false - binding.menuOptionRecycler.adapter = null - } + binding.sortCancel.setOnClickListener { dismiss() } - private fun updateMenu(menu: Menu?) { - if (menu == null) { - logD("No menu to show, navigating away") - findNavController().navigateUp() - return + 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() } - @Suppress("UNCHECKED_CAST") val casted = menu as? M - check(casted != null) { "Unexpected menu instance ${menu::class.simpleName}" } + // --- STATE SETUP --- + val initial = getInitialSort() - // 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) + modeAdapter.update(getModeChoices(), UpdateInstructions.Diff) + modeAdapter.setSelected(initial.mode) - // 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 + val directionId = + when (initial.direction) { + Sort.Direction.ASCENDING -> R.id.sort_direction_asc + Sort.Direction.DESCENDING -> R.id.sort_direction_dsc } - menuAdapter.update(visible, UpdateInstructions.Diff) - - // Delegate to impl how to show music - updateMenu(requireBinding(), casted) + binding.sortDirectionGroup.check(directionId) } - 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) } -} \ No newline at end of file +} diff --git a/app/src/main/res/navigation/inner.xml b/app/src/main/res/navigation/inner.xml index a980bf595..d87710469 100644 --- a/app/src/main/res/navigation/inner.xml +++ b/app/src/main/res/navigation/inner.xml @@ -210,9 +210,6 @@ -