home: re-add navigation code

Re-add support for the navToItem system [this time w/o any navbar
magic] and fix its associated issues.
This commit is contained in:
OxygenCobalt 2021-08-22 16:31:30 -06:00
parent 776776690d
commit 6c5a68c929
No known key found for this signature in database
GPG key ID: 37DBE3621FE9AD47
5 changed files with 63 additions and 20 deletions

View file

@ -29,7 +29,9 @@ import org.oxycblt.auxio.music.Song
import org.oxycblt.auxio.playback.PlaybackViewModel
/**
* A wrapper around the home
* A wrapper around the home fragment that shows the playback fragment and controls
* the more high-level navigation features.
* TODO: Re-add the nice playback slide in animation
*/
class MainFragment : Fragment() {
private val playbackModel: PlaybackViewModel by activityViewModels()

View file

@ -32,21 +32,29 @@ import com.google.android.material.tabs.TabLayoutMediator
import org.oxycblt.auxio.MainFragmentDirections
import org.oxycblt.auxio.R
import org.oxycblt.auxio.databinding.FragmentHomeBinding
import org.oxycblt.auxio.detail.DetailViewModel
import org.oxycblt.auxio.logD
import org.oxycblt.auxio.logE
import org.oxycblt.auxio.music.Album
import org.oxycblt.auxio.music.Artist
import org.oxycblt.auxio.music.Genre
import org.oxycblt.auxio.music.Song
import org.oxycblt.auxio.playback.PlaybackViewModel
import org.oxycblt.auxio.recycler.DisplayMode
import java.lang.Exception
/**
* The main "Launching Point" fragment of Auxio, allowing navigation to the detail
* views for each respective fragment.
* TODO: Re-add sorting (but new and improved)
* TODO: Add lift-on-scroll eventually
* TODO: Add lift-on-scroll eventually [when I can file a bug report or hack it into working]
* FIXME: Fix issue where for the toolbar will default to its collapsed state for basically no
* reason
* @author OxygenCobalt
*/
class HomeFragment : Fragment() {
private val playbackModel: PlaybackViewModel by activityViewModels()
private val detailModel: DetailViewModel by activityViewModels()
private val tabs = arrayOf(
DisplayMode.SHOW_SONGS, DisplayMode.SHOW_ALBUMS,
DisplayMode.SHOW_ARTISTS, DisplayMode.SHOW_GENRES
@ -63,6 +71,9 @@ class HomeFragment : Fragment() {
binding.lifecycleOwner = viewLifecycleOwner
// For some insane reason certain navigation actions will collapse the app bar
binding.homeAppbar.setExpanded(true)
binding.homeToolbar.setOnMenuItemClickListener { item ->
when (item.itemId) {
R.id.action_settings -> {
@ -89,7 +100,7 @@ class HomeFragment : Fragment() {
// Derived from: https://al-e-shevelev.medium.com/how-to-reduce-scroll-sensitivity-of-viewpager2-widget-87797ad02414
try {
val recycler = ViewPager2::class.java.getDeclaredField("mRecyclerView").apply {
val recycler = ViewPager2::class.java.getDeclaredField("mRecyclerView").run {
isAccessible = true
get(binding.homePager)
}
@ -98,7 +109,7 @@ class HomeFragment : Fragment() {
isAccessible = true
val slop = get(recycler) as Int
set(recycler, slop * 8)
set(recycler, slop * 3) // 3x seems to be the best fit here
}
} catch (e: Exception) {
logE("Unable to reduce ViewPager sensitivity")
@ -121,6 +132,29 @@ class HomeFragment : Fragment() {
playbackModel.setupPlayback(requireContext())
detailModel.navToItem.observe(viewLifecycleOwner) { item ->
when (item) {
is Song -> findNavController().navigate(
HomeFragmentDirections.actionShowAlbum(item.album.id)
)
is Album -> findNavController().navigate(
HomeFragmentDirections.actionShowAlbum(item.id)
)
is Artist -> findNavController().navigate(
HomeFragmentDirections.actionShowArtist(item.id)
)
is Genre -> findNavController().navigate(
HomeFragmentDirections.actionShowGenre(item.id)
)
else -> {
}
}
}
logD("Fragment Created.")
return binding.root

View file

@ -27,6 +27,7 @@ import androidx.fragment.app.viewModels
import androidx.navigation.fragment.findNavController
import androidx.recyclerview.widget.GridLayoutManager
import org.oxycblt.auxio.databinding.FragmentHomeListBinding
import org.oxycblt.auxio.logD
import org.oxycblt.auxio.music.Album
import org.oxycblt.auxio.music.Artist
import org.oxycblt.auxio.music.Genre
@ -88,17 +89,22 @@ class HomeListFragment : Fragment() {
// --- VIEWMODEL SETUP ---
val data = when (displayMode) {
val toObserve = when (displayMode) {
DisplayMode.SHOW_SONGS -> homeModel.songs
DisplayMode.SHOW_ALBUMS -> homeModel.albums
DisplayMode.SHOW_ARTISTS -> homeModel.artists
DisplayMode.SHOW_GENRES -> homeModel.genres
}
data.observe(viewLifecycleOwner) { data ->
// Make sure that this RecyclerView has data before startup
homeAdapter.updateData(toObserve.value!!)
toObserve.observe(viewLifecycleOwner) { data ->
homeAdapter.updateData(data)
}
logD("Fragment created")
return binding.root
}

View file

@ -72,34 +72,34 @@ class SearchViewModel : ViewModel() {
// A filter mode of null means to not filter at all.
if (mFilterMode == null || mFilterMode == DisplayMode.SHOW_SONGS) {
musicStore.songs.filterByOrNull(query)?.let { songs ->
results.add(Header(id = -2, name = context.getString(R.string.lbl_songs)))
results.addAll(songs)
if (mFilterMode == null || mFilterMode == DisplayMode.SHOW_ARTISTS) {
musicStore.artists.filterByOrNull(query)?.let { artists ->
results.add(Header(id = -1, name = context.getString(R.string.lbl_artists)))
results.addAll(artists)
}
}
if (mFilterMode == null || mFilterMode == DisplayMode.SHOW_ALBUMS) {
musicStore.albums.filterByOrNull(query)?.let { albums ->
results.add(Header(id = -3, name = context.getString(R.string.lbl_albums)))
results.add(Header(id = -2, name = context.getString(R.string.lbl_albums)))
results.addAll(albums)
}
}
if (mFilterMode == null || mFilterMode == DisplayMode.SHOW_ARTISTS) {
musicStore.artists.filterByOrNull(query)?.let { artists ->
results.add(Header(id = -4, name = context.getString(R.string.lbl_artists)))
results.addAll(artists)
}
}
if (mFilterMode == null || mFilterMode == DisplayMode.SHOW_GENRES) {
musicStore.genres.filterByOrNull(query)?.let { genres ->
results.add(Header(id = -5, name = context.getString(R.string.lbl_genres)))
results.add(Header(id = -3, name = context.getString(R.string.lbl_genres)))
results.addAll(genres)
}
}
if (mFilterMode == null || mFilterMode == DisplayMode.SHOW_SONGS) {
musicStore.songs.filterByOrNull(query)?.let { songs ->
results.add(Header(id = -4, name = context.getString(R.string.lbl_songs)))
results.addAll(songs)
}
}
mSearchResults.value = results
}
}

View file

@ -10,6 +10,7 @@
<!-- Base theme -->
<style name="Theme.Base" parent="Theme.Splash">
<!-- TODO: Improve the color contrast on the dark theme [colorOnSurface] -->
<!-- TODO: Migrate to liftOnScroll everywhere that is not HomeFragment -->
<!-- Colors -->
<item name="colorSurface">@color/surface</item>