Add click listeners to Song/Album items
Add the ripple selection effect & the click listener to the song and album items.
This commit is contained in:
parent
af26aed735
commit
5827ca492c
11 changed files with 43 additions and 16 deletions
|
@ -12,6 +12,7 @@ import org.oxycblt.auxio.R
|
|||
import org.oxycblt.auxio.databinding.FragmentLibraryBinding
|
||||
import org.oxycblt.auxio.recycler.adapters.AlbumAdapter
|
||||
import org.oxycblt.auxio.recycler.applyDivider
|
||||
import org.oxycblt.auxio.recycler.viewholders.ClickListener
|
||||
|
||||
class LibraryFragment : Fragment() {
|
||||
|
||||
|
@ -28,7 +29,12 @@ class LibraryFragment : Fragment() {
|
|||
inflater, R.layout.fragment_library, container, false
|
||||
)
|
||||
|
||||
binding.libraryRecycler.adapter = AlbumAdapter(libraryModel.albums.value!!)
|
||||
binding.libraryRecycler.adapter = AlbumAdapter(
|
||||
libraryModel.albums.value!!,
|
||||
ClickListener { album ->
|
||||
Log.d(this::class.simpleName, album.title)
|
||||
}
|
||||
)
|
||||
binding.libraryRecycler.applyDivider()
|
||||
binding.libraryRecycler.setHasFixedSize(true)
|
||||
|
||||
|
|
|
@ -64,8 +64,8 @@ class LoadingFragment : Fragment() {
|
|||
)
|
||||
|
||||
// Set up the permission launcher, as its disallowed outside of onCreate.
|
||||
permLauncher = registerForActivityResult(ActivityResultContracts.RequestPermission())
|
||||
{ granted: Boolean ->
|
||||
permLauncher =
|
||||
registerForActivityResult(ActivityResultContracts.RequestPermission()) { granted: Boolean ->
|
||||
|
||||
// If its actually granted, restart the loading process again.
|
||||
if (granted) {
|
||||
|
@ -82,7 +82,6 @@ class LoadingFragment : Fragment() {
|
|||
// This never seems to return true but Im apparently supposed to use it so
|
||||
if (shouldShowRequestPermissionRationale(Manifest.permission.READ_EXTERNAL_STORAGE)) {
|
||||
onMusicLoadResponse(MusicLoaderResponse.NO_PERMS)
|
||||
|
||||
} else {
|
||||
loadingModel.go()
|
||||
}
|
||||
|
@ -100,7 +99,6 @@ class LoadingFragment : Fragment() {
|
|||
this.findNavController().navigate(
|
||||
LoadingFragmentDirections.actionToMain()
|
||||
)
|
||||
|
||||
} else {
|
||||
// If the response wasn't a success, then show the specific error message
|
||||
// depending on which error response was given, along with a retry or grant button
|
||||
|
|
|
@ -6,8 +6,12 @@ import androidx.recyclerview.widget.RecyclerView
|
|||
import org.oxycblt.auxio.databinding.AlbumItemBinding
|
||||
import org.oxycblt.auxio.music.models.Album
|
||||
import org.oxycblt.auxio.recycler.viewholders.AlbumViewHolder
|
||||
import org.oxycblt.auxio.recycler.viewholders.ClickListener
|
||||
|
||||
class AlbumAdapter(private val data: List<Album>) : RecyclerView.Adapter<AlbumViewHolder>() {
|
||||
class AlbumAdapter(
|
||||
private val data: List<Album>,
|
||||
private val listener: ClickListener<Album>
|
||||
) : RecyclerView.Adapter<AlbumViewHolder>() {
|
||||
|
||||
override fun getItemCount(): Int = data.size
|
||||
|
||||
|
@ -25,6 +29,10 @@ class AlbumAdapter(private val data: List<Album>) : RecyclerView.Adapter<AlbumVi
|
|||
override fun onBindViewHolder(holder: AlbumViewHolder, position: Int) {
|
||||
val album = data[position]
|
||||
|
||||
holder.itemView.setOnClickListener {
|
||||
listener.onClick(album)
|
||||
}
|
||||
|
||||
holder.bind(album)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -5,9 +5,13 @@ import android.view.ViewGroup
|
|||
import androidx.recyclerview.widget.RecyclerView
|
||||
import org.oxycblt.auxio.databinding.SongItemBinding
|
||||
import org.oxycblt.auxio.music.models.Song
|
||||
import org.oxycblt.auxio.recycler.viewholders.ClickListener
|
||||
import org.oxycblt.auxio.recycler.viewholders.SongViewHolder
|
||||
|
||||
class SongAdapter(private val data: List<Song>) : RecyclerView.Adapter<SongViewHolder>() {
|
||||
class SongAdapter(
|
||||
private val data: List<Song>,
|
||||
private val listener: ClickListener<Song>
|
||||
) : RecyclerView.Adapter<SongViewHolder>() {
|
||||
|
||||
override fun getItemCount(): Int = data.size
|
||||
|
||||
|
@ -25,6 +29,10 @@ class SongAdapter(private val data: List<Song>) : RecyclerView.Adapter<SongViewH
|
|||
override fun onBindViewHolder(holder: SongViewHolder, position: Int) {
|
||||
val song = data[position]
|
||||
|
||||
holder.itemView.setOnClickListener {
|
||||
listener.onClick(song)
|
||||
}
|
||||
|
||||
holder.bind(song)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -6,7 +6,7 @@ import org.oxycblt.auxio.music.models.Album
|
|||
|
||||
// Generic ViewHolder for an album
|
||||
class AlbumViewHolder(
|
||||
private var binding: AlbumItemBinding
|
||||
private val binding: AlbumItemBinding
|
||||
) : RecyclerView.ViewHolder(binding.root) {
|
||||
|
||||
// Bind the view w/new data
|
||||
|
|
|
@ -1,6 +1,4 @@
|
|||
package org.oxycblt.auxio.recycler.viewholders
|
||||
|
||||
// Generic ClickListener
|
||||
class ClickListener<T>(val action: (T) -> Unit) {
|
||||
fun onClick(action: T) = action
|
||||
}
|
||||
class ClickListener<T>(val onClick: (T) -> Unit)
|
||||
|
|
|
@ -6,7 +6,7 @@ import org.oxycblt.auxio.music.models.Song
|
|||
|
||||
// Generic ViewHolder for a song
|
||||
class SongViewHolder(
|
||||
private var binding: SongItemBinding
|
||||
private val binding: SongItemBinding
|
||||
) : RecyclerView.ViewHolder(binding.root) {
|
||||
|
||||
// Bind the view w/new data
|
||||
|
|
|
@ -12,6 +12,7 @@ import org.oxycblt.auxio.R
|
|||
import org.oxycblt.auxio.databinding.FragmentSongsBinding
|
||||
import org.oxycblt.auxio.recycler.adapters.SongAdapter
|
||||
import org.oxycblt.auxio.recycler.applyDivider
|
||||
import org.oxycblt.auxio.recycler.viewholders.ClickListener
|
||||
|
||||
class SongsFragment : Fragment() {
|
||||
|
||||
|
@ -28,7 +29,12 @@ class SongsFragment : Fragment() {
|
|||
inflater, R.layout.fragment_songs, container, false
|
||||
)
|
||||
|
||||
binding.songRecycler.adapter = SongAdapter(songsModel.songs.value!!)
|
||||
binding.songRecycler.adapter = SongAdapter(
|
||||
songsModel.songs.value!!,
|
||||
ClickListener { song ->
|
||||
Log.d(this::class.simpleName, song.title)
|
||||
}
|
||||
)
|
||||
binding.songRecycler.applyDivider()
|
||||
binding.songRecycler.setHasFixedSize(true)
|
||||
|
||||
|
|
|
@ -1,3 +0,0 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:color="?attr/colorPrimary" />
|
|
@ -13,6 +13,9 @@
|
|||
<androidx.constraintlayout.widget.ConstraintLayout
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:background="?android:attr/selectableItemBackground"
|
||||
android:clickable="true"
|
||||
android:focusable="true"
|
||||
android:padding="@dimen/padding_medium">
|
||||
|
||||
<ImageView
|
||||
|
|
|
@ -13,6 +13,9 @@
|
|||
<androidx.constraintlayout.widget.ConstraintLayout
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:background="?android:attr/selectableItemBackground"
|
||||
android:clickable="true"
|
||||
android:focusable="true"
|
||||
android:padding="@dimen/padding_medium">
|
||||
|
||||
<ImageView
|
||||
|
|
Loading…
Reference in a new issue