Add navigation to LibraryFragment
Add navigiation to LibraryFragment. This navigation is nested so that one can still move through the ViewPager, which will certainly have no problems later on. /s
This commit is contained in:
@@ -0,0 +1,37 @@
|
||||
package org.oxycblt.auxio.detail
|
||||
|
||||
import android.os.Bundle
|
||||
import android.util.Log
|
||||
import android.view.LayoutInflater
|
||||
import android.view.View
|
||||
import android.view.ViewGroup
|
||||
import androidx.databinding.DataBindingUtil
|
||||
import androidx.fragment.app.Fragment
|
||||
import androidx.fragment.app.activityViewModels
|
||||
import org.oxycblt.auxio.R
|
||||
import org.oxycblt.auxio.databinding.FragmentArtistDetailBinding
|
||||
import org.oxycblt.auxio.music.MusicViewModel
|
||||
|
||||
class ArtistDetailFragment : Fragment() {
|
||||
|
||||
override fun onCreateView(
|
||||
inflater: LayoutInflater,
|
||||
container: ViewGroup?,
|
||||
savedInstanceState: Bundle?
|
||||
): View? {
|
||||
val binding = DataBindingUtil.inflate<FragmentArtistDetailBinding>(
|
||||
inflater, R.layout.fragment_artist_detail, container, false
|
||||
)
|
||||
|
||||
// I honestly don't want to turn of the any data classes into a parcelables due to how
|
||||
// many lists they store, so just pick up the artist id and find it from musicModel.
|
||||
val musicModel: MusicViewModel by activityViewModels()
|
||||
val artistId = ArtistDetailFragmentArgs.fromBundle(requireArguments()).artistId
|
||||
|
||||
binding.artist = musicModel.artists.value?.find { it.id == artistId }
|
||||
|
||||
Log.d(this::class.simpleName, "Fragment created.")
|
||||
|
||||
return binding.root
|
||||
}
|
||||
}
|
||||
@@ -5,21 +5,17 @@ import android.util.Log
|
||||
import android.view.LayoutInflater
|
||||
import android.view.View
|
||||
import android.view.ViewGroup
|
||||
import androidx.activity.OnBackPressedCallback
|
||||
import androidx.databinding.DataBindingUtil
|
||||
import androidx.fragment.app.Fragment
|
||||
import androidx.fragment.app.activityViewModels
|
||||
import androidx.navigation.NavController
|
||||
import androidx.navigation.fragment.NavHostFragment
|
||||
import org.oxycblt.auxio.R
|
||||
import org.oxycblt.auxio.databinding.FragmentLibraryBinding
|
||||
import org.oxycblt.auxio.library.adapters.ArtistAdapter
|
||||
import org.oxycblt.auxio.music.MusicViewModel
|
||||
import org.oxycblt.auxio.recycler.ClickListener
|
||||
import org.oxycblt.auxio.recycler.applyDivider
|
||||
|
||||
class LibraryFragment : Fragment() {
|
||||
|
||||
private val musicModel: MusicViewModel by activityViewModels {
|
||||
MusicViewModel.Factory(requireActivity().application)
|
||||
}
|
||||
private var navController: NavController? = null
|
||||
|
||||
override fun onCreateView(
|
||||
inflater: LayoutInflater,
|
||||
@@ -30,17 +26,44 @@ class LibraryFragment : Fragment() {
|
||||
inflater, R.layout.fragment_library, container, false
|
||||
)
|
||||
|
||||
binding.libraryRecycler.adapter = ArtistAdapter(
|
||||
musicModel.artists.value!!,
|
||||
ClickListener { artist ->
|
||||
Log.d(this::class.simpleName, artist.name)
|
||||
}
|
||||
requireActivity().onBackPressedDispatcher.addCallback(
|
||||
viewLifecycleOwner, callback
|
||||
)
|
||||
binding.libraryRecycler.applyDivider()
|
||||
binding.libraryRecycler.setHasFixedSize(true)
|
||||
|
||||
val navHost = childFragmentManager.findFragmentById(R.id.nav_host) as? NavHostFragment
|
||||
navController = navHost?.navController
|
||||
|
||||
Log.d(this::class.simpleName, "Fragment created.")
|
||||
|
||||
return binding.root
|
||||
}
|
||||
|
||||
override fun onResume() {
|
||||
super.onResume()
|
||||
|
||||
callback.isEnabled = true
|
||||
}
|
||||
|
||||
override fun onPause() {
|
||||
super.onPause()
|
||||
callback.isEnabled = false
|
||||
}
|
||||
|
||||
val callback = object : OnBackPressedCallback(false) {
|
||||
|
||||
override fun handleOnBackPressed() {
|
||||
|
||||
// If at the root of the navigation, perform onBackPressed from the main activity.
|
||||
if (navController?.currentDestination?.id == navController?.graph?.startDestination) {
|
||||
// Disable the callback as it will get caught in an infinite loop otherwise.
|
||||
isEnabled = false
|
||||
|
||||
requireActivity().onBackPressed()
|
||||
|
||||
isEnabled = true
|
||||
} else {
|
||||
navController?.navigateUp()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,49 @@
|
||||
package org.oxycblt.auxio.library
|
||||
|
||||
import android.os.Bundle
|
||||
import android.util.Log
|
||||
import android.view.LayoutInflater
|
||||
import android.view.View
|
||||
import android.view.ViewGroup
|
||||
import androidx.databinding.DataBindingUtil
|
||||
import androidx.fragment.app.Fragment
|
||||
import androidx.fragment.app.activityViewModels
|
||||
import androidx.navigation.fragment.findNavController
|
||||
import org.oxycblt.auxio.R
|
||||
import org.oxycblt.auxio.databinding.FragmentLibraryListBinding
|
||||
import org.oxycblt.auxio.library.adapters.ArtistAdapter
|
||||
import org.oxycblt.auxio.music.MusicViewModel
|
||||
import org.oxycblt.auxio.recycler.ClickListener
|
||||
import org.oxycblt.auxio.recycler.applyDivider
|
||||
|
||||
class LibraryListFragment : Fragment() {
|
||||
|
||||
private val musicModel: MusicViewModel by activityViewModels {
|
||||
MusicViewModel.Factory(requireActivity().application)
|
||||
}
|
||||
|
||||
override fun onCreateView(
|
||||
inflater: LayoutInflater,
|
||||
container: ViewGroup?,
|
||||
savedInstanceState: Bundle?
|
||||
): View? {
|
||||
val binding = DataBindingUtil.inflate<FragmentLibraryListBinding>(
|
||||
inflater, R.layout.fragment_library_list, container, false
|
||||
)
|
||||
|
||||
binding.libraryRecycler.adapter = ArtistAdapter(
|
||||
musicModel.artists.value!!,
|
||||
ClickListener { artist ->
|
||||
findNavController().navigate(
|
||||
LibraryListFragmentDirections.actionShowArtist(artist.id)
|
||||
)
|
||||
}
|
||||
)
|
||||
binding.libraryRecycler.applyDivider()
|
||||
binding.libraryRecycler.setHasFixedSize(true)
|
||||
|
||||
Log.d(this::class.simpleName, "Fragment created.")
|
||||
|
||||
return binding.root
|
||||
}
|
||||
}
|
||||
@@ -3,7 +3,7 @@ package org.oxycblt.auxio.library.adapters
|
||||
import android.view.LayoutInflater
|
||||
import android.view.ViewGroup
|
||||
import androidx.recyclerview.widget.RecyclerView
|
||||
import org.oxycblt.auxio.databinding.AlbumItemBinding
|
||||
import org.oxycblt.auxio.databinding.ItemAlbumBinding
|
||||
import org.oxycblt.auxio.music.models.Album
|
||||
import org.oxycblt.auxio.recycler.ClickListener
|
||||
import org.oxycblt.auxio.recycler.viewholders.AlbumViewHolder
|
||||
@@ -16,7 +16,7 @@ class AlbumAdapter(
|
||||
override fun getItemCount(): Int = data.size
|
||||
|
||||
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): AlbumViewHolder {
|
||||
val binding = AlbumItemBinding.inflate(LayoutInflater.from(parent.context))
|
||||
val binding = ItemAlbumBinding.inflate(LayoutInflater.from(parent.context))
|
||||
|
||||
// Force the item to *actually* be the screen width so ellipsizing can work.
|
||||
binding.root.layoutParams = RecyclerView.LayoutParams(
|
||||
|
||||
@@ -3,7 +3,7 @@ package org.oxycblt.auxio.library.adapters
|
||||
import android.view.LayoutInflater
|
||||
import android.view.ViewGroup
|
||||
import androidx.recyclerview.widget.RecyclerView
|
||||
import org.oxycblt.auxio.databinding.ArtistItemBinding
|
||||
import org.oxycblt.auxio.databinding.ItemArtistBinding
|
||||
import org.oxycblt.auxio.music.models.Artist
|
||||
import org.oxycblt.auxio.recycler.ClickListener
|
||||
import org.oxycblt.auxio.recycler.viewholders.ArtistViewHolder
|
||||
@@ -16,7 +16,7 @@ class ArtistAdapter(
|
||||
override fun getItemCount(): Int = data.size
|
||||
|
||||
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ArtistViewHolder {
|
||||
val binding = ArtistItemBinding.inflate(LayoutInflater.from(parent.context))
|
||||
val binding = ItemArtistBinding.inflate(LayoutInflater.from(parent.context))
|
||||
|
||||
// Force the item to *actually* be the screen width so ellipsizing can work.
|
||||
binding.root.layoutParams = RecyclerView.LayoutParams(
|
||||
|
||||
@@ -20,7 +20,7 @@ import org.oxycblt.auxio.music.processing.MusicLoader
|
||||
import org.oxycblt.auxio.music.processing.MusicLoaderResponse
|
||||
import org.oxycblt.auxio.music.processing.MusicSorter
|
||||
|
||||
// Storage for music data.
|
||||
// ViewModel for music storage. May also be a god object.
|
||||
class MusicViewModel(private val app: Application) : ViewModel() {
|
||||
|
||||
// Coroutine
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
package org.oxycblt.auxio.music.models
|
||||
|
||||
// Abstraction for mAlbums
|
||||
// Abstraction for albums
|
||||
data class Artist(
|
||||
val id: Long = 0,
|
||||
var name: String = "",
|
||||
|
||||
@@ -1,12 +1,12 @@
|
||||
package org.oxycblt.auxio.recycler.viewholders
|
||||
|
||||
import androidx.recyclerview.widget.RecyclerView
|
||||
import org.oxycblt.auxio.databinding.AlbumItemBinding
|
||||
import org.oxycblt.auxio.databinding.ItemAlbumBinding
|
||||
import org.oxycblt.auxio.music.models.Album
|
||||
|
||||
// Generic ViewHolder for an album
|
||||
class AlbumViewHolder(
|
||||
private val binding: AlbumItemBinding
|
||||
private val binding: ItemAlbumBinding
|
||||
) : RecyclerView.ViewHolder(binding.root) {
|
||||
|
||||
// Bind the view w/new data
|
||||
|
||||
@@ -1,12 +1,12 @@
|
||||
package org.oxycblt.auxio.recycler.viewholders
|
||||
|
||||
import androidx.recyclerview.widget.RecyclerView
|
||||
import org.oxycblt.auxio.databinding.ArtistItemBinding
|
||||
import org.oxycblt.auxio.databinding.ItemArtistBinding
|
||||
import org.oxycblt.auxio.music.models.Artist
|
||||
|
||||
// Generic ViewHolder for an album
|
||||
class ArtistViewHolder(
|
||||
private val binding: ArtistItemBinding
|
||||
private val binding: ItemArtistBinding
|
||||
) : RecyclerView.ViewHolder(binding.root) {
|
||||
|
||||
// Bind the view w/new data
|
||||
|
||||
@@ -1,12 +1,12 @@
|
||||
package org.oxycblt.auxio.recycler.viewholders
|
||||
|
||||
import androidx.recyclerview.widget.RecyclerView
|
||||
import org.oxycblt.auxio.databinding.SongItemBinding
|
||||
import org.oxycblt.auxio.databinding.ItemSongBinding
|
||||
import org.oxycblt.auxio.music.models.Song
|
||||
|
||||
// Generic ViewHolder for a song
|
||||
class SongViewHolder(
|
||||
private val binding: SongItemBinding
|
||||
private val binding: ItemSongBinding
|
||||
) : RecyclerView.ViewHolder(binding.root) {
|
||||
|
||||
// Bind the view w/new data
|
||||
|
||||
@@ -3,7 +3,7 @@ package org.oxycblt.auxio.songs
|
||||
import android.view.LayoutInflater
|
||||
import android.view.ViewGroup
|
||||
import androidx.recyclerview.widget.RecyclerView
|
||||
import org.oxycblt.auxio.databinding.SongItemBinding
|
||||
import org.oxycblt.auxio.databinding.ItemSongBinding
|
||||
import org.oxycblt.auxio.music.models.Song
|
||||
import org.oxycblt.auxio.recycler.ClickListener
|
||||
import org.oxycblt.auxio.recycler.viewholders.SongViewHolder
|
||||
@@ -16,7 +16,7 @@ class SongAdapter(
|
||||
override fun getItemCount(): Int = data.size
|
||||
|
||||
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): SongViewHolder {
|
||||
val binding = SongItemBinding.inflate(LayoutInflater.from(parent.context))
|
||||
val binding = ItemSongBinding.inflate(LayoutInflater.from(parent.context))
|
||||
|
||||
// Force the item to *actually* be the screen width so ellipsizing can work.
|
||||
binding.root.layoutParams = RecyclerView.LayoutParams(
|
||||
|
||||
Reference in New Issue
Block a user