Make ClickListener single-use
Change the RecyclerView click listener to only be able to be called once in order to eliminate the ViewModel isAlreadyNavigating variables.
This commit is contained in:
parent
8b0c2a7d2e
commit
3a44ce7e43
6 changed files with 28 additions and 62 deletions
|
@ -47,7 +47,9 @@ class ArtistDetailFragment : Fragment() {
|
|||
|
||||
val albumAdapter = DetailAlbumAdapter(
|
||||
ClickListener {
|
||||
navToAlbum(it)
|
||||
findNavController().navigate(
|
||||
ArtistDetailFragmentDirections.actionShowAlbum(it.id, false)
|
||||
)
|
||||
}
|
||||
)
|
||||
|
||||
|
@ -90,21 +92,4 @@ class ArtistDetailFragment : Fragment() {
|
|||
|
||||
return binding.root
|
||||
}
|
||||
|
||||
override fun onResume() {
|
||||
super.onResume()
|
||||
|
||||
detailModel.isAlreadyNavigating = false
|
||||
}
|
||||
|
||||
private fun navToAlbum(album: Album) {
|
||||
// Don't navigate if an item already has been selected.
|
||||
if (!detailModel.isAlreadyNavigating) {
|
||||
detailModel.isAlreadyNavigating = true
|
||||
|
||||
findNavController().navigate(
|
||||
ArtistDetailFragmentDirections.actionShowAlbum(album.id, false)
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -9,8 +9,6 @@ import org.oxycblt.auxio.music.Genre
|
|||
import org.oxycblt.auxio.recycler.SortMode
|
||||
|
||||
class DetailViewModel : ViewModel() {
|
||||
var isAlreadyNavigating = false
|
||||
|
||||
private val mGenreSortMode = MutableLiveData(SortMode.ALPHA_DOWN)
|
||||
val genreSortMode: LiveData<SortMode> get() = mGenreSortMode
|
||||
|
||||
|
|
|
@ -47,7 +47,9 @@ class GenreDetailFragment : Fragment() {
|
|||
|
||||
val albumAdapter = DetailArtistAdapter(
|
||||
ClickListener {
|
||||
navToArtist(it)
|
||||
findNavController().navigate(
|
||||
GenreDetailFragmentDirections.actionShowArtist(it.id)
|
||||
)
|
||||
}
|
||||
)
|
||||
|
||||
|
@ -88,21 +90,4 @@ class GenreDetailFragment : Fragment() {
|
|||
|
||||
return binding.root
|
||||
}
|
||||
|
||||
override fun onResume() {
|
||||
super.onResume()
|
||||
|
||||
detailModel.isAlreadyNavigating = false
|
||||
}
|
||||
|
||||
private fun navToArtist(artist: Artist) {
|
||||
// Don't navigate if an item already has been selected.
|
||||
if (!detailModel.isAlreadyNavigating) {
|
||||
detailModel.isAlreadyNavigating = true
|
||||
|
||||
findNavController().navigate(
|
||||
GenreDetailFragmentDirections.actionShowArtist(artist.id)
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -87,28 +87,17 @@ class LibraryFragment : Fragment() {
|
|||
return binding.root
|
||||
}
|
||||
|
||||
override fun onResume() {
|
||||
super.onResume()
|
||||
|
||||
libraryModel.isAlreadyNavigating = false
|
||||
}
|
||||
|
||||
private fun navToItem(baseModel: BaseModel) {
|
||||
// Don't navigate if an item has already been selected
|
||||
if (!libraryModel.isAlreadyNavigating) {
|
||||
libraryModel.isAlreadyNavigating = true
|
||||
Log.d(this::class.simpleName, "Navigating to the detail fragment for ${baseModel.name}")
|
||||
|
||||
Log.d(this::class.simpleName, "Navigating to the detail fragment for $baseModel.name")
|
||||
findNavController().navigate(
|
||||
when (baseModel) {
|
||||
is Genre -> MainFragmentDirections.actionShowGenre(baseModel.id)
|
||||
is Artist -> MainFragmentDirections.actionShowArtist(baseModel.id)
|
||||
is Album -> MainFragmentDirections.actionShowAlbum(baseModel.id, true)
|
||||
|
||||
findNavController().navigate(
|
||||
when (baseModel) {
|
||||
is Genre -> MainFragmentDirections.actionShowGenre(baseModel.id)
|
||||
is Artist -> MainFragmentDirections.actionShowArtist(baseModel.id)
|
||||
is Album -> MainFragmentDirections.actionShowAlbum(baseModel.id, true)
|
||||
|
||||
else -> return
|
||||
}
|
||||
)
|
||||
}
|
||||
else -> return
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -9,8 +9,6 @@ import org.oxycblt.auxio.recycler.SortMode
|
|||
import org.oxycblt.auxio.theme.SHOW_ARTISTS
|
||||
|
||||
class LibraryViewModel : ViewModel() {
|
||||
var isAlreadyNavigating = false
|
||||
|
||||
// TODO: Move these to prefs when they're added
|
||||
private val mShowMode = MutableLiveData(SHOW_ARTISTS)
|
||||
val showMode: LiveData<Int> get() = mShowMode
|
||||
|
|
|
@ -3,8 +3,19 @@ package org.oxycblt.auxio.recycler
|
|||
import androidx.recyclerview.widget.DiffUtil
|
||||
import org.oxycblt.auxio.music.BaseModel
|
||||
|
||||
// RecyclerView click listener
|
||||
class ClickListener<T>(val onClick: (T) -> Unit)
|
||||
// A RecyclerView click listener that can only be called once.
|
||||
// Primarily used for navigation to prevent bugs when multiple items are selected.
|
||||
class ClickListener<T>(private val clickAction: (T) -> Unit) {
|
||||
private var hasClicked = false
|
||||
|
||||
fun onClick(item: T) {
|
||||
if (!hasClicked) {
|
||||
hasClicked = true
|
||||
|
||||
clickAction(item)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Base Diff callback
|
||||
class DiffCallback<T : BaseModel> : DiffUtil.ItemCallback<T>() {
|
||||
|
|
Loading…
Reference in a new issue