home: reset selection when navigating

Reset the selection in the home view when navigating to other
selectable screens.

Without a unified Toolbar, this makes no sense.
This commit is contained in:
Alexander Capehart 2022-12-17 15:14:30 -07:00
parent c353ffd705
commit 3d03194878
No known key found for this signature in database
GPG key ID: 37DBE3621FE9AD47
16 changed files with 105 additions and 104 deletions

View file

@ -26,6 +26,8 @@ import androidx.core.view.ViewCompat
import androidx.core.view.isInvisible
import androidx.core.view.updatePadding
import androidx.fragment.app.activityViewModels
import androidx.navigation.NavController
import androidx.navigation.NavDestination
import androidx.navigation.findNavController
import androidx.navigation.fragment.findNavController
import com.google.android.material.bottomsheet.NeoBottomSheetBehavior
@ -43,15 +45,8 @@ import org.oxycblt.auxio.playback.queue.QueueSheetBehavior
import org.oxycblt.auxio.ui.MainNavigationAction
import org.oxycblt.auxio.ui.NavigationViewModel
import org.oxycblt.auxio.ui.fragment.ViewBindingFragment
import org.oxycblt.auxio.util.androidActivityViewModels
import org.oxycblt.auxio.util.collect
import org.oxycblt.auxio.util.collectImmediately
import org.oxycblt.auxio.util.context
import org.oxycblt.auxio.util.coordinatorLayoutBehavior
import org.oxycblt.auxio.util.getAttrColorCompat
import org.oxycblt.auxio.util.getDimen
import org.oxycblt.auxio.util.systemBarInsetsCompat
import org.oxycblt.auxio.util.unlikelyToBeNull
import org.oxycblt.auxio.ui.selection.SelectionViewModel
import org.oxycblt.auxio.util.*
/**
* A wrapper around the home fragment that shows the playback fragment and controls the more
@ -88,6 +83,7 @@ class MainFragment :
insets
}
// Send meaningful accessibility events for bottom sheets
ViewCompat.setAccessibilityPaneTitle(
binding.playbackSheet, context.getString(R.string.lbl_playback))
@ -222,6 +218,8 @@ class MainFragment :
when (action) {
is MainNavigationAction.Expand -> tryExpandAll()
is MainNavigationAction.Collapse -> tryCollapseAll()
// No need to reset selection despite navigating to another screen, as the
// main fragment destinations don't have selections
is MainNavigationAction.Directions -> findNavController().navigate(action.directions)
}

View file

@ -29,7 +29,6 @@ import androidx.core.view.iterator
import androidx.core.view.updatePadding
import androidx.fragment.app.Fragment
import androidx.fragment.app.activityViewModels
import androidx.fragment.app.viewModels
import androidx.navigation.fragment.findNavController
import androidx.recyclerview.widget.RecyclerView
import androidx.viewpager2.adapter.FragmentStateAdapter
@ -59,8 +58,8 @@ import org.oxycblt.auxio.music.system.Indexer
import org.oxycblt.auxio.playback.PlaybackViewModel
import org.oxycblt.auxio.ui.MainNavigationAction
import org.oxycblt.auxio.ui.NavigationViewModel
import org.oxycblt.auxio.ui.selection.SelectionViewModel
import org.oxycblt.auxio.ui.fragment.ViewBindingFragment
import org.oxycblt.auxio.ui.selection.SelectionViewModel
import org.oxycblt.auxio.util.*
/**
@ -73,7 +72,6 @@ class HomeFragment : ViewBindingFragment<FragmentHomeBinding>(), Toolbar.OnMenuI
private val homeModel: HomeViewModel by androidActivityViewModels()
private val musicModel: MusicViewModel by activityViewModels()
private val navModel: NavigationViewModel by activityViewModels()
// Makes no sense to share selections across screens
private val selectionModel: SelectionViewModel by activityViewModels()
// lifecycleObject builds this in the creation step, so doing this is okay.
@ -108,7 +106,8 @@ class HomeFragment : ViewBindingFragment<FragmentHomeBinding>(), Toolbar.OnMenuI
addOnOffsetChangedListener { _, offset ->
val range = binding.homeAppbar.totalScrollRange
binding.homeToolbarOverlay.alpha = 1f - (abs(offset.toFloat()) / (range.toFloat() / 2))
binding.homeToolbarOverlay.alpha =
1f - (abs(offset.toFloat()) / (range.toFloat() / 2))
binding.homeContent.updatePadding(
bottom = binding.homeAppbar.totalScrollRange + offset)
@ -116,9 +115,7 @@ class HomeFragment : ViewBindingFragment<FragmentHomeBinding>(), Toolbar.OnMenuI
}
binding.homeToolbarOverlay.registerListeners(
onExit = { selectionModel.consume() },
onMenuItemClick = this
)
onExit = { selectionModel.consume() }, onMenuItemClick = this)
binding.homeToolbar.setOnMenuItemClickListener(this@HomeFragment)
@ -187,6 +184,8 @@ class HomeFragment : ViewBindingFragment<FragmentHomeBinding>(), Toolbar.OnMenuI
R.id.action_search -> {
logD("Navigating to search")
// Reset selection (navigating to another selectable screen)
selectionModel.consume()
initAxisTransitions(MaterialSharedAxis.Z)
findNavController().navigate(HomeFragmentDirections.actionShowSearch())
}
@ -217,12 +216,10 @@ class HomeFragment : ViewBindingFragment<FragmentHomeBinding>(), Toolbar.OnMenuI
playbackModel.playNext(selectionModel.consume())
requireContext().showToast(R.string.lng_queue_added)
}
R.id.action_queue_add_selection -> {
playbackModel.addToQueue(selectionModel.consume())
requireContext().showToast(R.string.lng_queue_added)
}
else -> {
// Sorting option was selected, mark it as selected and update the mode
item.isChecked = true
@ -296,7 +293,7 @@ class HomeFragment : ViewBindingFragment<FragmentHomeBinding>(), Toolbar.OnMenuI
val binding = requireBinding()
val toolbarParams = binding.homeToolbarOverlay.layoutParams as AppBarLayout.LayoutParams
if (homeModel.tabs.size == 1) {
// A single tag makes the tab layout redundant, hide it and disable the collapsing
// A single tab makes the tab layout redundant, hide it and disable the collapsing
// behavior.
binding.homeTabs.isVisible = false
binding.homeAppbar.setExpanded(true, false)
@ -402,13 +399,12 @@ class HomeFragment : ViewBindingFragment<FragmentHomeBinding>(), Toolbar.OnMenuI
private fun updateSelection(selected: List<Music>) {
val binding = requireBinding()
if (binding.homeToolbarOverlay.updateSelectionAmount(selected.size) && selected.isNotEmpty()) {
if (binding.homeToolbarOverlay.updateSelectionAmount(selected.size) &&
selected.isNotEmpty()) {
logD("Significant selection occurred, expanding AppBar")
// Significant enough change where we want to expand the RecyclerView
binding.homeAppbar.expandWithRecycler(
binding.homePager.findViewById(
getRecyclerId(homeModel.currentTab.value))
)
binding.homePager.findViewById(getRecyclerId(homeModel.currentTab.value)))
}
}
@ -422,8 +418,9 @@ class HomeFragment : ViewBindingFragment<FragmentHomeBinding>(), Toolbar.OnMenuI
else -> return
}
// Reset selection (navigating to another selectable screen)
selectionModel.consume()
initAxisTransitions(MaterialSharedAxis.X)
findNavController().navigate(action)
}
@ -438,6 +435,17 @@ class HomeFragment : ViewBindingFragment<FragmentHomeBinding>(), Toolbar.OnMenuI
reenterTransition = MaterialSharedAxis(axis, false)
}
/**
* Returns the ID of a RecyclerView that the given [tab] contains
*/
private fun getRecyclerId(tab: MusicMode) =
when (tab) {
MusicMode.SONGS -> R.id.home_song_recycler
MusicMode.ALBUMS -> R.id.home_album_recycler
MusicMode.ARTISTS -> R.id.home_artist_recycler
MusicMode.GENRES -> R.id.home_genre_recycler
}
/**
* By default, ViewPager2's sensitivity is high enough to result in vertical scroll events being
* registered as horizontal scroll events. Reflect into the internal recyclerview and change the
@ -456,14 +464,6 @@ class HomeFragment : ViewBindingFragment<FragmentHomeBinding>(), Toolbar.OnMenuI
adapter = HomePagerAdapter()
}
private fun getRecyclerId(tab: MusicMode) =
when (tab) {
MusicMode.SONGS -> R.id.home_song_recycler
MusicMode.ALBUMS -> R.id.home_album_recycler
MusicMode.ARTISTS -> R.id.home_artist_recycler
MusicMode.GENRES -> R.id.home_genre_recycler
}
private inner class HomePagerAdapter :
FragmentStateAdapter(childFragmentManager, viewLifecycleOwner.lifecycle) {

View file

@ -24,11 +24,11 @@ import androidx.fragment.app.activityViewModels
import org.oxycblt.auxio.databinding.FragmentHomeListBinding
import org.oxycblt.auxio.home.HomeViewModel
import org.oxycblt.auxio.music.Music
import org.oxycblt.auxio.ui.selection.SelectionViewModel
import org.oxycblt.auxio.ui.fastscroll.FastScrollRecyclerView
import org.oxycblt.auxio.ui.fragment.MenuFragment
import org.oxycblt.auxio.ui.recycler.Item
import org.oxycblt.auxio.ui.recycler.MenuItemListener
import org.oxycblt.auxio.ui.selection.SelectionViewModel
import org.oxycblt.auxio.util.androidActivityViewModels
/**

View file

@ -27,9 +27,7 @@ import android.widget.FrameLayout
import android.widget.ImageView
import androidx.annotation.AttrRes
import androidx.core.view.updateMarginsRelative
import androidx.transition.TransitionManager
import com.google.android.material.shape.MaterialShapeDrawable
import com.google.android.material.transition.MaterialFade
import org.oxycblt.auxio.R
import org.oxycblt.auxio.music.Album
import org.oxycblt.auxio.music.Artist
@ -186,13 +184,12 @@ constructor(context: Context, attrs: AttributeSet? = null, @AttrRes defStyleAttr
fadeAnimator = null
}
fadeAnimator = ValueAnimator.ofFloat(selectionIndicator.alpha, targetAlpha).apply {
duration = targetDuration
addUpdateListener {
selectionIndicator.alpha = it.animatedValue as Float
fadeAnimator =
ValueAnimator.ofFloat(selectionIndicator.alpha, targetAlpha).apply {
duration = targetDuration
addUpdateListener { selectionIndicator.alpha = it.animatedValue as Float }
start()
}
start()
}
}
fun bind(song: Song) {

View file

@ -33,9 +33,9 @@ import org.oxycblt.auxio.util.getColorCompat
import org.oxycblt.auxio.util.getDrawableCompat
/**
* View that displays the playback indicator. Nominally emulates [StyledImageView], but
* relies on the existing ImageView infrastructure to achieve the same result while also
* allowing animation to work.
* View that displays the playback indicator. Nominally emulates [StyledImageView], but relies on
* the existing ImageView infrastructure to achieve the same result while also allowing animation to
* work.
* @author OxygenCobalt
*/
class IndicatorView

View file

@ -14,7 +14,7 @@
* 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.music.extractor
import android.content.Context

View file

@ -312,8 +312,8 @@ class Task(context: Context, private val raw: Song.Raw) {
}
/**
* Copies and sanitizes this string under the assumption that it is UTF-8. This should
* launder away any weird UTF-8 issues that ExoPlayer may cause.
* Copies and sanitizes this string under the assumption that it is UTF-8. This should launder
* away any weird UTF-8 issues that ExoPlayer may cause.
*/
private fun String.sanitize() = String(encodeToByteArray())
}

View file

@ -22,8 +22,8 @@ import com.google.android.exoplayer2.C
import com.google.android.exoplayer2.audio.AudioProcessor
import com.google.android.exoplayer2.audio.BaseAudioProcessor
import com.google.android.exoplayer2.metadata.Metadata
import com.google.android.exoplayer2.metadata.id3.TextInformationFrame
import com.google.android.exoplayer2.metadata.id3.InternalFrame
import com.google.android.exoplayer2.metadata.id3.TextInformationFrame
import com.google.android.exoplayer2.metadata.vorbis.VorbisComment
import java.nio.ByteBuffer
import kotlin.math.pow

View file

@ -42,8 +42,8 @@ import org.oxycblt.auxio.util.logD
/**
* The component managing the [MediaSessionCompat] instance, alongside the [NotificationComponent].
*
* Auxio does not directly rely on MediaSession, as it is extremely poorly designed. We instead
* just mirror the playback state into the media session.
* Auxio does not directly rely on MediaSession, as it is extremely poorly designed. We instead just
* mirror the playback state into the media session.
*
* @author OxygenCobalt
*/

View file

@ -27,8 +27,8 @@ import org.oxycblt.auxio.util.inflater
import org.oxycblt.auxio.util.logD
/**
* A wrapper around [Slider] that shows not only position and duration values, but also hacks
* in bounds checking to avoid app crashes if bad position input comes in.
* A wrapper around [Slider] that shows not only position and duration values, but also hacks in
* bounds checking to avoid app crashes if bad position input comes in.
*
* @author OxygenCobalt
*/

View file

@ -29,11 +29,10 @@ import androidx.core.content.res.ResourcesCompat
import androidx.recyclerview.widget.RecyclerView
import com.google.android.material.appbar.AppBarLayout
import org.oxycblt.auxio.util.coordinatorLayoutBehavior
import org.oxycblt.auxio.util.logD
/**
* An [AppBarLayout] that fixes several bugs with the default implementation where the lifted
* state will not properly respond to RecyclerView events.
* An [AppBarLayout] that fixes several bugs with the default implementation where the lifted state
* will not properly respond to RecyclerView events.
*
* **Note:** This layout relies on [AppBarLayout.liftOnScrollTargetViewId] to figure out what
* scrolling view to use. Failure to specify this will result in the layout not working.
@ -64,14 +63,11 @@ constructor(context: Context, attrs: AttributeSet? = null, @AttrRes defStyleAttr
}
/**
* Expand this app bar layout with the given recyclerview, preventing it from
* jumping around.
* Expand this app bar layout with the given recyclerview, preventing it from jumping around.
*/
fun expandWithRecycler(recycler: RecyclerView?) {
setExpanded(true)
recycler?.let {
addOnOffsetChangedListener(ExpansionHackListener(it))
}
recycler?.let { addOnOffsetChangedListener(ExpansionHackListener(it)) }
}
override fun onDetachedFromWindow() {
@ -104,20 +100,20 @@ constructor(context: Context, attrs: AttributeSet? = null, @AttrRes defStyleAttr
}
/**
* Hack to prevent RecyclerView jumping when the appbar expands.
* Adapted from Material Files:
* Hack to prevent RecyclerView jumping when the appbar expands. Adapted from Material Files:
* https://github.com/zhanghai/MaterialFiles/blob/master/app/src/main/java/me/zhanghai/android/files/ui/AppBarLayoutExpandHackListener.kt
*/
private class ExpansionHackListener(private val recycler: RecyclerView) : OnOffsetChangedListener {
private val offsetAnimationMaxEndTime = (AnimationUtils.currentAnimationTimeMillis()
+ 600)
private class ExpansionHackListener(private val recycler: RecyclerView) :
OnOffsetChangedListener {
private val offsetAnimationMaxEndTime = (AnimationUtils.currentAnimationTimeMillis() + 600)
private var lastVerticalOffset: Int? = null
override fun onOffsetChanged(appBarLayout: AppBarLayout, verticalOffset: Int) {
if (verticalOffset == 0
|| AnimationUtils.currentAnimationTimeMillis() > offsetAnimationMaxEndTime) {
// AppBarLayout crashes with IndexOutOfBoundsException when a non-last listener removes
if (verticalOffset == 0 ||
AnimationUtils.currentAnimationTimeMillis() > offsetAnimationMaxEndTime) {
// AppBarLayout crashes with IndexOutOfBoundsException when a non-last listener
// removes
// itself, so we have to do the removal asynchronously.
appBarLayout.postOnAnimation { appBarLayout.removeOnOffsetChangedListener(this) }
}

View file

@ -40,7 +40,6 @@ import org.oxycblt.auxio.util.getDimenSize
import org.oxycblt.auxio.util.getDrawableCompat
import org.oxycblt.auxio.util.isRtl
import org.oxycblt.auxio.util.isUnder
import org.oxycblt.auxio.util.logD
import org.oxycblt.auxio.util.systemBarInsetsCompat
/**

View file

@ -20,7 +20,6 @@ package org.oxycblt.auxio.ui.fragment
import android.view.View
import androidx.annotation.MenuRes
import androidx.appcompat.widget.PopupMenu
import androidx.core.view.children
import androidx.fragment.app.activityViewModels
import androidx.viewbinding.ViewBinding
import org.oxycblt.auxio.MainFragmentDirections
@ -184,9 +183,7 @@ abstract class MenuFragment<T : ViewBinding> : ViewBindingFragment<T>() {
}
private fun musicMenuImpl(anchor: View, @MenuRes menuRes: Int, onSelect: (Int) -> Boolean) {
menu(anchor, menuRes) {
setOnMenuItemClickListener { item -> onSelect(item.itemId) }
}
menu(anchor, menuRes) { setOnMenuItemClickListener { item -> onSelect(item.itemId) } }
}
/**

View file

@ -1,3 +1,20 @@
/*
* Copyright (c) 2022 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.ui.selection
import android.animation.ValueAnimator
@ -12,19 +29,20 @@ import org.oxycblt.auxio.R
import org.oxycblt.auxio.util.logD
/**
* A wrapper around a Toolbar that enables an overlaid toolbar showing information about
* an item selection.
* A wrapper around a Toolbar that enables an overlaid toolbar showing information about an item
* selection.
* @author OxygenCobalt
*/
class SelectionToolbarOverlay
@JvmOverloads
constructor(context: Context, attrs: AttributeSet? = null, @AttrRes defStyleAttr: Int = 0) :
FrameLayout(context, attrs, defStyleAttr){
FrameLayout(context, attrs, defStyleAttr) {
private lateinit var innerToolbar: MaterialToolbar
private val selectionToolbar = MaterialToolbar(context).apply {
inflateMenu(R.menu.menu_selection_actions)
setNavigationIcon(R.drawable.ic_close_24)
}
private val selectionToolbar =
MaterialToolbar(context).apply {
inflateMenu(R.menu.menu_selection_actions)
setNavigationIcon(R.drawable.ic_close_24)
}
private var fadeThroughAnimator: ValueAnimator? = null
@ -38,20 +56,18 @@ constructor(context: Context, attrs: AttributeSet? = null, @AttrRes defStyleAttr
addView(selectionToolbar)
}
/**
* Add listeners for the selection toolbar.
*/
fun registerListeners(onExit: OnClickListener,
onMenuItemClick: Toolbar.OnMenuItemClickListener) {
/** Add listeners for the selection toolbar. */
fun registerListeners(
onExit: OnClickListener,
onMenuItemClick: Toolbar.OnMenuItemClickListener
) {
selectionToolbar.apply {
setNavigationOnClickListener(onExit)
setOnMenuItemClickListener(onMenuItemClick)
}
}
/**
* Unregister listeners for this instance.
*/
/** Unregister listeners for this instance. */
fun unregisterListeners() {
selectionToolbar.apply {
setNavigationOnClickListener(null)
@ -60,8 +76,8 @@ constructor(context: Context, attrs: AttributeSet? = null, @AttrRes defStyleAttr
}
/**
* Update the selection amount in the selection Toolbar. This will animate the selection
* Toolbar into focus if there is now a selection to show.
* Update the selection amount in the selection Toolbar. This will animate the selection Toolbar
* into focus if there is now a selection to show.
*/
fun updateSelectionAmount(amount: Int): Boolean {
logD("Updating selection amount to $amount")
@ -83,11 +99,13 @@ constructor(context: Context, attrs: AttributeSet? = null, @AttrRes defStyleAttr
if (selectionVisible) {
targetInnerAlpha = 0f
targetSelectionAlpha = 1f
targetDuration = context.resources.getInteger(R.integer.anim_fade_enter_duration).toLong()
targetDuration =
context.resources.getInteger(R.integer.anim_fade_enter_duration).toLong()
} else {
targetInnerAlpha = 1f
targetSelectionAlpha = 0f
targetDuration = context.resources.getInteger(R.integer.anim_fade_exit_duration).toLong()
targetDuration =
context.resources.getInteger(R.integer.anim_fade_exit_duration).toLong()
}
if (innerToolbar.alpha == targetInnerAlpha &&
@ -107,18 +125,17 @@ constructor(context: Context, attrs: AttributeSet? = null, @AttrRes defStyleAttr
fadeThroughAnimator = null
}
fadeThroughAnimator = ValueAnimator.ofFloat(innerToolbar.alpha, targetInnerAlpha).apply {
duration = targetDuration
addUpdateListener {
changeToolbarAlpha(it.animatedValue as Float)
fadeThroughAnimator =
ValueAnimator.ofFloat(innerToolbar.alpha, targetInnerAlpha).apply {
duration = targetDuration
addUpdateListener { changeToolbarAlpha(it.animatedValue as Float) }
start()
}
start()
}
return true
}
private fun changeToolbarAlpha(innerAlpha: Float ) {
private fun changeToolbarAlpha(innerAlpha: Float) {
innerToolbar.apply {
alpha = innerAlpha
isInvisible = innerAlpha == 0f
@ -129,4 +146,4 @@ constructor(context: Context, attrs: AttributeSet? = null, @AttrRes defStyleAttr
isInvisible = innerAlpha == 1f
}
}
}
}

View file

@ -47,9 +47,7 @@ class SelectionViewModel : ViewModel() {
/** Clear and return all selected items. */
fun consume(): List<Music> {
return _selected.value.also {
_selected.value = listOf()
}
return _selected.value.also { _selected.value = listOf() }
}
override fun onCleared() {

View file

@ -202,7 +202,6 @@ fun <R> SQLiteDatabase.queryAll(tableName: String, block: (Cursor) -> R) =
// Note: WindowInsetsCompat and it's related methods cause too many issues.
// Use our own compat methods instead.
/**
* Resolve system bar insets in a version-aware manner. This can be used to apply padding to a view
* that properly follows all the changes that were made between Android 8-11.