list: rework sort dialog internals
Change the sort dialog to allow the following: 1. Allow null initial sort values in the dialog. 2. Make saving only possible if the sort changed.
This commit is contained in:
parent
ed08559b94
commit
a201cb15bc
1 changed files with 48 additions and 24 deletions
|
@ -22,6 +22,7 @@ import android.os.Bundle
|
|||
import android.view.LayoutInflater
|
||||
import androidx.core.view.updatePadding
|
||||
import androidx.recyclerview.widget.RecyclerView
|
||||
import com.google.android.material.button.MaterialButtonToggleGroup
|
||||
import org.oxycblt.auxio.R
|
||||
import org.oxycblt.auxio.databinding.DialogSortBinding
|
||||
import org.oxycblt.auxio.list.ClickableListListener
|
||||
|
@ -31,10 +32,12 @@ import org.oxycblt.auxio.ui.ViewBindingBottomSheetDialogFragment
|
|||
import org.oxycblt.auxio.util.systemBarInsetsCompat
|
||||
|
||||
abstract class SortDialog :
|
||||
ViewBindingBottomSheetDialogFragment<DialogSortBinding>(), ClickableListListener<Sort.Mode> {
|
||||
ViewBindingBottomSheetDialogFragment<DialogSortBinding>(),
|
||||
ClickableListListener<Sort.Mode>,
|
||||
MaterialButtonToggleGroup.OnButtonCheckedListener {
|
||||
private val modeAdapter = SortModeAdapter(this)
|
||||
|
||||
abstract fun getInitialSort(): Sort
|
||||
abstract fun getInitialSort(): Sort?
|
||||
|
||||
abstract fun applyChosenSort(sort: Sort)
|
||||
|
||||
|
@ -50,31 +53,19 @@ abstract class SortDialog :
|
|||
v.updatePadding(bottom = insets.systemBarInsetsCompat.bottom)
|
||||
insets
|
||||
}
|
||||
|
||||
binding.sortModeRecycler.adapter = modeAdapter
|
||||
|
||||
binding.sortDirectionGroup.addOnButtonCheckedListener(this)
|
||||
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))
|
||||
applyChosenSort(requireNotNull(getCurrentSort()))
|
||||
dismiss()
|
||||
}
|
||||
|
||||
// --- STATE SETUP ---
|
||||
val initial = getInitialSort()
|
||||
|
||||
if (initial != null) {
|
||||
modeAdapter.update(getModeChoices(), UpdateInstructions.Diff)
|
||||
modeAdapter.setSelected(initial.mode)
|
||||
|
||||
val directionId =
|
||||
when (initial.direction) {
|
||||
Sort.Direction.ASCENDING -> R.id.sort_direction_asc
|
||||
|
@ -82,8 +73,41 @@ abstract class SortDialog :
|
|||
}
|
||||
binding.sortDirectionGroup.check(directionId)
|
||||
}
|
||||
updateButtons()
|
||||
}
|
||||
|
||||
override fun onDestroyBinding(binding: DialogSortBinding) {
|
||||
super.onDestroyBinding(binding)
|
||||
binding.sortDirectionGroup.removeOnButtonCheckedListener(this)
|
||||
}
|
||||
|
||||
override fun onClick(item: Sort.Mode, viewHolder: RecyclerView.ViewHolder) {
|
||||
modeAdapter.setSelected(item)
|
||||
updateButtons()
|
||||
}
|
||||
|
||||
override fun onButtonChecked(
|
||||
group: MaterialButtonToggleGroup?,
|
||||
checkedId: Int,
|
||||
isChecked: Boolean
|
||||
) {
|
||||
updateButtons()
|
||||
}
|
||||
|
||||
private fun updateButtons() {
|
||||
val binding = requireBinding()
|
||||
binding.sortSave.isEnabled = getCurrentSort() != getInitialSort()
|
||||
}
|
||||
|
||||
private fun getCurrentSort(): Sort? {
|
||||
val initial = getInitialSort()
|
||||
val mode = modeAdapter.currentMode ?: initial?.mode ?: return null
|
||||
val direction =
|
||||
when (requireBinding().sortDirectionGroup.checkedButtonId) {
|
||||
R.id.sort_direction_asc -> Sort.Direction.ASCENDING
|
||||
R.id.sort_direction_dsc -> Sort.Direction.DESCENDING
|
||||
else -> initial?.direction ?: return null
|
||||
}
|
||||
return Sort(mode, direction)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue