home: make toolbar collapse

Make the home toolbar collapse on scroll.

I was planning to implement this back in 2.0.0, but visual bugs stopped
me. Now, knowing how much space the the Toolbar + Tab combination takes
up on smaller devices, I think it would be better to have this in
general.
This commit is contained in:
OxygenCobalt
2022-06-08 09:31:57 -06:00
parent cc2561f20f
commit a6c321cfa5
25 changed files with 82 additions and 51 deletions
@@ -34,7 +34,9 @@ import androidx.navigation.fragment.findNavController
import androidx.recyclerview.widget.RecyclerView
import androidx.viewpager2.adapter.FragmentStateAdapter
import androidx.viewpager2.widget.ViewPager2
import com.google.android.material.appbar.AppBarLayout
import com.google.android.material.tabs.TabLayoutMediator
import kotlin.math.abs
import org.oxycblt.auxio.R
import org.oxycblt.auxio.databinding.FragmentHomeBinding
import org.oxycblt.auxio.home.list.AlbumListFragment
@@ -86,6 +88,18 @@ class HomeFragment : ViewBindingFragment<FragmentHomeBinding>(), Toolbar.OnMenuI
indexerModel.reindex()
}
binding.homeAppbar.apply {
addOnOffsetChangedListener(
AppBarLayout.OnOffsetChangedListener { _, offset ->
val range = binding.homeAppbar.totalScrollRange
binding.homeToolbar.alpha = 1f - (abs(offset.toFloat()) / (range.toFloat() / 2))
binding.homePager.updatePadding(
bottom = binding.homeAppbar.totalScrollRange + offset)
})
}
binding.homeToolbar.apply {
sortItem = menu.findItem(R.id.submenu_sorting)
setOnMenuItemClickListener(this@HomeFragment)
@@ -29,6 +29,7 @@ import org.oxycblt.auxio.ui.Item
import org.oxycblt.auxio.ui.MenuItemListener
import org.oxycblt.auxio.ui.NavigationViewModel
import org.oxycblt.auxio.ui.ViewBindingFragment
import org.oxycblt.auxio.util.applySpans
/**
* A Base [Fragment] implementing the base features shared across all list fragments in the home UI.
@@ -49,6 +50,7 @@ abstract class HomeListFragment<T : Item> :
override fun onBindingCreated(binding: FragmentHomeListBinding, savedInstanceState: Bundle?) {
binding.homeRecycler.popupProvider = this
binding.homeRecycler.listener = this
binding.homeRecycler.applySpans()
}
override fun onDestroyBinding(binding: FragmentHomeListBinding) {
@@ -36,8 +36,11 @@ import org.oxycblt.auxio.ui.Sort
import org.oxycblt.auxio.util.logD
/**
* The [ViewModel] for the search functionality
* The [ViewModel] for search functionality
* @author OxygenCobalt
*
* TODO: Add a context to this ViewModel, not because I want to, but because it just makes the code
* easier to work with.
*/
class SearchViewModel : ViewModel() {
private val musicStore = MusicStore.getInstance()
@@ -143,9 +146,9 @@ class SearchViewModel : ViewModel() {
*/
private fun <T : Music> List<T>.filterByOrNull(context: Context, value: String): List<T>? {
val filtered = filter {
// First see if the normal item name will work. If that fails, try the "normalized"
// [e.g all accented/unicode chars become latin chars] instead. Hopefully this
// shouldn't break other language's search functionality.
// Compare normalized names, which are names with unicode characters that are
// normalized to their non-unicode forms. This is just for quality-of-life,
// and I hope it doesn't bork search functionality for other languages.
it.resolveNameNormalized(context).contains(value, ignoreCase = true) ||
it.resolveNameNormalized(context).contains(value, ignoreCase = true)
}
@@ -74,23 +74,26 @@ class AboutFragment : ViewBindingFragment<FragmentAboutBinding>() {
private fun updateSongCount(songs: List<Song>) {
val binding = requireBinding()
binding.aboutSongCount.textSafe = getString(R.string.fmt_song_count, songs.size)
binding.aboutSongCount.textSafe = getString(R.string.fmt_lib_song_count, songs.size)
binding.aboutTotalDuration.textSafe =
getString(
R.string.fmt_total_duration, songs.sumOf { it.durationSecs }.formatDuration(false))
R.string.fmt_lib_total_duration,
songs.sumOf { it.durationSecs }.formatDuration(false))
}
private fun updateAlbumCount(albums: List<Album>) {
requireBinding().aboutAlbumCount.textSafe = getString(R.string.fmt_album_count, albums.size)
requireBinding().aboutAlbumCount.textSafe =
getString(R.string.fmt_lib_album_count, albums.size)
}
private fun updateArtistCount(artists: List<Artist>) {
requireBinding().aboutArtistCount.textSafe =
getString(R.string.fmt_artist_count, artists.size)
getString(R.string.fmt_lib_artist_count, artists.size)
}
private fun updateGenreCount(genres: List<Genre>) {
requireBinding().aboutGenreCount.textSafe = getString(R.string.fmt_genre_count, genres.size)
requireBinding().aboutGenreCount.textSafe =
getString(R.string.fmt_lib_genre_count, genres.size)
}
/** Go through the process of opening a [link] in a browser. */
@@ -218,8 +218,8 @@ class SyncBackingData<T>(adapter: RecyclerView.Adapter<*>, diffCallback: DiffUti
/**
* Like [AsyncListDiffer], but synchronous. This may seem like it would be inefficient, but in
* practice Auxio's lists tend to be small enough to the point where this does not matter,
* and situations that would be inefficient are ruled out with [SyncBackingData.replaceList].
* practice Auxio's lists tend to be small enough to the point where this does not matter, and
* situations that would be inefficient are ruled out with [SyncBackingData.replaceList].
*/
private class SyncListDiffer<T>(
adapter: RecyclerView.Adapter<*>,
@@ -301,7 +301,7 @@ private class SyncListDiffer<T>(
}
}
})
_currentList = newList
result.dispatchUpdatesTo(updateCallback)
}
@@ -39,6 +39,8 @@ import org.oxycblt.auxio.util.getDrawableSafe
* useful for the playback buttons, as at times highlighting them is not enough to differentiate
* them.
* @author OxygenCobalt
*
* TODO: Remove this for Material Buttons (I hope)
*/
class StyledImageButton
@JvmOverloads
@@ -183,7 +183,7 @@ class WidgetProvider : AppWidgetProvider() {
logW("No good widget layout found")
val minimum =
unlikelyToBeNull(views.minByOrNull { it.key.width * it.key.height }?.value)
unlikelyToBeNull(views.minByOrNull { it.key.width * it.key.height }).value
updateAppWidget(id, minimum)
}